blob: 4af08809ab45400ccddba81a1759ee830bf80ac9 [file] [log] [blame]
Daniel Barkalow95fc7512005-06-06 16:31:29 -04001#include "cache.h"
Junio C Hamano85023572006-12-19 14:34:12 -08002#include "refs.h"
Junio C Hamanocf0adba2006-11-19 13:22:44 -08003#include "object.h"
4#include "tag.h"
Johannes Schindelin7155b722007-09-28 16:28:54 +01005#include "dir.h"
Daniel Barkalow95fc7512005-06-06 16:31:29 -04006
Michael Haggertybc5fd6d2012-04-10 07:30:13 +02007/*
8 * Make sure "ref" is something reasonable to have under ".git/refs/";
9 * We do not like it if:
10 *
11 * - any path component of it begins with ".", or
12 * - it has double dots "..", or
13 * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or
14 * - it ends with a "/".
15 * - it ends with ".lock"
16 * - it contains a "\" (backslash)
17 */
Junio C Hamanof4204ab2006-11-21 23:36:35 -080018
Michael Haggertybc5fd6d2012-04-10 07:30:13 +020019/* Return true iff ch is not allowed in reference names. */
20static inline int bad_ref_char(int ch)
21{
22 if (((unsigned) ch) <= ' ' || ch == 0x7f ||
23 ch == '~' || ch == '^' || ch == ':' || ch == '\\')
24 return 1;
25 /* 2.13 Pattern Matching Notation */
26 if (ch == '*' || ch == '?' || ch == '[') /* Unsupported */
27 return 1;
28 return 0;
29}
30
31/*
32 * Try to read one refname component from the front of refname. Return
33 * the length of the component found, or -1 if the component is not
34 * legal.
35 */
36static int check_refname_component(const char *refname, int flags)
37{
38 const char *cp;
39 char last = '\0';
40
41 for (cp = refname; ; cp++) {
42 char ch = *cp;
43 if (ch == '\0' || ch == '/')
44 break;
45 if (bad_ref_char(ch))
46 return -1; /* Illegal character in refname. */
47 if (last == '.' && ch == '.')
48 return -1; /* Refname contains "..". */
49 if (last == '@' && ch == '{')
50 return -1; /* Refname contains "@{". */
51 last = ch;
52 }
53 if (cp == refname)
Michael Haggertydac529e2012-04-10 07:30:22 +020054 return 0; /* Component has zero length. */
Michael Haggertybc5fd6d2012-04-10 07:30:13 +020055 if (refname[0] == '.') {
56 if (!(flags & REFNAME_DOT_COMPONENT))
57 return -1; /* Component starts with '.'. */
58 /*
59 * Even if leading dots are allowed, don't allow "."
60 * as a component (".." is prevented by a rule above).
61 */
62 if (refname[1] == '\0')
63 return -1; /* Component equals ".". */
64 }
65 if (cp - refname >= 5 && !memcmp(cp - 5, ".lock", 5))
66 return -1; /* Refname ends with ".lock". */
67 return cp - refname;
68}
69
70int check_refname_format(const char *refname, int flags)
71{
72 int component_len, component_count = 0;
73
74 while (1) {
75 /* We are at the start of a path component. */
76 component_len = check_refname_component(refname, flags);
Michael Haggertydac529e2012-04-10 07:30:22 +020077 if (component_len <= 0) {
Michael Haggertybc5fd6d2012-04-10 07:30:13 +020078 if ((flags & REFNAME_REFSPEC_PATTERN) &&
79 refname[0] == '*' &&
80 (refname[1] == '\0' || refname[1] == '/')) {
81 /* Accept one wildcard as a full refname component. */
82 flags &= ~REFNAME_REFSPEC_PATTERN;
83 component_len = 1;
84 } else {
85 return -1;
86 }
87 }
88 component_count++;
89 if (refname[component_len] == '\0')
90 break;
91 /* Skip to next component. */
92 refname += component_len + 1;
93 }
94
95 if (refname[component_len - 1] == '.')
96 return -1; /* Refname ends with '.'. */
97 if (!(flags & REFNAME_ALLOW_ONELEVEL) && component_count < 2)
98 return -1; /* Refname has only one component. */
99 return 0;
100}
101
102struct ref_entry;
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700103
Michael Haggerty593f1bb2012-04-10 07:30:23 +0200104struct ref_value {
105 unsigned char sha1[20];
106 unsigned char peeled[20];
107};
108
Michael Haggertyd3177272012-04-10 07:30:24 +0200109struct ref_dir {
Julian Phillipse9c4c112011-09-29 23:11:42 +0100110 int nr, alloc;
Michael Haggertye6ed3ca2012-01-17 06:50:32 +0100111
112 /*
113 * Entries with index 0 <= i < sorted are sorted by name. New
114 * entries are appended to the list unsorted, and are sorted
115 * only when required; thus we avoid the need to sort the list
116 * after the addition of every reference.
117 */
118 int sorted;
119
Michael Haggertyd3177272012-04-10 07:30:24 +0200120 struct ref_entry **entries;
Julian Phillipse9c4c112011-09-29 23:11:42 +0100121};
122
Michael Haggerty432ad412012-04-10 07:30:26 +0200123/* ISSYMREF=0x01, ISPACKED=0x02, and ISBROKEN=0x04 are public interfaces */
124#define REF_KNOWS_PEELED 0x08
125#define REF_DIR 0x10
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700126
Michael Haggerty432ad412012-04-10 07:30:26 +0200127/*
128 * A ref_entry represents either a reference or a "subdirectory" of
129 * references. Each directory in the reference namespace is
130 * represented by a ref_entry with (flags & REF_DIR) set and
131 * containing a subdir member that holds the entries in that
132 * directory. References are represented by a ref_entry with (flags &
133 * REF_DIR) unset and a value member that describes the reference's
134 * value. The flag member is at the ref_entry level, but it is also
135 * needed to interpret the contents of the value field (in other
136 * words, a ref_value object is not very much use without the
137 * enclosing ref_entry).
138 *
139 * Reference names cannot end with slash and directories' names are
140 * always stored with a trailing slash (except for the top-level
141 * directory, which is always denoted by ""). This has two nice
142 * consequences: (1) when the entries in each subdir are sorted
143 * lexicographically by name (as they usually are), the references in
144 * a whole tree can be generated in lexicographic order by traversing
145 * the tree in left-to-right, depth-first order; (2) the names of
146 * references and subdirectories cannot conflict, and therefore the
147 * presence of an empty subdirectory does not block the creation of a
148 * similarly-named reference. (The fact that reference names with the
149 * same leading components can conflict *with each other* is a
150 * separate issue that is regulated by is_refname_available().)
151 *
152 * Please note that the name field contains the fully-qualified
153 * reference (or subdirectory) name. Space could be saved by only
154 * storing the relative names. But that would require the full names
155 * to be generated on the fly when iterating in do_for_each_ref(), and
156 * would break callback functions, who have always been able to assume
157 * that the name strings that they are passed will not be freed during
158 * the iteration.
159 */
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200160struct ref_entry {
161 unsigned char flag; /* ISSYMREF? ISPACKED? */
Michael Haggerty593f1bb2012-04-10 07:30:23 +0200162 union {
Michael Haggerty432ad412012-04-10 07:30:26 +0200163 struct ref_value value; /* if not (flags&REF_DIR) */
164 struct ref_dir subdir; /* if (flags&REF_DIR) */
Michael Haggerty593f1bb2012-04-10 07:30:23 +0200165 } u;
Michael Haggerty432ad412012-04-10 07:30:26 +0200166 /*
167 * The full name of the reference (e.g., "refs/heads/master")
168 * or the full name of the directory with a trailing slash
169 * (e.g., "refs/heads/"):
170 */
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200171 char name[FLEX_ARRAY];
172};
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700173
Michael Haggertyd7826d52012-04-27 00:27:03 +0200174static struct ref_dir *get_ref_dir(struct ref_entry *entry)
175{
176 assert(entry->flag & REF_DIR);
177 return &entry->u.subdir;
178}
179
Michael Haggertycddc4252011-12-12 06:38:22 +0100180static struct ref_entry *create_ref_entry(const char *refname,
181 const unsigned char *sha1, int flag,
182 int check_name)
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700183{
184 int len;
Michael Haggertycddc4252011-12-12 06:38:22 +0100185 struct ref_entry *ref;
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700186
Junio C Hamano09116a12011-11-16 16:54:32 -0800187 if (check_name &&
Michael Haggertydfefa932011-12-12 06:38:09 +0100188 check_refname_format(refname, REFNAME_ALLOW_ONELEVEL|REFNAME_DOT_COMPONENT))
189 die("Reference has invalid format: '%s'", refname);
Michael Haggertycddc4252011-12-12 06:38:22 +0100190 len = strlen(refname) + 1;
191 ref = xmalloc(sizeof(struct ref_entry) + len);
Michael Haggerty593f1bb2012-04-10 07:30:23 +0200192 hashcpy(ref->u.value.sha1, sha1);
193 hashclr(ref->u.value.peeled);
Michael Haggertycddc4252011-12-12 06:38:22 +0100194 memcpy(ref->name, refname, len);
195 ref->flag = flag;
196 return ref;
197}
198
Michael Haggerty432ad412012-04-10 07:30:26 +0200199static void clear_ref_dir(struct ref_dir *dir);
200
Michael Haggerty732134e2012-04-10 07:30:21 +0200201static void free_ref_entry(struct ref_entry *entry)
202{
Michael Haggerty432ad412012-04-10 07:30:26 +0200203 if (entry->flag & REF_DIR)
Michael Haggertyd7826d52012-04-27 00:27:03 +0200204 clear_ref_dir(get_ref_dir(entry));
Michael Haggerty732134e2012-04-10 07:30:21 +0200205 free(entry);
206}
207
Michael Haggerty432ad412012-04-10 07:30:26 +0200208/*
209 * Add a ref_entry to the end of dir (unsorted). Entry is always
210 * stored directly in dir; no recursion into subdirectories is
211 * done.
212 */
213static void add_entry_to_dir(struct ref_dir *dir, struct ref_entry *entry)
Michael Haggertycddc4252011-12-12 06:38:22 +0100214{
Michael Haggerty432ad412012-04-10 07:30:26 +0200215 ALLOC_GROW(dir->entries, dir->nr + 1, dir->alloc);
216 dir->entries[dir->nr++] = entry;
Julian Phillipsc774aab2007-04-17 02:42:50 +0100217}
218
Michael Haggerty432ad412012-04-10 07:30:26 +0200219/*
220 * Clear and free all entries in dir, recursively.
221 */
Michael Haggertyd3177272012-04-10 07:30:24 +0200222static void clear_ref_dir(struct ref_dir *dir)
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200223{
224 int i;
Michael Haggertyd3177272012-04-10 07:30:24 +0200225 for (i = 0; i < dir->nr; i++)
226 free_ref_entry(dir->entries[i]);
227 free(dir->entries);
228 dir->sorted = dir->nr = dir->alloc = 0;
229 dir->entries = NULL;
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200230}
231
Michael Haggerty432ad412012-04-10 07:30:26 +0200232/*
233 * Create a struct ref_entry object for the specified dirname.
234 * dirname is the name of the directory with a trailing slash (e.g.,
235 * "refs/heads/") or "" for the top-level directory.
236 */
237static struct ref_entry *create_dir_entry(const char *dirname)
238{
239 struct ref_entry *direntry;
240 int len = strlen(dirname);
241 direntry = xcalloc(1, sizeof(struct ref_entry) + len + 1);
242 memcpy(direntry->name, dirname, len + 1);
243 direntry->flag = REF_DIR;
244 return direntry;
245}
246
Julian Phillipse9c4c112011-09-29 23:11:42 +0100247static int ref_entry_cmp(const void *a, const void *b)
Julian Phillipsc774aab2007-04-17 02:42:50 +0100248{
Julian Phillipse9c4c112011-09-29 23:11:42 +0100249 struct ref_entry *one = *(struct ref_entry **)a;
250 struct ref_entry *two = *(struct ref_entry **)b;
251 return strcmp(one->name, two->name);
252}
Julian Phillipsc774aab2007-04-17 02:42:50 +0100253
Michael Haggertyd3177272012-04-10 07:30:24 +0200254static void sort_ref_dir(struct ref_dir *dir);
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200255
Michael Haggerty432ad412012-04-10 07:30:26 +0200256/*
257 * Return the entry with the given refname from the ref_dir
258 * (non-recursively), sorting dir if necessary. Return NULL if no
259 * such entry is found.
260 */
Michael Haggertyd3177272012-04-10 07:30:24 +0200261static struct ref_entry *search_ref_dir(struct ref_dir *dir, const char *refname)
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200262{
263 struct ref_entry *e, **r;
264 int len;
265
Michael Haggerty432ad412012-04-10 07:30:26 +0200266 if (refname == NULL || !dir->nr)
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200267 return NULL;
268
Michael Haggertyd3177272012-04-10 07:30:24 +0200269 sort_ref_dir(dir);
Michael Haggerty432ad412012-04-10 07:30:26 +0200270
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200271 len = strlen(refname) + 1;
272 e = xmalloc(sizeof(struct ref_entry) + len);
273 memcpy(e->name, refname, len);
274
Michael Haggertyd3177272012-04-10 07:30:24 +0200275 r = bsearch(&e, dir->entries, dir->nr, sizeof(*dir->entries), ref_entry_cmp);
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200276
277 free(e);
278
279 if (r == NULL)
280 return NULL;
281
282 return *r;
283}
284
Michael Haggerty202a56a2011-12-12 06:38:15 +0100285/*
Michael Haggertyf348ac92012-04-25 00:45:11 +0200286 * Search for a directory entry directly within dir (without
287 * recursing). Sort dir if necessary. subdirname must be a directory
288 * name (i.e., end in '/'). If mkdir is set, then create the
289 * directory if it is missing; otherwise, return NULL if the desired
290 * directory cannot be found.
291 */
Michael Haggerty3f3aa1b2012-04-27 00:27:04 +0200292static struct ref_dir *search_for_subdir(struct ref_dir *dir,
293 const char *subdirname, int mkdir)
Michael Haggertyf348ac92012-04-25 00:45:11 +0200294{
295 struct ref_entry *entry = search_ref_dir(dir, subdirname);
296 if (!entry) {
297 if (!mkdir)
298 return NULL;
299 entry = create_dir_entry(subdirname);
300 add_entry_to_dir(dir, entry);
301 }
Michael Haggerty3f3aa1b2012-04-27 00:27:04 +0200302 return get_ref_dir(entry);
Michael Haggertyf348ac92012-04-25 00:45:11 +0200303}
304
305/*
Michael Haggerty432ad412012-04-10 07:30:26 +0200306 * If refname is a reference name, find the ref_dir within the dir
307 * tree that should hold refname. If refname is a directory name
308 * (i.e., ends in '/'), then return that ref_dir itself. dir must
309 * represent the top-level directory. Sort ref_dirs and recurse into
310 * subdirectories as necessary. If mkdir is set, then create any
311 * missing directories; otherwise, return NULL if the desired
312 * directory cannot be found.
313 */
314static struct ref_dir *find_containing_dir(struct ref_dir *dir,
315 const char *refname, int mkdir)
316{
Michael Haggerty5fa04412012-04-27 00:27:00 +0200317 struct strbuf dirname;
318 const char *slash;
319 strbuf_init(&dirname, PATH_MAX);
320 for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) {
Michael Haggerty3f3aa1b2012-04-27 00:27:04 +0200321 struct ref_dir *subdir;
Michael Haggerty5fa04412012-04-27 00:27:00 +0200322 strbuf_add(&dirname,
323 refname + dirname.len,
324 (slash + 1) - (refname + dirname.len));
Michael Haggerty3f3aa1b2012-04-27 00:27:04 +0200325 subdir = search_for_subdir(dir, dirname.buf, mkdir);
326 if (!subdir)
Michael Haggertyf348ac92012-04-25 00:45:11 +0200327 break;
Michael Haggerty3f3aa1b2012-04-27 00:27:04 +0200328 dir = subdir;
Michael Haggerty432ad412012-04-10 07:30:26 +0200329 }
330
Michael Haggerty5fa04412012-04-27 00:27:00 +0200331 strbuf_release(&dirname);
Michael Haggerty432ad412012-04-10 07:30:26 +0200332 return dir;
333}
334
335/*
336 * Find the value entry with the given name in dir, sorting ref_dirs
337 * and recursing into subdirectories as necessary. If the name is not
338 * found or it corresponds to a directory entry, return NULL.
339 */
340static struct ref_entry *find_ref(struct ref_dir *dir, const char *refname)
341{
342 struct ref_entry *entry;
343 dir = find_containing_dir(dir, refname, 0);
344 if (!dir)
345 return NULL;
346 entry = search_ref_dir(dir, refname);
347 return (entry && !(entry->flag & REF_DIR)) ? entry : NULL;
348}
349
350/*
351 * Add a ref_entry to the ref_dir (unsorted), recursing into
352 * subdirectories as necessary. dir must represent the top-level
353 * directory. Return 0 on success.
354 */
355static int add_ref(struct ref_dir *dir, struct ref_entry *ref)
356{
357 dir = find_containing_dir(dir, ref->name, 1);
358 if (!dir)
359 return -1;
360 add_entry_to_dir(dir, ref);
361 return 0;
362}
363
364/*
Michael Haggerty202a56a2011-12-12 06:38:15 +0100365 * Emit a warning and return true iff ref1 and ref2 have the same name
366 * and the same sha1. Die if they have the same name but different
367 * sha1s.
368 */
369static int is_dup_ref(const struct ref_entry *ref1, const struct ref_entry *ref2)
370{
Michael Haggerty432ad412012-04-10 07:30:26 +0200371 if (strcmp(ref1->name, ref2->name))
Michael Haggerty202a56a2011-12-12 06:38:15 +0100372 return 0;
Michael Haggerty432ad412012-04-10 07:30:26 +0200373
374 /* Duplicate name; make sure that they don't conflict: */
375
376 if ((ref1->flag & REF_DIR) || (ref2->flag & REF_DIR))
377 /* This is impossible by construction */
378 die("Reference directory conflict: %s", ref1->name);
379
380 if (hashcmp(ref1->u.value.sha1, ref2->u.value.sha1))
381 die("Duplicated ref, and SHA1s don't match: %s", ref1->name);
382
383 warning("Duplicated ref: %s", ref1->name);
384 return 1;
Michael Haggerty202a56a2011-12-12 06:38:15 +0100385}
386
Michael Haggertye6ed3ca2012-01-17 06:50:32 +0100387/*
Michael Haggerty432ad412012-04-10 07:30:26 +0200388 * Sort the entries in dir non-recursively (if they are not already
389 * sorted) and remove any duplicate entries.
Michael Haggertye6ed3ca2012-01-17 06:50:32 +0100390 */
Michael Haggertyd3177272012-04-10 07:30:24 +0200391static void sort_ref_dir(struct ref_dir *dir)
Julian Phillipse9c4c112011-09-29 23:11:42 +0100392{
Michael Haggerty202a56a2011-12-12 06:38:15 +0100393 int i, j;
Michael Haggerty81a79d82012-04-10 07:30:25 +0200394 struct ref_entry *last = NULL;
Julian Phillipsc774aab2007-04-17 02:42:50 +0100395
Michael Haggertye6ed3ca2012-01-17 06:50:32 +0100396 /*
397 * This check also prevents passing a zero-length array to qsort(),
398 * which is a problem on some platforms.
399 */
Michael Haggertyd3177272012-04-10 07:30:24 +0200400 if (dir->sorted == dir->nr)
Julian Phillipse9c4c112011-09-29 23:11:42 +0100401 return;
Julian Phillipsc774aab2007-04-17 02:42:50 +0100402
Michael Haggertyd3177272012-04-10 07:30:24 +0200403 qsort(dir->entries, dir->nr, sizeof(*dir->entries), ref_entry_cmp);
Julian Phillipsc774aab2007-04-17 02:42:50 +0100404
Michael Haggerty81a79d82012-04-10 07:30:25 +0200405 /* Remove any duplicates: */
406 for (i = 0, j = 0; j < dir->nr; j++) {
407 struct ref_entry *entry = dir->entries[j];
408 if (last && is_dup_ref(last, entry))
409 free_ref_entry(entry);
410 else
411 last = dir->entries[i++] = entry;
Julian Phillipse9c4c112011-09-29 23:11:42 +0100412 }
Michael Haggerty81a79d82012-04-10 07:30:25 +0200413 dir->sorted = dir->nr = i;
Julian Phillipse9c4c112011-09-29 23:11:42 +0100414}
Julian Phillipsc774aab2007-04-17 02:42:50 +0100415
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200416#define DO_FOR_EACH_INCLUDE_BROKEN 01
417
418static struct ref_entry *current_ref;
419
420static int do_one_ref(const char *base, each_ref_fn fn, int trim,
421 int flags, void *cb_data, struct ref_entry *entry)
Julian Phillipse9c4c112011-09-29 23:11:42 +0100422{
Michael Haggerty429213e2012-04-10 07:30:14 +0200423 int retval;
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200424 if (prefixcmp(entry->name, base))
425 return 0;
Julian Phillipsc774aab2007-04-17 02:42:50 +0100426
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200427 if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN)) {
428 if (entry->flag & REF_ISBROKEN)
429 return 0; /* ignore broken refs e.g. dangling symref */
Michael Haggerty593f1bb2012-04-10 07:30:23 +0200430 if (!has_sha1_file(entry->u.value.sha1)) {
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200431 error("%s does not point to a valid object!", entry->name);
432 return 0;
433 }
434 }
435 current_ref = entry;
Michael Haggerty593f1bb2012-04-10 07:30:23 +0200436 retval = fn(entry->name + trim, entry->u.value.sha1, entry->flag, cb_data);
Michael Haggerty429213e2012-04-10 07:30:14 +0200437 current_ref = NULL;
438 return retval;
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200439}
Julian Phillipsc774aab2007-04-17 02:42:50 +0100440
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200441/*
Michael Haggertyd3177272012-04-10 07:30:24 +0200442 * Call fn for each reference in dir that has index in the range
Michael Haggerty432ad412012-04-10 07:30:26 +0200443 * offset <= index < dir->nr. Recurse into subdirectories that are in
444 * that index range, sorting them before iterating. This function
445 * does not sort dir itself; it should be sorted beforehand.
Michael Haggertyc36b5bc2012-04-10 07:30:15 +0200446 */
Michael Haggertyd3177272012-04-10 07:30:24 +0200447static int do_for_each_ref_in_dir(struct ref_dir *dir, int offset,
448 const char *base,
449 each_ref_fn fn, int trim, int flags, void *cb_data)
Michael Haggertyc36b5bc2012-04-10 07:30:15 +0200450{
451 int i;
Michael Haggertyd3177272012-04-10 07:30:24 +0200452 assert(dir->sorted == dir->nr);
453 for (i = offset; i < dir->nr; i++) {
Michael Haggerty432ad412012-04-10 07:30:26 +0200454 struct ref_entry *entry = dir->entries[i];
455 int retval;
456 if (entry->flag & REF_DIR) {
Michael Haggertyd7826d52012-04-27 00:27:03 +0200457 struct ref_dir *subdir = get_ref_dir(entry);
458 sort_ref_dir(subdir);
459 retval = do_for_each_ref_in_dir(subdir, 0,
Michael Haggerty432ad412012-04-10 07:30:26 +0200460 base, fn, trim, flags, cb_data);
461 } else {
462 retval = do_one_ref(base, fn, trim, flags, cb_data, entry);
463 }
Michael Haggertyc36b5bc2012-04-10 07:30:15 +0200464 if (retval)
465 return retval;
466 }
467 return 0;
468}
469
470/*
Michael Haggertyd3177272012-04-10 07:30:24 +0200471 * Call fn for each reference in the union of dir1 and dir2, in order
Michael Haggerty432ad412012-04-10 07:30:26 +0200472 * by refname. Recurse into subdirectories. If a value entry appears
473 * in both dir1 and dir2, then only process the version that is in
474 * dir2. The input dirs must already be sorted, but subdirs will be
475 * sorted as needed.
Michael Haggertyb3fd0602012-04-10 07:30:16 +0200476 */
Michael Haggertyd3177272012-04-10 07:30:24 +0200477static int do_for_each_ref_in_dirs(struct ref_dir *dir1,
478 struct ref_dir *dir2,
479 const char *base, each_ref_fn fn, int trim,
480 int flags, void *cb_data)
Michael Haggertyb3fd0602012-04-10 07:30:16 +0200481{
482 int retval;
483 int i1 = 0, i2 = 0;
484
Michael Haggertyd3177272012-04-10 07:30:24 +0200485 assert(dir1->sorted == dir1->nr);
486 assert(dir2->sorted == dir2->nr);
Michael Haggerty432ad412012-04-10 07:30:26 +0200487 while (1) {
488 struct ref_entry *e1, *e2;
489 int cmp;
490 if (i1 == dir1->nr) {
491 return do_for_each_ref_in_dir(dir2, i2,
492 base, fn, trim, flags, cb_data);
493 }
494 if (i2 == dir2->nr) {
495 return do_for_each_ref_in_dir(dir1, i1,
496 base, fn, trim, flags, cb_data);
497 }
498 e1 = dir1->entries[i1];
499 e2 = dir2->entries[i2];
500 cmp = strcmp(e1->name, e2->name);
501 if (cmp == 0) {
502 if ((e1->flag & REF_DIR) && (e2->flag & REF_DIR)) {
503 /* Both are directories; descend them in parallel. */
Michael Haggertyd7826d52012-04-27 00:27:03 +0200504 struct ref_dir *subdir1 = get_ref_dir(e1);
505 struct ref_dir *subdir2 = get_ref_dir(e2);
506 sort_ref_dir(subdir1);
507 sort_ref_dir(subdir2);
Michael Haggerty432ad412012-04-10 07:30:26 +0200508 retval = do_for_each_ref_in_dirs(
Michael Haggertyd7826d52012-04-27 00:27:03 +0200509 subdir1, subdir2,
Michael Haggerty432ad412012-04-10 07:30:26 +0200510 base, fn, trim, flags, cb_data);
Michael Haggertyb3fd0602012-04-10 07:30:16 +0200511 i1++;
Michael Haggerty432ad412012-04-10 07:30:26 +0200512 i2++;
513 } else if (!(e1->flag & REF_DIR) && !(e2->flag & REF_DIR)) {
514 /* Both are references; ignore the one from dir1. */
515 retval = do_one_ref(base, fn, trim, flags, cb_data, e2);
516 i1++;
517 i2++;
518 } else {
519 die("conflict between reference and directory: %s",
520 e1->name);
521 }
522 } else {
523 struct ref_entry *e;
524 if (cmp < 0) {
525 e = e1;
526 i1++;
527 } else {
528 e = e2;
529 i2++;
530 }
531 if (e->flag & REF_DIR) {
Michael Haggertyd7826d52012-04-27 00:27:03 +0200532 struct ref_dir *subdir = get_ref_dir(e);
533 sort_ref_dir(subdir);
Michael Haggerty432ad412012-04-10 07:30:26 +0200534 retval = do_for_each_ref_in_dir(
Michael Haggertyd7826d52012-04-27 00:27:03 +0200535 subdir, 0,
Michael Haggerty432ad412012-04-10 07:30:26 +0200536 base, fn, trim, flags, cb_data);
537 } else {
538 retval = do_one_ref(base, fn, trim, flags, cb_data, e);
Michael Haggertyb3fd0602012-04-10 07:30:16 +0200539 }
540 }
541 if (retval)
542 return retval;
543 }
Michael Haggertyd3177272012-04-10 07:30:24 +0200544 if (i1 < dir1->nr)
545 return do_for_each_ref_in_dir(dir1, i1,
546 base, fn, trim, flags, cb_data);
547 if (i2 < dir2->nr)
548 return do_for_each_ref_in_dir(dir2, i2,
549 base, fn, trim, flags, cb_data);
Michael Haggertyb3fd0602012-04-10 07:30:16 +0200550 return 0;
551}
552
553/*
Michael Haggertyd66da472012-04-10 07:30:17 +0200554 * Return true iff refname1 and refname2 conflict with each other.
555 * Two reference names conflict if one of them exactly matches the
556 * leading components of the other; e.g., "foo/bar" conflicts with
557 * both "foo" and with "foo/bar/baz" but not with "foo/bar" or
558 * "foo/barbados".
559 */
560static int names_conflict(const char *refname1, const char *refname2)
561{
Michael Haggerty5a4d4942012-04-10 07:30:19 +0200562 for (; *refname1 && *refname1 == *refname2; refname1++, refname2++)
563 ;
564 return (*refname1 == '\0' && *refname2 == '/')
565 || (*refname1 == '/' && *refname2 == '\0');
566}
Michael Haggertyd66da472012-04-10 07:30:17 +0200567
Michael Haggerty5a4d4942012-04-10 07:30:19 +0200568struct name_conflict_cb {
569 const char *refname;
570 const char *oldrefname;
571 const char *conflicting_refname;
572};
573
574static int name_conflict_fn(const char *existingrefname, const unsigned char *sha1,
575 int flags, void *cb_data)
576{
577 struct name_conflict_cb *data = (struct name_conflict_cb *)cb_data;
578 if (data->oldrefname && !strcmp(data->oldrefname, existingrefname))
579 return 0;
580 if (names_conflict(data->refname, existingrefname)) {
581 data->conflicting_refname = existingrefname;
582 return 1;
Michael Haggertyd66da472012-04-10 07:30:17 +0200583 }
Michael Haggerty5a4d4942012-04-10 07:30:19 +0200584 return 0;
Michael Haggertyd66da472012-04-10 07:30:17 +0200585}
586
587/*
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200588 * Return true iff a reference named refname could be created without
Michael Haggerty5a4d4942012-04-10 07:30:19 +0200589 * conflicting with the name of an existing reference in array. If
590 * oldrefname is non-NULL, ignore potential conflicts with oldrefname
591 * (e.g., because oldrefname is scheduled for deletion in the same
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200592 * operation).
593 */
594static int is_refname_available(const char *refname, const char *oldrefname,
Michael Haggertyd3177272012-04-10 07:30:24 +0200595 struct ref_dir *dir)
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200596{
Michael Haggerty5a4d4942012-04-10 07:30:19 +0200597 struct name_conflict_cb data;
598 data.refname = refname;
599 data.oldrefname = oldrefname;
600 data.conflicting_refname = NULL;
601
Michael Haggertyd3177272012-04-10 07:30:24 +0200602 sort_ref_dir(dir);
603 if (do_for_each_ref_in_dir(dir, 0, "", name_conflict_fn,
604 0, DO_FOR_EACH_INCLUDE_BROKEN,
605 &data)) {
Michael Haggerty5a4d4942012-04-10 07:30:19 +0200606 error("'%s' exists; cannot create '%s'",
607 data.conflicting_refname, refname);
608 return 0;
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200609 }
610 return 1;
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700611}
612
Junio C Hamano5e290ff2006-09-30 12:37:37 -0700613/*
614 * Future: need to be in "struct repository"
615 * when doing a full libification.
616 */
Michael Haggerty79c7ca52011-10-17 04:38:05 +0200617static struct ref_cache {
618 struct ref_cache *next;
Michael Haggertyd12229f2012-04-27 00:27:01 +0200619 struct ref_entry *loose;
620 struct ref_entry *packed;
Michael Haggertyce409792011-08-13 00:36:28 +0200621 /* The submodule name, or "" for the main repo. */
622 char name[FLEX_ARRAY];
Michael Haggerty79c7ca52011-10-17 04:38:05 +0200623} *ref_cache;
Michael Haggerty0e88c132011-08-13 00:36:29 +0200624
Michael Haggerty760c4512011-10-17 04:38:09 +0200625static void clear_packed_ref_cache(struct ref_cache *refs)
Junio C Hamano5e290ff2006-09-30 12:37:37 -0700626{
Michael Haggertyd12229f2012-04-27 00:27:01 +0200627 if (refs->packed) {
628 free_ref_entry(refs->packed);
629 refs->packed = NULL;
630 }
Junio C Hamano5e290ff2006-09-30 12:37:37 -0700631}
632
Michael Haggerty760c4512011-10-17 04:38:09 +0200633static void clear_loose_ref_cache(struct ref_cache *refs)
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700634{
Michael Haggertyd12229f2012-04-27 00:27:01 +0200635 if (refs->loose) {
636 free_ref_entry(refs->loose);
637 refs->loose = NULL;
638 }
Michael Haggerty760c4512011-10-17 04:38:09 +0200639}
640
Michael Haggerty79c7ca52011-10-17 04:38:05 +0200641static struct ref_cache *create_ref_cache(const char *submodule)
Michael Haggertye5dbf602011-08-13 00:36:27 +0200642{
Michael Haggertyce409792011-08-13 00:36:28 +0200643 int len;
Michael Haggerty79c7ca52011-10-17 04:38:05 +0200644 struct ref_cache *refs;
Michael Haggertyce409792011-08-13 00:36:28 +0200645 if (!submodule)
646 submodule = "";
647 len = strlen(submodule) + 1;
Michael Haggerty79c7ca52011-10-17 04:38:05 +0200648 refs = xcalloc(1, sizeof(struct ref_cache) + len);
Michael Haggertyce409792011-08-13 00:36:28 +0200649 memcpy(refs->name, submodule, len);
Michael Haggertye5dbf602011-08-13 00:36:27 +0200650 return refs;
651}
652
Michael Haggerty4349a662011-08-13 00:36:25 +0200653/*
Michael Haggerty79c7ca52011-10-17 04:38:05 +0200654 * Return a pointer to a ref_cache for the specified submodule. For
Michael Haggerty4349a662011-08-13 00:36:25 +0200655 * the main repository, use submodule==NULL. The returned structure
656 * will be allocated and initialized but not necessarily populated; it
657 * should not be freed.
658 */
Michael Haggerty79c7ca52011-10-17 04:38:05 +0200659static struct ref_cache *get_ref_cache(const char *submodule)
Michael Haggerty4349a662011-08-13 00:36:25 +0200660{
Michael Haggerty79c7ca52011-10-17 04:38:05 +0200661 struct ref_cache *refs = ref_cache;
Michael Haggerty0e88c132011-08-13 00:36:29 +0200662 if (!submodule)
663 submodule = "";
664 while (refs) {
665 if (!strcmp(submodule, refs->name))
666 return refs;
667 refs = refs->next;
Michael Haggerty4349a662011-08-13 00:36:25 +0200668 }
Michael Haggerty0e88c132011-08-13 00:36:29 +0200669
Michael Haggerty79c7ca52011-10-17 04:38:05 +0200670 refs = create_ref_cache(submodule);
671 refs->next = ref_cache;
672 ref_cache = refs;
Michael Haggerty0e88c132011-08-13 00:36:29 +0200673 return refs;
Michael Haggerty4349a662011-08-13 00:36:25 +0200674}
675
Michael Haggerty8be8bde2011-10-17 04:38:07 +0200676void invalidate_ref_cache(const char *submodule)
Michael Haggertyf130b112011-08-13 00:36:24 +0200677{
Michael Haggertyc5f29ab2011-10-17 04:38:11 +0200678 struct ref_cache *refs = get_ref_cache(submodule);
679 clear_packed_ref_cache(refs);
680 clear_loose_ref_cache(refs);
Michael Haggertyf130b112011-08-13 00:36:24 +0200681}
682
Michael Haggertybc5fd6d2012-04-10 07:30:13 +0200683/*
684 * Parse one line from a packed-refs file. Write the SHA1 to sha1.
685 * Return a pointer to the refname within the line (null-terminated),
686 * or NULL if there was a problem.
687 */
688static const char *parse_ref_line(char *line, unsigned char *sha1)
689{
690 /*
691 * 42: the answer to everything.
692 *
693 * In this case, it happens to be the answer to
694 * 40 (length of sha1 hex representation)
695 * +1 (space in between hex and name)
696 * +1 (newline at the end of the line)
697 */
698 int len = strlen(line) - 42;
699
700 if (len <= 0)
701 return NULL;
702 if (get_sha1_hex(line, sha1) < 0)
703 return NULL;
704 if (!isspace(line[40]))
705 return NULL;
706 line += 41;
707 if (isspace(*line))
708 return NULL;
709 if (line[len] != '\n')
710 return NULL;
711 line[len] = 0;
712
713 return line;
714}
715
Michael Haggertyd3177272012-04-10 07:30:24 +0200716static void read_packed_refs(FILE *f, struct ref_dir *dir)
Junio C Hamanof4204ab2006-11-21 23:36:35 -0800717{
Julian Phillipse9c4c112011-09-29 23:11:42 +0100718 struct ref_entry *last = NULL;
Junio C Hamanof4204ab2006-11-21 23:36:35 -0800719 char refline[PATH_MAX];
720 int flag = REF_ISPACKED;
721
722 while (fgets(refline, sizeof(refline), f)) {
723 unsigned char sha1[20];
Michael Haggertydfefa932011-12-12 06:38:09 +0100724 const char *refname;
Junio C Hamanof4204ab2006-11-21 23:36:35 -0800725 static const char header[] = "# pack-refs with:";
726
727 if (!strncmp(refline, header, sizeof(header)-1)) {
728 const char *traits = refline + sizeof(header) - 1;
729 if (strstr(traits, " peeled "))
730 flag |= REF_KNOWS_PEELED;
731 /* perhaps other traits later as well */
732 continue;
733 }
734
Michael Haggertydfefa932011-12-12 06:38:09 +0100735 refname = parse_ref_line(refline, sha1);
736 if (refname) {
Michael Haggertydd73ecd2011-12-12 06:38:23 +0100737 last = create_ref_entry(refname, sha1, flag, 1);
Michael Haggertyd3177272012-04-10 07:30:24 +0200738 add_ref(dir, last);
Junio C Hamanof4204ab2006-11-21 23:36:35 -0800739 continue;
740 }
741 if (last &&
742 refline[0] == '^' &&
743 strlen(refline) == 42 &&
744 refline[41] == '\n' &&
745 !get_sha1_hex(refline + 1, sha1))
Michael Haggerty593f1bb2012-04-10 07:30:23 +0200746 hashcpy(last->u.value.peeled, sha1);
Junio C Hamanof4204ab2006-11-21 23:36:35 -0800747 }
Junio C Hamanof4204ab2006-11-21 23:36:35 -0800748}
749
Michael Haggertyd3177272012-04-10 07:30:24 +0200750static struct ref_dir *get_packed_refs(struct ref_cache *refs)
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700751{
Michael Haggertyd12229f2012-04-27 00:27:01 +0200752 if (!refs->packed) {
Michael Haggerty4349a662011-08-13 00:36:25 +0200753 const char *packed_refs_file;
754 FILE *f;
Heiko Voigt0bad6112010-07-07 15:39:11 +0200755
Michael Haggertyd12229f2012-04-27 00:27:01 +0200756 refs->packed = create_dir_entry("");
Michael Haggerty316b0972011-12-12 06:38:16 +0100757 if (*refs->name)
758 packed_refs_file = git_path_submodule(refs->name, "packed-refs");
Michael Haggerty4349a662011-08-13 00:36:25 +0200759 else
760 packed_refs_file = git_path("packed-refs");
761 f = fopen(packed_refs_file, "r");
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700762 if (f) {
Michael Haggertyd7826d52012-04-27 00:27:03 +0200763 read_packed_refs(f, get_ref_dir(refs->packed));
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700764 fclose(f);
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700765 }
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700766 }
Michael Haggertyd7826d52012-04-27 00:27:03 +0200767 return get_ref_dir(refs->packed);
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700768}
769
Michael Haggerty30249ee2012-01-17 06:50:33 +0100770void add_packed_ref(const char *refname, const unsigned char *sha1)
771{
772 add_ref(get_packed_refs(get_ref_cache(NULL)),
773 create_ref_entry(refname, sha1, REF_ISPACKED, 1));
774}
775
Michael Haggertyabc39092012-04-25 00:45:10 +0200776/*
777 * Read the loose references for refs from the namespace dirname.
Michael Haggerty9f2fb4a2012-04-25 00:45:12 +0200778 * dirname must end with '/'. dir must be the directory entry
779 * corresponding to dirname.
Michael Haggertyabc39092012-04-25 00:45:10 +0200780 */
Michael Haggerty1900b972012-04-27 00:27:02 +0200781static void read_loose_refs(struct ref_cache *refs, const char *dirname,
782 struct ref_dir *dir)
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700783{
Michael Haggertyd3177272012-04-10 07:30:24 +0200784 DIR *d;
Heiko Voigt0bad6112010-07-07 15:39:11 +0200785 const char *path;
Michael Haggertyd5fdae62012-04-25 00:45:07 +0200786 struct dirent *de;
Michael Haggertyabc39092012-04-25 00:45:10 +0200787 int dirnamelen = strlen(dirname);
Michael Haggerty72b64b42012-04-25 00:45:08 +0200788 struct strbuf refname;
Heiko Voigt0bad6112010-07-07 15:39:11 +0200789
Michael Haggerty3b124822011-12-12 06:38:17 +0100790 if (*refs->name)
Michael Haggerty66a3d202012-04-25 00:45:09 +0200791 path = git_path_submodule(refs->name, "%s", dirname);
Heiko Voigt0bad6112010-07-07 15:39:11 +0200792 else
Michael Haggerty66a3d202012-04-25 00:45:09 +0200793 path = git_path("%s", dirname);
Heiko Voigt0bad6112010-07-07 15:39:11 +0200794
Michael Haggertyd3177272012-04-10 07:30:24 +0200795 d = opendir(path);
Michael Haggertyd5fdae62012-04-25 00:45:07 +0200796 if (!d)
797 return;
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700798
Michael Haggerty66a3d202012-04-25 00:45:09 +0200799 strbuf_init(&refname, dirnamelen + 257);
800 strbuf_add(&refname, dirname, dirnamelen);
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700801
Michael Haggertyd5fdae62012-04-25 00:45:07 +0200802 while ((de = readdir(d)) != NULL) {
803 unsigned char sha1[20];
804 struct stat st;
805 int flag;
Michael Haggertyd5fdae62012-04-25 00:45:07 +0200806 const char *refdir;
807
808 if (de->d_name[0] == '.')
809 continue;
Michael Haggertyd5fdae62012-04-25 00:45:07 +0200810 if (has_extension(de->d_name, ".lock"))
811 continue;
Michael Haggerty72b64b42012-04-25 00:45:08 +0200812 strbuf_addstr(&refname, de->d_name);
Michael Haggertyd5fdae62012-04-25 00:45:07 +0200813 refdir = *refs->name
Michael Haggerty72b64b42012-04-25 00:45:08 +0200814 ? git_path_submodule(refs->name, "%s", refname.buf)
815 : git_path("%s", refname.buf);
816 if (stat(refdir, &st) < 0) {
817 ; /* silently ignore */
818 } else if (S_ISDIR(st.st_mode)) {
Michael Haggertyabc39092012-04-25 00:45:10 +0200819 strbuf_addch(&refname, '/');
Michael Haggerty1900b972012-04-27 00:27:02 +0200820 read_loose_refs(refs, refname.buf,
Michael Haggerty3f3aa1b2012-04-27 00:27:04 +0200821 search_for_subdir(dir, refname.buf, 1));
Michael Haggerty72b64b42012-04-25 00:45:08 +0200822 } else {
823 if (*refs->name) {
824 hashclr(sha1);
825 flag = 0;
826 if (resolve_gitlink_ref(refs->name, refname.buf, sha1) < 0) {
827 hashclr(sha1);
828 flag |= REF_ISBROKEN;
829 }
830 } else if (read_ref_full(refname.buf, sha1, 1, &flag)) {
Junio C Hamano09116a12011-11-16 16:54:32 -0800831 hashclr(sha1);
832 flag |= REF_ISBROKEN;
833 }
Michael Haggerty9f2fb4a2012-04-25 00:45:12 +0200834 add_entry_to_dir(dir,
835 create_ref_entry(refname.buf, sha1, flag, 1));
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700836 }
Michael Haggerty66a3d202012-04-25 00:45:09 +0200837 strbuf_setlen(&refname, dirnamelen);
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700838 }
Michael Haggerty72b64b42012-04-25 00:45:08 +0200839 strbuf_release(&refname);
Michael Haggertyd5fdae62012-04-25 00:45:07 +0200840 closedir(d);
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700841}
842
Michael Haggertyd3177272012-04-10 07:30:24 +0200843static struct ref_dir *get_loose_refs(struct ref_cache *refs)
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700844{
Michael Haggertyd12229f2012-04-27 00:27:01 +0200845 if (!refs->loose) {
846 refs->loose = create_dir_entry("");
Michael Haggerty1900b972012-04-27 00:27:02 +0200847 read_loose_refs(refs, "refs/",
Michael Haggerty3f3aa1b2012-04-27 00:27:04 +0200848 search_for_subdir(get_ref_dir(refs->loose),
849 "refs/", 1));
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700850 }
Michael Haggertyd7826d52012-04-27 00:27:03 +0200851 return get_ref_dir(refs->loose);
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700852}
853
Linus Torvaldsca8db142005-09-25 09:59:37 -0700854/* We allow "recursive" symbolic refs. Only within reason, though */
855#define MAXDEPTH 5
Linus Torvalds0ebde322007-04-09 21:14:26 -0700856#define MAXREFLEN (1024)
857
Junio C Hamanoe5fa45c2011-10-17 11:43:30 -0700858/*
859 * Called by resolve_gitlink_ref_recursive() after it failed to read
Michael Haggertyb0626602011-12-12 06:38:19 +0100860 * from the loose refs in ref_cache refs. Find <refname> in the
861 * packed-refs file for the submodule.
Junio C Hamanoe5fa45c2011-10-17 11:43:30 -0700862 */
Michael Haggertyb0626602011-12-12 06:38:19 +0100863static int resolve_gitlink_packed_ref(struct ref_cache *refs,
Michael Haggerty85be1fe2011-12-12 06:38:10 +0100864 const char *refname, unsigned char *sha1)
Linus Torvalds0ebde322007-04-09 21:14:26 -0700865{
Junio C Hamano2c5c66b2011-10-10 15:56:19 -0700866 struct ref_entry *ref;
Michael Haggertyd3177272012-04-10 07:30:24 +0200867 struct ref_dir *dir = get_packed_refs(refs);
Linus Torvalds0ebde322007-04-09 21:14:26 -0700868
Michael Haggerty432ad412012-04-10 07:30:26 +0200869 ref = find_ref(dir, refname);
Michael Haggertyb0626602011-12-12 06:38:19 +0100870 if (ref == NULL)
871 return -1;
872
Michael Haggerty593f1bb2012-04-10 07:30:23 +0200873 memcpy(sha1, ref->u.value.sha1, 20);
Michael Haggertyb0626602011-12-12 06:38:19 +0100874 return 0;
Linus Torvalds0ebde322007-04-09 21:14:26 -0700875}
876
Michael Haggertyb0626602011-12-12 06:38:19 +0100877static int resolve_gitlink_ref_recursive(struct ref_cache *refs,
Michael Haggerty85be1fe2011-12-12 06:38:10 +0100878 const char *refname, unsigned char *sha1,
Michael Haggertydfefa932011-12-12 06:38:09 +0100879 int recursion)
Linus Torvalds0ebde322007-04-09 21:14:26 -0700880{
Michael Haggerty064d51d2011-12-12 06:38:20 +0100881 int fd, len;
Linus Torvalds0ebde322007-04-09 21:14:26 -0700882 char buffer[128], *p;
Michael Haggerty064d51d2011-12-12 06:38:20 +0100883 char *path;
Linus Torvalds0ebde322007-04-09 21:14:26 -0700884
Michael Haggerty064d51d2011-12-12 06:38:20 +0100885 if (recursion > MAXDEPTH || strlen(refname) > MAXREFLEN)
Linus Torvalds0ebde322007-04-09 21:14:26 -0700886 return -1;
Michael Haggerty064d51d2011-12-12 06:38:20 +0100887 path = *refs->name
888 ? git_path_submodule(refs->name, "%s", refname)
889 : git_path("%s", refname);
890 fd = open(path, O_RDONLY);
Linus Torvalds0ebde322007-04-09 21:14:26 -0700891 if (fd < 0)
Michael Haggertyb0626602011-12-12 06:38:19 +0100892 return resolve_gitlink_packed_ref(refs, refname, sha1);
Linus Torvalds0ebde322007-04-09 21:14:26 -0700893
894 len = read(fd, buffer, sizeof(buffer)-1);
895 close(fd);
896 if (len < 0)
897 return -1;
898 while (len && isspace(buffer[len-1]))
899 len--;
900 buffer[len] = 0;
901
902 /* Was it a detached head or an old-fashioned symlink? */
Michael Haggerty85be1fe2011-12-12 06:38:10 +0100903 if (!get_sha1_hex(buffer, sha1))
Linus Torvalds0ebde322007-04-09 21:14:26 -0700904 return 0;
905
906 /* Symref? */
907 if (strncmp(buffer, "ref:", 4))
908 return -1;
909 p = buffer + 4;
910 while (isspace(*p))
911 p++;
912
Michael Haggerty064d51d2011-12-12 06:38:20 +0100913 return resolve_gitlink_ref_recursive(refs, p, sha1, recursion+1);
Linus Torvalds0ebde322007-04-09 21:14:26 -0700914}
915
Michael Haggerty85be1fe2011-12-12 06:38:10 +0100916int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sha1)
Linus Torvalds0ebde322007-04-09 21:14:26 -0700917{
918 int len = strlen(path), retval;
Michael Haggerty064d51d2011-12-12 06:38:20 +0100919 char *submodule;
Michael Haggertyb0626602011-12-12 06:38:19 +0100920 struct ref_cache *refs;
Linus Torvalds0ebde322007-04-09 21:14:26 -0700921
922 while (len && path[len-1] == '/')
923 len--;
924 if (!len)
925 return -1;
Michael Haggertyb0626602011-12-12 06:38:19 +0100926 submodule = xstrndup(path, len);
927 refs = get_ref_cache(submodule);
928 free(submodule);
Linus Torvalds0ebde322007-04-09 21:14:26 -0700929
Michael Haggerty064d51d2011-12-12 06:38:20 +0100930 retval = resolve_gitlink_ref_recursive(refs, refname, sha1, 0);
Linus Torvalds0ebde322007-04-09 21:14:26 -0700931 return retval;
932}
Linus Torvaldsca8db142005-09-25 09:59:37 -0700933
Christian Couder4886b892008-09-09 07:10:56 +0200934/*
Michael Haggertyc224ca72011-09-15 23:10:35 +0200935 * Try to read ref from the packed references. On success, set sha1
936 * and return 0; otherwise, return -1.
Christian Couder4886b892008-09-09 07:10:56 +0200937 */
Michael Haggertydfefa932011-12-12 06:38:09 +0100938static int get_packed_ref(const char *refname, unsigned char *sha1)
Michael Haggertyc224ca72011-09-15 23:10:35 +0200939{
Michael Haggertyd3177272012-04-10 07:30:24 +0200940 struct ref_dir *packed = get_packed_refs(get_ref_cache(NULL));
Michael Haggerty432ad412012-04-10 07:30:26 +0200941 struct ref_entry *entry = find_ref(packed, refname);
Junio C Hamano2c5c66b2011-10-10 15:56:19 -0700942 if (entry) {
Michael Haggerty593f1bb2012-04-10 07:30:23 +0200943 hashcpy(sha1, entry->u.value.sha1);
Junio C Hamano2c5c66b2011-10-10 15:56:19 -0700944 return 0;
Michael Haggertyc224ca72011-09-15 23:10:35 +0200945 }
946 return -1;
947}
948
Junio C Hamano8d684932011-12-20 13:25:53 -0800949const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int reading, int *flag)
Junio C Hamanoa876ed82005-09-30 14:08:25 -0700950{
Heikki Orsila0104ca02008-04-27 21:21:58 +0300951 int depth = MAXDEPTH;
952 ssize_t len;
Junio C Hamanoa876ed82005-09-30 14:08:25 -0700953 char buffer[256];
Michael Haggertydfefa932011-12-12 06:38:09 +0100954 static char refname_buffer[256];
Junio C Hamanoa876ed82005-09-30 14:08:25 -0700955
Junio C Hamano8da19772006-09-20 22:02:01 -0700956 if (flag)
957 *flag = 0;
958
Michael Haggertydfefa932011-12-12 06:38:09 +0100959 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL))
Michael Haggerty8384d782011-09-15 23:10:39 +0200960 return NULL;
961
Junio C Hamanoa876ed82005-09-30 14:08:25 -0700962 for (;;) {
Junio C Hamano55956352011-10-19 13:55:49 -0700963 char path[PATH_MAX];
Junio C Hamanoa876ed82005-09-30 14:08:25 -0700964 struct stat st;
965 char *buf;
966 int fd;
967
968 if (--depth < 0)
969 return NULL;
970
Michael Haggertydfefa932011-12-12 06:38:09 +0100971 git_snpath(path, sizeof(path), "%s", refname);
Michael Haggertyc224ca72011-09-15 23:10:35 +0200972
Junio C Hamanoa876ed82005-09-30 14:08:25 -0700973 if (lstat(path, &st) < 0) {
Michael Haggertyc224ca72011-09-15 23:10:35 +0200974 if (errno != ENOENT)
Junio C Hamanoa876ed82005-09-30 14:08:25 -0700975 return NULL;
Michael Haggertyc224ca72011-09-15 23:10:35 +0200976 /*
977 * The loose reference file does not exist;
978 * check for a packed reference.
979 */
Michael Haggertydfefa932011-12-12 06:38:09 +0100980 if (!get_packed_ref(refname, sha1)) {
Michael Haggertyc224ca72011-09-15 23:10:35 +0200981 if (flag)
982 *flag |= REF_ISPACKED;
Michael Haggertydfefa932011-12-12 06:38:09 +0100983 return refname;
Michael Haggertyc224ca72011-09-15 23:10:35 +0200984 }
985 /* The reference is not a packed reference, either. */
986 if (reading) {
987 return NULL;
988 } else {
989 hashclr(sha1);
Michael Haggertydfefa932011-12-12 06:38:09 +0100990 return refname;
Michael Haggertyc224ca72011-09-15 23:10:35 +0200991 }
Junio C Hamanoa876ed82005-09-30 14:08:25 -0700992 }
993
994 /* Follow "normalized" - ie "refs/.." symlinks by hand */
995 if (S_ISLNK(st.st_mode)) {
996 len = readlink(path, buffer, sizeof(buffer)-1);
Michael Haggerty7bb2bf82011-09-15 23:10:31 +0200997 if (len < 0)
998 return NULL;
Michael Haggertyb54cb792011-09-15 23:10:32 +0200999 buffer[len] = 0;
Michael Haggerty1f58a032011-09-15 23:10:33 +02001000 if (!prefixcmp(buffer, "refs/") &&
1001 !check_refname_format(buffer, 0)) {
Michael Haggertydfefa932011-12-12 06:38:09 +01001002 strcpy(refname_buffer, buffer);
1003 refname = refname_buffer;
Junio C Hamano8da19772006-09-20 22:02:01 -07001004 if (flag)
1005 *flag |= REF_ISSYMREF;
Junio C Hamanoa876ed82005-09-30 14:08:25 -07001006 continue;
1007 }
1008 }
1009
Dennis Stosberg7a216322006-10-02 19:23:53 +02001010 /* Is it a directory? */
1011 if (S_ISDIR(st.st_mode)) {
1012 errno = EISDIR;
1013 return NULL;
1014 }
1015
Junio C Hamanoa876ed82005-09-30 14:08:25 -07001016 /*
1017 * Anything else, just open it and try to use it as
1018 * a ref
1019 */
1020 fd = open(path, O_RDONLY);
1021 if (fd < 0)
1022 return NULL;
Andy Whitcroft93d26e42007-01-08 15:58:08 +00001023 len = read_in_full(fd, buffer, sizeof(buffer)-1);
Junio C Hamanoa876ed82005-09-30 14:08:25 -07001024 close(fd);
Michael Haggerty28775052011-09-15 23:10:34 +02001025 if (len < 0)
1026 return NULL;
1027 while (len && isspace(buffer[len-1]))
1028 len--;
1029 buffer[len] = '\0';
Junio C Hamanoa876ed82005-09-30 14:08:25 -07001030
1031 /*
1032 * Is it a symbolic ref?
1033 */
Michael Haggerty28775052011-09-15 23:10:34 +02001034 if (prefixcmp(buffer, "ref:"))
Junio C Hamanoa876ed82005-09-30 14:08:25 -07001035 break;
Junio C Hamano55956352011-10-19 13:55:49 -07001036 if (flag)
1037 *flag |= REF_ISSYMREF;
Junio C Hamanoa876ed82005-09-30 14:08:25 -07001038 buf = buffer + 4;
Michael Haggerty28775052011-09-15 23:10:34 +02001039 while (isspace(*buf))
1040 buf++;
Michael Haggerty313fb012011-09-15 23:10:36 +02001041 if (check_refname_format(buf, REFNAME_ALLOW_ONELEVEL)) {
Junio C Hamano55956352011-10-19 13:55:49 -07001042 if (flag)
1043 *flag |= REF_ISBROKEN;
Michael Haggerty313fb012011-09-15 23:10:36 +02001044 return NULL;
1045 }
Michael Haggertydfefa932011-12-12 06:38:09 +01001046 refname = strcpy(refname_buffer, buf);
Junio C Hamanoa876ed82005-09-30 14:08:25 -07001047 }
Michael Haggertyf989fea2011-09-15 23:10:41 +02001048 /* Please note that FETCH_HEAD has a second line containing other data. */
1049 if (get_sha1_hex(buffer, sha1) || (buffer[40] != '\0' && !isspace(buffer[40]))) {
Junio C Hamano55956352011-10-19 13:55:49 -07001050 if (flag)
1051 *flag |= REF_ISBROKEN;
Junio C Hamanoa876ed82005-09-30 14:08:25 -07001052 return NULL;
Michael Haggerty629cd3a2011-09-15 23:10:40 +02001053 }
Michael Haggertydfefa932011-12-12 06:38:09 +01001054 return refname;
Junio C Hamanoa876ed82005-09-30 14:08:25 -07001055}
1056
Nguyễn Thái Ngọc Duy96ec7b12011-12-13 21:17:48 +07001057char *resolve_refdup(const char *ref, unsigned char *sha1, int reading, int *flag)
1058{
Nguyễn Thái Ngọc Duy8cad4742011-12-12 18:20:32 +07001059 const char *ret = resolve_ref_unsafe(ref, sha1, reading, flag);
Nguyễn Thái Ngọc Duy96ec7b12011-12-13 21:17:48 +07001060 return ret ? xstrdup(ret) : NULL;
1061}
1062
Ilari Liusvaarad08bae72010-01-20 11:48:25 +02001063/* The argument to filter_refs */
1064struct ref_filter {
1065 const char *pattern;
1066 each_ref_fn *fn;
1067 void *cb_data;
1068};
1069
Michael Haggertydfefa932011-12-12 06:38:09 +01001070int read_ref_full(const char *refname, unsigned char *sha1, int reading, int *flags)
Linus Torvalds8a65ff72005-07-02 20:23:36 -07001071{
Junio C Hamano8d684932011-12-20 13:25:53 -08001072 if (resolve_ref_unsafe(refname, sha1, reading, flags))
Junio C Hamanoa876ed82005-09-30 14:08:25 -07001073 return 0;
1074 return -1;
Linus Torvalds8a65ff72005-07-02 20:23:36 -07001075}
1076
Michael Haggertydfefa932011-12-12 06:38:09 +01001077int read_ref(const char *refname, unsigned char *sha1)
Nguyễn Thái Ngọc Duyc6893322011-11-13 17:22:14 +07001078{
Michael Haggertydfefa932011-12-12 06:38:09 +01001079 return read_ref_full(refname, sha1, 1, NULL);
Nguyễn Thái Ngọc Duyc6893322011-11-13 17:22:14 +07001080}
1081
Michael Haggertybc5fd6d2012-04-10 07:30:13 +02001082int ref_exists(const char *refname)
Junio C Hamanoef06b912006-11-18 22:13:33 -08001083{
Michael Haggertybc5fd6d2012-04-10 07:30:13 +02001084 unsigned char sha1[20];
1085 return !!resolve_ref_unsafe(refname, sha1, 1, NULL);
Junio C Hamanoef06b912006-11-18 22:13:33 -08001086}
1087
Michael Haggerty85be1fe2011-12-12 06:38:10 +01001088static int filter_refs(const char *refname, const unsigned char *sha1, int flags,
Michael Haggertydfefa932011-12-12 06:38:09 +01001089 void *data)
Ilari Liusvaarad08bae72010-01-20 11:48:25 +02001090{
1091 struct ref_filter *filter = (struct ref_filter *)data;
Michael Haggertydfefa932011-12-12 06:38:09 +01001092 if (fnmatch(filter->pattern, refname, 0))
Ilari Liusvaarad08bae72010-01-20 11:48:25 +02001093 return 0;
Michael Haggerty85be1fe2011-12-12 06:38:10 +01001094 return filter->fn(refname, sha1, flags, filter->cb_data);
Ilari Liusvaarad08bae72010-01-20 11:48:25 +02001095}
1096
Michael Haggertydfefa932011-12-12 06:38:09 +01001097int peel_ref(const char *refname, unsigned char *sha1)
Junio C Hamanocf0adba2006-11-19 13:22:44 -08001098{
1099 int flag;
1100 unsigned char base[20];
1101 struct object *o;
1102
Michael Haggertydfefa932011-12-12 06:38:09 +01001103 if (current_ref && (current_ref->name == refname
1104 || !strcmp(current_ref->name, refname))) {
Shawn O. Pearce0ae91be2008-02-24 03:07:22 -05001105 if (current_ref->flag & REF_KNOWS_PEELED) {
Michael Haggerty593f1bb2012-04-10 07:30:23 +02001106 hashcpy(sha1, current_ref->u.value.peeled);
Shawn O. Pearce0ae91be2008-02-24 03:07:22 -05001107 return 0;
1108 }
Michael Haggerty593f1bb2012-04-10 07:30:23 +02001109 hashcpy(base, current_ref->u.value.sha1);
Shawn O. Pearce0ae91be2008-02-24 03:07:22 -05001110 goto fallback;
1111 }
1112
Michael Haggertydfefa932011-12-12 06:38:09 +01001113 if (read_ref_full(refname, base, 1, &flag))
Junio C Hamanocf0adba2006-11-19 13:22:44 -08001114 return -1;
1115
1116 if ((flag & REF_ISPACKED)) {
Michael Haggertyd3177272012-04-10 07:30:24 +02001117 struct ref_dir *dir = get_packed_refs(get_ref_cache(NULL));
Michael Haggerty432ad412012-04-10 07:30:26 +02001118 struct ref_entry *r = find_ref(dir, refname);
Junio C Hamanocf0adba2006-11-19 13:22:44 -08001119
Julian Phillipse9c4c112011-09-29 23:11:42 +01001120 if (r != NULL && r->flag & REF_KNOWS_PEELED) {
Michael Haggerty593f1bb2012-04-10 07:30:23 +02001121 hashcpy(sha1, r->u.value.peeled);
Julian Phillipse9c4c112011-09-29 23:11:42 +01001122 return 0;
Junio C Hamanocf0adba2006-11-19 13:22:44 -08001123 }
Junio C Hamanocf0adba2006-11-19 13:22:44 -08001124 }
1125
Shawn O. Pearce0ae91be2008-02-24 03:07:22 -05001126fallback:
Junio C Hamanocf0adba2006-11-19 13:22:44 -08001127 o = parse_object(base);
Shawn O. Pearce8c87dc72008-02-24 03:07:19 -05001128 if (o && o->type == OBJ_TAG) {
Michael Haggertydfefa932011-12-12 06:38:09 +01001129 o = deref_tag(o, refname, 0);
Junio C Hamanocf0adba2006-11-19 13:22:44 -08001130 if (o) {
1131 hashcpy(sha1, o->sha1);
1132 return 0;
1133 }
1134 }
1135 return -1;
1136}
1137
Michael Haggertybc5fd6d2012-04-10 07:30:13 +02001138struct warn_if_dangling_data {
1139 FILE *fp;
1140 const char *refname;
1141 const char *msg_fmt;
1142};
1143
1144static int warn_if_dangling_symref(const char *refname, const unsigned char *sha1,
1145 int flags, void *cb_data)
1146{
1147 struct warn_if_dangling_data *d = cb_data;
1148 const char *resolves_to;
1149 unsigned char junk[20];
1150
1151 if (!(flags & REF_ISSYMREF))
1152 return 0;
1153
1154 resolves_to = resolve_ref_unsafe(refname, junk, 0, NULL);
1155 if (!resolves_to || strcmp(resolves_to, d->refname))
1156 return 0;
1157
1158 fprintf(d->fp, d->msg_fmt, refname);
1159 return 0;
1160}
1161
1162void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname)
1163{
1164 struct warn_if_dangling_data data;
1165
1166 data.fp = fp;
1167 data.refname = refname;
1168 data.msg_fmt = msg_fmt;
1169 for_each_rawref(warn_if_dangling_symref, &data);
1170}
1171
Heiko Voigt0bad6112010-07-07 15:39:11 +02001172static int do_for_each_ref(const char *submodule, const char *base, each_ref_fn fn,
1173 int trim, int flags, void *cb_data)
Linus Torvalds8a65ff72005-07-02 20:23:36 -07001174{
Michael Haggerty316b0972011-12-12 06:38:16 +01001175 struct ref_cache *refs = get_ref_cache(submodule);
Michael Haggerty933ac032012-04-10 07:30:27 +02001176 struct ref_dir *packed_dir = get_packed_refs(refs);
1177 struct ref_dir *loose_dir = get_loose_refs(refs);
1178 int retval = 0;
1179
1180 if (base && *base) {
1181 packed_dir = find_containing_dir(packed_dir, base, 0);
1182 loose_dir = find_containing_dir(loose_dir, base, 0);
1183 }
1184
1185 if (packed_dir && loose_dir) {
1186 sort_ref_dir(packed_dir);
1187 sort_ref_dir(loose_dir);
1188 retval = do_for_each_ref_in_dirs(
1189 packed_dir, loose_dir,
1190 base, fn, trim, flags, cb_data);
1191 } else if (packed_dir) {
1192 sort_ref_dir(packed_dir);
1193 retval = do_for_each_ref_in_dir(
1194 packed_dir, 0,
1195 base, fn, trim, flags, cb_data);
1196 } else if (loose_dir) {
1197 sort_ref_dir(loose_dir);
1198 retval = do_for_each_ref_in_dir(
1199 loose_dir, 0,
1200 base, fn, trim, flags, cb_data);
1201 }
1202
1203 return retval;
Linus Torvalds8a65ff72005-07-02 20:23:36 -07001204}
1205
Heiko Voigt0bad6112010-07-07 15:39:11 +02001206static int do_head_ref(const char *submodule, each_ref_fn fn, void *cb_data)
Linus Torvalds723c31f2005-07-05 11:31:32 -07001207{
1208 unsigned char sha1[20];
Junio C Hamano8da19772006-09-20 22:02:01 -07001209 int flag;
1210
Heiko Voigt0bad6112010-07-07 15:39:11 +02001211 if (submodule) {
1212 if (resolve_gitlink_ref(submodule, "HEAD", sha1) == 0)
1213 return fn("HEAD", sha1, 0, cb_data);
1214
1215 return 0;
1216 }
1217
Nguyễn Thái Ngọc Duyc6893322011-11-13 17:22:14 +07001218 if (!read_ref_full("HEAD", sha1, 1, &flag))
Junio C Hamano8da19772006-09-20 22:02:01 -07001219 return fn("HEAD", sha1, flag, cb_data);
Heiko Voigt0bad6112010-07-07 15:39:11 +02001220
Linus Torvalds2f34ba32005-07-05 15:45:00 -07001221 return 0;
Linus Torvalds723c31f2005-07-05 11:31:32 -07001222}
1223
Heiko Voigt0bad6112010-07-07 15:39:11 +02001224int head_ref(each_ref_fn fn, void *cb_data)
1225{
1226 return do_head_ref(NULL, fn, cb_data);
1227}
1228
Heiko Voigt9ef6aeb2010-07-07 15:39:12 +02001229int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
1230{
1231 return do_head_ref(submodule, fn, cb_data);
1232}
1233
Junio C Hamanocb5d7092006-09-20 21:47:42 -07001234int for_each_ref(each_ref_fn fn, void *cb_data)
Linus Torvalds8a65ff72005-07-02 20:23:36 -07001235{
Josh Triplettb3cfc402011-07-05 10:54:21 -07001236 return do_for_each_ref(NULL, "", fn, 0, 0, cb_data);
Seana62be772006-05-13 21:43:00 -04001237}
1238
Heiko Voigt9ef6aeb2010-07-07 15:39:12 +02001239int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
1240{
Josh Triplettb3cfc402011-07-05 10:54:21 -07001241 return do_for_each_ref(submodule, "", fn, 0, 0, cb_data);
Seana62be772006-05-13 21:43:00 -04001242}
1243
Christian Couder2a8177b2009-03-30 05:07:15 +02001244int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data)
1245{
Heiko Voigt0bad6112010-07-07 15:39:11 +02001246 return do_for_each_ref(NULL, prefix, fn, strlen(prefix), 0, cb_data);
Christian Couder2a8177b2009-03-30 05:07:15 +02001247}
1248
Heiko Voigt9ef6aeb2010-07-07 15:39:12 +02001249int for_each_ref_in_submodule(const char *submodule, const char *prefix,
1250 each_ref_fn fn, void *cb_data)
1251{
1252 return do_for_each_ref(submodule, prefix, fn, strlen(prefix), 0, cb_data);
Seana62be772006-05-13 21:43:00 -04001253}
1254
Junio C Hamanocb5d7092006-09-20 21:47:42 -07001255int for_each_tag_ref(each_ref_fn fn, void *cb_data)
Seana62be772006-05-13 21:43:00 -04001256{
Christian Couder2a8177b2009-03-30 05:07:15 +02001257 return for_each_ref_in("refs/tags/", fn, cb_data);
Seana62be772006-05-13 21:43:00 -04001258}
1259
Heiko Voigt9ef6aeb2010-07-07 15:39:12 +02001260int for_each_tag_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
1261{
1262 return for_each_ref_in_submodule(submodule, "refs/tags/", fn, cb_data);
1263}
1264
Junio C Hamanocb5d7092006-09-20 21:47:42 -07001265int for_each_branch_ref(each_ref_fn fn, void *cb_data)
Seana62be772006-05-13 21:43:00 -04001266{
Christian Couder2a8177b2009-03-30 05:07:15 +02001267 return for_each_ref_in("refs/heads/", fn, cb_data);
Seana62be772006-05-13 21:43:00 -04001268}
1269
Heiko Voigt9ef6aeb2010-07-07 15:39:12 +02001270int for_each_branch_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
1271{
1272 return for_each_ref_in_submodule(submodule, "refs/heads/", fn, cb_data);
1273}
1274
Junio C Hamanocb5d7092006-09-20 21:47:42 -07001275int for_each_remote_ref(each_ref_fn fn, void *cb_data)
Seana62be772006-05-13 21:43:00 -04001276{
Christian Couder2a8177b2009-03-30 05:07:15 +02001277 return for_each_ref_in("refs/remotes/", fn, cb_data);
Junio C Hamanof8948e22009-02-08 23:27:10 -08001278}
1279
Heiko Voigt9ef6aeb2010-07-07 15:39:12 +02001280int for_each_remote_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
1281{
1282 return for_each_ref_in_submodule(submodule, "refs/remotes/", fn, cb_data);
1283}
1284
Christian Couder29268702009-01-23 10:06:38 +01001285int for_each_replace_ref(each_ref_fn fn, void *cb_data)
1286{
Heiko Voigt0bad6112010-07-07 15:39:11 +02001287 return do_for_each_ref(NULL, "refs/replace/", fn, 13, 0, cb_data);
Christian Couder29268702009-01-23 10:06:38 +01001288}
1289
Josh Tripletta1bea2c2011-07-05 10:54:44 -07001290int head_ref_namespaced(each_ref_fn fn, void *cb_data)
1291{
1292 struct strbuf buf = STRBUF_INIT;
1293 int ret = 0;
1294 unsigned char sha1[20];
1295 int flag;
1296
1297 strbuf_addf(&buf, "%sHEAD", get_git_namespace());
Nguyễn Thái Ngọc Duyc6893322011-11-13 17:22:14 +07001298 if (!read_ref_full(buf.buf, sha1, 1, &flag))
Josh Tripletta1bea2c2011-07-05 10:54:44 -07001299 ret = fn(buf.buf, sha1, flag, cb_data);
1300 strbuf_release(&buf);
1301
1302 return ret;
1303}
1304
1305int for_each_namespaced_ref(each_ref_fn fn, void *cb_data)
1306{
1307 struct strbuf buf = STRBUF_INIT;
1308 int ret;
1309 strbuf_addf(&buf, "%srefs/", get_git_namespace());
1310 ret = do_for_each_ref(NULL, buf.buf, fn, 0, 0, cb_data);
1311 strbuf_release(&buf);
1312 return ret;
1313}
1314
Ilari Liusvaarab09fe972010-01-20 11:48:26 +02001315int for_each_glob_ref_in(each_ref_fn fn, const char *pattern,
1316 const char *prefix, void *cb_data)
Ilari Liusvaarad08bae72010-01-20 11:48:25 +02001317{
1318 struct strbuf real_pattern = STRBUF_INIT;
1319 struct ref_filter filter;
Ilari Liusvaarad08bae72010-01-20 11:48:25 +02001320 int ret;
1321
Ilari Liusvaarab09fe972010-01-20 11:48:26 +02001322 if (!prefix && prefixcmp(pattern, "refs/"))
Ilari Liusvaarad08bae72010-01-20 11:48:25 +02001323 strbuf_addstr(&real_pattern, "refs/");
Ilari Liusvaarab09fe972010-01-20 11:48:26 +02001324 else if (prefix)
1325 strbuf_addstr(&real_pattern, prefix);
Ilari Liusvaarad08bae72010-01-20 11:48:25 +02001326 strbuf_addstr(&real_pattern, pattern);
1327
Thomas Rast894a9d32010-03-12 18:04:26 +01001328 if (!has_glob_specials(pattern)) {
Junio C Hamano9517e6b2010-02-03 21:23:18 -08001329 /* Append implied '/' '*' if not present. */
Ilari Liusvaarad08bae72010-01-20 11:48:25 +02001330 if (real_pattern.buf[real_pattern.len - 1] != '/')
1331 strbuf_addch(&real_pattern, '/');
1332 /* No need to check for '*', there is none. */
1333 strbuf_addch(&real_pattern, '*');
1334 }
1335
1336 filter.pattern = real_pattern.buf;
1337 filter.fn = fn;
1338 filter.cb_data = cb_data;
1339 ret = for_each_ref(filter_refs, &filter);
1340
1341 strbuf_release(&real_pattern);
1342 return ret;
1343}
1344
Ilari Liusvaarab09fe972010-01-20 11:48:26 +02001345int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data)
1346{
1347 return for_each_glob_ref_in(fn, pattern, NULL, cb_data);
1348}
1349
Junio C Hamanof8948e22009-02-08 23:27:10 -08001350int for_each_rawref(each_ref_fn fn, void *cb_data)
1351{
Josh Triplettb3cfc402011-07-05 10:54:21 -07001352 return do_for_each_ref(NULL, "", fn, 0,
Junio C Hamanof8948e22009-02-08 23:27:10 -08001353 DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
Linus Torvalds8a65ff72005-07-02 20:23:36 -07001354}
1355
Felipe Contreras4577e482009-05-14 00:22:04 +03001356const char *prettify_refname(const char *name)
Daniel Barkalowa9c37a72009-03-08 21:06:05 -04001357{
Daniel Barkalowa9c37a72009-03-08 21:06:05 -04001358 return name + (
1359 !prefixcmp(name, "refs/heads/") ? 11 :
1360 !prefixcmp(name, "refs/tags/") ? 10 :
1361 !prefixcmp(name, "refs/remotes/") ? 13 :
1362 0);
1363}
1364
Steffen Prohaska79803322007-11-11 15:01:46 +01001365const char *ref_rev_parse_rules[] = {
1366 "%.*s",
1367 "refs/%.*s",
1368 "refs/tags/%.*s",
1369 "refs/heads/%.*s",
1370 "refs/remotes/%.*s",
1371 "refs/remotes/%.*s/HEAD",
1372 NULL
1373};
1374
1375int refname_match(const char *abbrev_name, const char *full_name, const char **rules)
1376{
1377 const char **p;
1378 const int abbrev_name_len = strlen(abbrev_name);
1379
1380 for (p = rules; *p; p++) {
1381 if (!strcmp(full_name, mkpath(*p, abbrev_name_len, abbrev_name))) {
1382 return 1;
1383 }
1384 }
1385
1386 return 0;
1387}
1388
Junio C Hamanoe5f38ec2006-06-06 14:04:17 -07001389static struct ref_lock *verify_lock(struct ref_lock *lock,
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001390 const unsigned char *old_sha1, int mustexist)
Daniel Barkalow95fc7512005-06-06 16:31:29 -04001391{
Nguyễn Thái Ngọc Duyc6893322011-11-13 17:22:14 +07001392 if (read_ref_full(lock->ref_name, lock->old_sha1, mustexist, NULL)) {
Linus Torvalds434cd0c2006-09-14 10:14:47 -07001393 error("Can't verify ref %s", lock->ref_name);
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001394 unlock_ref(lock);
1395 return NULL;
1396 }
David Rientjesa89fccd2006-08-17 11:54:57 -07001397 if (hashcmp(lock->old_sha1, old_sha1)) {
Linus Torvalds434cd0c2006-09-14 10:14:47 -07001398 error("Ref %s is at %s but expected %s", lock->ref_name,
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001399 sha1_to_hex(lock->old_sha1), sha1_to_hex(old_sha1));
1400 unlock_ref(lock);
1401 return NULL;
1402 }
1403 return lock;
1404}
1405
Johannes Schindelin7155b722007-09-28 16:28:54 +01001406static int remove_empty_directories(const char *file)
Junio C Hamanobc7127e2006-09-30 02:25:30 -07001407{
1408 /* we want to create a file but there is a directory there;
1409 * if that is an empty directory (or a directory that contains
1410 * only empty directories), remove them.
1411 */
Johannes Schindelin7155b722007-09-28 16:28:54 +01001412 struct strbuf path;
1413 int result;
Junio C Hamanobc7127e2006-09-30 02:25:30 -07001414
Johannes Schindelin7155b722007-09-28 16:28:54 +01001415 strbuf_init(&path, 20);
1416 strbuf_addstr(&path, file);
1417
Junio C Hamanoa0f4afb2009-06-30 15:33:45 -07001418 result = remove_dir_recursively(&path, REMOVE_DIR_EMPTY_ONLY);
Johannes Schindelin7155b722007-09-28 16:28:54 +01001419
1420 strbuf_release(&path);
1421
1422 return result;
Junio C Hamanobc7127e2006-09-30 02:25:30 -07001423}
1424
Michael Haggerty19b68b12011-12-12 06:38:12 +01001425/*
Junio C Hamanoff74f7f2011-10-12 10:35:38 -07001426 * *string and *len will only be substituted, and *string returned (for
1427 * later free()ing) if the string passed in is a magic short-hand form
1428 * to name a branch.
1429 */
1430static char *substitute_branch_name(const char **string, int *len)
1431{
1432 struct strbuf buf = STRBUF_INIT;
1433 int ret = interpret_branch_name(*string, &buf);
1434
1435 if (ret == *len) {
1436 size_t size;
1437 *string = strbuf_detach(&buf, &size);
1438 *len = size;
1439 return (char *)*string;
1440 }
1441
1442 return NULL;
1443}
1444
1445int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref)
1446{
1447 char *last_branch = substitute_branch_name(&str, &len);
1448 const char **p, *r;
1449 int refs_found = 0;
1450
1451 *ref = NULL;
1452 for (p = ref_rev_parse_rules; *p; p++) {
1453 char fullref[PATH_MAX];
1454 unsigned char sha1_from_ref[20];
1455 unsigned char *this_result;
1456 int flag;
1457
1458 this_result = refs_found ? sha1_from_ref : sha1;
1459 mksnpath(fullref, sizeof(fullref), *p, len, str);
Nguyễn Thái Ngọc Duy8cad4742011-12-12 18:20:32 +07001460 r = resolve_ref_unsafe(fullref, this_result, 1, &flag);
Junio C Hamanoff74f7f2011-10-12 10:35:38 -07001461 if (r) {
1462 if (!refs_found++)
1463 *ref = xstrdup(r);
1464 if (!warn_ambiguous_refs)
1465 break;
Junio C Hamano55956352011-10-19 13:55:49 -07001466 } else if ((flag & REF_ISSYMREF) && strcmp(fullref, "HEAD")) {
Junio C Hamanoff74f7f2011-10-12 10:35:38 -07001467 warning("ignoring dangling symref %s.", fullref);
Junio C Hamano55956352011-10-19 13:55:49 -07001468 } else if ((flag & REF_ISBROKEN) && strchr(fullref, '/')) {
1469 warning("ignoring broken ref %s.", fullref);
1470 }
Junio C Hamanoff74f7f2011-10-12 10:35:38 -07001471 }
1472 free(last_branch);
1473 return refs_found;
1474}
1475
1476int dwim_log(const char *str, int len, unsigned char *sha1, char **log)
1477{
1478 char *last_branch = substitute_branch_name(&str, &len);
1479 const char **p;
1480 int logs_found = 0;
1481
1482 *log = NULL;
1483 for (p = ref_rev_parse_rules; *p; p++) {
1484 struct stat st;
1485 unsigned char hash[20];
1486 char path[PATH_MAX];
1487 const char *ref, *it;
1488
1489 mksnpath(path, sizeof(path), *p, len, str);
Nguyễn Thái Ngọc Duy8cad4742011-12-12 18:20:32 +07001490 ref = resolve_ref_unsafe(path, hash, 1, NULL);
Junio C Hamanoff74f7f2011-10-12 10:35:38 -07001491 if (!ref)
1492 continue;
1493 if (!stat(git_path("logs/%s", path), &st) &&
1494 S_ISREG(st.st_mode))
1495 it = path;
1496 else if (strcmp(ref, path) &&
1497 !stat(git_path("logs/%s", ref), &st) &&
1498 S_ISREG(st.st_mode))
1499 it = ref;
1500 else
1501 continue;
1502 if (!logs_found++) {
1503 *log = xstrdup(it);
1504 hashcpy(sha1, hash);
1505 }
1506 if (!warn_ambiguous_refs)
1507 break;
1508 }
1509 free(last_branch);
1510 return logs_found;
1511}
1512
Michael Haggertydfefa932011-12-12 06:38:09 +01001513static struct ref_lock *lock_ref_sha1_basic(const char *refname,
1514 const unsigned char *old_sha1,
1515 int flags, int *type_p)
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001516{
Linus Torvalds434cd0c2006-09-14 10:14:47 -07001517 char *ref_file;
Michael Haggertydfefa932011-12-12 06:38:09 +01001518 const char *orig_refname = refname;
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001519 struct ref_lock *lock;
Junio C Hamano5cc3cef2006-09-30 14:14:31 -07001520 int last_errno = 0;
Junio C Hamanoacd3b9e2008-10-17 15:44:39 -07001521 int type, lflags;
Junio C Hamano4431fcc2006-09-27 01:09:18 -07001522 int mustexist = (old_sha1 && !is_null_sha1(old_sha1));
Clemens Buchacher5bdd8d42008-11-05 21:55:53 +01001523 int missing = 0;
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001524
1525 lock = xcalloc(1, sizeof(struct ref_lock));
1526 lock->lock_fd = -1;
1527
Junio C Hamano8d684932011-12-20 13:25:53 -08001528 refname = resolve_ref_unsafe(refname, lock->old_sha1, mustexist, &type);
Michael Haggertydfefa932011-12-12 06:38:09 +01001529 if (!refname && errno == EISDIR) {
Junio C Hamanobc7127e2006-09-30 02:25:30 -07001530 /* we are trying to lock foo but we used to
1531 * have foo/bar which now does not exist;
1532 * it is normal for the empty directory 'foo'
1533 * to remain.
1534 */
Michael Haggertydfefa932011-12-12 06:38:09 +01001535 ref_file = git_path("%s", orig_refname);
Junio C Hamano5cc3cef2006-09-30 14:14:31 -07001536 if (remove_empty_directories(ref_file)) {
1537 last_errno = errno;
Michael Haggertydfefa932011-12-12 06:38:09 +01001538 error("there are still refs under '%s'", orig_refname);
Junio C Hamano5cc3cef2006-09-30 14:14:31 -07001539 goto error_return;
1540 }
Junio C Hamano8d684932011-12-20 13:25:53 -08001541 refname = resolve_ref_unsafe(orig_refname, lock->old_sha1, mustexist, &type);
Junio C Hamanobc7127e2006-09-30 02:25:30 -07001542 }
Sven Verdoolaege68db31c2007-05-09 12:33:20 +02001543 if (type_p)
1544 *type_p = type;
Michael Haggertydfefa932011-12-12 06:38:09 +01001545 if (!refname) {
Junio C Hamano5cc3cef2006-09-30 14:14:31 -07001546 last_errno = errno;
Shawn Pearce818f4772006-07-28 23:44:51 -04001547 error("unable to resolve reference %s: %s",
Michael Haggertydfefa932011-12-12 06:38:09 +01001548 orig_refname, strerror(errno));
Junio C Hamano5cc3cef2006-09-30 14:14:31 -07001549 goto error_return;
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001550 }
Clemens Buchacher5bdd8d42008-11-05 21:55:53 +01001551 missing = is_null_sha1(lock->old_sha1);
Lars Hjemlic976d412006-11-28 15:47:40 +01001552 /* When the ref did not exist and we are creating it,
1553 * make sure there is no existing ref that is packed
1554 * whose name begins with our refname, nor a ref whose
1555 * name is a proper prefix of our refname.
1556 */
Clemens Buchacher5bdd8d42008-11-05 21:55:53 +01001557 if (missing &&
Michael Haggerty316b0972011-12-12 06:38:16 +01001558 !is_refname_available(refname, NULL, get_packed_refs(get_ref_cache(NULL)))) {
Jeff Kingf475e082009-05-25 06:37:15 -04001559 last_errno = ENOTDIR;
Lars Hjemlic976d412006-11-28 15:47:40 +01001560 goto error_return;
Jeff Kingf475e082009-05-25 06:37:15 -04001561 }
Junio C Hamano22a38442006-09-30 14:19:25 -07001562
Junio C Hamanoc33d5172006-06-06 13:54:14 -07001563 lock->lk = xcalloc(1, sizeof(struct lock_file));
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001564
Junio C Hamanoacd3b9e2008-10-17 15:44:39 -07001565 lflags = LOCK_DIE_ON_ERROR;
1566 if (flags & REF_NODEREF) {
Michael Haggertydfefa932011-12-12 06:38:09 +01001567 refname = orig_refname;
Junio C Hamanoacd3b9e2008-10-17 15:44:39 -07001568 lflags |= LOCK_NODEREF;
1569 }
Michael Haggertydfefa932011-12-12 06:38:09 +01001570 lock->ref_name = xstrdup(refname);
1571 lock->orig_ref_name = xstrdup(orig_refname);
1572 ref_file = git_path("%s", refname);
Clemens Buchacher5bdd8d42008-11-05 21:55:53 +01001573 if (missing)
Sven Verdoolaege68db31c2007-05-09 12:33:20 +02001574 lock->force_write = 1;
1575 if ((flags & REF_NODEREF) && (type & REF_ISSYMREF))
1576 lock->force_write = 1;
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001577
Junio C Hamano5cc3cef2006-09-30 14:14:31 -07001578 if (safe_create_leading_directories(ref_file)) {
1579 last_errno = errno;
1580 error("unable to create directory for %s", ref_file);
1581 goto error_return;
1582 }
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001583
Junio C Hamanoacd3b9e2008-10-17 15:44:39 -07001584 lock->lock_fd = hold_lock_file_for_update(lock->lk, ref_file, lflags);
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001585 return old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock;
Junio C Hamano5cc3cef2006-09-30 14:14:31 -07001586
1587 error_return:
1588 unlock_ref(lock);
1589 errno = last_errno;
1590 return NULL;
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001591}
1592
Michael Haggertydfefa932011-12-12 06:38:09 +01001593struct ref_lock *lock_ref_sha1(const char *refname, const unsigned char *old_sha1)
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001594{
Petr Baudis53cce842006-09-19 22:58:23 +02001595 char refpath[PATH_MAX];
Michael Haggertydfefa932011-12-12 06:38:09 +01001596 if (check_refname_format(refname, 0))
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001597 return NULL;
Michael Haggertydfefa932011-12-12 06:38:09 +01001598 strcpy(refpath, mkpath("refs/%s", refname));
Sven Verdoolaege68db31c2007-05-09 12:33:20 +02001599 return lock_ref_sha1_basic(refpath, old_sha1, 0, NULL);
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001600}
1601
Michael Haggertydfefa932011-12-12 06:38:09 +01001602struct ref_lock *lock_any_ref_for_update(const char *refname,
1603 const unsigned char *old_sha1, int flags)
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001604{
Michael Haggertydfefa932011-12-12 06:38:09 +01001605 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL))
Junio C Hamano257f3022008-01-02 11:14:40 -08001606 return NULL;
Michael Haggertydfefa932011-12-12 06:38:09 +01001607 return lock_ref_sha1_basic(refname, old_sha1, flags, NULL);
Junio C Hamanoc0277d12006-09-30 15:02:00 -07001608}
1609
Michael Haggertyd66da472012-04-10 07:30:17 +02001610struct repack_without_ref_sb {
1611 const char *refname;
1612 int fd;
1613};
1614
1615static int repack_without_ref_fn(const char *refname, const unsigned char *sha1,
1616 int flags, void *cb_data)
1617{
1618 struct repack_without_ref_sb *data = cb_data;
1619 char line[PATH_MAX + 100];
1620 int len;
1621
1622 if (!strcmp(data->refname, refname))
1623 return 0;
1624 len = snprintf(line, sizeof(line), "%s %s\n",
1625 sha1_to_hex(sha1), refname);
1626 /* this should not happen but just being defensive */
1627 if (len > sizeof(line))
1628 die("too long a refname '%s'", refname);
1629 write_or_die(data->fd, line, len);
1630 return 0;
1631}
1632
Junio C Hamano26a063a2006-10-01 11:41:00 -07001633static struct lock_file packlock;
1634
Junio C Hamanoc0277d12006-09-30 15:02:00 -07001635static int repack_without_ref(const char *refname)
1636{
Michael Haggertyd66da472012-04-10 07:30:17 +02001637 struct repack_without_ref_sb data;
Michael Haggertyd3177272012-04-10 07:30:24 +02001638 struct ref_dir *packed = get_packed_refs(get_ref_cache(NULL));
Michael Haggerty432ad412012-04-10 07:30:26 +02001639 if (find_ref(packed, refname) == NULL)
Junio C Hamanoc0277d12006-09-30 15:02:00 -07001640 return 0;
Michael Haggertyd66da472012-04-10 07:30:17 +02001641 data.refname = refname;
1642 data.fd = hold_lock_file_for_update(&packlock, git_path("packed-refs"), 0);
1643 if (data.fd < 0) {
Miklos Vajna1b018fd2009-09-27 01:15:09 +02001644 unable_to_lock_error(git_path("packed-refs"), errno);
Junio C Hamanoc0277d12006-09-30 15:02:00 -07001645 return error("cannot delete '%s' from packed refs", refname);
Miklos Vajna1b018fd2009-09-27 01:15:09 +02001646 }
Michael Haggertyd3177272012-04-10 07:30:24 +02001647 do_for_each_ref_in_dir(packed, 0, "", repack_without_ref_fn, 0, 0, &data);
Junio C Hamanoc0277d12006-09-30 15:02:00 -07001648 return commit_lock_file(&packlock);
1649}
1650
Miklos Vajnaeca35a22008-10-26 03:33:56 +01001651int delete_ref(const char *refname, const unsigned char *sha1, int delopt)
Junio C Hamanoc0277d12006-09-30 15:02:00 -07001652{
1653 struct ref_lock *lock;
Miklos Vajnaeca35a22008-10-26 03:33:56 +01001654 int err, i = 0, ret = 0, flag = 0;
Junio C Hamanoc0277d12006-09-30 15:02:00 -07001655
Sven Verdoolaege68db31c2007-05-09 12:33:20 +02001656 lock = lock_ref_sha1_basic(refname, sha1, 0, &flag);
Junio C Hamanoc0277d12006-09-30 15:02:00 -07001657 if (!lock)
1658 return 1;
Miklos Vajna045a4762008-11-01 00:25:44 +01001659 if (!(flag & REF_ISPACKED) || flag & REF_ISSYMREF) {
Junio C Hamanoc0277d12006-09-30 15:02:00 -07001660 /* loose */
Miklos Vajnaeca35a22008-10-26 03:33:56 +01001661 const char *path;
1662
1663 if (!(delopt & REF_NODEREF)) {
1664 i = strlen(lock->lk->filename) - 5; /* .lock */
1665 lock->lk->filename[i] = 0;
1666 path = lock->lk->filename;
1667 } else {
Daniel Lowe9db56f72008-11-10 16:07:52 -05001668 path = git_path("%s", refname);
Miklos Vajnaeca35a22008-10-26 03:33:56 +01001669 }
Alex Riesen691f1a22009-04-29 23:22:56 +02001670 err = unlink_or_warn(path);
1671 if (err && errno != ENOENT)
Junio C Hamanoc0277d12006-09-30 15:02:00 -07001672 ret = 1;
Alex Riesen691f1a22009-04-29 23:22:56 +02001673
Miklos Vajnaeca35a22008-10-26 03:33:56 +01001674 if (!(delopt & REF_NODEREF))
1675 lock->lk->filename[i] = '.';
Junio C Hamanoc0277d12006-09-30 15:02:00 -07001676 }
1677 /* removing the loose one could have resurrected an earlier
1678 * packed one. Also, if it was not loose we need to repack
1679 * without it.
1680 */
1681 ret |= repack_without_ref(refname);
1682
Alex Riesen691f1a22009-04-29 23:22:56 +02001683 unlink_or_warn(git_path("logs/%s", lock->ref_name));
Michael Haggerty3870a0d2011-10-17 04:38:06 +02001684 invalidate_ref_cache(NULL);
Junio C Hamanoc0277d12006-09-30 15:02:00 -07001685 unlock_ref(lock);
1686 return ret;
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001687}
1688
Pierre Habouzit765c2252010-07-07 09:47:20 +02001689/*
1690 * People using contrib's git-new-workdir have .git/logs/refs ->
1691 * /some/other/path/.git/logs/refs, and that may live on another device.
1692 *
1693 * IOW, to avoid cross device rename errors, the temporary renamed log must
1694 * live into logs/refs.
1695 */
1696#define TMP_RENAMED_LOG "logs/refs/.tmp-renamed-log"
1697
Michael Haggertydfefa932011-12-12 06:38:09 +01001698int rename_ref(const char *oldrefname, const char *newrefname, const char *logmsg)
Lars Hjemlic976d412006-11-28 15:47:40 +01001699{
Lars Hjemlic976d412006-11-28 15:47:40 +01001700 unsigned char sha1[20], orig_sha1[20];
1701 int flag = 0, logmoved = 0;
1702 struct ref_lock *lock;
Lars Hjemlic976d412006-11-28 15:47:40 +01001703 struct stat loginfo;
Michael Haggertydfefa932011-12-12 06:38:09 +01001704 int log = !lstat(git_path("logs/%s", oldrefname), &loginfo);
Miklos Vajnaeca35a22008-10-26 03:33:56 +01001705 const char *symref = NULL;
Michael Haggerty316b0972011-12-12 06:38:16 +01001706 struct ref_cache *refs = get_ref_cache(NULL);
Lars Hjemlic976d412006-11-28 15:47:40 +01001707
Miklos Vajna450d4c02008-10-26 03:33:57 +01001708 if (log && S_ISLNK(loginfo.st_mode))
Michael Haggertydfefa932011-12-12 06:38:09 +01001709 return error("reflog for %s is a symlink", oldrefname);
Lars Hjemlic976d412006-11-28 15:47:40 +01001710
Junio C Hamano8d684932011-12-20 13:25:53 -08001711 symref = resolve_ref_unsafe(oldrefname, orig_sha1, 1, &flag);
Miklos Vajnaeca35a22008-10-26 03:33:56 +01001712 if (flag & REF_ISSYMREF)
Miklos Vajnafa581862008-10-29 01:05:27 +01001713 return error("refname %s is a symbolic ref, renaming it is not supported",
Michael Haggertydfefa932011-12-12 06:38:09 +01001714 oldrefname);
Miklos Vajnaeca35a22008-10-26 03:33:56 +01001715 if (!symref)
Michael Haggertydfefa932011-12-12 06:38:09 +01001716 return error("refname %s not found", oldrefname);
Lars Hjemlic976d412006-11-28 15:47:40 +01001717
Michael Haggerty316b0972011-12-12 06:38:16 +01001718 if (!is_refname_available(newrefname, oldrefname, get_packed_refs(refs)))
Lars Hjemlic976d412006-11-28 15:47:40 +01001719 return 1;
1720
Michael Haggerty316b0972011-12-12 06:38:16 +01001721 if (!is_refname_available(newrefname, oldrefname, get_loose_refs(refs)))
Lars Hjemlic976d412006-11-28 15:47:40 +01001722 return 1;
1723
Michael Haggertydfefa932011-12-12 06:38:09 +01001724 if (log && rename(git_path("logs/%s", oldrefname), git_path(TMP_RENAMED_LOG)))
Pierre Habouzit765c2252010-07-07 09:47:20 +02001725 return error("unable to move logfile logs/%s to "TMP_RENAMED_LOG": %s",
Michael Haggertydfefa932011-12-12 06:38:09 +01001726 oldrefname, strerror(errno));
Lars Hjemlic976d412006-11-28 15:47:40 +01001727
Michael Haggertydfefa932011-12-12 06:38:09 +01001728 if (delete_ref(oldrefname, orig_sha1, REF_NODEREF)) {
1729 error("unable to delete old %s", oldrefname);
Lars Hjemlic976d412006-11-28 15:47:40 +01001730 goto rollback;
1731 }
1732
Michael Haggertydfefa932011-12-12 06:38:09 +01001733 if (!read_ref_full(newrefname, sha1, 1, &flag) &&
1734 delete_ref(newrefname, sha1, REF_NODEREF)) {
Lars Hjemlic976d412006-11-28 15:47:40 +01001735 if (errno==EISDIR) {
Michael Haggertydfefa932011-12-12 06:38:09 +01001736 if (remove_empty_directories(git_path("%s", newrefname))) {
1737 error("Directory not empty: %s", newrefname);
Lars Hjemlic976d412006-11-28 15:47:40 +01001738 goto rollback;
1739 }
1740 } else {
Michael Haggertydfefa932011-12-12 06:38:09 +01001741 error("unable to delete existing %s", newrefname);
Lars Hjemlic976d412006-11-28 15:47:40 +01001742 goto rollback;
1743 }
1744 }
1745
Michael Haggertydfefa932011-12-12 06:38:09 +01001746 if (log && safe_create_leading_directories(git_path("logs/%s", newrefname))) {
1747 error("unable to create directory for %s", newrefname);
Lars Hjemlic976d412006-11-28 15:47:40 +01001748 goto rollback;
1749 }
1750
1751 retry:
Michael Haggertydfefa932011-12-12 06:38:09 +01001752 if (log && rename(git_path(TMP_RENAMED_LOG), git_path("logs/%s", newrefname))) {
Jason Riedyd9e74d52007-01-15 17:30:59 -08001753 if (errno==EISDIR || errno==ENOTDIR) {
1754 /*
1755 * rename(a, b) when b is an existing
1756 * directory ought to result in ISDIR, but
1757 * Solaris 5.8 gives ENOTDIR. Sheesh.
1758 */
Michael Haggertydfefa932011-12-12 06:38:09 +01001759 if (remove_empty_directories(git_path("logs/%s", newrefname))) {
1760 error("Directory not empty: logs/%s", newrefname);
Lars Hjemlic976d412006-11-28 15:47:40 +01001761 goto rollback;
1762 }
1763 goto retry;
1764 } else {
Pierre Habouzit765c2252010-07-07 09:47:20 +02001765 error("unable to move logfile "TMP_RENAMED_LOG" to logs/%s: %s",
Michael Haggertydfefa932011-12-12 06:38:09 +01001766 newrefname, strerror(errno));
Lars Hjemlic976d412006-11-28 15:47:40 +01001767 goto rollback;
1768 }
1769 }
1770 logmoved = log;
1771
Michael Haggertydfefa932011-12-12 06:38:09 +01001772 lock = lock_ref_sha1_basic(newrefname, NULL, 0, NULL);
Lars Hjemlic976d412006-11-28 15:47:40 +01001773 if (!lock) {
Michael Haggertydfefa932011-12-12 06:38:09 +01001774 error("unable to lock %s for update", newrefname);
Lars Hjemlic976d412006-11-28 15:47:40 +01001775 goto rollback;
1776 }
Lars Hjemlic976d412006-11-28 15:47:40 +01001777 lock->force_write = 1;
1778 hashcpy(lock->old_sha1, orig_sha1);
Lars Hjemli678d0f42006-11-30 03:16:56 +01001779 if (write_ref_sha1(lock, orig_sha1, logmsg)) {
Michael Haggertydfefa932011-12-12 06:38:09 +01001780 error("unable to write current sha1 into %s", newrefname);
Lars Hjemlic976d412006-11-28 15:47:40 +01001781 goto rollback;
1782 }
1783
1784 return 0;
1785
1786 rollback:
Michael Haggertydfefa932011-12-12 06:38:09 +01001787 lock = lock_ref_sha1_basic(oldrefname, NULL, 0, NULL);
Lars Hjemlic976d412006-11-28 15:47:40 +01001788 if (!lock) {
Michael Haggertydfefa932011-12-12 06:38:09 +01001789 error("unable to lock %s for rollback", oldrefname);
Lars Hjemlic976d412006-11-28 15:47:40 +01001790 goto rollbacklog;
1791 }
1792
1793 lock->force_write = 1;
1794 flag = log_all_ref_updates;
1795 log_all_ref_updates = 0;
1796 if (write_ref_sha1(lock, orig_sha1, NULL))
Michael Haggertydfefa932011-12-12 06:38:09 +01001797 error("unable to write current sha1 into %s", oldrefname);
Lars Hjemlic976d412006-11-28 15:47:40 +01001798 log_all_ref_updates = flag;
1799
1800 rollbacklog:
Michael Haggertydfefa932011-12-12 06:38:09 +01001801 if (logmoved && rename(git_path("logs/%s", newrefname), git_path("logs/%s", oldrefname)))
Lars Hjemlic976d412006-11-28 15:47:40 +01001802 error("unable to restore logfile %s from %s: %s",
Michael Haggertydfefa932011-12-12 06:38:09 +01001803 oldrefname, newrefname, strerror(errno));
Lars Hjemlic976d412006-11-28 15:47:40 +01001804 if (!logmoved && log &&
Michael Haggertydfefa932011-12-12 06:38:09 +01001805 rename(git_path(TMP_RENAMED_LOG), git_path("logs/%s", oldrefname)))
Pierre Habouzit765c2252010-07-07 09:47:20 +02001806 error("unable to restore logfile %s from "TMP_RENAMED_LOG": %s",
Michael Haggertydfefa932011-12-12 06:38:09 +01001807 oldrefname, strerror(errno));
Lars Hjemlic976d412006-11-28 15:47:40 +01001808
1809 return 1;
1810}
1811
Brandon Casey435fc852008-02-22 12:57:30 -06001812int close_ref(struct ref_lock *lock)
Brandon Caseyb5313942008-01-16 13:14:30 -06001813{
1814 if (close_lock_file(lock->lk))
1815 return -1;
1816 lock->lock_fd = -1;
1817 return 0;
1818}
1819
Brandon Casey435fc852008-02-22 12:57:30 -06001820int commit_ref(struct ref_lock *lock)
Brandon Caseyb5313942008-01-16 13:14:30 -06001821{
1822 if (commit_lock_file(lock->lk))
1823 return -1;
1824 lock->lock_fd = -1;
1825 return 0;
1826}
1827
Junio C Hamanoe5f38ec2006-06-06 14:04:17 -07001828void unlock_ref(struct ref_lock *lock)
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001829{
Brandon Casey4ed7cd32008-01-16 13:12:46 -06001830 /* Do not free lock->lk -- atexit() still looks at them */
1831 if (lock->lk)
1832 rollback_lock_file(lock->lk);
Linus Torvalds434cd0c2006-09-14 10:14:47 -07001833 free(lock->ref_name);
Nicolas Pitre16557072007-01-26 17:26:06 -05001834 free(lock->orig_ref_name);
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001835 free(lock);
1836}
1837
Junio C Hamano0ec29a42007-07-28 17:17:17 -07001838/*
1839 * copy the reflog message msg to buf, which has been allocated sufficiently
1840 * large, while cleaning up the whitespaces. Especially, convert LF to space,
1841 * because reflog file is one line per entry.
1842 */
1843static int copy_msg(char *buf, const char *msg)
1844{
1845 char *cp = buf;
1846 char c;
1847 int wasspace = 1;
1848
1849 *cp++ = '\t';
1850 while ((c = *msg++)) {
1851 if (wasspace && isspace(c))
1852 continue;
1853 wasspace = isspace(c);
1854 if (wasspace)
1855 c = ' ';
1856 *cp++ = c;
1857 }
1858 while (buf < cp && isspace(cp[-1]))
1859 cp--;
1860 *cp++ = '\n';
1861 return cp - buf;
1862}
1863
Michael Haggertydfefa932011-12-12 06:38:09 +01001864int log_ref_setup(const char *refname, char *logfile, int bufsize)
Erick Mattos859c3012010-05-21 21:28:36 -03001865{
1866 int logfd, oflags = O_APPEND | O_WRONLY;
Erick Mattos859c3012010-05-21 21:28:36 -03001867
Michael Haggertydfefa932011-12-12 06:38:09 +01001868 git_snpath(logfile, bufsize, "logs/%s", refname);
Erick Mattos859c3012010-05-21 21:28:36 -03001869 if (log_all_ref_updates &&
Michael Haggertydfefa932011-12-12 06:38:09 +01001870 (!prefixcmp(refname, "refs/heads/") ||
1871 !prefixcmp(refname, "refs/remotes/") ||
1872 !prefixcmp(refname, "refs/notes/") ||
1873 !strcmp(refname, "HEAD"))) {
Thomas Rast157aaea2010-06-10 14:54:03 +02001874 if (safe_create_leading_directories(logfile) < 0)
Erick Mattos859c3012010-05-21 21:28:36 -03001875 return error("unable to create directory for %s",
Thomas Rast157aaea2010-06-10 14:54:03 +02001876 logfile);
Erick Mattos859c3012010-05-21 21:28:36 -03001877 oflags |= O_CREAT;
1878 }
1879
Thomas Rast157aaea2010-06-10 14:54:03 +02001880 logfd = open(logfile, oflags, 0666);
Erick Mattos859c3012010-05-21 21:28:36 -03001881 if (logfd < 0) {
1882 if (!(oflags & O_CREAT) && errno == ENOENT)
1883 return 0;
1884
1885 if ((oflags & O_CREAT) && errno == EISDIR) {
Thomas Rast157aaea2010-06-10 14:54:03 +02001886 if (remove_empty_directories(logfile)) {
Erick Mattos859c3012010-05-21 21:28:36 -03001887 return error("There are still logs under '%s'",
Thomas Rast157aaea2010-06-10 14:54:03 +02001888 logfile);
Erick Mattos859c3012010-05-21 21:28:36 -03001889 }
Thomas Rast157aaea2010-06-10 14:54:03 +02001890 logfd = open(logfile, oflags, 0666);
Erick Mattos859c3012010-05-21 21:28:36 -03001891 }
1892
1893 if (logfd < 0)
1894 return error("Unable to append to %s: %s",
Thomas Rast157aaea2010-06-10 14:54:03 +02001895 logfile, strerror(errno));
Erick Mattos859c3012010-05-21 21:28:36 -03001896 }
1897
Thomas Rast157aaea2010-06-10 14:54:03 +02001898 adjust_shared_perm(logfile);
Erick Mattos859c3012010-05-21 21:28:36 -03001899 close(logfd);
1900 return 0;
1901}
1902
Michael Haggertydfefa932011-12-12 06:38:09 +01001903static int log_ref_write(const char *refname, const unsigned char *old_sha1,
Nicolas Pitre9a13f0b2007-01-26 17:26:05 -05001904 const unsigned char *new_sha1, const char *msg)
Shawn Pearce6de08ae2006-05-17 05:55:40 -04001905{
Erick Mattos859c3012010-05-21 21:28:36 -03001906 int logfd, result, written, oflags = O_APPEND | O_WRONLY;
Shawn Pearce6de08ae2006-05-17 05:55:40 -04001907 unsigned maxlen, len;
Junio C Hamano8ac65932007-01-26 02:26:04 -08001908 int msglen;
Thomas Rast157aaea2010-06-10 14:54:03 +02001909 char log_file[PATH_MAX];
Alex Riesen958a4782008-10-27 11:11:40 +01001910 char *logrec;
Alp Tokerff4c8482006-07-09 10:36:24 +01001911 const char *committer;
Shawn Pearce6de08ae2006-05-17 05:55:40 -04001912
Junio C Hamano510c5a82007-01-07 01:35:34 -08001913 if (log_all_ref_updates < 0)
Junio C Hamano7d1864c2007-01-07 02:00:28 -08001914 log_all_ref_updates = !is_bare_repository();
Junio C Hamano510c5a82007-01-07 01:35:34 -08001915
Michael Haggertydfefa932011-12-12 06:38:09 +01001916 result = log_ref_setup(refname, log_file, sizeof(log_file));
Erick Mattos859c3012010-05-21 21:28:36 -03001917 if (result)
1918 return result;
Nicolas Pitre9a13f0b2007-01-26 17:26:05 -05001919
Erick Mattos859c3012010-05-21 21:28:36 -03001920 logfd = open(log_file, oflags);
1921 if (logfd < 0)
1922 return 0;
Junio C Hamano0ec29a42007-07-28 17:17:17 -07001923 msglen = msg ? strlen(msg) : 0;
Junio C Hamano774751a2007-12-08 17:32:08 -08001924 committer = git_committer_info(0);
Junio C Hamano8ac65932007-01-26 02:26:04 -08001925 maxlen = strlen(committer) + msglen + 100;
1926 logrec = xmalloc(maxlen);
1927 len = sprintf(logrec, "%s %s %s\n",
Nicolas Pitre9a13f0b2007-01-26 17:26:05 -05001928 sha1_to_hex(old_sha1),
1929 sha1_to_hex(new_sha1),
Junio C Hamano8ac65932007-01-26 02:26:04 -08001930 committer);
1931 if (msglen)
Junio C Hamano0ec29a42007-07-28 17:17:17 -07001932 len += copy_msg(logrec + len - 1, msg) - 1;
Andy Whitcroft93822c22007-01-08 15:58:23 +00001933 written = len <= maxlen ? write_in_full(logfd, logrec, len) : -1;
Shawn Pearce6de08ae2006-05-17 05:55:40 -04001934 free(logrec);
Jim Meyering91c8d592007-06-24 21:20:41 +02001935 if (close(logfd) != 0 || written != len)
Nicolas Pitre9a13f0b2007-01-26 17:26:05 -05001936 return error("Unable to append to %s", log_file);
Shawn Pearce6de08ae2006-05-17 05:55:40 -04001937 return 0;
1938}
1939
Linus Torvaldsc3b0dec2008-01-15 15:50:17 -08001940static int is_branch(const char *refname)
1941{
1942 return !strcmp(refname, "HEAD") || !prefixcmp(refname, "refs/heads/");
1943}
1944
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001945int write_ref_sha1(struct ref_lock *lock,
1946 const unsigned char *sha1, const char *logmsg)
1947{
1948 static char term = '\n';
Linus Torvaldsc3b0dec2008-01-15 15:50:17 -08001949 struct object *o;
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001950
1951 if (!lock)
1952 return -1;
David Rientjesa89fccd2006-08-17 11:54:57 -07001953 if (!lock->force_write && !hashcmp(lock->old_sha1, sha1)) {
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001954 unlock_ref(lock);
1955 return 0;
1956 }
Linus Torvaldsc3b0dec2008-01-15 15:50:17 -08001957 o = parse_object(sha1);
1958 if (!o) {
Dmitry Ivankov7be8b3b2011-06-16 19:42:48 +06001959 error("Trying to write ref %s with nonexistent object %s",
Linus Torvaldsc3b0dec2008-01-15 15:50:17 -08001960 lock->ref_name, sha1_to_hex(sha1));
1961 unlock_ref(lock);
1962 return -1;
1963 }
1964 if (o->type != OBJ_COMMIT && is_branch(lock->ref_name)) {
1965 error("Trying to write non-commit object %s to branch %s",
1966 sha1_to_hex(sha1), lock->ref_name);
1967 unlock_ref(lock);
1968 return -1;
1969 }
Andy Whitcroft93822c22007-01-08 15:58:23 +00001970 if (write_in_full(lock->lock_fd, sha1_to_hex(sha1), 40) != 40 ||
1971 write_in_full(lock->lock_fd, &term, 1) != 1
Brandon Caseyb5313942008-01-16 13:14:30 -06001972 || close_ref(lock) < 0) {
Junio C Hamanoc33d5172006-06-06 13:54:14 -07001973 error("Couldn't write %s", lock->lk->filename);
Shawn Pearce4bd18c42006-05-17 05:55:02 -04001974 unlock_ref(lock);
1975 return -1;
1976 }
Michael Haggerty8bf90dc2011-10-17 04:38:10 +02001977 clear_loose_ref_cache(get_ref_cache(NULL));
Nicolas Pitrebd104db2007-01-26 17:26:07 -05001978 if (log_ref_write(lock->ref_name, lock->old_sha1, sha1, logmsg) < 0 ||
1979 (strcmp(lock->ref_name, lock->orig_ref_name) &&
1980 log_ref_write(lock->orig_ref_name, lock->old_sha1, sha1, logmsg) < 0)) {
Shawn Pearce6de08ae2006-05-17 05:55:40 -04001981 unlock_ref(lock);
1982 return -1;
1983 }
Nicolas Pitre605fac82007-03-21 17:11:44 -04001984 if (strcmp(lock->orig_ref_name, "HEAD") != 0) {
1985 /*
1986 * Special hack: If a branch is updated directly and HEAD
1987 * points to it (may happen on the remote side of a push
1988 * for example) then logically the HEAD reflog should be
1989 * updated too.
1990 * A generic solution implies reverse symref information,
1991 * but finding all symrefs pointing to the given branch
1992 * would be rather costly for this rare event (the direct
1993 * update of a branch) to be worth it. So let's cheat and
1994 * check with HEAD only which should cover 99% of all usage
1995 * scenarios (even 100% of the default ones).
1996 */
1997 unsigned char head_sha1[20];
1998 int head_flag;
1999 const char *head_ref;
Nguyễn Thái Ngọc Duy8cad4742011-12-12 18:20:32 +07002000 head_ref = resolve_ref_unsafe("HEAD", head_sha1, 1, &head_flag);
Nicolas Pitre605fac82007-03-21 17:11:44 -04002001 if (head_ref && (head_flag & REF_ISSYMREF) &&
2002 !strcmp(head_ref, lock->ref_name))
2003 log_ref_write("HEAD", lock->old_sha1, sha1, logmsg);
2004 }
Brandon Caseyb5313942008-01-16 13:14:30 -06002005 if (commit_ref(lock)) {
Linus Torvalds434cd0c2006-09-14 10:14:47 -07002006 error("Couldn't set %s", lock->ref_name);
Shawn Pearce4bd18c42006-05-17 05:55:02 -04002007 unlock_ref(lock);
2008 return -1;
2009 }
Shawn Pearce4bd18c42006-05-17 05:55:02 -04002010 unlock_ref(lock);
2011 return 0;
Daniel Barkalow95fc7512005-06-06 16:31:29 -04002012}
Shawn Pearced556fae2006-05-17 05:56:09 -04002013
Nicolas Pitre8b5157e2007-01-26 17:26:10 -05002014int create_symref(const char *ref_target, const char *refs_heads_master,
2015 const char *logmsg)
Nicolas Pitre41b625b2007-01-26 17:26:09 -05002016{
2017 const char *lockpath;
2018 char ref[1000];
2019 int fd, len, written;
Alex Riesena4f34cb2008-10-27 11:22:09 +01002020 char *git_HEAD = git_pathdup("%s", ref_target);
Nicolas Pitre8b5157e2007-01-26 17:26:10 -05002021 unsigned char old_sha1[20], new_sha1[20];
2022
2023 if (logmsg && read_ref(ref_target, old_sha1))
2024 hashclr(old_sha1);
Nicolas Pitre41b625b2007-01-26 17:26:09 -05002025
Junio C Hamanod48744d2007-02-07 23:41:43 -08002026 if (safe_create_leading_directories(git_HEAD) < 0)
2027 return error("unable to create directory for %s", git_HEAD);
2028
Nicolas Pitre41b625b2007-01-26 17:26:09 -05002029#ifndef NO_SYMLINK_HEAD
2030 if (prefer_symlink_refs) {
2031 unlink(git_HEAD);
2032 if (!symlink(refs_heads_master, git_HEAD))
Nicolas Pitre8b5157e2007-01-26 17:26:10 -05002033 goto done;
Nicolas Pitre41b625b2007-01-26 17:26:09 -05002034 fprintf(stderr, "no symlink - falling back to symbolic ref\n");
2035 }
2036#endif
2037
2038 len = snprintf(ref, sizeof(ref), "ref: %s\n", refs_heads_master);
2039 if (sizeof(ref) <= len) {
2040 error("refname too long: %s", refs_heads_master);
Junio C Hamano47fc52e2007-01-26 17:49:00 -08002041 goto error_free_return;
Nicolas Pitre41b625b2007-01-26 17:26:09 -05002042 }
2043 lockpath = mkpath("%s.lock", git_HEAD);
2044 fd = open(lockpath, O_CREAT | O_EXCL | O_WRONLY, 0666);
2045 if (fd < 0) {
2046 error("Unable to open %s for writing", lockpath);
Junio C Hamano47fc52e2007-01-26 17:49:00 -08002047 goto error_free_return;
Nicolas Pitre41b625b2007-01-26 17:26:09 -05002048 }
2049 written = write_in_full(fd, ref, len);
Jim Meyering91c8d592007-06-24 21:20:41 +02002050 if (close(fd) != 0 || written != len) {
Nicolas Pitre41b625b2007-01-26 17:26:09 -05002051 error("Unable to write to %s", lockpath);
Junio C Hamano47fc52e2007-01-26 17:49:00 -08002052 goto error_unlink_return;
Nicolas Pitre41b625b2007-01-26 17:26:09 -05002053 }
2054 if (rename(lockpath, git_HEAD) < 0) {
Nicolas Pitre41b625b2007-01-26 17:26:09 -05002055 error("Unable to create %s", git_HEAD);
Junio C Hamano47fc52e2007-01-26 17:49:00 -08002056 goto error_unlink_return;
Nicolas Pitre41b625b2007-01-26 17:26:09 -05002057 }
2058 if (adjust_shared_perm(git_HEAD)) {
Nicolas Pitre41b625b2007-01-26 17:26:09 -05002059 error("Unable to fix permissions on %s", lockpath);
Junio C Hamano47fc52e2007-01-26 17:49:00 -08002060 error_unlink_return:
Alex Riesen691f1a22009-04-29 23:22:56 +02002061 unlink_or_warn(lockpath);
Junio C Hamano47fc52e2007-01-26 17:49:00 -08002062 error_free_return:
2063 free(git_HEAD);
2064 return -1;
Nicolas Pitre41b625b2007-01-26 17:26:09 -05002065 }
Nicolas Pitre8b5157e2007-01-26 17:26:10 -05002066
Ramsay Jonesee96d112007-03-03 18:28:46 +00002067#ifndef NO_SYMLINK_HEAD
Nicolas Pitre8b5157e2007-01-26 17:26:10 -05002068 done:
Ramsay Jonesee96d112007-03-03 18:28:46 +00002069#endif
Nicolas Pitre8b5157e2007-01-26 17:26:10 -05002070 if (logmsg && !read_ref(refs_heads_master, new_sha1))
2071 log_ref_write(ref_target, old_sha1, new_sha1, logmsg);
2072
Junio C Hamano47fc52e2007-01-26 17:49:00 -08002073 free(git_HEAD);
Nicolas Pitre41b625b2007-01-26 17:26:09 -05002074 return 0;
2075}
2076
Junio C Hamano16d7cc92007-01-19 01:19:05 -08002077static char *ref_msg(const char *line, const char *endp)
2078{
2079 const char *ep;
Junio C Hamano16d7cc92007-01-19 01:19:05 -08002080 line += 82;
Pierre Habouzit182af832007-09-16 00:32:36 +02002081 ep = memchr(line, '\n', endp - line);
2082 if (!ep)
2083 ep = endp;
2084 return xmemdupz(line, ep - line);
Junio C Hamano16d7cc92007-01-19 01:19:05 -08002085}
2086
Michael Haggertydfefa932011-12-12 06:38:09 +01002087int read_ref_at(const char *refname, unsigned long at_time, int cnt,
2088 unsigned char *sha1, char **msg,
2089 unsigned long *cutoff_time, int *cutoff_tz, int *cutoff_cnt)
Shawn Pearced556fae2006-05-17 05:56:09 -04002090{
Shawn Pearcee5229042006-05-19 03:28:19 -04002091 const char *logfile, *logdata, *logend, *rec, *lastgt, *lastrec;
Shawn Pearced556fae2006-05-17 05:56:09 -04002092 char *tz_c;
Junio C Hamanoe29cb532006-12-18 22:07:45 -08002093 int logfd, tz, reccnt = 0;
Shawn Pearced556fae2006-05-17 05:56:09 -04002094 struct stat st;
2095 unsigned long date;
Shawn Pearcee5229042006-05-19 03:28:19 -04002096 unsigned char logged_sha1[20];
Junio C Hamanocb48cb52007-01-19 00:39:32 -08002097 void *log_mapped;
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05002098 size_t mapsz;
Shawn Pearced556fae2006-05-17 05:56:09 -04002099
Michael Haggertydfefa932011-12-12 06:38:09 +01002100 logfile = git_path("logs/%s", refname);
Shawn Pearced556fae2006-05-17 05:56:09 -04002101 logfd = open(logfile, O_RDONLY, 0);
2102 if (logfd < 0)
Thomas Rastd824cbb2009-06-27 17:58:46 +02002103 die_errno("Unable to read log '%s'", logfile);
Shawn Pearced556fae2006-05-17 05:56:09 -04002104 fstat(logfd, &st);
2105 if (!st.st_size)
2106 die("Log %s is empty.", logfile);
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05002107 mapsz = xsize_t(st.st_size);
2108 log_mapped = xmmap(NULL, mapsz, PROT_READ, MAP_PRIVATE, logfd, 0);
Junio C Hamanocb48cb52007-01-19 00:39:32 -08002109 logdata = log_mapped;
Shawn Pearced556fae2006-05-17 05:56:09 -04002110 close(logfd);
2111
Shawn Pearcee5229042006-05-19 03:28:19 -04002112 lastrec = NULL;
Shawn Pearced556fae2006-05-17 05:56:09 -04002113 rec = logend = logdata + st.st_size;
2114 while (logdata < rec) {
Junio C Hamanoe29cb532006-12-18 22:07:45 -08002115 reccnt++;
Shawn Pearced556fae2006-05-17 05:56:09 -04002116 if (logdata < rec && *(rec-1) == '\n')
2117 rec--;
Shawn Pearcee5229042006-05-19 03:28:19 -04002118 lastgt = NULL;
2119 while (logdata < rec && *(rec-1) != '\n') {
Shawn Pearced556fae2006-05-17 05:56:09 -04002120 rec--;
Shawn Pearcee5229042006-05-19 03:28:19 -04002121 if (*rec == '>')
2122 lastgt = rec;
2123 }
2124 if (!lastgt)
Shawn Pearced556fae2006-05-17 05:56:09 -04002125 die("Log %s is corrupt.", logfile);
Shawn Pearcee5229042006-05-19 03:28:19 -04002126 date = strtoul(lastgt + 1, &tz_c, 10);
Junio C Hamanoab2a1a32006-10-05 23:16:15 -07002127 if (date <= at_time || cnt == 0) {
Junio C Hamano76a44c52007-01-19 01:20:23 -08002128 tz = strtoul(tz_c, NULL, 10);
Junio C Hamano16d7cc92007-01-19 01:19:05 -08002129 if (msg)
2130 *msg = ref_msg(rec, logend);
2131 if (cutoff_time)
2132 *cutoff_time = date;
2133 if (cutoff_tz)
2134 *cutoff_tz = tz;
2135 if (cutoff_cnt)
Junio C Hamano76a44c52007-01-19 01:20:23 -08002136 *cutoff_cnt = reccnt - 1;
Shawn Pearcee5229042006-05-19 03:28:19 -04002137 if (lastrec) {
2138 if (get_sha1_hex(lastrec, logged_sha1))
2139 die("Log %s is corrupt.", logfile);
2140 if (get_sha1_hex(rec + 41, sha1))
2141 die("Log %s is corrupt.", logfile);
David Rientjesa89fccd2006-08-17 11:54:57 -07002142 if (hashcmp(logged_sha1, sha1)) {
Miklos Vajnaedbc25c2009-03-24 02:09:17 +01002143 warning("Log %s has gap after %s.",
Junio C Hamano73013af2007-07-13 23:14:52 -07002144 logfile, show_date(date, tz, DATE_RFC2822));
Shawn Pearcee5229042006-05-19 03:28:19 -04002145 }
Junio C Hamanoe5f38ec2006-06-06 14:04:17 -07002146 }
2147 else if (date == at_time) {
Shawn Pearcee5229042006-05-19 03:28:19 -04002148 if (get_sha1_hex(rec + 41, sha1))
2149 die("Log %s is corrupt.", logfile);
Junio C Hamanoe5f38ec2006-06-06 14:04:17 -07002150 }
2151 else {
Shawn Pearcee5229042006-05-19 03:28:19 -04002152 if (get_sha1_hex(rec + 41, logged_sha1))
2153 die("Log %s is corrupt.", logfile);
David Rientjesa89fccd2006-08-17 11:54:57 -07002154 if (hashcmp(logged_sha1, sha1)) {
Miklos Vajnaedbc25c2009-03-24 02:09:17 +01002155 warning("Log %s unexpectedly ended on %s.",
Junio C Hamano73013af2007-07-13 23:14:52 -07002156 logfile, show_date(date, tz, DATE_RFC2822));
Shawn Pearcee5229042006-05-19 03:28:19 -04002157 }
2158 }
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05002159 munmap(log_mapped, mapsz);
Shawn Pearced556fae2006-05-17 05:56:09 -04002160 return 0;
2161 }
Shawn Pearcee5229042006-05-19 03:28:19 -04002162 lastrec = rec;
Junio C Hamanoab2a1a32006-10-05 23:16:15 -07002163 if (cnt > 0)
2164 cnt--;
Shawn Pearced556fae2006-05-17 05:56:09 -04002165 }
2166
Shawn Pearcee5229042006-05-19 03:28:19 -04002167 rec = logdata;
2168 while (rec < logend && *rec != '>' && *rec != '\n')
2169 rec++;
2170 if (rec == logend || *rec == '\n')
Shawn Pearced556fae2006-05-17 05:56:09 -04002171 die("Log %s is corrupt.", logfile);
Shawn Pearcee5229042006-05-19 03:28:19 -04002172 date = strtoul(rec + 1, &tz_c, 10);
Shawn Pearced556fae2006-05-17 05:56:09 -04002173 tz = strtoul(tz_c, NULL, 10);
2174 if (get_sha1_hex(logdata, sha1))
2175 die("Log %s is corrupt.", logfile);
Jeff Kingd1a44892008-07-08 00:38:54 -04002176 if (is_null_sha1(sha1)) {
2177 if (get_sha1_hex(logdata + 41, sha1))
2178 die("Log %s is corrupt.", logfile);
2179 }
Junio C Hamano16d7cc92007-01-19 01:19:05 -08002180 if (msg)
2181 *msg = ref_msg(logdata, logend);
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05002182 munmap(log_mapped, mapsz);
Junio C Hamano16d7cc92007-01-19 01:19:05 -08002183
2184 if (cutoff_time)
2185 *cutoff_time = date;
2186 if (cutoff_tz)
2187 *cutoff_tz = tz;
2188 if (cutoff_cnt)
2189 *cutoff_cnt = reccnt;
2190 return 1;
Shawn Pearced556fae2006-05-17 05:56:09 -04002191}
Junio C Hamano2ff81662006-12-18 01:18:16 -08002192
Michael Haggertydfefa932011-12-12 06:38:09 +01002193int for_each_recent_reflog_ent(const char *refname, each_reflog_ent_fn fn, long ofs, void *cb_data)
Junio C Hamano2ff81662006-12-18 01:18:16 -08002194{
2195 const char *logfile;
2196 FILE *logfp;
René Scharfe8ca78802010-03-13 18:37:50 +01002197 struct strbuf sb = STRBUF_INIT;
Junio C Hamano2266bf22007-01-18 23:25:54 -08002198 int ret = 0;
Junio C Hamano2ff81662006-12-18 01:18:16 -08002199
Michael Haggertydfefa932011-12-12 06:38:09 +01002200 logfile = git_path("logs/%s", refname);
Junio C Hamano2ff81662006-12-18 01:18:16 -08002201 logfp = fopen(logfile, "r");
2202 if (!logfp)
Johannes Schindelin883d60f2007-01-08 01:59:54 +01002203 return -1;
Junio C Hamano101d15e2009-01-19 22:18:29 -08002204
2205 if (ofs) {
2206 struct stat statbuf;
2207 if (fstat(fileno(logfp), &statbuf) ||
2208 statbuf.st_size < ofs ||
2209 fseek(logfp, -ofs, SEEK_END) ||
René Scharfe8ca78802010-03-13 18:37:50 +01002210 strbuf_getwholeline(&sb, logfp, '\n')) {
Brandon Casey9d33f7c2009-07-16 16:25:18 -05002211 fclose(logfp);
René Scharfe8ca78802010-03-13 18:37:50 +01002212 strbuf_release(&sb);
Junio C Hamano101d15e2009-01-19 22:18:29 -08002213 return -1;
Brandon Casey9d33f7c2009-07-16 16:25:18 -05002214 }
Junio C Hamano101d15e2009-01-19 22:18:29 -08002215 }
2216
René Scharfe8ca78802010-03-13 18:37:50 +01002217 while (!strbuf_getwholeline(&sb, logfp, '\n')) {
Junio C Hamano2ff81662006-12-18 01:18:16 -08002218 unsigned char osha1[20], nsha1[20];
Johannes Schindelin883d60f2007-01-08 01:59:54 +01002219 char *email_end, *message;
2220 unsigned long timestamp;
René Scharfe8ca78802010-03-13 18:37:50 +01002221 int tz;
Junio C Hamano2ff81662006-12-18 01:18:16 -08002222
2223 /* old SP new SP name <email> SP time TAB msg LF */
René Scharfe8ca78802010-03-13 18:37:50 +01002224 if (sb.len < 83 || sb.buf[sb.len - 1] != '\n' ||
2225 get_sha1_hex(sb.buf, osha1) || sb.buf[40] != ' ' ||
2226 get_sha1_hex(sb.buf + 41, nsha1) || sb.buf[81] != ' ' ||
2227 !(email_end = strchr(sb.buf + 82, '>')) ||
Johannes Schindelin883d60f2007-01-08 01:59:54 +01002228 email_end[1] != ' ' ||
2229 !(timestamp = strtoul(email_end + 2, &message, 10)) ||
2230 !message || message[0] != ' ' ||
2231 (message[1] != '+' && message[1] != '-') ||
2232 !isdigit(message[2]) || !isdigit(message[3]) ||
Johannes Schindelinb4dd4852007-02-09 00:59:47 +01002233 !isdigit(message[4]) || !isdigit(message[5]))
Junio C Hamano2ff81662006-12-18 01:18:16 -08002234 continue; /* corrupt? */
Johannes Schindelin883d60f2007-01-08 01:59:54 +01002235 email_end[1] = '\0';
2236 tz = strtol(message + 1, NULL, 10);
Johannes Schindelinb4dd4852007-02-09 00:59:47 +01002237 if (message[6] != '\t')
2238 message += 6;
2239 else
2240 message += 7;
René Scharfe8ca78802010-03-13 18:37:50 +01002241 ret = fn(osha1, nsha1, sb.buf + 82, timestamp, tz, message,
2242 cb_data);
Johannes Schindelin883d60f2007-01-08 01:59:54 +01002243 if (ret)
Junio C Hamano2266bf22007-01-18 23:25:54 -08002244 break;
Junio C Hamano2ff81662006-12-18 01:18:16 -08002245 }
2246 fclose(logfp);
René Scharfe8ca78802010-03-13 18:37:50 +01002247 strbuf_release(&sb);
Junio C Hamano2266bf22007-01-18 23:25:54 -08002248 return ret;
Junio C Hamano2ff81662006-12-18 01:18:16 -08002249}
Junio C Hamanoe29cb532006-12-18 22:07:45 -08002250
Michael Haggertydfefa932011-12-12 06:38:09 +01002251int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn, void *cb_data)
Junio C Hamano101d15e2009-01-19 22:18:29 -08002252{
Michael Haggertydfefa932011-12-12 06:38:09 +01002253 return for_each_recent_reflog_ent(refname, fn, 0, cb_data);
Junio C Hamano101d15e2009-01-19 22:18:29 -08002254}
2255
Michael Haggerty989c0e52012-04-25 00:45:14 +02002256/*
2257 * Call fn for each reflog in the namespace indicated by name. name
2258 * must be empty or end with '/'. Name will be used as a scratch
2259 * space, but its contents will be restored before return.
2260 */
2261static int do_for_each_reflog(struct strbuf *name, each_ref_fn fn, void *cb_data)
Nicolas Pitreeb8381c2007-02-03 13:25:43 -05002262{
Michael Haggerty989c0e52012-04-25 00:45:14 +02002263 DIR *d = opendir(git_path("logs/%s", name->buf));
Junio C Hamanofcee5a12007-02-07 09:18:57 -08002264 int retval = 0;
Michael Haggerty93c603f2012-04-25 00:45:13 +02002265 struct dirent *de;
Michael Haggerty989c0e52012-04-25 00:45:14 +02002266 int oldlen = name->len;
Nicolas Pitreeb8381c2007-02-03 13:25:43 -05002267
Michael Haggerty93c603f2012-04-25 00:45:13 +02002268 if (!d)
Michael Haggerty989c0e52012-04-25 00:45:14 +02002269 return name->len ? errno : 0;
Nicolas Pitreeb8381c2007-02-03 13:25:43 -05002270
Michael Haggerty93c603f2012-04-25 00:45:13 +02002271 while ((de = readdir(d)) != NULL) {
2272 struct stat st;
Nicolas Pitreeb8381c2007-02-03 13:25:43 -05002273
Michael Haggerty93c603f2012-04-25 00:45:13 +02002274 if (de->d_name[0] == '.')
2275 continue;
Michael Haggerty93c603f2012-04-25 00:45:13 +02002276 if (has_extension(de->d_name, ".lock"))
2277 continue;
Michael Haggerty989c0e52012-04-25 00:45:14 +02002278 strbuf_addstr(name, de->d_name);
2279 if (stat(git_path("logs/%s", name->buf), &st) < 0) {
2280 ; /* silently ignore */
Michael Haggerty93c603f2012-04-25 00:45:13 +02002281 } else {
Michael Haggerty989c0e52012-04-25 00:45:14 +02002282 if (S_ISDIR(st.st_mode)) {
2283 strbuf_addch(name, '/');
2284 retval = do_for_each_reflog(name, fn, cb_data);
2285 } else {
2286 unsigned char sha1[20];
2287 if (read_ref_full(name->buf, sha1, 0, NULL))
2288 retval = error("bad ref for %s", name->buf);
2289 else
2290 retval = fn(name->buf, sha1, 0, cb_data);
2291 }
2292 if (retval)
2293 break;
Nicolas Pitreeb8381c2007-02-03 13:25:43 -05002294 }
Michael Haggerty989c0e52012-04-25 00:45:14 +02002295 strbuf_setlen(name, oldlen);
Nicolas Pitreeb8381c2007-02-03 13:25:43 -05002296 }
Michael Haggerty93c603f2012-04-25 00:45:13 +02002297 closedir(d);
Nicolas Pitreeb8381c2007-02-03 13:25:43 -05002298 return retval;
2299}
2300
2301int for_each_reflog(each_ref_fn fn, void *cb_data)
2302{
Michael Haggerty989c0e52012-04-25 00:45:14 +02002303 int retval;
2304 struct strbuf name;
2305 strbuf_init(&name, PATH_MAX);
2306 retval = do_for_each_reflog(&name, fn, cb_data);
2307 strbuf_release(&name);
2308 return retval;
Nicolas Pitreeb8381c2007-02-03 13:25:43 -05002309}
Carlos Rica3d9f0372007-09-05 03:38:24 +02002310
2311int update_ref(const char *action, const char *refname,
2312 const unsigned char *sha1, const unsigned char *oldval,
2313 int flags, enum action_on_err onerr)
2314{
2315 static struct ref_lock *lock;
2316 lock = lock_any_ref_for_update(refname, oldval, flags);
2317 if (!lock) {
2318 const char *str = "Cannot lock the ref '%s'.";
2319 switch (onerr) {
2320 case MSG_ON_ERR: error(str, refname); break;
2321 case DIE_ON_ERR: die(str, refname); break;
2322 case QUIET_ON_ERR: break;
2323 }
2324 return 1;
2325 }
2326 if (write_ref_sha1(lock, sha1, action) < 0) {
2327 const char *str = "Cannot update the ref '%s'.";
2328 switch (onerr) {
2329 case MSG_ON_ERR: error(str, refname); break;
2330 case DIE_ON_ERR: die(str, refname); break;
2331 case QUIET_ON_ERR: break;
2332 }
2333 return 1;
2334 }
2335 return 0;
2336}
Jeff Kingcda69f42007-11-18 02:13:10 -05002337
Jeff King5483f792009-02-25 03:32:10 -05002338struct ref *find_ref_by_name(const struct ref *list, const char *name)
Jeff Kingcda69f42007-11-18 02:13:10 -05002339{
2340 for ( ; list; list = list->next)
2341 if (!strcmp(list->name, name))
Jeff King5483f792009-02-25 03:32:10 -05002342 return (struct ref *)list;
Jeff Kingcda69f42007-11-18 02:13:10 -05002343 return NULL;
2344}
Jeff King7c2b3022009-04-07 03:14:20 -04002345
2346/*
2347 * generate a format suitable for scanf from a ref_rev_parse_rules
2348 * rule, that is replace the "%.*s" spec with a "%s" spec
2349 */
2350static void gen_scanf_fmt(char *scanf_fmt, const char *rule)
2351{
2352 char *spec;
2353
2354 spec = strstr(rule, "%.*s");
2355 if (!spec || strstr(spec + 4, "%.*s"))
2356 die("invalid rule in ref_rev_parse_rules: %s", rule);
2357
2358 /* copy all until spec */
2359 strncpy(scanf_fmt, rule, spec - rule);
2360 scanf_fmt[spec - rule] = '\0';
2361 /* copy new spec */
2362 strcat(scanf_fmt, "%s");
2363 /* copy remaining rule */
2364 strcat(scanf_fmt, spec + 4);
2365
2366 return;
2367}
2368
Michael Haggertydfefa932011-12-12 06:38:09 +01002369char *shorten_unambiguous_ref(const char *refname, int strict)
Jeff King7c2b3022009-04-07 03:14:20 -04002370{
2371 int i;
2372 static char **scanf_fmts;
2373 static int nr_rules;
2374 char *short_name;
2375
2376 /* pre generate scanf formats from ref_rev_parse_rules[] */
2377 if (!nr_rules) {
2378 size_t total_len = 0;
2379
2380 /* the rule list is NULL terminated, count them first */
2381 for (; ref_rev_parse_rules[nr_rules]; nr_rules++)
2382 /* no +1 because strlen("%s") < strlen("%.*s") */
2383 total_len += strlen(ref_rev_parse_rules[nr_rules]);
2384
2385 scanf_fmts = xmalloc(nr_rules * sizeof(char *) + total_len);
2386
2387 total_len = 0;
2388 for (i = 0; i < nr_rules; i++) {
2389 scanf_fmts[i] = (char *)&scanf_fmts[nr_rules]
2390 + total_len;
2391 gen_scanf_fmt(scanf_fmts[i], ref_rev_parse_rules[i]);
2392 total_len += strlen(ref_rev_parse_rules[i]);
2393 }
2394 }
2395
2396 /* bail out if there are no rules */
2397 if (!nr_rules)
Michael Haggertydfefa932011-12-12 06:38:09 +01002398 return xstrdup(refname);
Jeff King7c2b3022009-04-07 03:14:20 -04002399
Michael Haggertydfefa932011-12-12 06:38:09 +01002400 /* buffer for scanf result, at most refname must fit */
2401 short_name = xstrdup(refname);
Jeff King7c2b3022009-04-07 03:14:20 -04002402
2403 /* skip first rule, it will always match */
2404 for (i = nr_rules - 1; i > 0 ; --i) {
2405 int j;
Bert Wesarg6e7b3302009-04-13 12:25:46 +02002406 int rules_to_fail = i;
Jeff King7c2b3022009-04-07 03:14:20 -04002407 int short_name_len;
2408
Michael Haggertydfefa932011-12-12 06:38:09 +01002409 if (1 != sscanf(refname, scanf_fmts[i], short_name))
Jeff King7c2b3022009-04-07 03:14:20 -04002410 continue;
2411
2412 short_name_len = strlen(short_name);
2413
2414 /*
Bert Wesarg6e7b3302009-04-13 12:25:46 +02002415 * in strict mode, all (except the matched one) rules
2416 * must fail to resolve to a valid non-ambiguous ref
2417 */
2418 if (strict)
2419 rules_to_fail = nr_rules;
2420
2421 /*
Jeff King7c2b3022009-04-07 03:14:20 -04002422 * check if the short name resolves to a valid ref,
2423 * but use only rules prior to the matched one
2424 */
Bert Wesarg6e7b3302009-04-13 12:25:46 +02002425 for (j = 0; j < rules_to_fail; j++) {
Jeff King7c2b3022009-04-07 03:14:20 -04002426 const char *rule = ref_rev_parse_rules[j];
Jeff King7c2b3022009-04-07 03:14:20 -04002427 char refname[PATH_MAX];
2428
Bert Wesarg6e7b3302009-04-13 12:25:46 +02002429 /* skip matched rule */
2430 if (i == j)
2431 continue;
2432
Jeff King7c2b3022009-04-07 03:14:20 -04002433 /*
2434 * the short name is ambiguous, if it resolves
2435 * (with this previous rule) to a valid ref
2436 * read_ref() returns 0 on success
2437 */
2438 mksnpath(refname, sizeof(refname),
2439 rule, short_name_len, short_name);
Nguyễn Thái Ngọc Duyc6893322011-11-13 17:22:14 +07002440 if (ref_exists(refname))
Jeff King7c2b3022009-04-07 03:14:20 -04002441 break;
2442 }
2443
2444 /*
2445 * short name is non-ambiguous if all previous rules
2446 * haven't resolved to a valid ref
2447 */
Bert Wesarg6e7b3302009-04-13 12:25:46 +02002448 if (j == rules_to_fail)
Jeff King7c2b3022009-04-07 03:14:20 -04002449 return short_name;
2450 }
2451
2452 free(short_name);
Michael Haggertydfefa932011-12-12 06:38:09 +01002453 return xstrdup(refname);
Jeff King7c2b3022009-04-07 03:14:20 -04002454}