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