]>
Commit | Line | Data |
---|---|---|
95fc7512 | 1 | #include "cache.h" |
85023577 | 2 | #include "refs.h" |
cf0adba7 JH |
3 | #include "object.h" |
4 | #include "tag.h" | |
7155b727 | 5 | #include "dir.h" |
95fc7512 | 6 | |
bc5fd6d3 MH |
7 | /* |
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 | */ | |
f4204ab9 | 18 | |
bc5fd6d3 MH |
19 | /* Return true iff ch is not allowed in reference names. */ |
20 | static 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 | */ | |
36 | static 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) | |
dac529e4 | 54 | return 0; /* Component has zero length. */ |
bc5fd6d3 MH |
55 | 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 | ||
70 | int 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); | |
dac529e4 | 77 | if (component_len <= 0) { |
bc5fd6d3 MH |
78 | 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 | ||
102 | struct ref_entry; | |
e1e22e37 | 103 | |
28e6a34e MH |
104 | /* |
105 | * Information used (along with the information in ref_entry) to | |
106 | * describe a single cached reference. This data structure only | |
107 | * occurs embedded in a union in struct ref_entry, and only when | |
108 | * (ref_entry->flag & REF_DIR) is zero. | |
109 | */ | |
593f1bb8 MH |
110 | struct ref_value { |
111 | unsigned char sha1[20]; | |
112 | unsigned char peeled[20]; | |
113 | }; | |
114 | ||
f006c42a MH |
115 | struct ref_cache; |
116 | ||
28e6a34e MH |
117 | /* |
118 | * Information used (along with the information in ref_entry) to | |
119 | * describe a level in the hierarchy of references. This data | |
120 | * structure only occurs embedded in a union in struct ref_entry, and | |
121 | * only when (ref_entry.flag & REF_DIR) is set. In that case, | |
122 | * (ref_entry.flag & REF_INCOMPLETE) determines whether the references | |
123 | * in the directory have already been read: | |
124 | * | |
125 | * (ref_entry.flag & REF_INCOMPLETE) unset -- a directory of loose | |
126 | * or packed references, already read. | |
127 | * | |
128 | * (ref_entry.flag & REF_INCOMPLETE) set -- a directory of loose | |
129 | * references that hasn't been read yet (nor has any of its | |
130 | * subdirectories). | |
131 | * | |
132 | * Entries within a directory are stored within a growable array of | |
133 | * pointers to ref_entries (entries, nr, alloc). Entries 0 <= i < | |
134 | * sorted are sorted by their component name in strcmp() order and the | |
135 | * remaining entries are unsorted. | |
136 | * | |
137 | * Loose references are read lazily, one directory at a time. When a | |
138 | * directory of loose references is read, then all of the references | |
139 | * in that directory are stored, and REF_INCOMPLETE stubs are created | |
140 | * for any subdirectories, but the subdirectories themselves are not | |
141 | * read. The reading is triggered by get_ref_dir(). | |
142 | */ | |
d3177275 | 143 | struct ref_dir { |
e9c4c111 | 144 | int nr, alloc; |
e6ed3ca6 MH |
145 | |
146 | /* | |
147 | * Entries with index 0 <= i < sorted are sorted by name. New | |
148 | * entries are appended to the list unsorted, and are sorted | |
149 | * only when required; thus we avoid the need to sort the list | |
150 | * after the addition of every reference. | |
151 | */ | |
152 | int sorted; | |
153 | ||
f006c42a MH |
154 | /* A pointer to the ref_cache that contains this ref_dir. */ |
155 | struct ref_cache *ref_cache; | |
156 | ||
d3177275 | 157 | struct ref_entry **entries; |
e9c4c111 JP |
158 | }; |
159 | ||
432ad41e MH |
160 | /* ISSYMREF=0x01, ISPACKED=0x02, and ISBROKEN=0x04 are public interfaces */ |
161 | #define REF_KNOWS_PEELED 0x08 | |
28e6a34e MH |
162 | |
163 | /* ref_entry represents a directory of references */ | |
432ad41e | 164 | #define REF_DIR 0x10 |
cf0adba7 | 165 | |
28e6a34e MH |
166 | /* |
167 | * Entry has not yet been read from disk (used only for REF_DIR | |
168 | * entries representing loose references) | |
169 | */ | |
170 | #define REF_INCOMPLETE 0x20 | |
171 | ||
432ad41e MH |
172 | /* |
173 | * A ref_entry represents either a reference or a "subdirectory" of | |
28e6a34e MH |
174 | * references. |
175 | * | |
176 | * Each directory in the reference namespace is represented by a | |
177 | * ref_entry with (flags & REF_DIR) set and containing a subdir member | |
178 | * that holds the entries in that directory that have been read so | |
179 | * far. If (flags & REF_INCOMPLETE) is set, then the directory and | |
180 | * its subdirectories haven't been read yet. REF_INCOMPLETE is only | |
181 | * used for loose reference directories. | |
182 | * | |
183 | * References are represented by a ref_entry with (flags & REF_DIR) | |
184 | * unset and a value member that describes the reference's value. The | |
185 | * flag member is at the ref_entry level, but it is also needed to | |
186 | * interpret the contents of the value field (in other words, a | |
187 | * ref_value object is not very much use without the enclosing | |
188 | * ref_entry). | |
432ad41e MH |
189 | * |
190 | * Reference names cannot end with slash and directories' names are | |
191 | * always stored with a trailing slash (except for the top-level | |
192 | * directory, which is always denoted by ""). This has two nice | |
193 | * consequences: (1) when the entries in each subdir are sorted | |
194 | * lexicographically by name (as they usually are), the references in | |
195 | * a whole tree can be generated in lexicographic order by traversing | |
196 | * the tree in left-to-right, depth-first order; (2) the names of | |
197 | * references and subdirectories cannot conflict, and therefore the | |
198 | * presence of an empty subdirectory does not block the creation of a | |
199 | * similarly-named reference. (The fact that reference names with the | |
200 | * same leading components can conflict *with each other* is a | |
201 | * separate issue that is regulated by is_refname_available().) | |
202 | * | |
203 | * Please note that the name field contains the fully-qualified | |
204 | * reference (or subdirectory) name. Space could be saved by only | |
205 | * storing the relative names. But that would require the full names | |
206 | * to be generated on the fly when iterating in do_for_each_ref(), and | |
207 | * would break callback functions, who have always been able to assume | |
208 | * that the name strings that they are passed will not be freed during | |
209 | * the iteration. | |
210 | */ | |
bc5fd6d3 MH |
211 | struct ref_entry { |
212 | unsigned char flag; /* ISSYMREF? ISPACKED? */ | |
593f1bb8 | 213 | union { |
432ad41e MH |
214 | struct ref_value value; /* if not (flags&REF_DIR) */ |
215 | struct ref_dir subdir; /* if (flags&REF_DIR) */ | |
593f1bb8 | 216 | } u; |
432ad41e MH |
217 | /* |
218 | * The full name of the reference (e.g., "refs/heads/master") | |
219 | * or the full name of the directory with a trailing slash | |
220 | * (e.g., "refs/heads/"): | |
221 | */ | |
bc5fd6d3 MH |
222 | char name[FLEX_ARRAY]; |
223 | }; | |
e1e22e37 | 224 | |
28e6a34e MH |
225 | static void read_loose_refs(const char *dirname, struct ref_dir *dir); |
226 | ||
d7826d54 MH |
227 | static struct ref_dir *get_ref_dir(struct ref_entry *entry) |
228 | { | |
28e6a34e | 229 | struct ref_dir *dir; |
d7826d54 | 230 | assert(entry->flag & REF_DIR); |
28e6a34e MH |
231 | dir = &entry->u.subdir; |
232 | if (entry->flag & REF_INCOMPLETE) { | |
233 | read_loose_refs(entry->name, dir); | |
234 | entry->flag &= ~REF_INCOMPLETE; | |
235 | } | |
236 | return dir; | |
d7826d54 MH |
237 | } |
238 | ||
cddc4258 MH |
239 | static struct ref_entry *create_ref_entry(const char *refname, |
240 | const unsigned char *sha1, int flag, | |
241 | int check_name) | |
e1e22e37 LT |
242 | { |
243 | int len; | |
cddc4258 | 244 | struct ref_entry *ref; |
e1e22e37 | 245 | |
09116a1c | 246 | if (check_name && |
dfefa935 MH |
247 | check_refname_format(refname, REFNAME_ALLOW_ONELEVEL|REFNAME_DOT_COMPONENT)) |
248 | die("Reference has invalid format: '%s'", refname); | |
cddc4258 MH |
249 | len = strlen(refname) + 1; |
250 | ref = xmalloc(sizeof(struct ref_entry) + len); | |
593f1bb8 MH |
251 | hashcpy(ref->u.value.sha1, sha1); |
252 | hashclr(ref->u.value.peeled); | |
cddc4258 MH |
253 | memcpy(ref->name, refname, len); |
254 | ref->flag = flag; | |
255 | return ref; | |
256 | } | |
257 | ||
432ad41e MH |
258 | static void clear_ref_dir(struct ref_dir *dir); |
259 | ||
732134ed MH |
260 | static void free_ref_entry(struct ref_entry *entry) |
261 | { | |
432ad41e | 262 | if (entry->flag & REF_DIR) |
d7826d54 | 263 | clear_ref_dir(get_ref_dir(entry)); |
732134ed MH |
264 | free(entry); |
265 | } | |
266 | ||
432ad41e MH |
267 | /* |
268 | * Add a ref_entry to the end of dir (unsorted). Entry is always | |
269 | * stored directly in dir; no recursion into subdirectories is | |
270 | * done. | |
271 | */ | |
272 | static void add_entry_to_dir(struct ref_dir *dir, struct ref_entry *entry) | |
cddc4258 | 273 | { |
432ad41e MH |
274 | ALLOC_GROW(dir->entries, dir->nr + 1, dir->alloc); |
275 | dir->entries[dir->nr++] = entry; | |
c774aab9 JP |
276 | } |
277 | ||
432ad41e MH |
278 | /* |
279 | * Clear and free all entries in dir, recursively. | |
280 | */ | |
d3177275 | 281 | static void clear_ref_dir(struct ref_dir *dir) |
bc5fd6d3 MH |
282 | { |
283 | int i; | |
d3177275 MH |
284 | for (i = 0; i < dir->nr; i++) |
285 | free_ref_entry(dir->entries[i]); | |
286 | free(dir->entries); | |
287 | dir->sorted = dir->nr = dir->alloc = 0; | |
288 | dir->entries = NULL; | |
bc5fd6d3 MH |
289 | } |
290 | ||
432ad41e MH |
291 | /* |
292 | * Create a struct ref_entry object for the specified dirname. | |
293 | * dirname is the name of the directory with a trailing slash (e.g., | |
294 | * "refs/heads/") or "" for the top-level directory. | |
295 | */ | |
f006c42a | 296 | static struct ref_entry *create_dir_entry(struct ref_cache *ref_cache, |
28e6a34e | 297 | const char *dirname, int incomplete) |
432ad41e MH |
298 | { |
299 | struct ref_entry *direntry; | |
300 | int len = strlen(dirname); | |
301 | direntry = xcalloc(1, sizeof(struct ref_entry) + len + 1); | |
302 | memcpy(direntry->name, dirname, len + 1); | |
f006c42a | 303 | direntry->u.subdir.ref_cache = ref_cache; |
28e6a34e | 304 | direntry->flag = REF_DIR | (incomplete ? REF_INCOMPLETE : 0); |
432ad41e MH |
305 | return direntry; |
306 | } | |
307 | ||
e9c4c111 | 308 | static int ref_entry_cmp(const void *a, const void *b) |
c774aab9 | 309 | { |
e9c4c111 JP |
310 | struct ref_entry *one = *(struct ref_entry **)a; |
311 | struct ref_entry *two = *(struct ref_entry **)b; | |
312 | return strcmp(one->name, two->name); | |
313 | } | |
c774aab9 | 314 | |
d3177275 | 315 | static void sort_ref_dir(struct ref_dir *dir); |
bc5fd6d3 | 316 | |
432ad41e MH |
317 | /* |
318 | * Return the entry with the given refname from the ref_dir | |
319 | * (non-recursively), sorting dir if necessary. Return NULL if no | |
28e6a34e | 320 | * such entry is found. dir must already be complete. |
432ad41e | 321 | */ |
40ad937d RS |
322 | static struct ref_entry *search_ref_dir(struct ref_dir *dir, |
323 | const char *refname, size_t len) | |
bc5fd6d3 MH |
324 | { |
325 | struct ref_entry *e, **r; | |
bc5fd6d3 | 326 | |
432ad41e | 327 | if (refname == NULL || !dir->nr) |
bc5fd6d3 MH |
328 | return NULL; |
329 | ||
d3177275 | 330 | sort_ref_dir(dir); |
432ad41e | 331 | |
40ad937d | 332 | e = xmalloc(sizeof(struct ref_entry) + len + 1); |
bc5fd6d3 | 333 | memcpy(e->name, refname, len); |
40ad937d | 334 | e->name[len] = '\0'; |
bc5fd6d3 | 335 | |
d3177275 | 336 | r = bsearch(&e, dir->entries, dir->nr, sizeof(*dir->entries), ref_entry_cmp); |
bc5fd6d3 MH |
337 | |
338 | free(e); | |
339 | ||
340 | if (r == NULL) | |
341 | return NULL; | |
342 | ||
343 | return *r; | |
344 | } | |
345 | ||
f348ac92 MH |
346 | /* |
347 | * Search for a directory entry directly within dir (without | |
348 | * recursing). Sort dir if necessary. subdirname must be a directory | |
349 | * name (i.e., end in '/'). If mkdir is set, then create the | |
350 | * directory if it is missing; otherwise, return NULL if the desired | |
28e6a34e | 351 | * directory cannot be found. dir must already be complete. |
f348ac92 | 352 | */ |
3f3aa1bc MH |
353 | static struct ref_dir *search_for_subdir(struct ref_dir *dir, |
354 | const char *subdirname, int mkdir) | |
f348ac92 | 355 | { |
40ad937d RS |
356 | size_t len = strlen(subdirname); |
357 | struct ref_entry *entry = search_ref_dir(dir, subdirname, len); | |
f348ac92 MH |
358 | if (!entry) { |
359 | if (!mkdir) | |
360 | return NULL; | |
28e6a34e MH |
361 | /* |
362 | * Since dir is complete, the absence of a subdir | |
363 | * means that the subdir really doesn't exist; | |
364 | * therefore, create an empty record for it but mark | |
365 | * the record complete. | |
366 | */ | |
367 | entry = create_dir_entry(dir->ref_cache, subdirname, 0); | |
f348ac92 MH |
368 | add_entry_to_dir(dir, entry); |
369 | } | |
3f3aa1bc | 370 | return get_ref_dir(entry); |
f348ac92 MH |
371 | } |
372 | ||
432ad41e MH |
373 | /* |
374 | * If refname is a reference name, find the ref_dir within the dir | |
375 | * tree that should hold refname. If refname is a directory name | |
376 | * (i.e., ends in '/'), then return that ref_dir itself. dir must | |
28e6a34e MH |
377 | * represent the top-level directory and must already be complete. |
378 | * Sort ref_dirs and recurse into subdirectories as necessary. If | |
379 | * mkdir is set, then create any missing directories; otherwise, | |
380 | * return NULL if the desired directory cannot be found. | |
432ad41e MH |
381 | */ |
382 | static struct ref_dir *find_containing_dir(struct ref_dir *dir, | |
383 | const char *refname, int mkdir) | |
384 | { | |
5fa04418 MH |
385 | struct strbuf dirname; |
386 | const char *slash; | |
387 | strbuf_init(&dirname, PATH_MAX); | |
388 | for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) { | |
3f3aa1bc | 389 | struct ref_dir *subdir; |
5fa04418 MH |
390 | strbuf_add(&dirname, |
391 | refname + dirname.len, | |
392 | (slash + 1) - (refname + dirname.len)); | |
3f3aa1bc | 393 | subdir = search_for_subdir(dir, dirname.buf, mkdir); |
663c1295 JH |
394 | if (!subdir) { |
395 | dir = NULL; | |
f348ac92 | 396 | break; |
432ad41e | 397 | } |
3f3aa1bc | 398 | dir = subdir; |
432ad41e MH |
399 | } |
400 | ||
5fa04418 | 401 | strbuf_release(&dirname); |
432ad41e MH |
402 | return dir; |
403 | } | |
404 | ||
405 | /* | |
406 | * Find the value entry with the given name in dir, sorting ref_dirs | |
407 | * and recursing into subdirectories as necessary. If the name is not | |
408 | * found or it corresponds to a directory entry, return NULL. | |
409 | */ | |
410 | static struct ref_entry *find_ref(struct ref_dir *dir, const char *refname) | |
411 | { | |
412 | struct ref_entry *entry; | |
413 | dir = find_containing_dir(dir, refname, 0); | |
414 | if (!dir) | |
415 | return NULL; | |
40ad937d | 416 | entry = search_ref_dir(dir, refname, strlen(refname)); |
432ad41e MH |
417 | return (entry && !(entry->flag & REF_DIR)) ? entry : NULL; |
418 | } | |
419 | ||
420 | /* | |
421 | * Add a ref_entry to the ref_dir (unsorted), recursing into | |
422 | * subdirectories as necessary. dir must represent the top-level | |
423 | * directory. Return 0 on success. | |
424 | */ | |
425 | static int add_ref(struct ref_dir *dir, struct ref_entry *ref) | |
426 | { | |
427 | dir = find_containing_dir(dir, ref->name, 1); | |
428 | if (!dir) | |
429 | return -1; | |
430 | add_entry_to_dir(dir, ref); | |
431 | return 0; | |
432 | } | |
433 | ||
202a56a9 MH |
434 | /* |
435 | * Emit a warning and return true iff ref1 and ref2 have the same name | |
436 | * and the same sha1. Die if they have the same name but different | |
437 | * sha1s. | |
438 | */ | |
439 | static int is_dup_ref(const struct ref_entry *ref1, const struct ref_entry *ref2) | |
440 | { | |
432ad41e | 441 | if (strcmp(ref1->name, ref2->name)) |
202a56a9 | 442 | return 0; |
432ad41e MH |
443 | |
444 | /* Duplicate name; make sure that they don't conflict: */ | |
445 | ||
446 | if ((ref1->flag & REF_DIR) || (ref2->flag & REF_DIR)) | |
447 | /* This is impossible by construction */ | |
448 | die("Reference directory conflict: %s", ref1->name); | |
449 | ||
450 | if (hashcmp(ref1->u.value.sha1, ref2->u.value.sha1)) | |
451 | die("Duplicated ref, and SHA1s don't match: %s", ref1->name); | |
452 | ||
453 | warning("Duplicated ref: %s", ref1->name); | |
454 | return 1; | |
202a56a9 MH |
455 | } |
456 | ||
e6ed3ca6 | 457 | /* |
432ad41e MH |
458 | * Sort the entries in dir non-recursively (if they are not already |
459 | * sorted) and remove any duplicate entries. | |
e6ed3ca6 | 460 | */ |
d3177275 | 461 | static void sort_ref_dir(struct ref_dir *dir) |
e9c4c111 | 462 | { |
202a56a9 | 463 | int i, j; |
81a79d8e | 464 | struct ref_entry *last = NULL; |
c774aab9 | 465 | |
e6ed3ca6 MH |
466 | /* |
467 | * This check also prevents passing a zero-length array to qsort(), | |
468 | * which is a problem on some platforms. | |
469 | */ | |
d3177275 | 470 | if (dir->sorted == dir->nr) |
e9c4c111 | 471 | return; |
c774aab9 | 472 | |
d3177275 | 473 | qsort(dir->entries, dir->nr, sizeof(*dir->entries), ref_entry_cmp); |
c774aab9 | 474 | |
81a79d8e MH |
475 | /* Remove any duplicates: */ |
476 | for (i = 0, j = 0; j < dir->nr; j++) { | |
477 | struct ref_entry *entry = dir->entries[j]; | |
478 | if (last && is_dup_ref(last, entry)) | |
479 | free_ref_entry(entry); | |
480 | else | |
481 | last = dir->entries[i++] = entry; | |
e9c4c111 | 482 | } |
81a79d8e | 483 | dir->sorted = dir->nr = i; |
e9c4c111 | 484 | } |
c774aab9 | 485 | |
bc5fd6d3 | 486 | #define DO_FOR_EACH_INCLUDE_BROKEN 01 |
c774aab9 | 487 | |
bc5fd6d3 | 488 | static struct ref_entry *current_ref; |
c774aab9 | 489 | |
bc5fd6d3 MH |
490 | static int do_one_ref(const char *base, each_ref_fn fn, int trim, |
491 | int flags, void *cb_data, struct ref_entry *entry) | |
492 | { | |
429213e4 | 493 | int retval; |
bc5fd6d3 MH |
494 | if (prefixcmp(entry->name, base)) |
495 | return 0; | |
c774aab9 | 496 | |
bc5fd6d3 MH |
497 | if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN)) { |
498 | if (entry->flag & REF_ISBROKEN) | |
499 | return 0; /* ignore broken refs e.g. dangling symref */ | |
593f1bb8 | 500 | if (!has_sha1_file(entry->u.value.sha1)) { |
bc5fd6d3 MH |
501 | error("%s does not point to a valid object!", entry->name); |
502 | return 0; | |
503 | } | |
504 | } | |
505 | current_ref = entry; | |
593f1bb8 | 506 | retval = fn(entry->name + trim, entry->u.value.sha1, entry->flag, cb_data); |
429213e4 MH |
507 | current_ref = NULL; |
508 | return retval; | |
bc5fd6d3 | 509 | } |
c774aab9 | 510 | |
c36b5bc2 | 511 | /* |
d3177275 | 512 | * Call fn for each reference in dir that has index in the range |
432ad41e MH |
513 | * offset <= index < dir->nr. Recurse into subdirectories that are in |
514 | * that index range, sorting them before iterating. This function | |
515 | * does not sort dir itself; it should be sorted beforehand. | |
c36b5bc2 | 516 | */ |
d3177275 MH |
517 | static int do_for_each_ref_in_dir(struct ref_dir *dir, int offset, |
518 | const char *base, | |
519 | each_ref_fn fn, int trim, int flags, void *cb_data) | |
c36b5bc2 MH |
520 | { |
521 | int i; | |
d3177275 MH |
522 | assert(dir->sorted == dir->nr); |
523 | for (i = offset; i < dir->nr; i++) { | |
432ad41e MH |
524 | struct ref_entry *entry = dir->entries[i]; |
525 | int retval; | |
526 | if (entry->flag & REF_DIR) { | |
d7826d54 MH |
527 | struct ref_dir *subdir = get_ref_dir(entry); |
528 | sort_ref_dir(subdir); | |
529 | retval = do_for_each_ref_in_dir(subdir, 0, | |
432ad41e MH |
530 | base, fn, trim, flags, cb_data); |
531 | } else { | |
532 | retval = do_one_ref(base, fn, trim, flags, cb_data, entry); | |
533 | } | |
c36b5bc2 MH |
534 | if (retval) |
535 | return retval; | |
536 | } | |
537 | return 0; | |
538 | } | |
539 | ||
b3fd060f | 540 | /* |
d3177275 | 541 | * Call fn for each reference in the union of dir1 and dir2, in order |
432ad41e MH |
542 | * by refname. Recurse into subdirectories. If a value entry appears |
543 | * in both dir1 and dir2, then only process the version that is in | |
544 | * dir2. The input dirs must already be sorted, but subdirs will be | |
545 | * sorted as needed. | |
b3fd060f | 546 | */ |
d3177275 MH |
547 | static int do_for_each_ref_in_dirs(struct ref_dir *dir1, |
548 | struct ref_dir *dir2, | |
549 | const char *base, each_ref_fn fn, int trim, | |
550 | int flags, void *cb_data) | |
b3fd060f MH |
551 | { |
552 | int retval; | |
553 | int i1 = 0, i2 = 0; | |
554 | ||
d3177275 MH |
555 | assert(dir1->sorted == dir1->nr); |
556 | assert(dir2->sorted == dir2->nr); | |
432ad41e MH |
557 | while (1) { |
558 | struct ref_entry *e1, *e2; | |
559 | int cmp; | |
560 | if (i1 == dir1->nr) { | |
561 | return do_for_each_ref_in_dir(dir2, i2, | |
562 | base, fn, trim, flags, cb_data); | |
563 | } | |
564 | if (i2 == dir2->nr) { | |
565 | return do_for_each_ref_in_dir(dir1, i1, | |
566 | base, fn, trim, flags, cb_data); | |
567 | } | |
568 | e1 = dir1->entries[i1]; | |
569 | e2 = dir2->entries[i2]; | |
570 | cmp = strcmp(e1->name, e2->name); | |
571 | if (cmp == 0) { | |
572 | if ((e1->flag & REF_DIR) && (e2->flag & REF_DIR)) { | |
573 | /* Both are directories; descend them in parallel. */ | |
d7826d54 MH |
574 | struct ref_dir *subdir1 = get_ref_dir(e1); |
575 | struct ref_dir *subdir2 = get_ref_dir(e2); | |
576 | sort_ref_dir(subdir1); | |
577 | sort_ref_dir(subdir2); | |
432ad41e | 578 | retval = do_for_each_ref_in_dirs( |
d7826d54 | 579 | subdir1, subdir2, |
432ad41e MH |
580 | base, fn, trim, flags, cb_data); |
581 | i1++; | |
582 | i2++; | |
583 | } else if (!(e1->flag & REF_DIR) && !(e2->flag & REF_DIR)) { | |
584 | /* Both are references; ignore the one from dir1. */ | |
585 | retval = do_one_ref(base, fn, trim, flags, cb_data, e2); | |
586 | i1++; | |
587 | i2++; | |
588 | } else { | |
589 | die("conflict between reference and directory: %s", | |
590 | e1->name); | |
591 | } | |
b3fd060f | 592 | } else { |
432ad41e MH |
593 | struct ref_entry *e; |
594 | if (cmp < 0) { | |
595 | e = e1; | |
b3fd060f | 596 | i1++; |
432ad41e MH |
597 | } else { |
598 | e = e2; | |
599 | i2++; | |
600 | } | |
601 | if (e->flag & REF_DIR) { | |
d7826d54 MH |
602 | struct ref_dir *subdir = get_ref_dir(e); |
603 | sort_ref_dir(subdir); | |
432ad41e | 604 | retval = do_for_each_ref_in_dir( |
d7826d54 | 605 | subdir, 0, |
432ad41e MH |
606 | base, fn, trim, flags, cb_data); |
607 | } else { | |
608 | retval = do_one_ref(base, fn, trim, flags, cb_data, e); | |
b3fd060f MH |
609 | } |
610 | } | |
611 | if (retval) | |
612 | return retval; | |
613 | } | |
d3177275 MH |
614 | if (i1 < dir1->nr) |
615 | return do_for_each_ref_in_dir(dir1, i1, | |
616 | base, fn, trim, flags, cb_data); | |
617 | if (i2 < dir2->nr) | |
618 | return do_for_each_ref_in_dir(dir2, i2, | |
619 | base, fn, trim, flags, cb_data); | |
b3fd060f MH |
620 | return 0; |
621 | } | |
622 | ||
d66da478 MH |
623 | /* |
624 | * Return true iff refname1 and refname2 conflict with each other. | |
625 | * Two reference names conflict if one of them exactly matches the | |
626 | * leading components of the other; e.g., "foo/bar" conflicts with | |
627 | * both "foo" and with "foo/bar/baz" but not with "foo/bar" or | |
628 | * "foo/barbados". | |
629 | */ | |
630 | static int names_conflict(const char *refname1, const char *refname2) | |
631 | { | |
5a4d4947 MH |
632 | for (; *refname1 && *refname1 == *refname2; refname1++, refname2++) |
633 | ; | |
634 | return (*refname1 == '\0' && *refname2 == '/') | |
635 | || (*refname1 == '/' && *refname2 == '\0'); | |
636 | } | |
637 | ||
638 | struct name_conflict_cb { | |
639 | const char *refname; | |
640 | const char *oldrefname; | |
641 | const char *conflicting_refname; | |
642 | }; | |
643 | ||
644 | static int name_conflict_fn(const char *existingrefname, const unsigned char *sha1, | |
645 | int flags, void *cb_data) | |
646 | { | |
647 | struct name_conflict_cb *data = (struct name_conflict_cb *)cb_data; | |
648 | if (data->oldrefname && !strcmp(data->oldrefname, existingrefname)) | |
649 | return 0; | |
650 | if (names_conflict(data->refname, existingrefname)) { | |
651 | data->conflicting_refname = existingrefname; | |
652 | return 1; | |
d66da478 | 653 | } |
5a4d4947 | 654 | return 0; |
d66da478 MH |
655 | } |
656 | ||
bc5fd6d3 MH |
657 | /* |
658 | * Return true iff a reference named refname could be created without | |
5a4d4947 MH |
659 | * conflicting with the name of an existing reference in array. If |
660 | * oldrefname is non-NULL, ignore potential conflicts with oldrefname | |
661 | * (e.g., because oldrefname is scheduled for deletion in the same | |
bc5fd6d3 MH |
662 | * operation). |
663 | */ | |
664 | static int is_refname_available(const char *refname, const char *oldrefname, | |
d3177275 | 665 | struct ref_dir *dir) |
bc5fd6d3 | 666 | { |
5a4d4947 MH |
667 | struct name_conflict_cb data; |
668 | data.refname = refname; | |
669 | data.oldrefname = oldrefname; | |
670 | data.conflicting_refname = NULL; | |
671 | ||
d3177275 MH |
672 | sort_ref_dir(dir); |
673 | if (do_for_each_ref_in_dir(dir, 0, "", name_conflict_fn, | |
674 | 0, DO_FOR_EACH_INCLUDE_BROKEN, | |
675 | &data)) { | |
5a4d4947 MH |
676 | error("'%s' exists; cannot create '%s'", |
677 | data.conflicting_refname, refname); | |
678 | return 0; | |
bc5fd6d3 MH |
679 | } |
680 | return 1; | |
e1e22e37 LT |
681 | } |
682 | ||
5e290ff7 JH |
683 | /* |
684 | * Future: need to be in "struct repository" | |
685 | * when doing a full libification. | |
686 | */ | |
79c7ca54 MH |
687 | static struct ref_cache { |
688 | struct ref_cache *next; | |
d12229f5 MH |
689 | struct ref_entry *loose; |
690 | struct ref_entry *packed; | |
ce40979c MH |
691 | /* The submodule name, or "" for the main repo. */ |
692 | char name[FLEX_ARRAY]; | |
79c7ca54 | 693 | } *ref_cache; |
0e88c130 | 694 | |
760c4512 | 695 | static void clear_packed_ref_cache(struct ref_cache *refs) |
e1e22e37 | 696 | { |
d12229f5 MH |
697 | if (refs->packed) { |
698 | free_ref_entry(refs->packed); | |
699 | refs->packed = NULL; | |
700 | } | |
5e290ff7 | 701 | } |
e1e22e37 | 702 | |
760c4512 MH |
703 | static void clear_loose_ref_cache(struct ref_cache *refs) |
704 | { | |
d12229f5 MH |
705 | if (refs->loose) { |
706 | free_ref_entry(refs->loose); | |
707 | refs->loose = NULL; | |
708 | } | |
760c4512 MH |
709 | } |
710 | ||
79c7ca54 | 711 | static struct ref_cache *create_ref_cache(const char *submodule) |
e5dbf605 | 712 | { |
ce40979c | 713 | int len; |
79c7ca54 | 714 | struct ref_cache *refs; |
ce40979c MH |
715 | if (!submodule) |
716 | submodule = ""; | |
717 | len = strlen(submodule) + 1; | |
79c7ca54 | 718 | refs = xcalloc(1, sizeof(struct ref_cache) + len); |
ce40979c | 719 | memcpy(refs->name, submodule, len); |
e5dbf605 MH |
720 | return refs; |
721 | } | |
722 | ||
4349a668 | 723 | /* |
79c7ca54 | 724 | * Return a pointer to a ref_cache for the specified submodule. For |
4349a668 MH |
725 | * the main repository, use submodule==NULL. The returned structure |
726 | * will be allocated and initialized but not necessarily populated; it | |
727 | * should not be freed. | |
728 | */ | |
79c7ca54 | 729 | static struct ref_cache *get_ref_cache(const char *submodule) |
4349a668 | 730 | { |
79c7ca54 | 731 | struct ref_cache *refs = ref_cache; |
0e88c130 MH |
732 | if (!submodule) |
733 | submodule = ""; | |
734 | while (refs) { | |
735 | if (!strcmp(submodule, refs->name)) | |
736 | return refs; | |
737 | refs = refs->next; | |
4349a668 | 738 | } |
0e88c130 | 739 | |
79c7ca54 MH |
740 | refs = create_ref_cache(submodule); |
741 | refs->next = ref_cache; | |
742 | ref_cache = refs; | |
0e88c130 | 743 | return refs; |
4349a668 MH |
744 | } |
745 | ||
8be8bde7 | 746 | void invalidate_ref_cache(const char *submodule) |
f130b116 | 747 | { |
c5f29abd MH |
748 | struct ref_cache *refs = get_ref_cache(submodule); |
749 | clear_packed_ref_cache(refs); | |
750 | clear_loose_ref_cache(refs); | |
5e290ff7 | 751 | } |
e1e22e37 | 752 | |
bc5fd6d3 MH |
753 | /* |
754 | * Parse one line from a packed-refs file. Write the SHA1 to sha1. | |
755 | * Return a pointer to the refname within the line (null-terminated), | |
756 | * or NULL if there was a problem. | |
757 | */ | |
758 | static const char *parse_ref_line(char *line, unsigned char *sha1) | |
759 | { | |
760 | /* | |
761 | * 42: the answer to everything. | |
762 | * | |
763 | * In this case, it happens to be the answer to | |
764 | * 40 (length of sha1 hex representation) | |
765 | * +1 (space in between hex and name) | |
766 | * +1 (newline at the end of the line) | |
767 | */ | |
768 | int len = strlen(line) - 42; | |
769 | ||
770 | if (len <= 0) | |
771 | return NULL; | |
772 | if (get_sha1_hex(line, sha1) < 0) | |
773 | return NULL; | |
774 | if (!isspace(line[40])) | |
775 | return NULL; | |
776 | line += 41; | |
777 | if (isspace(*line)) | |
778 | return NULL; | |
779 | if (line[len] != '\n') | |
780 | return NULL; | |
781 | line[len] = 0; | |
782 | ||
783 | return line; | |
784 | } | |
785 | ||
d3177275 | 786 | static void read_packed_refs(FILE *f, struct ref_dir *dir) |
f4204ab9 | 787 | { |
e9c4c111 | 788 | struct ref_entry *last = NULL; |
f4204ab9 JH |
789 | char refline[PATH_MAX]; |
790 | int flag = REF_ISPACKED; | |
791 | ||
792 | while (fgets(refline, sizeof(refline), f)) { | |
793 | unsigned char sha1[20]; | |
dfefa935 | 794 | const char *refname; |
f4204ab9 JH |
795 | static const char header[] = "# pack-refs with:"; |
796 | ||
797 | if (!strncmp(refline, header, sizeof(header)-1)) { | |
798 | const char *traits = refline + sizeof(header) - 1; | |
799 | if (strstr(traits, " peeled ")) | |
800 | flag |= REF_KNOWS_PEELED; | |
801 | /* perhaps other traits later as well */ | |
802 | continue; | |
803 | } | |
804 | ||
dfefa935 MH |
805 | refname = parse_ref_line(refline, sha1); |
806 | if (refname) { | |
dd73ecd1 | 807 | last = create_ref_entry(refname, sha1, flag, 1); |
d3177275 | 808 | add_ref(dir, last); |
f4204ab9 JH |
809 | continue; |
810 | } | |
811 | if (last && | |
812 | refline[0] == '^' && | |
813 | strlen(refline) == 42 && | |
814 | refline[41] == '\n' && | |
815 | !get_sha1_hex(refline + 1, sha1)) | |
593f1bb8 | 816 | hashcpy(last->u.value.peeled, sha1); |
f4204ab9 | 817 | } |
f4204ab9 JH |
818 | } |
819 | ||
d3177275 | 820 | static struct ref_dir *get_packed_refs(struct ref_cache *refs) |
5e290ff7 | 821 | { |
d12229f5 | 822 | if (!refs->packed) { |
4349a668 MH |
823 | const char *packed_refs_file; |
824 | FILE *f; | |
0bad611b | 825 | |
28e6a34e | 826 | refs->packed = create_dir_entry(refs, "", 0); |
316b097a MH |
827 | if (*refs->name) |
828 | packed_refs_file = git_path_submodule(refs->name, "packed-refs"); | |
4349a668 MH |
829 | else |
830 | packed_refs_file = git_path("packed-refs"); | |
831 | f = fopen(packed_refs_file, "r"); | |
e1e22e37 | 832 | if (f) { |
d7826d54 | 833 | read_packed_refs(f, get_ref_dir(refs->packed)); |
e1e22e37 | 834 | fclose(f); |
e1e22e37 | 835 | } |
e1e22e37 | 836 | } |
d7826d54 | 837 | return get_ref_dir(refs->packed); |
e1e22e37 LT |
838 | } |
839 | ||
30249ee6 MH |
840 | void add_packed_ref(const char *refname, const unsigned char *sha1) |
841 | { | |
842 | add_ref(get_packed_refs(get_ref_cache(NULL)), | |
843 | create_ref_entry(refname, sha1, REF_ISPACKED, 1)); | |
844 | } | |
845 | ||
abc39098 | 846 | /* |
28e6a34e MH |
847 | * Read the loose references from the namespace dirname into dir |
848 | * (without recursing). dirname must end with '/'. dir must be the | |
849 | * directory entry corresponding to dirname. | |
abc39098 | 850 | */ |
423a1afc | 851 | static void read_loose_refs(const char *dirname, struct ref_dir *dir) |
e1e22e37 | 852 | { |
423a1afc | 853 | struct ref_cache *refs = dir->ref_cache; |
d3177275 | 854 | DIR *d; |
0bad611b | 855 | const char *path; |
d5fdae67 | 856 | struct dirent *de; |
abc39098 | 857 | int dirnamelen = strlen(dirname); |
72b64b44 | 858 | struct strbuf refname; |
0bad611b | 859 | |
3b124823 | 860 | if (*refs->name) |
66a3d20b | 861 | path = git_path_submodule(refs->name, "%s", dirname); |
0bad611b | 862 | else |
66a3d20b | 863 | path = git_path("%s", dirname); |
0bad611b | 864 | |
d3177275 | 865 | d = opendir(path); |
d5fdae67 MH |
866 | if (!d) |
867 | return; | |
868 | ||
66a3d20b MH |
869 | strbuf_init(&refname, dirnamelen + 257); |
870 | strbuf_add(&refname, dirname, dirnamelen); | |
d5fdae67 MH |
871 | |
872 | while ((de = readdir(d)) != NULL) { | |
873 | unsigned char sha1[20]; | |
874 | struct stat st; | |
875 | int flag; | |
d5fdae67 MH |
876 | const char *refdir; |
877 | ||
878 | if (de->d_name[0] == '.') | |
879 | continue; | |
d5fdae67 MH |
880 | if (has_extension(de->d_name, ".lock")) |
881 | continue; | |
72b64b44 | 882 | strbuf_addstr(&refname, de->d_name); |
d5fdae67 | 883 | refdir = *refs->name |
72b64b44 MH |
884 | ? git_path_submodule(refs->name, "%s", refname.buf) |
885 | : git_path("%s", refname.buf); | |
886 | if (stat(refdir, &st) < 0) { | |
887 | ; /* silently ignore */ | |
888 | } else if (S_ISDIR(st.st_mode)) { | |
abc39098 | 889 | strbuf_addch(&refname, '/'); |
28e6a34e MH |
890 | add_entry_to_dir(dir, |
891 | create_dir_entry(refs, refname.buf, 1)); | |
72b64b44 | 892 | } else { |
3b124823 | 893 | if (*refs->name) { |
f8948e2f | 894 | hashclr(sha1); |
0bad611b | 895 | flag = 0; |
72b64b44 | 896 | if (resolve_gitlink_ref(refs->name, refname.buf, sha1) < 0) { |
0bad611b | 897 | hashclr(sha1); |
98ac34b2 | 898 | flag |= REF_ISBROKEN; |
0bad611b | 899 | } |
72b64b44 | 900 | } else if (read_ref_full(refname.buf, sha1, 1, &flag)) { |
09116a1c JH |
901 | hashclr(sha1); |
902 | flag |= REF_ISBROKEN; | |
903 | } | |
9f2fb4a3 MH |
904 | add_entry_to_dir(dir, |
905 | create_ref_entry(refname.buf, sha1, flag, 1)); | |
e1e22e37 | 906 | } |
66a3d20b | 907 | strbuf_setlen(&refname, dirnamelen); |
e1e22e37 | 908 | } |
72b64b44 | 909 | strbuf_release(&refname); |
d5fdae67 | 910 | closedir(d); |
e1e22e37 LT |
911 | } |
912 | ||
d3177275 | 913 | static struct ref_dir *get_loose_refs(struct ref_cache *refs) |
e1e22e37 | 914 | { |
d12229f5 | 915 | if (!refs->loose) { |
28e6a34e MH |
916 | /* |
917 | * Mark the top-level directory complete because we | |
918 | * are about to read the only subdirectory that can | |
919 | * hold references: | |
920 | */ | |
921 | refs->loose = create_dir_entry(refs, "", 0); | |
922 | /* | |
923 | * Create an incomplete entry for "refs/": | |
924 | */ | |
925 | add_entry_to_dir(get_ref_dir(refs->loose), | |
926 | create_dir_entry(refs, "refs/", 1)); | |
e1e22e37 | 927 | } |
d7826d54 | 928 | return get_ref_dir(refs->loose); |
e1e22e37 LT |
929 | } |
930 | ||
ca8db142 LT |
931 | /* We allow "recursive" symbolic refs. Only within reason, though */ |
932 | #define MAXDEPTH 5 | |
0ebde32c LT |
933 | #define MAXREFLEN (1024) |
934 | ||
e5fa45c1 JH |
935 | /* |
936 | * Called by resolve_gitlink_ref_recursive() after it failed to read | |
b0626608 MH |
937 | * from the loose refs in ref_cache refs. Find <refname> in the |
938 | * packed-refs file for the submodule. | |
e5fa45c1 | 939 | */ |
b0626608 | 940 | static int resolve_gitlink_packed_ref(struct ref_cache *refs, |
85be1fe3 | 941 | const char *refname, unsigned char *sha1) |
0ebde32c | 942 | { |
2c5c66be | 943 | struct ref_entry *ref; |
d3177275 | 944 | struct ref_dir *dir = get_packed_refs(refs); |
0ebde32c | 945 | |
432ad41e | 946 | ref = find_ref(dir, refname); |
b0626608 MH |
947 | if (ref == NULL) |
948 | return -1; | |
949 | ||
593f1bb8 | 950 | memcpy(sha1, ref->u.value.sha1, 20); |
b0626608 | 951 | return 0; |
0ebde32c LT |
952 | } |
953 | ||
b0626608 | 954 | static int resolve_gitlink_ref_recursive(struct ref_cache *refs, |
85be1fe3 | 955 | const char *refname, unsigned char *sha1, |
dfefa935 | 956 | int recursion) |
0ebde32c | 957 | { |
064d51dc | 958 | int fd, len; |
0ebde32c | 959 | char buffer[128], *p; |
064d51dc | 960 | char *path; |
0ebde32c | 961 | |
064d51dc | 962 | if (recursion > MAXDEPTH || strlen(refname) > MAXREFLEN) |
0ebde32c | 963 | return -1; |
064d51dc MH |
964 | path = *refs->name |
965 | ? git_path_submodule(refs->name, "%s", refname) | |
966 | : git_path("%s", refname); | |
967 | fd = open(path, O_RDONLY); | |
0ebde32c | 968 | if (fd < 0) |
b0626608 | 969 | return resolve_gitlink_packed_ref(refs, refname, sha1); |
0ebde32c LT |
970 | |
971 | len = read(fd, buffer, sizeof(buffer)-1); | |
972 | close(fd); | |
973 | if (len < 0) | |
974 | return -1; | |
975 | while (len && isspace(buffer[len-1])) | |
976 | len--; | |
977 | buffer[len] = 0; | |
978 | ||
979 | /* Was it a detached head or an old-fashioned symlink? */ | |
85be1fe3 | 980 | if (!get_sha1_hex(buffer, sha1)) |
0ebde32c LT |
981 | return 0; |
982 | ||
983 | /* Symref? */ | |
984 | if (strncmp(buffer, "ref:", 4)) | |
985 | return -1; | |
986 | p = buffer + 4; | |
987 | while (isspace(*p)) | |
988 | p++; | |
989 | ||
064d51dc | 990 | return resolve_gitlink_ref_recursive(refs, p, sha1, recursion+1); |
0ebde32c LT |
991 | } |
992 | ||
85be1fe3 | 993 | int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sha1) |
0ebde32c LT |
994 | { |
995 | int len = strlen(path), retval; | |
064d51dc | 996 | char *submodule; |
b0626608 | 997 | struct ref_cache *refs; |
0ebde32c LT |
998 | |
999 | while (len && path[len-1] == '/') | |
1000 | len--; | |
1001 | if (!len) | |
1002 | return -1; | |
b0626608 MH |
1003 | submodule = xstrndup(path, len); |
1004 | refs = get_ref_cache(submodule); | |
1005 | free(submodule); | |
1006 | ||
064d51dc | 1007 | retval = resolve_gitlink_ref_recursive(refs, refname, sha1, 0); |
0ebde32c LT |
1008 | return retval; |
1009 | } | |
ca8db142 | 1010 | |
4886b89f | 1011 | /* |
c224ca7f MH |
1012 | * Try to read ref from the packed references. On success, set sha1 |
1013 | * and return 0; otherwise, return -1. | |
4886b89f | 1014 | */ |
dfefa935 | 1015 | static int get_packed_ref(const char *refname, unsigned char *sha1) |
c224ca7f | 1016 | { |
d3177275 | 1017 | struct ref_dir *packed = get_packed_refs(get_ref_cache(NULL)); |
432ad41e | 1018 | struct ref_entry *entry = find_ref(packed, refname); |
2c5c66be | 1019 | if (entry) { |
593f1bb8 | 1020 | hashcpy(sha1, entry->u.value.sha1); |
2c5c66be | 1021 | return 0; |
c224ca7f MH |
1022 | } |
1023 | return -1; | |
1024 | } | |
1025 | ||
8d68493f | 1026 | const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int reading, int *flag) |
8a65ff76 | 1027 | { |
0104ca09 HO |
1028 | int depth = MAXDEPTH; |
1029 | ssize_t len; | |
a876ed83 | 1030 | char buffer[256]; |
dfefa935 | 1031 | static char refname_buffer[256]; |
ca8db142 | 1032 | |
8da19775 JH |
1033 | if (flag) |
1034 | *flag = 0; | |
1035 | ||
dfefa935 | 1036 | if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) |
8384d788 MH |
1037 | return NULL; |
1038 | ||
a876ed83 | 1039 | for (;;) { |
55956350 | 1040 | char path[PATH_MAX]; |
a876ed83 JH |
1041 | struct stat st; |
1042 | char *buf; | |
1043 | int fd; | |
8a65ff76 | 1044 | |
a876ed83 JH |
1045 | if (--depth < 0) |
1046 | return NULL; | |
ca8db142 | 1047 | |
dfefa935 | 1048 | git_snpath(path, sizeof(path), "%s", refname); |
c224ca7f | 1049 | |
a876ed83 | 1050 | if (lstat(path, &st) < 0) { |
c224ca7f MH |
1051 | if (errno != ENOENT) |
1052 | return NULL; | |
1053 | /* | |
1054 | * The loose reference file does not exist; | |
1055 | * check for a packed reference. | |
1056 | */ | |
dfefa935 | 1057 | if (!get_packed_ref(refname, sha1)) { |
c224ca7f MH |
1058 | if (flag) |
1059 | *flag |= REF_ISPACKED; | |
dfefa935 | 1060 | return refname; |
434cd0cd | 1061 | } |
c224ca7f MH |
1062 | /* The reference is not a packed reference, either. */ |
1063 | if (reading) { | |
a876ed83 | 1064 | return NULL; |
c224ca7f MH |
1065 | } else { |
1066 | hashclr(sha1); | |
dfefa935 | 1067 | return refname; |
c224ca7f | 1068 | } |
a876ed83 | 1069 | } |
ca8db142 | 1070 | |
a876ed83 JH |
1071 | /* Follow "normalized" - ie "refs/.." symlinks by hand */ |
1072 | if (S_ISLNK(st.st_mode)) { | |
1073 | len = readlink(path, buffer, sizeof(buffer)-1); | |
7bb2bf8e MH |
1074 | if (len < 0) |
1075 | return NULL; | |
b54cb795 | 1076 | buffer[len] = 0; |
1f58a038 MH |
1077 | if (!prefixcmp(buffer, "refs/") && |
1078 | !check_refname_format(buffer, 0)) { | |
dfefa935 MH |
1079 | strcpy(refname_buffer, buffer); |
1080 | refname = refname_buffer; | |
8da19775 JH |
1081 | if (flag) |
1082 | *flag |= REF_ISSYMREF; | |
a876ed83 JH |
1083 | continue; |
1084 | } | |
ca8db142 | 1085 | } |
a876ed83 | 1086 | |
7a21632f DS |
1087 | /* Is it a directory? */ |
1088 | if (S_ISDIR(st.st_mode)) { | |
1089 | errno = EISDIR; | |
1090 | return NULL; | |
1091 | } | |
1092 | ||
a876ed83 JH |
1093 | /* |
1094 | * Anything else, just open it and try to use it as | |
1095 | * a ref | |
1096 | */ | |
1097 | fd = open(path, O_RDONLY); | |
1098 | if (fd < 0) | |
1099 | return NULL; | |
93d26e4c | 1100 | len = read_in_full(fd, buffer, sizeof(buffer)-1); |
a876ed83 | 1101 | close(fd); |
28775050 MH |
1102 | if (len < 0) |
1103 | return NULL; | |
1104 | while (len && isspace(buffer[len-1])) | |
1105 | len--; | |
1106 | buffer[len] = '\0'; | |
a876ed83 JH |
1107 | |
1108 | /* | |
1109 | * Is it a symbolic ref? | |
1110 | */ | |
28775050 | 1111 | if (prefixcmp(buffer, "ref:")) |
a876ed83 | 1112 | break; |
55956350 JH |
1113 | if (flag) |
1114 | *flag |= REF_ISSYMREF; | |
a876ed83 | 1115 | buf = buffer + 4; |
28775050 MH |
1116 | while (isspace(*buf)) |
1117 | buf++; | |
313fb010 | 1118 | if (check_refname_format(buf, REFNAME_ALLOW_ONELEVEL)) { |
55956350 JH |
1119 | if (flag) |
1120 | *flag |= REF_ISBROKEN; | |
313fb010 MH |
1121 | return NULL; |
1122 | } | |
dfefa935 | 1123 | refname = strcpy(refname_buffer, buf); |
8a65ff76 | 1124 | } |
f989fea0 MH |
1125 | /* Please note that FETCH_HEAD has a second line containing other data. */ |
1126 | if (get_sha1_hex(buffer, sha1) || (buffer[40] != '\0' && !isspace(buffer[40]))) { | |
55956350 JH |
1127 | if (flag) |
1128 | *flag |= REF_ISBROKEN; | |
a876ed83 | 1129 | return NULL; |
629cd3ac | 1130 | } |
dfefa935 | 1131 | return refname; |
a876ed83 JH |
1132 | } |
1133 | ||
96ec7b1e NTND |
1134 | char *resolve_refdup(const char *ref, unsigned char *sha1, int reading, int *flag) |
1135 | { | |
8cad4744 | 1136 | const char *ret = resolve_ref_unsafe(ref, sha1, reading, flag); |
96ec7b1e NTND |
1137 | return ret ? xstrdup(ret) : NULL; |
1138 | } | |
1139 | ||
d08bae7e IL |
1140 | /* The argument to filter_refs */ |
1141 | struct ref_filter { | |
1142 | const char *pattern; | |
1143 | each_ref_fn *fn; | |
1144 | void *cb_data; | |
1145 | }; | |
1146 | ||
dfefa935 | 1147 | int read_ref_full(const char *refname, unsigned char *sha1, int reading, int *flags) |
a876ed83 | 1148 | { |
8d68493f | 1149 | if (resolve_ref_unsafe(refname, sha1, reading, flags)) |
a876ed83 JH |
1150 | return 0; |
1151 | return -1; | |
8a65ff76 LT |
1152 | } |
1153 | ||
dfefa935 | 1154 | int read_ref(const char *refname, unsigned char *sha1) |
c6893323 | 1155 | { |
dfefa935 | 1156 | return read_ref_full(refname, sha1, 1, NULL); |
c6893323 NTND |
1157 | } |
1158 | ||
bc5fd6d3 | 1159 | int ref_exists(const char *refname) |
ef06b918 | 1160 | { |
bc5fd6d3 MH |
1161 | unsigned char sha1[20]; |
1162 | return !!resolve_ref_unsafe(refname, sha1, 1, NULL); | |
ef06b918 JH |
1163 | } |
1164 | ||
85be1fe3 | 1165 | static int filter_refs(const char *refname, const unsigned char *sha1, int flags, |
dfefa935 | 1166 | void *data) |
d08bae7e IL |
1167 | { |
1168 | struct ref_filter *filter = (struct ref_filter *)data; | |
dfefa935 | 1169 | if (fnmatch(filter->pattern, refname, 0)) |
d08bae7e | 1170 | return 0; |
85be1fe3 | 1171 | return filter->fn(refname, sha1, flags, filter->cb_data); |
d08bae7e IL |
1172 | } |
1173 | ||
dfefa935 | 1174 | int peel_ref(const char *refname, unsigned char *sha1) |
cf0adba7 JH |
1175 | { |
1176 | int flag; | |
1177 | unsigned char base[20]; | |
1178 | struct object *o; | |
1179 | ||
dfefa935 MH |
1180 | if (current_ref && (current_ref->name == refname |
1181 | || !strcmp(current_ref->name, refname))) { | |
0ae91be0 | 1182 | if (current_ref->flag & REF_KNOWS_PEELED) { |
593f1bb8 | 1183 | hashcpy(sha1, current_ref->u.value.peeled); |
0ae91be0 SP |
1184 | return 0; |
1185 | } | |
593f1bb8 | 1186 | hashcpy(base, current_ref->u.value.sha1); |
0ae91be0 SP |
1187 | goto fallback; |
1188 | } | |
1189 | ||
dfefa935 | 1190 | if (read_ref_full(refname, base, 1, &flag)) |
cf0adba7 JH |
1191 | return -1; |
1192 | ||
1193 | if ((flag & REF_ISPACKED)) { | |
d3177275 | 1194 | struct ref_dir *dir = get_packed_refs(get_ref_cache(NULL)); |
432ad41e | 1195 | struct ref_entry *r = find_ref(dir, refname); |
cf0adba7 | 1196 | |
e9c4c111 | 1197 | if (r != NULL && r->flag & REF_KNOWS_PEELED) { |
593f1bb8 | 1198 | hashcpy(sha1, r->u.value.peeled); |
e9c4c111 | 1199 | return 0; |
cf0adba7 | 1200 | } |
cf0adba7 JH |
1201 | } |
1202 | ||
0ae91be0 | 1203 | fallback: |
cf0adba7 | 1204 | o = parse_object(base); |
8c87dc77 | 1205 | if (o && o->type == OBJ_TAG) { |
dfefa935 | 1206 | o = deref_tag(o, refname, 0); |
cf0adba7 JH |
1207 | if (o) { |
1208 | hashcpy(sha1, o->sha1); | |
1209 | return 0; | |
1210 | } | |
1211 | } | |
1212 | return -1; | |
1213 | } | |
1214 | ||
bc5fd6d3 MH |
1215 | struct warn_if_dangling_data { |
1216 | FILE *fp; | |
1217 | const char *refname; | |
1218 | const char *msg_fmt; | |
1219 | }; | |
1220 | ||
1221 | static int warn_if_dangling_symref(const char *refname, const unsigned char *sha1, | |
1222 | int flags, void *cb_data) | |
1223 | { | |
1224 | struct warn_if_dangling_data *d = cb_data; | |
1225 | const char *resolves_to; | |
1226 | unsigned char junk[20]; | |
1227 | ||
1228 | if (!(flags & REF_ISSYMREF)) | |
1229 | return 0; | |
1230 | ||
1231 | resolves_to = resolve_ref_unsafe(refname, junk, 0, NULL); | |
1232 | if (!resolves_to || strcmp(resolves_to, d->refname)) | |
1233 | return 0; | |
1234 | ||
1235 | fprintf(d->fp, d->msg_fmt, refname); | |
1be65eda | 1236 | fputc('\n', d->fp); |
bc5fd6d3 MH |
1237 | return 0; |
1238 | } | |
1239 | ||
1240 | void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname) | |
1241 | { | |
1242 | struct warn_if_dangling_data data; | |
1243 | ||
1244 | data.fp = fp; | |
1245 | data.refname = refname; | |
1246 | data.msg_fmt = msg_fmt; | |
1247 | for_each_rawref(warn_if_dangling_symref, &data); | |
1248 | } | |
1249 | ||
0bad611b HV |
1250 | static int do_for_each_ref(const char *submodule, const char *base, each_ref_fn fn, |
1251 | int trim, int flags, void *cb_data) | |
8a65ff76 | 1252 | { |
316b097a | 1253 | struct ref_cache *refs = get_ref_cache(submodule); |
933ac036 MH |
1254 | struct ref_dir *packed_dir = get_packed_refs(refs); |
1255 | struct ref_dir *loose_dir = get_loose_refs(refs); | |
1256 | int retval = 0; | |
1257 | ||
1258 | if (base && *base) { | |
1259 | packed_dir = find_containing_dir(packed_dir, base, 0); | |
1260 | loose_dir = find_containing_dir(loose_dir, base, 0); | |
1261 | } | |
1262 | ||
1263 | if (packed_dir && loose_dir) { | |
1264 | sort_ref_dir(packed_dir); | |
1265 | sort_ref_dir(loose_dir); | |
1266 | retval = do_for_each_ref_in_dirs( | |
1267 | packed_dir, loose_dir, | |
1268 | base, fn, trim, flags, cb_data); | |
1269 | } else if (packed_dir) { | |
1270 | sort_ref_dir(packed_dir); | |
1271 | retval = do_for_each_ref_in_dir( | |
1272 | packed_dir, 0, | |
1273 | base, fn, trim, flags, cb_data); | |
1274 | } else if (loose_dir) { | |
1275 | sort_ref_dir(loose_dir); | |
1276 | retval = do_for_each_ref_in_dir( | |
1277 | loose_dir, 0, | |
1278 | base, fn, trim, flags, cb_data); | |
1279 | } | |
1280 | ||
1281 | return retval; | |
8a65ff76 LT |
1282 | } |
1283 | ||
0bad611b | 1284 | static int do_head_ref(const char *submodule, each_ref_fn fn, void *cb_data) |
723c31fe LT |
1285 | { |
1286 | unsigned char sha1[20]; | |
8da19775 JH |
1287 | int flag; |
1288 | ||
0bad611b HV |
1289 | if (submodule) { |
1290 | if (resolve_gitlink_ref(submodule, "HEAD", sha1) == 0) | |
1291 | return fn("HEAD", sha1, 0, cb_data); | |
1292 | ||
1293 | return 0; | |
1294 | } | |
1295 | ||
c6893323 | 1296 | if (!read_ref_full("HEAD", sha1, 1, &flag)) |
8da19775 | 1297 | return fn("HEAD", sha1, flag, cb_data); |
0bad611b | 1298 | |
2f34ba32 | 1299 | return 0; |
723c31fe LT |
1300 | } |
1301 | ||
0bad611b HV |
1302 | int head_ref(each_ref_fn fn, void *cb_data) |
1303 | { | |
1304 | return do_head_ref(NULL, fn, cb_data); | |
1305 | } | |
1306 | ||
9ef6aeb0 HV |
1307 | int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data) |
1308 | { | |
1309 | return do_head_ref(submodule, fn, cb_data); | |
1310 | } | |
1311 | ||
cb5d709f | 1312 | int for_each_ref(each_ref_fn fn, void *cb_data) |
8a65ff76 | 1313 | { |
b3cfc406 | 1314 | return do_for_each_ref(NULL, "", fn, 0, 0, cb_data); |
a62be77f SE |
1315 | } |
1316 | ||
9ef6aeb0 HV |
1317 | int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data) |
1318 | { | |
b3cfc406 | 1319 | return do_for_each_ref(submodule, "", fn, 0, 0, cb_data); |
a62be77f SE |
1320 | } |
1321 | ||
2a8177b6 CC |
1322 | int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data) |
1323 | { | |
0bad611b | 1324 | return do_for_each_ref(NULL, prefix, fn, strlen(prefix), 0, cb_data); |
2a8177b6 CC |
1325 | } |
1326 | ||
9ef6aeb0 HV |
1327 | int for_each_ref_in_submodule(const char *submodule, const char *prefix, |
1328 | each_ref_fn fn, void *cb_data) | |
1329 | { | |
1330 | return do_for_each_ref(submodule, prefix, fn, strlen(prefix), 0, cb_data); | |
2a8177b6 CC |
1331 | } |
1332 | ||
cb5d709f | 1333 | int for_each_tag_ref(each_ref_fn fn, void *cb_data) |
a62be77f | 1334 | { |
2a8177b6 | 1335 | return for_each_ref_in("refs/tags/", fn, cb_data); |
a62be77f SE |
1336 | } |
1337 | ||
9ef6aeb0 HV |
1338 | int for_each_tag_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data) |
1339 | { | |
1340 | return for_each_ref_in_submodule(submodule, "refs/tags/", fn, cb_data); | |
1341 | } | |
1342 | ||
cb5d709f | 1343 | int for_each_branch_ref(each_ref_fn fn, void *cb_data) |
a62be77f | 1344 | { |
2a8177b6 | 1345 | return for_each_ref_in("refs/heads/", fn, cb_data); |
a62be77f SE |
1346 | } |
1347 | ||
9ef6aeb0 HV |
1348 | int for_each_branch_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data) |
1349 | { | |
1350 | return for_each_ref_in_submodule(submodule, "refs/heads/", fn, cb_data); | |
1351 | } | |
1352 | ||
cb5d709f | 1353 | int for_each_remote_ref(each_ref_fn fn, void *cb_data) |
a62be77f | 1354 | { |
2a8177b6 | 1355 | return for_each_ref_in("refs/remotes/", fn, cb_data); |
f8948e2f JH |
1356 | } |
1357 | ||
9ef6aeb0 HV |
1358 | int for_each_remote_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data) |
1359 | { | |
1360 | return for_each_ref_in_submodule(submodule, "refs/remotes/", fn, cb_data); | |
1361 | } | |
1362 | ||
29268700 CC |
1363 | int for_each_replace_ref(each_ref_fn fn, void *cb_data) |
1364 | { | |
0bad611b | 1365 | return do_for_each_ref(NULL, "refs/replace/", fn, 13, 0, cb_data); |
29268700 CC |
1366 | } |
1367 | ||
a1bea2c1 JT |
1368 | int head_ref_namespaced(each_ref_fn fn, void *cb_data) |
1369 | { | |
1370 | struct strbuf buf = STRBUF_INIT; | |
1371 | int ret = 0; | |
1372 | unsigned char sha1[20]; | |
1373 | int flag; | |
1374 | ||
1375 | strbuf_addf(&buf, "%sHEAD", get_git_namespace()); | |
c6893323 | 1376 | if (!read_ref_full(buf.buf, sha1, 1, &flag)) |
a1bea2c1 JT |
1377 | ret = fn(buf.buf, sha1, flag, cb_data); |
1378 | strbuf_release(&buf); | |
1379 | ||
1380 | return ret; | |
1381 | } | |
1382 | ||
1383 | int for_each_namespaced_ref(each_ref_fn fn, void *cb_data) | |
1384 | { | |
1385 | struct strbuf buf = STRBUF_INIT; | |
1386 | int ret; | |
1387 | strbuf_addf(&buf, "%srefs/", get_git_namespace()); | |
1388 | ret = do_for_each_ref(NULL, buf.buf, fn, 0, 0, cb_data); | |
1389 | strbuf_release(&buf); | |
1390 | return ret; | |
1391 | } | |
1392 | ||
b09fe971 IL |
1393 | int for_each_glob_ref_in(each_ref_fn fn, const char *pattern, |
1394 | const char *prefix, void *cb_data) | |
d08bae7e IL |
1395 | { |
1396 | struct strbuf real_pattern = STRBUF_INIT; | |
1397 | struct ref_filter filter; | |
d08bae7e IL |
1398 | int ret; |
1399 | ||
b09fe971 | 1400 | if (!prefix && prefixcmp(pattern, "refs/")) |
d08bae7e | 1401 | strbuf_addstr(&real_pattern, "refs/"); |
b09fe971 IL |
1402 | else if (prefix) |
1403 | strbuf_addstr(&real_pattern, prefix); | |
d08bae7e IL |
1404 | strbuf_addstr(&real_pattern, pattern); |
1405 | ||
894a9d33 | 1406 | if (!has_glob_specials(pattern)) { |
9517e6b8 | 1407 | /* Append implied '/' '*' if not present. */ |
d08bae7e IL |
1408 | if (real_pattern.buf[real_pattern.len - 1] != '/') |
1409 | strbuf_addch(&real_pattern, '/'); | |
1410 | /* No need to check for '*', there is none. */ | |
1411 | strbuf_addch(&real_pattern, '*'); | |
1412 | } | |
1413 | ||
1414 | filter.pattern = real_pattern.buf; | |
1415 | filter.fn = fn; | |
1416 | filter.cb_data = cb_data; | |
1417 | ret = for_each_ref(filter_refs, &filter); | |
1418 | ||
1419 | strbuf_release(&real_pattern); | |
1420 | return ret; | |
1421 | } | |
1422 | ||
b09fe971 IL |
1423 | int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data) |
1424 | { | |
1425 | return for_each_glob_ref_in(fn, pattern, NULL, cb_data); | |
1426 | } | |
1427 | ||
f8948e2f JH |
1428 | int for_each_rawref(each_ref_fn fn, void *cb_data) |
1429 | { | |
b3cfc406 | 1430 | return do_for_each_ref(NULL, "", fn, 0, |
f8948e2f | 1431 | DO_FOR_EACH_INCLUDE_BROKEN, cb_data); |
8a65ff76 LT |
1432 | } |
1433 | ||
4577e483 | 1434 | const char *prettify_refname(const char *name) |
a9c37a72 | 1435 | { |
a9c37a72 DB |
1436 | return name + ( |
1437 | !prefixcmp(name, "refs/heads/") ? 11 : | |
1438 | !prefixcmp(name, "refs/tags/") ? 10 : | |
1439 | !prefixcmp(name, "refs/remotes/") ? 13 : | |
1440 | 0); | |
1441 | } | |
1442 | ||
79803322 SP |
1443 | const char *ref_rev_parse_rules[] = { |
1444 | "%.*s", | |
1445 | "refs/%.*s", | |
1446 | "refs/tags/%.*s", | |
1447 | "refs/heads/%.*s", | |
1448 | "refs/remotes/%.*s", | |
1449 | "refs/remotes/%.*s/HEAD", | |
1450 | NULL | |
1451 | }; | |
1452 | ||
1453 | int refname_match(const char *abbrev_name, const char *full_name, const char **rules) | |
1454 | { | |
1455 | const char **p; | |
1456 | const int abbrev_name_len = strlen(abbrev_name); | |
1457 | ||
1458 | for (p = rules; *p; p++) { | |
1459 | if (!strcmp(full_name, mkpath(*p, abbrev_name_len, abbrev_name))) { | |
1460 | return 1; | |
1461 | } | |
1462 | } | |
1463 | ||
1464 | return 0; | |
1465 | } | |
1466 | ||
e5f38ec3 | 1467 | static struct ref_lock *verify_lock(struct ref_lock *lock, |
4bd18c43 SP |
1468 | const unsigned char *old_sha1, int mustexist) |
1469 | { | |
c6893323 | 1470 | if (read_ref_full(lock->ref_name, lock->old_sha1, mustexist, NULL)) { |
434cd0cd | 1471 | error("Can't verify ref %s", lock->ref_name); |
4bd18c43 SP |
1472 | unlock_ref(lock); |
1473 | return NULL; | |
1474 | } | |
a89fccd2 | 1475 | if (hashcmp(lock->old_sha1, old_sha1)) { |
434cd0cd | 1476 | error("Ref %s is at %s but expected %s", lock->ref_name, |
4bd18c43 SP |
1477 | sha1_to_hex(lock->old_sha1), sha1_to_hex(old_sha1)); |
1478 | unlock_ref(lock); | |
1479 | return NULL; | |
1480 | } | |
1481 | return lock; | |
1482 | } | |
1483 | ||
7155b727 | 1484 | static int remove_empty_directories(const char *file) |
bc7127ef JH |
1485 | { |
1486 | /* we want to create a file but there is a directory there; | |
1487 | * if that is an empty directory (or a directory that contains | |
1488 | * only empty directories), remove them. | |
1489 | */ | |
7155b727 JS |
1490 | struct strbuf path; |
1491 | int result; | |
bc7127ef | 1492 | |
7155b727 JS |
1493 | strbuf_init(&path, 20); |
1494 | strbuf_addstr(&path, file); | |
1495 | ||
a0f4afbe | 1496 | result = remove_dir_recursively(&path, REMOVE_DIR_EMPTY_ONLY); |
7155b727 JS |
1497 | |
1498 | strbuf_release(&path); | |
1499 | ||
1500 | return result; | |
bc7127ef JH |
1501 | } |
1502 | ||
ff74f7f1 JH |
1503 | /* |
1504 | * *string and *len will only be substituted, and *string returned (for | |
1505 | * later free()ing) if the string passed in is a magic short-hand form | |
1506 | * to name a branch. | |
1507 | */ | |
1508 | static char *substitute_branch_name(const char **string, int *len) | |
1509 | { | |
1510 | struct strbuf buf = STRBUF_INIT; | |
1511 | int ret = interpret_branch_name(*string, &buf); | |
1512 | ||
1513 | if (ret == *len) { | |
1514 | size_t size; | |
1515 | *string = strbuf_detach(&buf, &size); | |
1516 | *len = size; | |
1517 | return (char *)*string; | |
1518 | } | |
1519 | ||
1520 | return NULL; | |
1521 | } | |
1522 | ||
1523 | int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref) | |
1524 | { | |
1525 | char *last_branch = substitute_branch_name(&str, &len); | |
1526 | const char **p, *r; | |
1527 | int refs_found = 0; | |
1528 | ||
1529 | *ref = NULL; | |
1530 | for (p = ref_rev_parse_rules; *p; p++) { | |
1531 | char fullref[PATH_MAX]; | |
1532 | unsigned char sha1_from_ref[20]; | |
1533 | unsigned char *this_result; | |
1534 | int flag; | |
1535 | ||
1536 | this_result = refs_found ? sha1_from_ref : sha1; | |
1537 | mksnpath(fullref, sizeof(fullref), *p, len, str); | |
8cad4744 | 1538 | r = resolve_ref_unsafe(fullref, this_result, 1, &flag); |
ff74f7f1 JH |
1539 | if (r) { |
1540 | if (!refs_found++) | |
1541 | *ref = xstrdup(r); | |
1542 | if (!warn_ambiguous_refs) | |
1543 | break; | |
55956350 | 1544 | } else if ((flag & REF_ISSYMREF) && strcmp(fullref, "HEAD")) { |
ff74f7f1 | 1545 | warning("ignoring dangling symref %s.", fullref); |
55956350 JH |
1546 | } else if ((flag & REF_ISBROKEN) && strchr(fullref, '/')) { |
1547 | warning("ignoring broken ref %s.", fullref); | |
1548 | } | |
ff74f7f1 JH |
1549 | } |
1550 | free(last_branch); | |
1551 | return refs_found; | |
1552 | } | |
1553 | ||
1554 | int dwim_log(const char *str, int len, unsigned char *sha1, char **log) | |
1555 | { | |
1556 | char *last_branch = substitute_branch_name(&str, &len); | |
1557 | const char **p; | |
1558 | int logs_found = 0; | |
1559 | ||
1560 | *log = NULL; | |
1561 | for (p = ref_rev_parse_rules; *p; p++) { | |
1562 | struct stat st; | |
1563 | unsigned char hash[20]; | |
1564 | char path[PATH_MAX]; | |
1565 | const char *ref, *it; | |
1566 | ||
1567 | mksnpath(path, sizeof(path), *p, len, str); | |
8cad4744 | 1568 | ref = resolve_ref_unsafe(path, hash, 1, NULL); |
ff74f7f1 JH |
1569 | if (!ref) |
1570 | continue; | |
1571 | if (!stat(git_path("logs/%s", path), &st) && | |
1572 | S_ISREG(st.st_mode)) | |
1573 | it = path; | |
1574 | else if (strcmp(ref, path) && | |
1575 | !stat(git_path("logs/%s", ref), &st) && | |
1576 | S_ISREG(st.st_mode)) | |
1577 | it = ref; | |
1578 | else | |
1579 | continue; | |
1580 | if (!logs_found++) { | |
1581 | *log = xstrdup(it); | |
1582 | hashcpy(sha1, hash); | |
1583 | } | |
1584 | if (!warn_ambiguous_refs) | |
1585 | break; | |
1586 | } | |
1587 | free(last_branch); | |
1588 | return logs_found; | |
1589 | } | |
1590 | ||
dfefa935 MH |
1591 | static struct ref_lock *lock_ref_sha1_basic(const char *refname, |
1592 | const unsigned char *old_sha1, | |
1593 | int flags, int *type_p) | |
4bd18c43 | 1594 | { |
434cd0cd | 1595 | char *ref_file; |
dfefa935 | 1596 | const char *orig_refname = refname; |
4bd18c43 | 1597 | struct ref_lock *lock; |
5cc3cef9 | 1598 | int last_errno = 0; |
acd3b9ec | 1599 | int type, lflags; |
4431fcc4 | 1600 | int mustexist = (old_sha1 && !is_null_sha1(old_sha1)); |
5bdd8d4a | 1601 | int missing = 0; |
4bd18c43 SP |
1602 | |
1603 | lock = xcalloc(1, sizeof(struct ref_lock)); | |
1604 | lock->lock_fd = -1; | |
1605 | ||
8d68493f | 1606 | refname = resolve_ref_unsafe(refname, lock->old_sha1, mustexist, &type); |
dfefa935 | 1607 | if (!refname && errno == EISDIR) { |
bc7127ef JH |
1608 | /* we are trying to lock foo but we used to |
1609 | * have foo/bar which now does not exist; | |
1610 | * it is normal for the empty directory 'foo' | |
1611 | * to remain. | |
1612 | */ | |
dfefa935 | 1613 | ref_file = git_path("%s", orig_refname); |
5cc3cef9 JH |
1614 | if (remove_empty_directories(ref_file)) { |
1615 | last_errno = errno; | |
dfefa935 | 1616 | error("there are still refs under '%s'", orig_refname); |
5cc3cef9 JH |
1617 | goto error_return; |
1618 | } | |
8d68493f | 1619 | refname = resolve_ref_unsafe(orig_refname, lock->old_sha1, mustexist, &type); |
bc7127ef | 1620 | } |
68db31cc SV |
1621 | if (type_p) |
1622 | *type_p = type; | |
dfefa935 | 1623 | if (!refname) { |
5cc3cef9 | 1624 | last_errno = errno; |
818f477c | 1625 | error("unable to resolve reference %s: %s", |
dfefa935 | 1626 | orig_refname, strerror(errno)); |
5cc3cef9 | 1627 | goto error_return; |
4bd18c43 | 1628 | } |
5bdd8d4a | 1629 | missing = is_null_sha1(lock->old_sha1); |
c976d415 LH |
1630 | /* When the ref did not exist and we are creating it, |
1631 | * make sure there is no existing ref that is packed | |
1632 | * whose name begins with our refname, nor a ref whose | |
1633 | * name is a proper prefix of our refname. | |
1634 | */ | |
5bdd8d4a | 1635 | if (missing && |
316b097a | 1636 | !is_refname_available(refname, NULL, get_packed_refs(get_ref_cache(NULL)))) { |
f475e08e | 1637 | last_errno = ENOTDIR; |
c976d415 | 1638 | goto error_return; |
f475e08e | 1639 | } |
22a3844e | 1640 | |
c33d5174 | 1641 | lock->lk = xcalloc(1, sizeof(struct lock_file)); |
4bd18c43 | 1642 | |
acd3b9ec JH |
1643 | lflags = LOCK_DIE_ON_ERROR; |
1644 | if (flags & REF_NODEREF) { | |
dfefa935 | 1645 | refname = orig_refname; |
acd3b9ec JH |
1646 | lflags |= LOCK_NODEREF; |
1647 | } | |
dfefa935 MH |
1648 | lock->ref_name = xstrdup(refname); |
1649 | lock->orig_ref_name = xstrdup(orig_refname); | |
1650 | ref_file = git_path("%s", refname); | |
5bdd8d4a | 1651 | if (missing) |
68db31cc SV |
1652 | lock->force_write = 1; |
1653 | if ((flags & REF_NODEREF) && (type & REF_ISSYMREF)) | |
1654 | lock->force_write = 1; | |
4bd18c43 | 1655 | |
5cc3cef9 JH |
1656 | if (safe_create_leading_directories(ref_file)) { |
1657 | last_errno = errno; | |
1658 | error("unable to create directory for %s", ref_file); | |
1659 | goto error_return; | |
1660 | } | |
4bd18c43 | 1661 | |
acd3b9ec | 1662 | lock->lock_fd = hold_lock_file_for_update(lock->lk, ref_file, lflags); |
4bd18c43 | 1663 | return old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock; |
5cc3cef9 JH |
1664 | |
1665 | error_return: | |
1666 | unlock_ref(lock); | |
1667 | errno = last_errno; | |
1668 | return NULL; | |
4bd18c43 SP |
1669 | } |
1670 | ||
dfefa935 | 1671 | struct ref_lock *lock_ref_sha1(const char *refname, const unsigned char *old_sha1) |
95fc7512 | 1672 | { |
53cce84c | 1673 | char refpath[PATH_MAX]; |
dfefa935 | 1674 | if (check_refname_format(refname, 0)) |
4bd18c43 | 1675 | return NULL; |
dfefa935 | 1676 | strcpy(refpath, mkpath("refs/%s", refname)); |
68db31cc | 1677 | return lock_ref_sha1_basic(refpath, old_sha1, 0, NULL); |
4bd18c43 SP |
1678 | } |
1679 | ||
dfefa935 MH |
1680 | struct ref_lock *lock_any_ref_for_update(const char *refname, |
1681 | const unsigned char *old_sha1, int flags) | |
4bd18c43 | 1682 | { |
dfefa935 | 1683 | if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) |
257f3020 | 1684 | return NULL; |
dfefa935 | 1685 | return lock_ref_sha1_basic(refname, old_sha1, flags, NULL); |
c0277d15 JH |
1686 | } |
1687 | ||
d66da478 MH |
1688 | struct repack_without_ref_sb { |
1689 | const char *refname; | |
1690 | int fd; | |
1691 | }; | |
1692 | ||
1693 | static int repack_without_ref_fn(const char *refname, const unsigned char *sha1, | |
1694 | int flags, void *cb_data) | |
1695 | { | |
1696 | struct repack_without_ref_sb *data = cb_data; | |
1697 | char line[PATH_MAX + 100]; | |
1698 | int len; | |
1699 | ||
1700 | if (!strcmp(data->refname, refname)) | |
1701 | return 0; | |
1702 | len = snprintf(line, sizeof(line), "%s %s\n", | |
1703 | sha1_to_hex(sha1), refname); | |
1704 | /* this should not happen but just being defensive */ | |
1705 | if (len > sizeof(line)) | |
1706 | die("too long a refname '%s'", refname); | |
1707 | write_or_die(data->fd, line, len); | |
1708 | return 0; | |
1709 | } | |
1710 | ||
26a063a1 JH |
1711 | static struct lock_file packlock; |
1712 | ||
c0277d15 JH |
1713 | static int repack_without_ref(const char *refname) |
1714 | { | |
d66da478 | 1715 | struct repack_without_ref_sb data; |
d3177275 | 1716 | struct ref_dir *packed = get_packed_refs(get_ref_cache(NULL)); |
432ad41e | 1717 | if (find_ref(packed, refname) == NULL) |
c0277d15 | 1718 | return 0; |
d66da478 MH |
1719 | data.refname = refname; |
1720 | data.fd = hold_lock_file_for_update(&packlock, git_path("packed-refs"), 0); | |
1721 | if (data.fd < 0) { | |
1b018fd9 | 1722 | unable_to_lock_error(git_path("packed-refs"), errno); |
c0277d15 | 1723 | return error("cannot delete '%s' from packed refs", refname); |
1b018fd9 | 1724 | } |
d3177275 | 1725 | do_for_each_ref_in_dir(packed, 0, "", repack_without_ref_fn, 0, 0, &data); |
c0277d15 JH |
1726 | return commit_lock_file(&packlock); |
1727 | } | |
1728 | ||
eca35a25 | 1729 | int delete_ref(const char *refname, const unsigned char *sha1, int delopt) |
c0277d15 JH |
1730 | { |
1731 | struct ref_lock *lock; | |
eca35a25 | 1732 | int err, i = 0, ret = 0, flag = 0; |
c0277d15 | 1733 | |
68db31cc | 1734 | lock = lock_ref_sha1_basic(refname, sha1, 0, &flag); |
c0277d15 JH |
1735 | if (!lock) |
1736 | return 1; | |
045a476f | 1737 | if (!(flag & REF_ISPACKED) || flag & REF_ISSYMREF) { |
c0277d15 | 1738 | /* loose */ |
eca35a25 MV |
1739 | const char *path; |
1740 | ||
1741 | if (!(delopt & REF_NODEREF)) { | |
1742 | i = strlen(lock->lk->filename) - 5; /* .lock */ | |
1743 | lock->lk->filename[i] = 0; | |
1744 | path = lock->lk->filename; | |
1745 | } else { | |
9db56f71 | 1746 | path = git_path("%s", refname); |
eca35a25 | 1747 | } |
691f1a28 AR |
1748 | err = unlink_or_warn(path); |
1749 | if (err && errno != ENOENT) | |
c0277d15 | 1750 | ret = 1; |
691f1a28 | 1751 | |
eca35a25 MV |
1752 | if (!(delopt & REF_NODEREF)) |
1753 | lock->lk->filename[i] = '.'; | |
c0277d15 JH |
1754 | } |
1755 | /* removing the loose one could have resurrected an earlier | |
1756 | * packed one. Also, if it was not loose we need to repack | |
1757 | * without it. | |
1758 | */ | |
1759 | ret |= repack_without_ref(refname); | |
1760 | ||
691f1a28 | 1761 | unlink_or_warn(git_path("logs/%s", lock->ref_name)); |
3870a0d1 | 1762 | invalidate_ref_cache(NULL); |
c0277d15 JH |
1763 | unlock_ref(lock); |
1764 | return ret; | |
4bd18c43 SP |
1765 | } |
1766 | ||
765c2258 PH |
1767 | /* |
1768 | * People using contrib's git-new-workdir have .git/logs/refs -> | |
1769 | * /some/other/path/.git/logs/refs, and that may live on another device. | |
1770 | * | |
1771 | * IOW, to avoid cross device rename errors, the temporary renamed log must | |
1772 | * live into logs/refs. | |
1773 | */ | |
1774 | #define TMP_RENAMED_LOG "logs/refs/.tmp-renamed-log" | |
1775 | ||
dfefa935 | 1776 | int rename_ref(const char *oldrefname, const char *newrefname, const char *logmsg) |
c976d415 | 1777 | { |
c976d415 LH |
1778 | unsigned char sha1[20], orig_sha1[20]; |
1779 | int flag = 0, logmoved = 0; | |
1780 | struct ref_lock *lock; | |
c976d415 | 1781 | struct stat loginfo; |
dfefa935 | 1782 | int log = !lstat(git_path("logs/%s", oldrefname), &loginfo); |
eca35a25 | 1783 | const char *symref = NULL; |
316b097a | 1784 | struct ref_cache *refs = get_ref_cache(NULL); |
c976d415 | 1785 | |
450d4c0f | 1786 | if (log && S_ISLNK(loginfo.st_mode)) |
dfefa935 | 1787 | return error("reflog for %s is a symlink", oldrefname); |
c976d415 | 1788 | |
8d68493f | 1789 | symref = resolve_ref_unsafe(oldrefname, orig_sha1, 1, &flag); |
eca35a25 | 1790 | if (flag & REF_ISSYMREF) |
fa58186c | 1791 | return error("refname %s is a symbolic ref, renaming it is not supported", |
dfefa935 | 1792 | oldrefname); |
eca35a25 | 1793 | if (!symref) |
dfefa935 | 1794 | return error("refname %s not found", oldrefname); |
c976d415 | 1795 | |
316b097a | 1796 | if (!is_refname_available(newrefname, oldrefname, get_packed_refs(refs))) |
c976d415 LH |
1797 | return 1; |
1798 | ||
316b097a | 1799 | if (!is_refname_available(newrefname, oldrefname, get_loose_refs(refs))) |
c976d415 LH |
1800 | return 1; |
1801 | ||
dfefa935 | 1802 | if (log && rename(git_path("logs/%s", oldrefname), git_path(TMP_RENAMED_LOG))) |
765c2258 | 1803 | return error("unable to move logfile logs/%s to "TMP_RENAMED_LOG": %s", |
dfefa935 | 1804 | oldrefname, strerror(errno)); |
c976d415 | 1805 | |
dfefa935 MH |
1806 | if (delete_ref(oldrefname, orig_sha1, REF_NODEREF)) { |
1807 | error("unable to delete old %s", oldrefname); | |
c976d415 LH |
1808 | goto rollback; |
1809 | } | |
1810 | ||
dfefa935 MH |
1811 | if (!read_ref_full(newrefname, sha1, 1, &flag) && |
1812 | delete_ref(newrefname, sha1, REF_NODEREF)) { | |
c976d415 | 1813 | if (errno==EISDIR) { |
dfefa935 MH |
1814 | if (remove_empty_directories(git_path("%s", newrefname))) { |
1815 | error("Directory not empty: %s", newrefname); | |
c976d415 LH |
1816 | goto rollback; |
1817 | } | |
1818 | } else { | |
dfefa935 | 1819 | error("unable to delete existing %s", newrefname); |
c976d415 LH |
1820 | goto rollback; |
1821 | } | |
1822 | } | |
1823 | ||
dfefa935 MH |
1824 | if (log && safe_create_leading_directories(git_path("logs/%s", newrefname))) { |
1825 | error("unable to create directory for %s", newrefname); | |
c976d415 LH |
1826 | goto rollback; |
1827 | } | |
1828 | ||
1829 | retry: | |
dfefa935 | 1830 | if (log && rename(git_path(TMP_RENAMED_LOG), git_path("logs/%s", newrefname))) { |
d9e74d57 JR |
1831 | if (errno==EISDIR || errno==ENOTDIR) { |
1832 | /* | |
1833 | * rename(a, b) when b is an existing | |
1834 | * directory ought to result in ISDIR, but | |
1835 | * Solaris 5.8 gives ENOTDIR. Sheesh. | |
1836 | */ | |
dfefa935 MH |
1837 | if (remove_empty_directories(git_path("logs/%s", newrefname))) { |
1838 | error("Directory not empty: logs/%s", newrefname); | |
c976d415 LH |
1839 | goto rollback; |
1840 | } | |
1841 | goto retry; | |
1842 | } else { | |
765c2258 | 1843 | error("unable to move logfile "TMP_RENAMED_LOG" to logs/%s: %s", |
dfefa935 | 1844 | newrefname, strerror(errno)); |
c976d415 LH |
1845 | goto rollback; |
1846 | } | |
1847 | } | |
1848 | logmoved = log; | |
1849 | ||
dfefa935 | 1850 | lock = lock_ref_sha1_basic(newrefname, NULL, 0, NULL); |
c976d415 | 1851 | if (!lock) { |
dfefa935 | 1852 | error("unable to lock %s for update", newrefname); |
c976d415 LH |
1853 | goto rollback; |
1854 | } | |
c976d415 LH |
1855 | lock->force_write = 1; |
1856 | hashcpy(lock->old_sha1, orig_sha1); | |
678d0f4c | 1857 | if (write_ref_sha1(lock, orig_sha1, logmsg)) { |
dfefa935 | 1858 | error("unable to write current sha1 into %s", newrefname); |
c976d415 LH |
1859 | goto rollback; |
1860 | } | |
1861 | ||
1862 | return 0; | |
1863 | ||
1864 | rollback: | |
dfefa935 | 1865 | lock = lock_ref_sha1_basic(oldrefname, NULL, 0, NULL); |
c976d415 | 1866 | if (!lock) { |
dfefa935 | 1867 | error("unable to lock %s for rollback", oldrefname); |
c976d415 LH |
1868 | goto rollbacklog; |
1869 | } | |
1870 | ||
1871 | lock->force_write = 1; | |
1872 | flag = log_all_ref_updates; | |
1873 | log_all_ref_updates = 0; | |
1874 | if (write_ref_sha1(lock, orig_sha1, NULL)) | |
dfefa935 | 1875 | error("unable to write current sha1 into %s", oldrefname); |
c976d415 LH |
1876 | log_all_ref_updates = flag; |
1877 | ||
1878 | rollbacklog: | |
dfefa935 | 1879 | if (logmoved && rename(git_path("logs/%s", newrefname), git_path("logs/%s", oldrefname))) |
c976d415 | 1880 | error("unable to restore logfile %s from %s: %s", |
dfefa935 | 1881 | oldrefname, newrefname, strerror(errno)); |
c976d415 | 1882 | if (!logmoved && log && |
dfefa935 | 1883 | rename(git_path(TMP_RENAMED_LOG), git_path("logs/%s", oldrefname))) |
765c2258 | 1884 | error("unable to restore logfile %s from "TMP_RENAMED_LOG": %s", |
dfefa935 | 1885 | oldrefname, strerror(errno)); |
c976d415 LH |
1886 | |
1887 | return 1; | |
1888 | } | |
1889 | ||
435fc852 | 1890 | int close_ref(struct ref_lock *lock) |
b531394d BC |
1891 | { |
1892 | if (close_lock_file(lock->lk)) | |
1893 | return -1; | |
1894 | lock->lock_fd = -1; | |
1895 | return 0; | |
1896 | } | |
1897 | ||
435fc852 | 1898 | int commit_ref(struct ref_lock *lock) |
b531394d BC |
1899 | { |
1900 | if (commit_lock_file(lock->lk)) | |
1901 | return -1; | |
1902 | lock->lock_fd = -1; | |
1903 | return 0; | |
1904 | } | |
1905 | ||
e5f38ec3 | 1906 | void unlock_ref(struct ref_lock *lock) |
4bd18c43 | 1907 | { |
4ed7cd3a BC |
1908 | /* Do not free lock->lk -- atexit() still looks at them */ |
1909 | if (lock->lk) | |
1910 | rollback_lock_file(lock->lk); | |
434cd0cd | 1911 | free(lock->ref_name); |
1655707c | 1912 | free(lock->orig_ref_name); |
4bd18c43 SP |
1913 | free(lock); |
1914 | } | |
1915 | ||
0ec29a47 JH |
1916 | /* |
1917 | * copy the reflog message msg to buf, which has been allocated sufficiently | |
1918 | * large, while cleaning up the whitespaces. Especially, convert LF to space, | |
1919 | * because reflog file is one line per entry. | |
1920 | */ | |
1921 | static int copy_msg(char *buf, const char *msg) | |
1922 | { | |
1923 | char *cp = buf; | |
1924 | char c; | |
1925 | int wasspace = 1; | |
1926 | ||
1927 | *cp++ = '\t'; | |
1928 | while ((c = *msg++)) { | |
1929 | if (wasspace && isspace(c)) | |
1930 | continue; | |
1931 | wasspace = isspace(c); | |
1932 | if (wasspace) | |
1933 | c = ' '; | |
1934 | *cp++ = c; | |
1935 | } | |
1936 | while (buf < cp && isspace(cp[-1])) | |
1937 | cp--; | |
1938 | *cp++ = '\n'; | |
1939 | return cp - buf; | |
1940 | } | |
1941 | ||
dfefa935 | 1942 | int log_ref_setup(const char *refname, char *logfile, int bufsize) |
6de08ae6 | 1943 | { |
859c3017 | 1944 | int logfd, oflags = O_APPEND | O_WRONLY; |
9a13f0b7 | 1945 | |
dfefa935 | 1946 | git_snpath(logfile, bufsize, "logs/%s", refname); |
4057deb5 | 1947 | if (log_all_ref_updates && |
dfefa935 MH |
1948 | (!prefixcmp(refname, "refs/heads/") || |
1949 | !prefixcmp(refname, "refs/remotes/") || | |
1950 | !prefixcmp(refname, "refs/notes/") || | |
1951 | !strcmp(refname, "HEAD"))) { | |
157aaea5 | 1952 | if (safe_create_leading_directories(logfile) < 0) |
6de08ae6 | 1953 | return error("unable to create directory for %s", |
157aaea5 | 1954 | logfile); |
6de08ae6 SP |
1955 | oflags |= O_CREAT; |
1956 | } | |
1957 | ||
157aaea5 | 1958 | logfd = open(logfile, oflags, 0666); |
6de08ae6 | 1959 | if (logfd < 0) { |
1974bf62 | 1960 | if (!(oflags & O_CREAT) && errno == ENOENT) |
6de08ae6 | 1961 | return 0; |
3b463c3f JH |
1962 | |
1963 | if ((oflags & O_CREAT) && errno == EISDIR) { | |
157aaea5 | 1964 | if (remove_empty_directories(logfile)) { |
3b463c3f | 1965 | return error("There are still logs under '%s'", |
157aaea5 | 1966 | logfile); |
3b463c3f | 1967 | } |
157aaea5 | 1968 | logfd = open(logfile, oflags, 0666); |
3b463c3f JH |
1969 | } |
1970 | ||
1971 | if (logfd < 0) | |
1972 | return error("Unable to append to %s: %s", | |
157aaea5 | 1973 | logfile, strerror(errno)); |
6de08ae6 SP |
1974 | } |
1975 | ||
157aaea5 | 1976 | adjust_shared_perm(logfile); |
859c3017 EM |
1977 | close(logfd); |
1978 | return 0; | |
1979 | } | |
443b92b6 | 1980 | |
dfefa935 | 1981 | static int log_ref_write(const char *refname, const unsigned char *old_sha1, |
859c3017 EM |
1982 | const unsigned char *new_sha1, const char *msg) |
1983 | { | |
1984 | int logfd, result, written, oflags = O_APPEND | O_WRONLY; | |
1985 | unsigned maxlen, len; | |
1986 | int msglen; | |
157aaea5 | 1987 | char log_file[PATH_MAX]; |
859c3017 EM |
1988 | char *logrec; |
1989 | const char *committer; | |
1990 | ||
1991 | if (log_all_ref_updates < 0) | |
1992 | log_all_ref_updates = !is_bare_repository(); | |
1993 | ||
dfefa935 | 1994 | result = log_ref_setup(refname, log_file, sizeof(log_file)); |
859c3017 EM |
1995 | if (result) |
1996 | return result; | |
1997 | ||
1998 | logfd = open(log_file, oflags); | |
1999 | if (logfd < 0) | |
2000 | return 0; | |
0ec29a47 | 2001 | msglen = msg ? strlen(msg) : 0; |
774751a8 | 2002 | committer = git_committer_info(0); |
8ac65937 JH |
2003 | maxlen = strlen(committer) + msglen + 100; |
2004 | logrec = xmalloc(maxlen); | |
2005 | len = sprintf(logrec, "%s %s %s\n", | |
9a13f0b7 NP |
2006 | sha1_to_hex(old_sha1), |
2007 | sha1_to_hex(new_sha1), | |
8ac65937 JH |
2008 | committer); |
2009 | if (msglen) | |
0ec29a47 | 2010 | len += copy_msg(logrec + len - 1, msg) - 1; |
93822c22 | 2011 | written = len <= maxlen ? write_in_full(logfd, logrec, len) : -1; |
6de08ae6 | 2012 | free(logrec); |
91c8d590 | 2013 | if (close(logfd) != 0 || written != len) |
9a13f0b7 | 2014 | return error("Unable to append to %s", log_file); |
6de08ae6 SP |
2015 | return 0; |
2016 | } | |
2017 | ||
c3b0dec5 LT |
2018 | static int is_branch(const char *refname) |
2019 | { | |
2020 | return !strcmp(refname, "HEAD") || !prefixcmp(refname, "refs/heads/"); | |
2021 | } | |
2022 | ||
4bd18c43 SP |
2023 | int write_ref_sha1(struct ref_lock *lock, |
2024 | const unsigned char *sha1, const char *logmsg) | |
2025 | { | |
2026 | static char term = '\n'; | |
c3b0dec5 | 2027 | struct object *o; |
4bd18c43 SP |
2028 | |
2029 | if (!lock) | |
95fc7512 | 2030 | return -1; |
a89fccd2 | 2031 | if (!lock->force_write && !hashcmp(lock->old_sha1, sha1)) { |
4bd18c43 SP |
2032 | unlock_ref(lock); |
2033 | return 0; | |
95fc7512 | 2034 | } |
c3b0dec5 LT |
2035 | o = parse_object(sha1); |
2036 | if (!o) { | |
7be8b3ba | 2037 | error("Trying to write ref %s with nonexistent object %s", |
c3b0dec5 LT |
2038 | lock->ref_name, sha1_to_hex(sha1)); |
2039 | unlock_ref(lock); | |
2040 | return -1; | |
2041 | } | |
2042 | if (o->type != OBJ_COMMIT && is_branch(lock->ref_name)) { | |
2043 | error("Trying to write non-commit object %s to branch %s", | |
2044 | sha1_to_hex(sha1), lock->ref_name); | |
2045 | unlock_ref(lock); | |
2046 | return -1; | |
2047 | } | |
93822c22 AW |
2048 | if (write_in_full(lock->lock_fd, sha1_to_hex(sha1), 40) != 40 || |
2049 | write_in_full(lock->lock_fd, &term, 1) != 1 | |
b531394d | 2050 | || close_ref(lock) < 0) { |
c33d5174 | 2051 | error("Couldn't write %s", lock->lk->filename); |
4bd18c43 SP |
2052 | unlock_ref(lock); |
2053 | return -1; | |
2054 | } | |
8bf90dc9 | 2055 | clear_loose_ref_cache(get_ref_cache(NULL)); |
bd104db1 NP |
2056 | if (log_ref_write(lock->ref_name, lock->old_sha1, sha1, logmsg) < 0 || |
2057 | (strcmp(lock->ref_name, lock->orig_ref_name) && | |
2058 | log_ref_write(lock->orig_ref_name, lock->old_sha1, sha1, logmsg) < 0)) { | |
6de08ae6 SP |
2059 | unlock_ref(lock); |
2060 | return -1; | |
2061 | } | |
605fac8b NP |
2062 | if (strcmp(lock->orig_ref_name, "HEAD") != 0) { |
2063 | /* | |
2064 | * Special hack: If a branch is updated directly and HEAD | |
2065 | * points to it (may happen on the remote side of a push | |
2066 | * for example) then logically the HEAD reflog should be | |
2067 | * updated too. | |
2068 | * A generic solution implies reverse symref information, | |
2069 | * but finding all symrefs pointing to the given branch | |
2070 | * would be rather costly for this rare event (the direct | |
2071 | * update of a branch) to be worth it. So let's cheat and | |
2072 | * check with HEAD only which should cover 99% of all usage | |
2073 | * scenarios (even 100% of the default ones). | |
2074 | */ | |
2075 | unsigned char head_sha1[20]; | |
2076 | int head_flag; | |
2077 | const char *head_ref; | |
8cad4744 | 2078 | head_ref = resolve_ref_unsafe("HEAD", head_sha1, 1, &head_flag); |
605fac8b NP |
2079 | if (head_ref && (head_flag & REF_ISSYMREF) && |
2080 | !strcmp(head_ref, lock->ref_name)) | |
2081 | log_ref_write("HEAD", lock->old_sha1, sha1, logmsg); | |
2082 | } | |
b531394d | 2083 | if (commit_ref(lock)) { |
434cd0cd | 2084 | error("Couldn't set %s", lock->ref_name); |
4bd18c43 SP |
2085 | unlock_ref(lock); |
2086 | return -1; | |
2087 | } | |
4bd18c43 SP |
2088 | unlock_ref(lock); |
2089 | return 0; | |
95fc7512 | 2090 | } |
d556fae2 | 2091 | |
8b5157e4 NP |
2092 | int create_symref(const char *ref_target, const char *refs_heads_master, |
2093 | const char *logmsg) | |
41b625b0 NP |
2094 | { |
2095 | const char *lockpath; | |
2096 | char ref[1000]; | |
2097 | int fd, len, written; | |
a4f34cbb | 2098 | char *git_HEAD = git_pathdup("%s", ref_target); |
8b5157e4 NP |
2099 | unsigned char old_sha1[20], new_sha1[20]; |
2100 | ||
2101 | if (logmsg && read_ref(ref_target, old_sha1)) | |
2102 | hashclr(old_sha1); | |
41b625b0 | 2103 | |
d48744d1 JH |
2104 | if (safe_create_leading_directories(git_HEAD) < 0) |
2105 | return error("unable to create directory for %s", git_HEAD); | |
2106 | ||
41b625b0 NP |
2107 | #ifndef NO_SYMLINK_HEAD |
2108 | if (prefer_symlink_refs) { | |
2109 | unlink(git_HEAD); | |
2110 | if (!symlink(refs_heads_master, git_HEAD)) | |
8b5157e4 | 2111 | goto done; |
41b625b0 NP |
2112 | fprintf(stderr, "no symlink - falling back to symbolic ref\n"); |
2113 | } | |
2114 | #endif | |
2115 | ||
2116 | len = snprintf(ref, sizeof(ref), "ref: %s\n", refs_heads_master); | |
2117 | if (sizeof(ref) <= len) { | |
2118 | error("refname too long: %s", refs_heads_master); | |
47fc52e2 | 2119 | goto error_free_return; |
41b625b0 NP |
2120 | } |
2121 | lockpath = mkpath("%s.lock", git_HEAD); | |
2122 | fd = open(lockpath, O_CREAT | O_EXCL | O_WRONLY, 0666); | |
2123 | if (fd < 0) { | |
2124 | error("Unable to open %s for writing", lockpath); | |
47fc52e2 | 2125 | goto error_free_return; |
41b625b0 NP |
2126 | } |
2127 | written = write_in_full(fd, ref, len); | |
91c8d590 | 2128 | if (close(fd) != 0 || written != len) { |
41b625b0 | 2129 | error("Unable to write to %s", lockpath); |
47fc52e2 | 2130 | goto error_unlink_return; |
41b625b0 NP |
2131 | } |
2132 | if (rename(lockpath, git_HEAD) < 0) { | |
41b625b0 | 2133 | error("Unable to create %s", git_HEAD); |
47fc52e2 | 2134 | goto error_unlink_return; |
41b625b0 NP |
2135 | } |
2136 | if (adjust_shared_perm(git_HEAD)) { | |
41b625b0 | 2137 | error("Unable to fix permissions on %s", lockpath); |
47fc52e2 | 2138 | error_unlink_return: |
691f1a28 | 2139 | unlink_or_warn(lockpath); |
47fc52e2 JH |
2140 | error_free_return: |
2141 | free(git_HEAD); | |
2142 | return -1; | |
41b625b0 | 2143 | } |
8b5157e4 | 2144 | |
ee96d11b | 2145 | #ifndef NO_SYMLINK_HEAD |
8b5157e4 | 2146 | done: |
ee96d11b | 2147 | #endif |
8b5157e4 NP |
2148 | if (logmsg && !read_ref(refs_heads_master, new_sha1)) |
2149 | log_ref_write(ref_target, old_sha1, new_sha1, logmsg); | |
2150 | ||
47fc52e2 | 2151 | free(git_HEAD); |
41b625b0 NP |
2152 | return 0; |
2153 | } | |
2154 | ||
16d7cc90 JH |
2155 | static char *ref_msg(const char *line, const char *endp) |
2156 | { | |
2157 | const char *ep; | |
16d7cc90 | 2158 | line += 82; |
182af834 PH |
2159 | ep = memchr(line, '\n', endp - line); |
2160 | if (!ep) | |
2161 | ep = endp; | |
2162 | return xmemdupz(line, ep - line); | |
16d7cc90 JH |
2163 | } |
2164 | ||
dfefa935 MH |
2165 | int read_ref_at(const char *refname, unsigned long at_time, int cnt, |
2166 | unsigned char *sha1, char **msg, | |
2167 | unsigned long *cutoff_time, int *cutoff_tz, int *cutoff_cnt) | |
d556fae2 | 2168 | { |
e5229042 | 2169 | const char *logfile, *logdata, *logend, *rec, *lastgt, *lastrec; |
d556fae2 | 2170 | char *tz_c; |
e29cb53a | 2171 | int logfd, tz, reccnt = 0; |
d556fae2 SP |
2172 | struct stat st; |
2173 | unsigned long date; | |
e5229042 | 2174 | unsigned char logged_sha1[20]; |
cb48cb58 | 2175 | void *log_mapped; |
dc49cd76 | 2176 | size_t mapsz; |
d556fae2 | 2177 | |
dfefa935 | 2178 | logfile = git_path("logs/%s", refname); |
d556fae2 SP |
2179 | logfd = open(logfile, O_RDONLY, 0); |
2180 | if (logfd < 0) | |
d824cbba | 2181 | die_errno("Unable to read log '%s'", logfile); |
d556fae2 SP |
2182 | fstat(logfd, &st); |
2183 | if (!st.st_size) | |
2184 | die("Log %s is empty.", logfile); | |
dc49cd76 SP |
2185 | mapsz = xsize_t(st.st_size); |
2186 | log_mapped = xmmap(NULL, mapsz, PROT_READ, MAP_PRIVATE, logfd, 0); | |
cb48cb58 | 2187 | logdata = log_mapped; |
d556fae2 SP |
2188 | close(logfd); |
2189 | ||
e5229042 | 2190 | lastrec = NULL; |
d556fae2 SP |
2191 | rec = logend = logdata + st.st_size; |
2192 | while (logdata < rec) { | |
e29cb53a | 2193 | reccnt++; |
d556fae2 SP |
2194 | if (logdata < rec && *(rec-1) == '\n') |
2195 | rec--; | |
e5229042 SP |
2196 | lastgt = NULL; |
2197 | while (logdata < rec && *(rec-1) != '\n') { | |
d556fae2 | 2198 | rec--; |
e5229042 SP |
2199 | if (*rec == '>') |
2200 | lastgt = rec; | |
2201 | } | |
2202 | if (!lastgt) | |
d556fae2 | 2203 | die("Log %s is corrupt.", logfile); |
e5229042 | 2204 | date = strtoul(lastgt + 1, &tz_c, 10); |
ab2a1a32 | 2205 | if (date <= at_time || cnt == 0) { |
76a44c5c | 2206 | tz = strtoul(tz_c, NULL, 10); |
16d7cc90 JH |
2207 | if (msg) |
2208 | *msg = ref_msg(rec, logend); | |
2209 | if (cutoff_time) | |
2210 | *cutoff_time = date; | |
2211 | if (cutoff_tz) | |
2212 | *cutoff_tz = tz; | |
2213 | if (cutoff_cnt) | |
76a44c5c | 2214 | *cutoff_cnt = reccnt - 1; |
e5229042 SP |
2215 | if (lastrec) { |
2216 | if (get_sha1_hex(lastrec, logged_sha1)) | |
2217 | die("Log %s is corrupt.", logfile); | |
2218 | if (get_sha1_hex(rec + 41, sha1)) | |
2219 | die("Log %s is corrupt.", logfile); | |
a89fccd2 | 2220 | if (hashcmp(logged_sha1, sha1)) { |
edbc25c5 | 2221 | warning("Log %s has gap after %s.", |
73013afd | 2222 | logfile, show_date(date, tz, DATE_RFC2822)); |
e5229042 | 2223 | } |
e5f38ec3 JH |
2224 | } |
2225 | else if (date == at_time) { | |
e5229042 SP |
2226 | if (get_sha1_hex(rec + 41, sha1)) |
2227 | die("Log %s is corrupt.", logfile); | |
e5f38ec3 JH |
2228 | } |
2229 | else { | |
e5229042 SP |
2230 | if (get_sha1_hex(rec + 41, logged_sha1)) |
2231 | die("Log %s is corrupt.", logfile); | |
a89fccd2 | 2232 | if (hashcmp(logged_sha1, sha1)) { |
edbc25c5 | 2233 | warning("Log %s unexpectedly ended on %s.", |
73013afd | 2234 | logfile, show_date(date, tz, DATE_RFC2822)); |
e5229042 SP |
2235 | } |
2236 | } | |
dc49cd76 | 2237 | munmap(log_mapped, mapsz); |
d556fae2 SP |
2238 | return 0; |
2239 | } | |
e5229042 | 2240 | lastrec = rec; |
ab2a1a32 JH |
2241 | if (cnt > 0) |
2242 | cnt--; | |
d556fae2 SP |
2243 | } |
2244 | ||
e5229042 SP |
2245 | rec = logdata; |
2246 | while (rec < logend && *rec != '>' && *rec != '\n') | |
2247 | rec++; | |
2248 | if (rec == logend || *rec == '\n') | |
d556fae2 | 2249 | die("Log %s is corrupt.", logfile); |
e5229042 | 2250 | date = strtoul(rec + 1, &tz_c, 10); |
d556fae2 SP |
2251 | tz = strtoul(tz_c, NULL, 10); |
2252 | if (get_sha1_hex(logdata, sha1)) | |
2253 | die("Log %s is corrupt.", logfile); | |
d1a4489a JK |
2254 | if (is_null_sha1(sha1)) { |
2255 | if (get_sha1_hex(logdata + 41, sha1)) | |
2256 | die("Log %s is corrupt.", logfile); | |
2257 | } | |
16d7cc90 JH |
2258 | if (msg) |
2259 | *msg = ref_msg(logdata, logend); | |
dc49cd76 | 2260 | munmap(log_mapped, mapsz); |
16d7cc90 JH |
2261 | |
2262 | if (cutoff_time) | |
2263 | *cutoff_time = date; | |
2264 | if (cutoff_tz) | |
2265 | *cutoff_tz = tz; | |
2266 | if (cutoff_cnt) | |
2267 | *cutoff_cnt = reccnt; | |
2268 | return 1; | |
d556fae2 | 2269 | } |
2ff81662 | 2270 | |
dfefa935 | 2271 | int for_each_recent_reflog_ent(const char *refname, each_reflog_ent_fn fn, long ofs, void *cb_data) |
2ff81662 JH |
2272 | { |
2273 | const char *logfile; | |
2274 | FILE *logfp; | |
8ca78803 | 2275 | struct strbuf sb = STRBUF_INIT; |
2266bf27 | 2276 | int ret = 0; |
2ff81662 | 2277 | |
dfefa935 | 2278 | logfile = git_path("logs/%s", refname); |
2ff81662 JH |
2279 | logfp = fopen(logfile, "r"); |
2280 | if (!logfp) | |
883d60fa | 2281 | return -1; |
101d15e0 JH |
2282 | |
2283 | if (ofs) { | |
2284 | struct stat statbuf; | |
2285 | if (fstat(fileno(logfp), &statbuf) || | |
2286 | statbuf.st_size < ofs || | |
2287 | fseek(logfp, -ofs, SEEK_END) || | |
8ca78803 | 2288 | strbuf_getwholeline(&sb, logfp, '\n')) { |
9d33f7c2 | 2289 | fclose(logfp); |
8ca78803 | 2290 | strbuf_release(&sb); |
101d15e0 | 2291 | return -1; |
9d33f7c2 | 2292 | } |
101d15e0 JH |
2293 | } |
2294 | ||
8ca78803 | 2295 | while (!strbuf_getwholeline(&sb, logfp, '\n')) { |
2ff81662 | 2296 | unsigned char osha1[20], nsha1[20]; |
883d60fa JS |
2297 | char *email_end, *message; |
2298 | unsigned long timestamp; | |
8ca78803 | 2299 | int tz; |
2ff81662 JH |
2300 | |
2301 | /* old SP new SP name <email> SP time TAB msg LF */ | |
8ca78803 RS |
2302 | if (sb.len < 83 || sb.buf[sb.len - 1] != '\n' || |
2303 | get_sha1_hex(sb.buf, osha1) || sb.buf[40] != ' ' || | |
2304 | get_sha1_hex(sb.buf + 41, nsha1) || sb.buf[81] != ' ' || | |
2305 | !(email_end = strchr(sb.buf + 82, '>')) || | |
883d60fa JS |
2306 | email_end[1] != ' ' || |
2307 | !(timestamp = strtoul(email_end + 2, &message, 10)) || | |
2308 | !message || message[0] != ' ' || | |
2309 | (message[1] != '+' && message[1] != '-') || | |
2310 | !isdigit(message[2]) || !isdigit(message[3]) || | |
b4dd4856 | 2311 | !isdigit(message[4]) || !isdigit(message[5])) |
2ff81662 | 2312 | continue; /* corrupt? */ |
883d60fa JS |
2313 | email_end[1] = '\0'; |
2314 | tz = strtol(message + 1, NULL, 10); | |
b4dd4856 JS |
2315 | if (message[6] != '\t') |
2316 | message += 6; | |
2317 | else | |
2318 | message += 7; | |
8ca78803 RS |
2319 | ret = fn(osha1, nsha1, sb.buf + 82, timestamp, tz, message, |
2320 | cb_data); | |
883d60fa | 2321 | if (ret) |
2266bf27 | 2322 | break; |
2ff81662 JH |
2323 | } |
2324 | fclose(logfp); | |
8ca78803 | 2325 | strbuf_release(&sb); |
2266bf27 | 2326 | return ret; |
2ff81662 | 2327 | } |
e29cb53a | 2328 | |
dfefa935 | 2329 | int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn, void *cb_data) |
101d15e0 | 2330 | { |
dfefa935 | 2331 | return for_each_recent_reflog_ent(refname, fn, 0, cb_data); |
101d15e0 JH |
2332 | } |
2333 | ||
989c0e5d MH |
2334 | /* |
2335 | * Call fn for each reflog in the namespace indicated by name. name | |
2336 | * must be empty or end with '/'. Name will be used as a scratch | |
2337 | * space, but its contents will be restored before return. | |
2338 | */ | |
2339 | static int do_for_each_reflog(struct strbuf *name, each_ref_fn fn, void *cb_data) | |
eb8381c8 | 2340 | { |
989c0e5d | 2341 | DIR *d = opendir(git_path("logs/%s", name->buf)); |
fcee5a14 | 2342 | int retval = 0; |
93c603fc | 2343 | struct dirent *de; |
989c0e5d | 2344 | int oldlen = name->len; |
eb8381c8 | 2345 | |
93c603fc | 2346 | if (!d) |
989c0e5d | 2347 | return name->len ? errno : 0; |
eb8381c8 | 2348 | |
93c603fc MH |
2349 | while ((de = readdir(d)) != NULL) { |
2350 | struct stat st; | |
eb8381c8 | 2351 | |
93c603fc MH |
2352 | if (de->d_name[0] == '.') |
2353 | continue; | |
93c603fc MH |
2354 | if (has_extension(de->d_name, ".lock")) |
2355 | continue; | |
989c0e5d MH |
2356 | strbuf_addstr(name, de->d_name); |
2357 | if (stat(git_path("logs/%s", name->buf), &st) < 0) { | |
2358 | ; /* silently ignore */ | |
93c603fc | 2359 | } else { |
eb8381c8 | 2360 | if (S_ISDIR(st.st_mode)) { |
989c0e5d MH |
2361 | strbuf_addch(name, '/'); |
2362 | retval = do_for_each_reflog(name, fn, cb_data); | |
eb8381c8 NP |
2363 | } else { |
2364 | unsigned char sha1[20]; | |
989c0e5d MH |
2365 | if (read_ref_full(name->buf, sha1, 0, NULL)) |
2366 | retval = error("bad ref for %s", name->buf); | |
eb8381c8 | 2367 | else |
989c0e5d | 2368 | retval = fn(name->buf, sha1, 0, cb_data); |
eb8381c8 NP |
2369 | } |
2370 | if (retval) | |
2371 | break; | |
2372 | } | |
989c0e5d | 2373 | strbuf_setlen(name, oldlen); |
eb8381c8 | 2374 | } |
93c603fc | 2375 | closedir(d); |
eb8381c8 NP |
2376 | return retval; |
2377 | } | |
2378 | ||
2379 | int for_each_reflog(each_ref_fn fn, void *cb_data) | |
2380 | { | |
989c0e5d MH |
2381 | int retval; |
2382 | struct strbuf name; | |
2383 | strbuf_init(&name, PATH_MAX); | |
2384 | retval = do_for_each_reflog(&name, fn, cb_data); | |
2385 | strbuf_release(&name); | |
2386 | return retval; | |
eb8381c8 | 2387 | } |
3d9f037c CR |
2388 | |
2389 | int update_ref(const char *action, const char *refname, | |
2390 | const unsigned char *sha1, const unsigned char *oldval, | |
2391 | int flags, enum action_on_err onerr) | |
2392 | { | |
2393 | static struct ref_lock *lock; | |
2394 | lock = lock_any_ref_for_update(refname, oldval, flags); | |
2395 | if (!lock) { | |
2396 | const char *str = "Cannot lock the ref '%s'."; | |
2397 | switch (onerr) { | |
2398 | case MSG_ON_ERR: error(str, refname); break; | |
2399 | case DIE_ON_ERR: die(str, refname); break; | |
2400 | case QUIET_ON_ERR: break; | |
2401 | } | |
2402 | return 1; | |
2403 | } | |
2404 | if (write_ref_sha1(lock, sha1, action) < 0) { | |
2405 | const char *str = "Cannot update the ref '%s'."; | |
2406 | switch (onerr) { | |
2407 | case MSG_ON_ERR: error(str, refname); break; | |
2408 | case DIE_ON_ERR: die(str, refname); break; | |
2409 | case QUIET_ON_ERR: break; | |
2410 | } | |
2411 | return 1; | |
2412 | } | |
2413 | return 0; | |
2414 | } | |
cda69f48 | 2415 | |
5483f799 | 2416 | struct ref *find_ref_by_name(const struct ref *list, const char *name) |
cda69f48 JK |
2417 | { |
2418 | for ( ; list; list = list->next) | |
2419 | if (!strcmp(list->name, name)) | |
5483f799 | 2420 | return (struct ref *)list; |
cda69f48 JK |
2421 | return NULL; |
2422 | } | |
7c2b3029 JK |
2423 | |
2424 | /* | |
2425 | * generate a format suitable for scanf from a ref_rev_parse_rules | |
2426 | * rule, that is replace the "%.*s" spec with a "%s" spec | |
2427 | */ | |
2428 | static void gen_scanf_fmt(char *scanf_fmt, const char *rule) | |
2429 | { | |
2430 | char *spec; | |
2431 | ||
2432 | spec = strstr(rule, "%.*s"); | |
2433 | if (!spec || strstr(spec + 4, "%.*s")) | |
2434 | die("invalid rule in ref_rev_parse_rules: %s", rule); | |
2435 | ||
2436 | /* copy all until spec */ | |
2437 | strncpy(scanf_fmt, rule, spec - rule); | |
2438 | scanf_fmt[spec - rule] = '\0'; | |
2439 | /* copy new spec */ | |
2440 | strcat(scanf_fmt, "%s"); | |
2441 | /* copy remaining rule */ | |
2442 | strcat(scanf_fmt, spec + 4); | |
2443 | ||
2444 | return; | |
2445 | } | |
2446 | ||
dfefa935 | 2447 | char *shorten_unambiguous_ref(const char *refname, int strict) |
7c2b3029 JK |
2448 | { |
2449 | int i; | |
2450 | static char **scanf_fmts; | |
2451 | static int nr_rules; | |
2452 | char *short_name; | |
2453 | ||
2454 | /* pre generate scanf formats from ref_rev_parse_rules[] */ | |
2455 | if (!nr_rules) { | |
2456 | size_t total_len = 0; | |
2457 | ||
2458 | /* the rule list is NULL terminated, count them first */ | |
2459 | for (; ref_rev_parse_rules[nr_rules]; nr_rules++) | |
2460 | /* no +1 because strlen("%s") < strlen("%.*s") */ | |
2461 | total_len += strlen(ref_rev_parse_rules[nr_rules]); | |
2462 | ||
2463 | scanf_fmts = xmalloc(nr_rules * sizeof(char *) + total_len); | |
2464 | ||
2465 | total_len = 0; | |
2466 | for (i = 0; i < nr_rules; i++) { | |
2467 | scanf_fmts[i] = (char *)&scanf_fmts[nr_rules] | |
2468 | + total_len; | |
2469 | gen_scanf_fmt(scanf_fmts[i], ref_rev_parse_rules[i]); | |
2470 | total_len += strlen(ref_rev_parse_rules[i]); | |
2471 | } | |
2472 | } | |
2473 | ||
2474 | /* bail out if there are no rules */ | |
2475 | if (!nr_rules) | |
dfefa935 | 2476 | return xstrdup(refname); |
7c2b3029 | 2477 | |
dfefa935 MH |
2478 | /* buffer for scanf result, at most refname must fit */ |
2479 | short_name = xstrdup(refname); | |
7c2b3029 JK |
2480 | |
2481 | /* skip first rule, it will always match */ | |
2482 | for (i = nr_rules - 1; i > 0 ; --i) { | |
2483 | int j; | |
6e7b3309 | 2484 | int rules_to_fail = i; |
7c2b3029 JK |
2485 | int short_name_len; |
2486 | ||
dfefa935 | 2487 | if (1 != sscanf(refname, scanf_fmts[i], short_name)) |
7c2b3029 JK |
2488 | continue; |
2489 | ||
2490 | short_name_len = strlen(short_name); | |
2491 | ||
6e7b3309 BW |
2492 | /* |
2493 | * in strict mode, all (except the matched one) rules | |
2494 | * must fail to resolve to a valid non-ambiguous ref | |
2495 | */ | |
2496 | if (strict) | |
2497 | rules_to_fail = nr_rules; | |
2498 | ||
7c2b3029 JK |
2499 | /* |
2500 | * check if the short name resolves to a valid ref, | |
2501 | * but use only rules prior to the matched one | |
2502 | */ | |
6e7b3309 | 2503 | for (j = 0; j < rules_to_fail; j++) { |
7c2b3029 | 2504 | const char *rule = ref_rev_parse_rules[j]; |
7c2b3029 JK |
2505 | char refname[PATH_MAX]; |
2506 | ||
6e7b3309 BW |
2507 | /* skip matched rule */ |
2508 | if (i == j) | |
2509 | continue; | |
2510 | ||
7c2b3029 JK |
2511 | /* |
2512 | * the short name is ambiguous, if it resolves | |
2513 | * (with this previous rule) to a valid ref | |
2514 | * read_ref() returns 0 on success | |
2515 | */ | |
2516 | mksnpath(refname, sizeof(refname), | |
2517 | rule, short_name_len, short_name); | |
c6893323 | 2518 | if (ref_exists(refname)) |
7c2b3029 JK |
2519 | break; |
2520 | } | |
2521 | ||
2522 | /* | |
2523 | * short name is non-ambiguous if all previous rules | |
2524 | * haven't resolved to a valid ref | |
2525 | */ | |
6e7b3309 | 2526 | if (j == rules_to_fail) |
7c2b3029 JK |
2527 | return short_name; |
2528 | } | |
2529 | ||
2530 | free(short_name); | |
dfefa935 | 2531 | return xstrdup(refname); |
7c2b3029 | 2532 | } |