]>
Commit | Line | Data |
---|---|---|
95fc7512 | 1 | #include "cache.h" |
697cc8ef | 2 | #include "lockfile.h" |
85023577 | 3 | #include "refs.h" |
cf0adba7 JH |
4 | #include "object.h" |
5 | #include "tag.h" | |
7155b727 | 6 | #include "dir.h" |
daebaa78 | 7 | #include "string-list.h" |
95fc7512 | 8 | |
3581d793 SB |
9 | struct ref_lock { |
10 | char *ref_name; | |
11 | char *orig_ref_name; | |
12 | struct lock_file *lk; | |
13 | unsigned char old_sha1[20]; | |
14 | int lock_fd; | |
15 | int force_write; | |
16 | }; | |
17 | ||
bc5fd6d3 | 18 | /* |
dde8a902 DT |
19 | * How to handle various characters in refnames: |
20 | * 0: An acceptable character for refs | |
5e650228 JH |
21 | * 1: End-of-component |
22 | * 2: ., look for a preceding . to reject .. in refs | |
23 | * 3: {, look for a preceding @ to reject @{ in refs | |
24 | * 4: A bad character: ASCII control characters, "~", "^", ":" or SP | |
dde8a902 DT |
25 | */ |
26 | static unsigned char refname_disposition[256] = { | |
5e650228 JH |
27 | 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, |
28 | 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, | |
29 | 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 2, 1, | |
30 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 4, | |
dde8a902 | 31 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
5e650228 JH |
32 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 4, 0, |
33 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
34 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 4, 4 | |
dde8a902 DT |
35 | }; |
36 | ||
581d4e0c MH |
37 | /* |
38 | * Flag passed to lock_ref_sha1_basic() telling it to tolerate broken | |
39 | * refs (i.e., because the reference is about to be deleted anyway). | |
40 | */ | |
41 | #define REF_DELETING 0x02 | |
42 | ||
029cdb4a RS |
43 | /* |
44 | * Used as a flag to ref_transaction_delete when a loose ref is being | |
45 | * pruned. | |
46 | */ | |
47 | #define REF_ISPRUNING 0x0100 | |
dde8a902 DT |
48 | /* |
49 | * Try to read one refname component from the front of refname. | |
50 | * Return the length of the component found, or -1 if the component is | |
51 | * not legal. It is legal if it is something reasonable to have under | |
52 | * ".git/refs/"; We do not like it if: | |
bc5fd6d3 MH |
53 | * |
54 | * - any path component of it begins with ".", or | |
55 | * - it has double dots "..", or | |
56 | * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or | |
5e650228 JH |
57 | * - it ends with a "/". |
58 | * - it ends with ".lock" | |
bc5fd6d3 MH |
59 | * - it contains a "\" (backslash) |
60 | */ | |
bc5fd6d3 MH |
61 | static int check_refname_component(const char *refname, int flags) |
62 | { | |
63 | const char *cp; | |
64 | char last = '\0'; | |
65 | ||
66 | for (cp = refname; ; cp++) { | |
dde8a902 DT |
67 | int ch = *cp & 255; |
68 | unsigned char disp = refname_disposition[ch]; | |
69 | switch (disp) { | |
5e650228 | 70 | case 1: |
dde8a902 | 71 | goto out; |
5e650228 | 72 | case 2: |
dde8a902 DT |
73 | if (last == '.') |
74 | return -1; /* Refname contains "..". */ | |
75 | break; | |
5e650228 | 76 | case 3: |
dde8a902 DT |
77 | if (last == '@') |
78 | return -1; /* Refname contains "@{". */ | |
bc5fd6d3 | 79 | break; |
5e650228 | 80 | case 4: |
dde8a902 DT |
81 | return -1; |
82 | } | |
bc5fd6d3 MH |
83 | last = ch; |
84 | } | |
dde8a902 | 85 | out: |
bc5fd6d3 | 86 | if (cp == refname) |
dac529e4 | 87 | return 0; /* Component has zero length. */ |
f3cc52d8 JN |
88 | if (refname[0] == '.') |
89 | return -1; /* Component starts with '.'. */ | |
7108ad23 MH |
90 | if (cp - refname >= LOCK_SUFFIX_LEN && |
91 | !memcmp(cp - LOCK_SUFFIX_LEN, LOCK_SUFFIX, LOCK_SUFFIX_LEN)) | |
bc5fd6d3 MH |
92 | return -1; /* Refname ends with ".lock". */ |
93 | return cp - refname; | |
94 | } | |
95 | ||
5e650228 | 96 | int check_refname_format(const char *refname, int flags) |
bc5fd6d3 MH |
97 | { |
98 | int component_len, component_count = 0; | |
99 | ||
9ba89f48 FC |
100 | if (!strcmp(refname, "@")) |
101 | /* Refname is a single character '@'. */ | |
102 | return -1; | |
103 | ||
bc5fd6d3 MH |
104 | while (1) { |
105 | /* We are at the start of a path component. */ | |
106 | component_len = check_refname_component(refname, flags); | |
dac529e4 | 107 | if (component_len <= 0) { |
bc5fd6d3 MH |
108 | if ((flags & REFNAME_REFSPEC_PATTERN) && |
109 | refname[0] == '*' && | |
110 | (refname[1] == '\0' || refname[1] == '/')) { | |
111 | /* Accept one wildcard as a full refname component. */ | |
112 | flags &= ~REFNAME_REFSPEC_PATTERN; | |
113 | component_len = 1; | |
114 | } else { | |
115 | return -1; | |
116 | } | |
117 | } | |
118 | component_count++; | |
119 | if (refname[component_len] == '\0') | |
120 | break; | |
121 | /* Skip to next component. */ | |
122 | refname += component_len + 1; | |
123 | } | |
124 | ||
125 | if (refname[component_len - 1] == '.') | |
126 | return -1; /* Refname ends with '.'. */ | |
127 | if (!(flags & REFNAME_ALLOW_ONELEVEL) && component_count < 2) | |
128 | return -1; /* Refname has only one component. */ | |
129 | return 0; | |
130 | } | |
131 | ||
132 | struct ref_entry; | |
e1e22e37 | 133 | |
28e6a34e MH |
134 | /* |
135 | * Information used (along with the information in ref_entry) to | |
136 | * describe a single cached reference. This data structure only | |
137 | * occurs embedded in a union in struct ref_entry, and only when | |
138 | * (ref_entry->flag & REF_DIR) is zero. | |
139 | */ | |
593f1bb8 | 140 | struct ref_value { |
6c6f58df MH |
141 | /* |
142 | * The name of the object to which this reference resolves | |
143 | * (which may be a tag object). If REF_ISBROKEN, this is | |
144 | * null. If REF_ISSYMREF, then this is the name of the object | |
145 | * referred to by the last reference in the symlink chain. | |
146 | */ | |
593f1bb8 | 147 | unsigned char sha1[20]; |
6c6f58df MH |
148 | |
149 | /* | |
150 | * If REF_KNOWS_PEELED, then this field holds the peeled value | |
151 | * of this reference, or null if the reference is known not to | |
2312a793 MH |
152 | * be peelable. See the documentation for peel_ref() for an |
153 | * exact definition of "peelable". | |
6c6f58df | 154 | */ |
593f1bb8 MH |
155 | unsigned char peeled[20]; |
156 | }; | |
157 | ||
f006c42a MH |
158 | struct ref_cache; |
159 | ||
28e6a34e MH |
160 | /* |
161 | * Information used (along with the information in ref_entry) to | |
162 | * describe a level in the hierarchy of references. This data | |
163 | * structure only occurs embedded in a union in struct ref_entry, and | |
164 | * only when (ref_entry.flag & REF_DIR) is set. In that case, | |
165 | * (ref_entry.flag & REF_INCOMPLETE) determines whether the references | |
166 | * in the directory have already been read: | |
167 | * | |
168 | * (ref_entry.flag & REF_INCOMPLETE) unset -- a directory of loose | |
169 | * or packed references, already read. | |
170 | * | |
171 | * (ref_entry.flag & REF_INCOMPLETE) set -- a directory of loose | |
172 | * references that hasn't been read yet (nor has any of its | |
173 | * subdirectories). | |
174 | * | |
175 | * Entries within a directory are stored within a growable array of | |
176 | * pointers to ref_entries (entries, nr, alloc). Entries 0 <= i < | |
177 | * sorted are sorted by their component name in strcmp() order and the | |
178 | * remaining entries are unsorted. | |
179 | * | |
180 | * Loose references are read lazily, one directory at a time. When a | |
181 | * directory of loose references is read, then all of the references | |
182 | * in that directory are stored, and REF_INCOMPLETE stubs are created | |
183 | * for any subdirectories, but the subdirectories themselves are not | |
184 | * read. The reading is triggered by get_ref_dir(). | |
185 | */ | |
d3177275 | 186 | struct ref_dir { |
e9c4c111 | 187 | int nr, alloc; |
e6ed3ca6 MH |
188 | |
189 | /* | |
190 | * Entries with index 0 <= i < sorted are sorted by name. New | |
191 | * entries are appended to the list unsorted, and are sorted | |
192 | * only when required; thus we avoid the need to sort the list | |
193 | * after the addition of every reference. | |
194 | */ | |
195 | int sorted; | |
196 | ||
f006c42a MH |
197 | /* A pointer to the ref_cache that contains this ref_dir. */ |
198 | struct ref_cache *ref_cache; | |
199 | ||
d3177275 | 200 | struct ref_entry **entries; |
e9c4c111 JP |
201 | }; |
202 | ||
89df9c84 MH |
203 | /* |
204 | * Bit values for ref_entry::flag. REF_ISSYMREF=0x01, | |
d0f810f0 RS |
205 | * REF_ISPACKED=0x02, REF_ISBROKEN=0x04 and REF_BAD_NAME=0x08 are |
206 | * public values; see refs.h. | |
89df9c84 MH |
207 | */ |
208 | ||
209 | /* | |
210 | * The field ref_entry->u.value.peeled of this value entry contains | |
211 | * the correct peeled value for the reference, which might be | |
212 | * null_sha1 if the reference is not a tag or if it is broken. | |
213 | */ | |
d0f810f0 | 214 | #define REF_KNOWS_PEELED 0x10 |
28e6a34e MH |
215 | |
216 | /* ref_entry represents a directory of references */ | |
d0f810f0 | 217 | #define REF_DIR 0x20 |
cf0adba7 | 218 | |
28e6a34e MH |
219 | /* |
220 | * Entry has not yet been read from disk (used only for REF_DIR | |
221 | * entries representing loose references) | |
222 | */ | |
d0f810f0 | 223 | #define REF_INCOMPLETE 0x40 |
28e6a34e | 224 | |
432ad41e MH |
225 | /* |
226 | * A ref_entry represents either a reference or a "subdirectory" of | |
28e6a34e MH |
227 | * references. |
228 | * | |
229 | * Each directory in the reference namespace is represented by a | |
230 | * ref_entry with (flags & REF_DIR) set and containing a subdir member | |
231 | * that holds the entries in that directory that have been read so | |
232 | * far. If (flags & REF_INCOMPLETE) is set, then the directory and | |
233 | * its subdirectories haven't been read yet. REF_INCOMPLETE is only | |
234 | * used for loose reference directories. | |
235 | * | |
236 | * References are represented by a ref_entry with (flags & REF_DIR) | |
237 | * unset and a value member that describes the reference's value. The | |
238 | * flag member is at the ref_entry level, but it is also needed to | |
239 | * interpret the contents of the value field (in other words, a | |
240 | * ref_value object is not very much use without the enclosing | |
241 | * ref_entry). | |
432ad41e MH |
242 | * |
243 | * Reference names cannot end with slash and directories' names are | |
244 | * always stored with a trailing slash (except for the top-level | |
245 | * directory, which is always denoted by ""). This has two nice | |
246 | * consequences: (1) when the entries in each subdir are sorted | |
247 | * lexicographically by name (as they usually are), the references in | |
248 | * a whole tree can be generated in lexicographic order by traversing | |
249 | * the tree in left-to-right, depth-first order; (2) the names of | |
250 | * references and subdirectories cannot conflict, and therefore the | |
251 | * presence of an empty subdirectory does not block the creation of a | |
252 | * similarly-named reference. (The fact that reference names with the | |
253 | * same leading components can conflict *with each other* is a | |
254 | * separate issue that is regulated by is_refname_available().) | |
255 | * | |
256 | * Please note that the name field contains the fully-qualified | |
257 | * reference (or subdirectory) name. Space could be saved by only | |
258 | * storing the relative names. But that would require the full names | |
259 | * to be generated on the fly when iterating in do_for_each_ref(), and | |
260 | * would break callback functions, who have always been able to assume | |
261 | * that the name strings that they are passed will not be freed during | |
262 | * the iteration. | |
263 | */ | |
bc5fd6d3 MH |
264 | struct ref_entry { |
265 | unsigned char flag; /* ISSYMREF? ISPACKED? */ | |
593f1bb8 | 266 | union { |
432ad41e MH |
267 | struct ref_value value; /* if not (flags&REF_DIR) */ |
268 | struct ref_dir subdir; /* if (flags&REF_DIR) */ | |
593f1bb8 | 269 | } u; |
432ad41e MH |
270 | /* |
271 | * The full name of the reference (e.g., "refs/heads/master") | |
272 | * or the full name of the directory with a trailing slash | |
273 | * (e.g., "refs/heads/"): | |
274 | */ | |
bc5fd6d3 MH |
275 | char name[FLEX_ARRAY]; |
276 | }; | |
e1e22e37 | 277 | |
28e6a34e MH |
278 | static void read_loose_refs(const char *dirname, struct ref_dir *dir); |
279 | ||
d7826d54 MH |
280 | static struct ref_dir *get_ref_dir(struct ref_entry *entry) |
281 | { | |
28e6a34e | 282 | struct ref_dir *dir; |
d7826d54 | 283 | assert(entry->flag & REF_DIR); |
28e6a34e MH |
284 | dir = &entry->u.subdir; |
285 | if (entry->flag & REF_INCOMPLETE) { | |
286 | read_loose_refs(entry->name, dir); | |
287 | entry->flag &= ~REF_INCOMPLETE; | |
288 | } | |
289 | return dir; | |
d7826d54 MH |
290 | } |
291 | ||
d0f810f0 RS |
292 | /* |
293 | * Check if a refname is safe. | |
294 | * For refs that start with "refs/" we consider it safe as long they do | |
295 | * not try to resolve to outside of refs/. | |
296 | * | |
297 | * For all other refs we only consider them safe iff they only contain | |
298 | * upper case characters and '_' (like "HEAD" AND "MERGE_HEAD", and not like | |
299 | * "config"). | |
300 | */ | |
301 | static int refname_is_safe(const char *refname) | |
302 | { | |
303 | if (starts_with(refname, "refs/")) { | |
304 | char *buf; | |
305 | int result; | |
306 | ||
307 | buf = xmalloc(strlen(refname) + 1); | |
308 | /* | |
309 | * Does the refname try to escape refs/? | |
310 | * For example: refs/foo/../bar is safe but refs/foo/../../bar | |
311 | * is not. | |
312 | */ | |
313 | result = !normalize_path_copy(buf, refname + strlen("refs/")); | |
314 | free(buf); | |
315 | return result; | |
316 | } | |
317 | while (*refname) { | |
318 | if (!isupper(*refname) && *refname != '_') | |
319 | return 0; | |
320 | refname++; | |
321 | } | |
322 | return 1; | |
323 | } | |
324 | ||
cddc4258 MH |
325 | static struct ref_entry *create_ref_entry(const char *refname, |
326 | const unsigned char *sha1, int flag, | |
327 | int check_name) | |
e1e22e37 LT |
328 | { |
329 | int len; | |
cddc4258 | 330 | struct ref_entry *ref; |
e1e22e37 | 331 | |
09116a1c | 332 | if (check_name && |
f3cc52d8 | 333 | check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) |
dfefa935 | 334 | die("Reference has invalid format: '%s'", refname); |
d0f810f0 RS |
335 | if (!check_name && !refname_is_safe(refname)) |
336 | die("Reference has invalid name: '%s'", refname); | |
cddc4258 MH |
337 | len = strlen(refname) + 1; |
338 | ref = xmalloc(sizeof(struct ref_entry) + len); | |
593f1bb8 MH |
339 | hashcpy(ref->u.value.sha1, sha1); |
340 | hashclr(ref->u.value.peeled); | |
cddc4258 MH |
341 | memcpy(ref->name, refname, len); |
342 | ref->flag = flag; | |
343 | return ref; | |
344 | } | |
345 | ||
432ad41e MH |
346 | static void clear_ref_dir(struct ref_dir *dir); |
347 | ||
732134ed MH |
348 | static void free_ref_entry(struct ref_entry *entry) |
349 | { | |
27b5587c MH |
350 | if (entry->flag & REF_DIR) { |
351 | /* | |
352 | * Do not use get_ref_dir() here, as that might | |
353 | * trigger the reading of loose refs. | |
354 | */ | |
355 | clear_ref_dir(&entry->u.subdir); | |
356 | } | |
732134ed MH |
357 | free(entry); |
358 | } | |
359 | ||
432ad41e MH |
360 | /* |
361 | * Add a ref_entry to the end of dir (unsorted). Entry is always | |
362 | * stored directly in dir; no recursion into subdirectories is | |
363 | * done. | |
364 | */ | |
365 | static void add_entry_to_dir(struct ref_dir *dir, struct ref_entry *entry) | |
cddc4258 | 366 | { |
432ad41e MH |
367 | ALLOC_GROW(dir->entries, dir->nr + 1, dir->alloc); |
368 | dir->entries[dir->nr++] = entry; | |
654ad400 MH |
369 | /* optimize for the case that entries are added in order */ |
370 | if (dir->nr == 1 || | |
371 | (dir->nr == dir->sorted + 1 && | |
372 | strcmp(dir->entries[dir->nr - 2]->name, | |
373 | dir->entries[dir->nr - 1]->name) < 0)) | |
374 | dir->sorted = dir->nr; | |
c774aab9 JP |
375 | } |
376 | ||
432ad41e MH |
377 | /* |
378 | * Clear and free all entries in dir, recursively. | |
379 | */ | |
d3177275 | 380 | static void clear_ref_dir(struct ref_dir *dir) |
bc5fd6d3 MH |
381 | { |
382 | int i; | |
d3177275 MH |
383 | for (i = 0; i < dir->nr; i++) |
384 | free_ref_entry(dir->entries[i]); | |
385 | free(dir->entries); | |
386 | dir->sorted = dir->nr = dir->alloc = 0; | |
387 | dir->entries = NULL; | |
bc5fd6d3 MH |
388 | } |
389 | ||
432ad41e MH |
390 | /* |
391 | * Create a struct ref_entry object for the specified dirname. | |
392 | * dirname is the name of the directory with a trailing slash (e.g., | |
393 | * "refs/heads/") or "" for the top-level directory. | |
394 | */ | |
f006c42a | 395 | static struct ref_entry *create_dir_entry(struct ref_cache *ref_cache, |
b9146f51 RS |
396 | const char *dirname, size_t len, |
397 | int incomplete) | |
432ad41e MH |
398 | { |
399 | struct ref_entry *direntry; | |
432ad41e | 400 | direntry = xcalloc(1, sizeof(struct ref_entry) + len + 1); |
b9146f51 RS |
401 | memcpy(direntry->name, dirname, len); |
402 | direntry->name[len] = '\0'; | |
f006c42a | 403 | direntry->u.subdir.ref_cache = ref_cache; |
28e6a34e | 404 | direntry->flag = REF_DIR | (incomplete ? REF_INCOMPLETE : 0); |
432ad41e MH |
405 | return direntry; |
406 | } | |
407 | ||
e9c4c111 | 408 | static int ref_entry_cmp(const void *a, const void *b) |
c774aab9 | 409 | { |
e9c4c111 JP |
410 | struct ref_entry *one = *(struct ref_entry **)a; |
411 | struct ref_entry *two = *(struct ref_entry **)b; | |
412 | return strcmp(one->name, two->name); | |
413 | } | |
c774aab9 | 414 | |
d3177275 | 415 | static void sort_ref_dir(struct ref_dir *dir); |
bc5fd6d3 | 416 | |
e1980c9d JH |
417 | struct string_slice { |
418 | size_t len; | |
419 | const char *str; | |
420 | }; | |
421 | ||
422 | static int ref_entry_cmp_sslice(const void *key_, const void *ent_) | |
423 | { | |
c971ddfd RS |
424 | const struct string_slice *key = key_; |
425 | const struct ref_entry *ent = *(const struct ref_entry * const *)ent_; | |
426 | int cmp = strncmp(key->str, ent->name, key->len); | |
e1980c9d JH |
427 | if (cmp) |
428 | return cmp; | |
c971ddfd | 429 | return '\0' - (unsigned char)ent->name[key->len]; |
e1980c9d JH |
430 | } |
431 | ||
432ad41e | 432 | /* |
9fc0a648 MH |
433 | * Return the index of the entry with the given refname from the |
434 | * ref_dir (non-recursively), sorting dir if necessary. Return -1 if | |
435 | * no such entry is found. dir must already be complete. | |
432ad41e | 436 | */ |
9fc0a648 | 437 | static int search_ref_dir(struct ref_dir *dir, const char *refname, size_t len) |
bc5fd6d3 | 438 | { |
e1980c9d JH |
439 | struct ref_entry **r; |
440 | struct string_slice key; | |
bc5fd6d3 | 441 | |
432ad41e | 442 | if (refname == NULL || !dir->nr) |
9fc0a648 | 443 | return -1; |
bc5fd6d3 | 444 | |
d3177275 | 445 | sort_ref_dir(dir); |
e1980c9d JH |
446 | key.len = len; |
447 | key.str = refname; | |
448 | r = bsearch(&key, dir->entries, dir->nr, sizeof(*dir->entries), | |
449 | ref_entry_cmp_sslice); | |
bc5fd6d3 MH |
450 | |
451 | if (r == NULL) | |
9fc0a648 | 452 | return -1; |
bc5fd6d3 | 453 | |
9fc0a648 | 454 | return r - dir->entries; |
bc5fd6d3 MH |
455 | } |
456 | ||
f348ac92 MH |
457 | /* |
458 | * Search for a directory entry directly within dir (without | |
459 | * recursing). Sort dir if necessary. subdirname must be a directory | |
460 | * name (i.e., end in '/'). If mkdir is set, then create the | |
461 | * directory if it is missing; otherwise, return NULL if the desired | |
28e6a34e | 462 | * directory cannot be found. dir must already be complete. |
f348ac92 | 463 | */ |
3f3aa1bc | 464 | static struct ref_dir *search_for_subdir(struct ref_dir *dir, |
dd02e728 RS |
465 | const char *subdirname, size_t len, |
466 | int mkdir) | |
f348ac92 | 467 | { |
9fc0a648 MH |
468 | int entry_index = search_ref_dir(dir, subdirname, len); |
469 | struct ref_entry *entry; | |
470 | if (entry_index == -1) { | |
f348ac92 MH |
471 | if (!mkdir) |
472 | return NULL; | |
28e6a34e MH |
473 | /* |
474 | * Since dir is complete, the absence of a subdir | |
475 | * means that the subdir really doesn't exist; | |
476 | * therefore, create an empty record for it but mark | |
477 | * the record complete. | |
478 | */ | |
b9146f51 | 479 | entry = create_dir_entry(dir->ref_cache, subdirname, len, 0); |
f348ac92 | 480 | add_entry_to_dir(dir, entry); |
9fc0a648 MH |
481 | } else { |
482 | entry = dir->entries[entry_index]; | |
f348ac92 | 483 | } |
3f3aa1bc | 484 | return get_ref_dir(entry); |
f348ac92 MH |
485 | } |
486 | ||
432ad41e MH |
487 | /* |
488 | * If refname is a reference name, find the ref_dir within the dir | |
489 | * tree that should hold refname. If refname is a directory name | |
490 | * (i.e., ends in '/'), then return that ref_dir itself. dir must | |
28e6a34e MH |
491 | * represent the top-level directory and must already be complete. |
492 | * Sort ref_dirs and recurse into subdirectories as necessary. If | |
493 | * mkdir is set, then create any missing directories; otherwise, | |
494 | * return NULL if the desired directory cannot be found. | |
432ad41e MH |
495 | */ |
496 | static struct ref_dir *find_containing_dir(struct ref_dir *dir, | |
497 | const char *refname, int mkdir) | |
498 | { | |
5fa04418 | 499 | const char *slash; |
5fa04418 | 500 | for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) { |
dd02e728 | 501 | size_t dirnamelen = slash - refname + 1; |
3f3aa1bc | 502 | struct ref_dir *subdir; |
dd02e728 | 503 | subdir = search_for_subdir(dir, refname, dirnamelen, mkdir); |
663c1295 JH |
504 | if (!subdir) { |
505 | dir = NULL; | |
f348ac92 | 506 | break; |
432ad41e | 507 | } |
3f3aa1bc | 508 | dir = subdir; |
432ad41e MH |
509 | } |
510 | ||
432ad41e MH |
511 | return dir; |
512 | } | |
513 | ||
514 | /* | |
515 | * Find the value entry with the given name in dir, sorting ref_dirs | |
516 | * and recursing into subdirectories as necessary. If the name is not | |
517 | * found or it corresponds to a directory entry, return NULL. | |
518 | */ | |
519 | static struct ref_entry *find_ref(struct ref_dir *dir, const char *refname) | |
520 | { | |
9fc0a648 | 521 | int entry_index; |
432ad41e MH |
522 | struct ref_entry *entry; |
523 | dir = find_containing_dir(dir, refname, 0); | |
524 | if (!dir) | |
525 | return NULL; | |
9fc0a648 MH |
526 | entry_index = search_ref_dir(dir, refname, strlen(refname)); |
527 | if (entry_index == -1) | |
528 | return NULL; | |
529 | entry = dir->entries[entry_index]; | |
530 | return (entry->flag & REF_DIR) ? NULL : entry; | |
432ad41e MH |
531 | } |
532 | ||
506a760d MH |
533 | /* |
534 | * Remove the entry with the given name from dir, recursing into | |
535 | * subdirectories as necessary. If refname is the name of a directory | |
536 | * (i.e., ends with '/'), then remove the directory and its contents. | |
537 | * If the removal was successful, return the number of entries | |
538 | * remaining in the directory entry that contained the deleted entry. | |
539 | * If the name was not found, return -1. Please note that this | |
540 | * function only deletes the entry from the cache; it does not delete | |
541 | * it from the filesystem or ensure that other cache entries (which | |
542 | * might be symbolic references to the removed entry) are updated. | |
543 | * Nor does it remove any containing dir entries that might be made | |
544 | * empty by the removal. dir must represent the top-level directory | |
545 | * and must already be complete. | |
546 | */ | |
547 | static int remove_entry(struct ref_dir *dir, const char *refname) | |
548 | { | |
549 | int refname_len = strlen(refname); | |
550 | int entry_index; | |
551 | struct ref_entry *entry; | |
552 | int is_dir = refname[refname_len - 1] == '/'; | |
553 | if (is_dir) { | |
554 | /* | |
555 | * refname represents a reference directory. Remove | |
556 | * the trailing slash; otherwise we will get the | |
557 | * directory *representing* refname rather than the | |
558 | * one *containing* it. | |
559 | */ | |
560 | char *dirname = xmemdupz(refname, refname_len - 1); | |
561 | dir = find_containing_dir(dir, dirname, 0); | |
562 | free(dirname); | |
563 | } else { | |
564 | dir = find_containing_dir(dir, refname, 0); | |
565 | } | |
566 | if (!dir) | |
567 | return -1; | |
568 | entry_index = search_ref_dir(dir, refname, refname_len); | |
569 | if (entry_index == -1) | |
570 | return -1; | |
571 | entry = dir->entries[entry_index]; | |
572 | ||
573 | memmove(&dir->entries[entry_index], | |
574 | &dir->entries[entry_index + 1], | |
575 | (dir->nr - entry_index - 1) * sizeof(*dir->entries) | |
576 | ); | |
577 | dir->nr--; | |
578 | if (dir->sorted > entry_index) | |
579 | dir->sorted--; | |
580 | free_ref_entry(entry); | |
581 | return dir->nr; | |
432ad41e MH |
582 | } |
583 | ||
584 | /* | |
585 | * Add a ref_entry to the ref_dir (unsorted), recursing into | |
586 | * subdirectories as necessary. dir must represent the top-level | |
587 | * directory. Return 0 on success. | |
588 | */ | |
589 | static int add_ref(struct ref_dir *dir, struct ref_entry *ref) | |
590 | { | |
591 | dir = find_containing_dir(dir, ref->name, 1); | |
592 | if (!dir) | |
593 | return -1; | |
594 | add_entry_to_dir(dir, ref); | |
595 | return 0; | |
596 | } | |
597 | ||
202a56a9 MH |
598 | /* |
599 | * Emit a warning and return true iff ref1 and ref2 have the same name | |
600 | * and the same sha1. Die if they have the same name but different | |
601 | * sha1s. | |
602 | */ | |
603 | static int is_dup_ref(const struct ref_entry *ref1, const struct ref_entry *ref2) | |
604 | { | |
432ad41e | 605 | if (strcmp(ref1->name, ref2->name)) |
202a56a9 | 606 | return 0; |
432ad41e MH |
607 | |
608 | /* Duplicate name; make sure that they don't conflict: */ | |
609 | ||
610 | if ((ref1->flag & REF_DIR) || (ref2->flag & REF_DIR)) | |
611 | /* This is impossible by construction */ | |
612 | die("Reference directory conflict: %s", ref1->name); | |
613 | ||
614 | if (hashcmp(ref1->u.value.sha1, ref2->u.value.sha1)) | |
615 | die("Duplicated ref, and SHA1s don't match: %s", ref1->name); | |
616 | ||
617 | warning("Duplicated ref: %s", ref1->name); | |
618 | return 1; | |
202a56a9 MH |
619 | } |
620 | ||
e6ed3ca6 | 621 | /* |
432ad41e MH |
622 | * Sort the entries in dir non-recursively (if they are not already |
623 | * sorted) and remove any duplicate entries. | |
e6ed3ca6 | 624 | */ |
d3177275 | 625 | static void sort_ref_dir(struct ref_dir *dir) |
e9c4c111 | 626 | { |
202a56a9 | 627 | int i, j; |
81a79d8e | 628 | struct ref_entry *last = NULL; |
c774aab9 | 629 | |
e6ed3ca6 MH |
630 | /* |
631 | * This check also prevents passing a zero-length array to qsort(), | |
632 | * which is a problem on some platforms. | |
633 | */ | |
d3177275 | 634 | if (dir->sorted == dir->nr) |
e9c4c111 | 635 | return; |
c774aab9 | 636 | |
d3177275 | 637 | qsort(dir->entries, dir->nr, sizeof(*dir->entries), ref_entry_cmp); |
c774aab9 | 638 | |
81a79d8e MH |
639 | /* Remove any duplicates: */ |
640 | for (i = 0, j = 0; j < dir->nr; j++) { | |
641 | struct ref_entry *entry = dir->entries[j]; | |
642 | if (last && is_dup_ref(last, entry)) | |
643 | free_ref_entry(entry); | |
644 | else | |
645 | last = dir->entries[i++] = entry; | |
e9c4c111 | 646 | } |
81a79d8e | 647 | dir->sorted = dir->nr = i; |
e9c4c111 | 648 | } |
c774aab9 | 649 | |
fcce1703 MH |
650 | /* Include broken references in a do_for_each_ref*() iteration: */ |
651 | #define DO_FOR_EACH_INCLUDE_BROKEN 0x01 | |
c774aab9 | 652 | |
662428f4 MH |
653 | /* |
654 | * Return true iff the reference described by entry can be resolved to | |
655 | * an object in the database. Emit a warning if the referred-to | |
656 | * object does not exist. | |
657 | */ | |
658 | static int ref_resolves_to_object(struct ref_entry *entry) | |
659 | { | |
660 | if (entry->flag & REF_ISBROKEN) | |
661 | return 0; | |
662 | if (!has_sha1_file(entry->u.value.sha1)) { | |
663 | error("%s does not point to a valid object!", entry->name); | |
664 | return 0; | |
665 | } | |
666 | return 1; | |
667 | } | |
c774aab9 | 668 | |
7d76fdc8 MH |
669 | /* |
670 | * current_ref is a performance hack: when iterating over references | |
671 | * using the for_each_ref*() functions, current_ref is set to the | |
672 | * current reference's entry before calling the callback function. If | |
673 | * the callback function calls peel_ref(), then peel_ref() first | |
674 | * checks whether the reference to be peeled is the current reference | |
675 | * (it usually is) and if so, returns that reference's peeled version | |
676 | * if it is available. This avoids a refname lookup in a common case. | |
677 | */ | |
bc5fd6d3 | 678 | static struct ref_entry *current_ref; |
c774aab9 | 679 | |
624cac35 MH |
680 | typedef int each_ref_entry_fn(struct ref_entry *entry, void *cb_data); |
681 | ||
682 | struct ref_entry_cb { | |
683 | const char *base; | |
684 | int trim; | |
685 | int flags; | |
686 | each_ref_fn *fn; | |
687 | void *cb_data; | |
688 | }; | |
689 | ||
fcce1703 | 690 | /* |
624cac35 MH |
691 | * Handle one reference in a do_for_each_ref*()-style iteration, |
692 | * calling an each_ref_fn for each entry. | |
fcce1703 | 693 | */ |
624cac35 | 694 | static int do_one_ref(struct ref_entry *entry, void *cb_data) |
bc5fd6d3 | 695 | { |
624cac35 | 696 | struct ref_entry_cb *data = cb_data; |
d0cf51e9 | 697 | struct ref_entry *old_current_ref; |
429213e4 | 698 | int retval; |
d0cf51e9 | 699 | |
59556548 | 700 | if (!starts_with(entry->name, data->base)) |
bc5fd6d3 | 701 | return 0; |
c774aab9 | 702 | |
624cac35 | 703 | if (!(data->flags & DO_FOR_EACH_INCLUDE_BROKEN) && |
662428f4 | 704 | !ref_resolves_to_object(entry)) |
bc5fd6d3 | 705 | return 0; |
c774aab9 | 706 | |
d0cf51e9 MH |
707 | /* Store the old value, in case this is a recursive call: */ |
708 | old_current_ref = current_ref; | |
bc5fd6d3 | 709 | current_ref = entry; |
624cac35 MH |
710 | retval = data->fn(entry->name + data->trim, entry->u.value.sha1, |
711 | entry->flag, data->cb_data); | |
d0cf51e9 | 712 | current_ref = old_current_ref; |
429213e4 | 713 | return retval; |
bc5fd6d3 | 714 | } |
c774aab9 | 715 | |
c36b5bc2 | 716 | /* |
d3177275 | 717 | * Call fn for each reference in dir that has index in the range |
432ad41e MH |
718 | * offset <= index < dir->nr. Recurse into subdirectories that are in |
719 | * that index range, sorting them before iterating. This function | |
624cac35 MH |
720 | * does not sort dir itself; it should be sorted beforehand. fn is |
721 | * called for all references, including broken ones. | |
c36b5bc2 | 722 | */ |
624cac35 MH |
723 | static int do_for_each_entry_in_dir(struct ref_dir *dir, int offset, |
724 | each_ref_entry_fn fn, void *cb_data) | |
c36b5bc2 MH |
725 | { |
726 | int i; | |
d3177275 MH |
727 | assert(dir->sorted == dir->nr); |
728 | for (i = offset; i < dir->nr; i++) { | |
432ad41e MH |
729 | struct ref_entry *entry = dir->entries[i]; |
730 | int retval; | |
731 | if (entry->flag & REF_DIR) { | |
d7826d54 MH |
732 | struct ref_dir *subdir = get_ref_dir(entry); |
733 | sort_ref_dir(subdir); | |
624cac35 | 734 | retval = do_for_each_entry_in_dir(subdir, 0, fn, cb_data); |
432ad41e | 735 | } else { |
624cac35 | 736 | retval = fn(entry, cb_data); |
432ad41e | 737 | } |
c36b5bc2 MH |
738 | if (retval) |
739 | return retval; | |
740 | } | |
741 | return 0; | |
742 | } | |
743 | ||
b3fd060f | 744 | /* |
d3177275 | 745 | * Call fn for each reference in the union of dir1 and dir2, in order |
432ad41e MH |
746 | * by refname. Recurse into subdirectories. If a value entry appears |
747 | * in both dir1 and dir2, then only process the version that is in | |
748 | * dir2. The input dirs must already be sorted, but subdirs will be | |
624cac35 MH |
749 | * sorted as needed. fn is called for all references, including |
750 | * broken ones. | |
b3fd060f | 751 | */ |
624cac35 MH |
752 | static int do_for_each_entry_in_dirs(struct ref_dir *dir1, |
753 | struct ref_dir *dir2, | |
754 | each_ref_entry_fn fn, void *cb_data) | |
b3fd060f MH |
755 | { |
756 | int retval; | |
757 | int i1 = 0, i2 = 0; | |
758 | ||
d3177275 MH |
759 | assert(dir1->sorted == dir1->nr); |
760 | assert(dir2->sorted == dir2->nr); | |
432ad41e MH |
761 | while (1) { |
762 | struct ref_entry *e1, *e2; | |
763 | int cmp; | |
764 | if (i1 == dir1->nr) { | |
624cac35 | 765 | return do_for_each_entry_in_dir(dir2, i2, fn, cb_data); |
432ad41e MH |
766 | } |
767 | if (i2 == dir2->nr) { | |
624cac35 | 768 | return do_for_each_entry_in_dir(dir1, i1, fn, cb_data); |
432ad41e MH |
769 | } |
770 | e1 = dir1->entries[i1]; | |
771 | e2 = dir2->entries[i2]; | |
772 | cmp = strcmp(e1->name, e2->name); | |
773 | if (cmp == 0) { | |
774 | if ((e1->flag & REF_DIR) && (e2->flag & REF_DIR)) { | |
775 | /* Both are directories; descend them in parallel. */ | |
d7826d54 MH |
776 | struct ref_dir *subdir1 = get_ref_dir(e1); |
777 | struct ref_dir *subdir2 = get_ref_dir(e2); | |
778 | sort_ref_dir(subdir1); | |
779 | sort_ref_dir(subdir2); | |
624cac35 MH |
780 | retval = do_for_each_entry_in_dirs( |
781 | subdir1, subdir2, fn, cb_data); | |
432ad41e MH |
782 | i1++; |
783 | i2++; | |
784 | } else if (!(e1->flag & REF_DIR) && !(e2->flag & REF_DIR)) { | |
785 | /* Both are references; ignore the one from dir1. */ | |
624cac35 | 786 | retval = fn(e2, cb_data); |
432ad41e MH |
787 | i1++; |
788 | i2++; | |
789 | } else { | |
790 | die("conflict between reference and directory: %s", | |
791 | e1->name); | |
792 | } | |
b3fd060f | 793 | } else { |
432ad41e MH |
794 | struct ref_entry *e; |
795 | if (cmp < 0) { | |
796 | e = e1; | |
b3fd060f | 797 | i1++; |
432ad41e MH |
798 | } else { |
799 | e = e2; | |
800 | i2++; | |
801 | } | |
802 | if (e->flag & REF_DIR) { | |
d7826d54 MH |
803 | struct ref_dir *subdir = get_ref_dir(e); |
804 | sort_ref_dir(subdir); | |
624cac35 MH |
805 | retval = do_for_each_entry_in_dir( |
806 | subdir, 0, fn, cb_data); | |
432ad41e | 807 | } else { |
624cac35 | 808 | retval = fn(e, cb_data); |
b3fd060f MH |
809 | } |
810 | } | |
811 | if (retval) | |
812 | return retval; | |
813 | } | |
b3fd060f MH |
814 | } |
815 | ||
98eeb09e JK |
816 | /* |
817 | * Load all of the refs from the dir into our in-memory cache. The hard work | |
818 | * of loading loose refs is done by get_ref_dir(), so we just need to recurse | |
819 | * through all of the sub-directories. We do not even need to care about | |
820 | * sorting, as traversal order does not matter to us. | |
821 | */ | |
822 | static void prime_ref_dir(struct ref_dir *dir) | |
823 | { | |
824 | int i; | |
825 | for (i = 0; i < dir->nr; i++) { | |
826 | struct ref_entry *entry = dir->entries[i]; | |
827 | if (entry->flag & REF_DIR) | |
828 | prime_ref_dir(get_ref_dir(entry)); | |
829 | } | |
830 | } | |
cbe73331 | 831 | |
5fe7d825 | 832 | static int entry_matches(struct ref_entry *entry, const struct string_list *list) |
d66da478 | 833 | { |
5fe7d825 | 834 | return list && string_list_has_string(list, entry->name); |
5a4d4947 MH |
835 | } |
836 | ||
cbe73331 | 837 | struct nonmatching_ref_data { |
5fe7d825 | 838 | const struct string_list *skip; |
cbe73331 | 839 | struct ref_entry *found; |
5a4d4947 MH |
840 | }; |
841 | ||
cbe73331 | 842 | static int nonmatching_ref_fn(struct ref_entry *entry, void *vdata) |
5a4d4947 | 843 | { |
cbe73331 JK |
844 | struct nonmatching_ref_data *data = vdata; |
845 | ||
846 | if (entry_matches(entry, data->skip)) | |
5a4d4947 | 847 | return 0; |
cbe73331 JK |
848 | |
849 | data->found = entry; | |
850 | return 1; | |
851 | } | |
852 | ||
853 | static void report_refname_conflict(struct ref_entry *entry, | |
854 | const char *refname) | |
855 | { | |
856 | error("'%s' exists; cannot create '%s'", entry->name, refname); | |
d66da478 MH |
857 | } |
858 | ||
bc5fd6d3 MH |
859 | /* |
860 | * Return true iff a reference named refname could be created without | |
624cac35 | 861 | * conflicting with the name of an existing reference in dir. If |
5fe7d825 RS |
862 | * skip is non-NULL, ignore potential conflicts with refs in skip |
863 | * (e.g., because they are scheduled for deletion in the same | |
bc5fd6d3 | 864 | * operation). |
cbe73331 JK |
865 | * |
866 | * Two reference names conflict if one of them exactly matches the | |
867 | * leading components of the other; e.g., "foo/bar" conflicts with | |
868 | * both "foo" and with "foo/bar/baz" but not with "foo/bar" or | |
869 | * "foo/barbados". | |
5fe7d825 RS |
870 | * |
871 | * skip must be sorted. | |
bc5fd6d3 | 872 | */ |
5fe7d825 RS |
873 | static int is_refname_available(const char *refname, |
874 | const struct string_list *skip, | |
d3177275 | 875 | struct ref_dir *dir) |
bc5fd6d3 | 876 | { |
cbe73331 JK |
877 | const char *slash; |
878 | size_t len; | |
879 | int pos; | |
880 | char *dirname; | |
5a4d4947 | 881 | |
cbe73331 JK |
882 | for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) { |
883 | /* | |
884 | * We are still at a leading dir of the refname; we are | |
885 | * looking for a conflict with a leaf entry. | |
886 | * | |
887 | * If we find one, we still must make sure it is | |
5fe7d825 | 888 | * not in "skip". |
cbe73331 JK |
889 | */ |
890 | pos = search_ref_dir(dir, refname, slash - refname); | |
891 | if (pos >= 0) { | |
892 | struct ref_entry *entry = dir->entries[pos]; | |
5fe7d825 | 893 | if (entry_matches(entry, skip)) |
cbe73331 JK |
894 | return 1; |
895 | report_refname_conflict(entry, refname); | |
896 | return 0; | |
897 | } | |
898 | ||
899 | ||
900 | /* | |
901 | * Otherwise, we can try to continue our search with | |
902 | * the next component; if we come up empty, we know | |
903 | * there is nothing under this whole prefix. | |
904 | */ | |
905 | pos = search_ref_dir(dir, refname, slash + 1 - refname); | |
906 | if (pos < 0) | |
907 | return 1; | |
908 | ||
909 | dir = get_ref_dir(dir->entries[pos]); | |
910 | } | |
911 | ||
912 | /* | |
913 | * We are at the leaf of our refname; we want to | |
914 | * make sure there are no directories which match it. | |
915 | */ | |
916 | len = strlen(refname); | |
917 | dirname = xmallocz(len + 1); | |
918 | sprintf(dirname, "%s/", refname); | |
919 | pos = search_ref_dir(dir, dirname, len + 1); | |
920 | free(dirname); | |
921 | ||
922 | if (pos >= 0) { | |
923 | /* | |
924 | * We found a directory named "refname". It is a | |
925 | * problem iff it contains any ref that is not | |
5fe7d825 | 926 | * in "skip". |
cbe73331 JK |
927 | */ |
928 | struct ref_entry *entry = dir->entries[pos]; | |
929 | struct ref_dir *dir = get_ref_dir(entry); | |
930 | struct nonmatching_ref_data data; | |
931 | ||
5fe7d825 | 932 | data.skip = skip; |
cbe73331 JK |
933 | sort_ref_dir(dir); |
934 | if (!do_for_each_entry_in_dir(dir, 0, nonmatching_ref_fn, &data)) | |
935 | return 1; | |
936 | ||
937 | report_refname_conflict(data.found, refname); | |
5a4d4947 | 938 | return 0; |
bc5fd6d3 | 939 | } |
cbe73331 JK |
940 | |
941 | /* | |
942 | * There is no point in searching for another leaf | |
943 | * node which matches it; such an entry would be the | |
944 | * ref we are looking for, not a conflict. | |
945 | */ | |
bc5fd6d3 | 946 | return 1; |
e1e22e37 LT |
947 | } |
948 | ||
2fff7812 MH |
949 | struct packed_ref_cache { |
950 | struct ref_entry *root; | |
9f69d297 | 951 | |
5f5e2a88 MH |
952 | /* |
953 | * Count of references to the data structure in this instance, | |
954 | * including the pointer from ref_cache::packed if any. The | |
955 | * data will not be freed as long as the reference count is | |
956 | * nonzero. | |
957 | */ | |
958 | unsigned int referrers; | |
959 | ||
9f69d297 MH |
960 | /* |
961 | * Iff the packed-refs file associated with this instance is | |
962 | * currently locked for writing, this points at the associated | |
4f6b83e3 MH |
963 | * lock (which is owned by somebody else). The referrer count |
964 | * is also incremented when the file is locked and decremented | |
965 | * when it is unlocked. | |
9f69d297 MH |
966 | */ |
967 | struct lock_file *lock; | |
ca919930 JK |
968 | |
969 | /* The metadata from when this packed-refs cache was read */ | |
970 | struct stat_validity validity; | |
2fff7812 MH |
971 | }; |
972 | ||
5e290ff7 JH |
973 | /* |
974 | * Future: need to be in "struct repository" | |
975 | * when doing a full libification. | |
976 | */ | |
79c7ca54 MH |
977 | static struct ref_cache { |
978 | struct ref_cache *next; | |
d12229f5 | 979 | struct ref_entry *loose; |
2fff7812 | 980 | struct packed_ref_cache *packed; |
9da31cb0 MH |
981 | /* |
982 | * The submodule name, or "" for the main repo. We allocate | |
983 | * length 1 rather than FLEX_ARRAY so that the main ref_cache | |
984 | * is initialized correctly. | |
985 | */ | |
986 | char name[1]; | |
987 | } ref_cache, *submodule_ref_caches; | |
0e88c130 | 988 | |
9f69d297 MH |
989 | /* Lock used for the main packed-refs file: */ |
990 | static struct lock_file packlock; | |
991 | ||
5f5e2a88 MH |
992 | /* |
993 | * Increment the reference count of *packed_refs. | |
994 | */ | |
995 | static void acquire_packed_ref_cache(struct packed_ref_cache *packed_refs) | |
996 | { | |
997 | packed_refs->referrers++; | |
998 | } | |
999 | ||
1000 | /* | |
1001 | * Decrease the reference count of *packed_refs. If it goes to zero, | |
1002 | * free *packed_refs and return true; otherwise return false. | |
1003 | */ | |
1004 | static int release_packed_ref_cache(struct packed_ref_cache *packed_refs) | |
1005 | { | |
1006 | if (!--packed_refs->referrers) { | |
1007 | free_ref_entry(packed_refs->root); | |
ca919930 | 1008 | stat_validity_clear(&packed_refs->validity); |
5f5e2a88 MH |
1009 | free(packed_refs); |
1010 | return 1; | |
1011 | } else { | |
1012 | return 0; | |
1013 | } | |
1014 | } | |
1015 | ||
760c4512 | 1016 | static void clear_packed_ref_cache(struct ref_cache *refs) |
e1e22e37 | 1017 | { |
d12229f5 | 1018 | if (refs->packed) { |
5f5e2a88 MH |
1019 | struct packed_ref_cache *packed_refs = refs->packed; |
1020 | ||
1021 | if (packed_refs->lock) | |
9f69d297 | 1022 | die("internal error: packed-ref cache cleared while locked"); |
d12229f5 | 1023 | refs->packed = NULL; |
5f5e2a88 | 1024 | release_packed_ref_cache(packed_refs); |
d12229f5 | 1025 | } |
5e290ff7 | 1026 | } |
e1e22e37 | 1027 | |
760c4512 MH |
1028 | static void clear_loose_ref_cache(struct ref_cache *refs) |
1029 | { | |
d12229f5 MH |
1030 | if (refs->loose) { |
1031 | free_ref_entry(refs->loose); | |
1032 | refs->loose = NULL; | |
1033 | } | |
760c4512 MH |
1034 | } |
1035 | ||
79c7ca54 | 1036 | static struct ref_cache *create_ref_cache(const char *submodule) |
e5dbf605 | 1037 | { |
ce40979c | 1038 | int len; |
79c7ca54 | 1039 | struct ref_cache *refs; |
ce40979c MH |
1040 | if (!submodule) |
1041 | submodule = ""; | |
1042 | len = strlen(submodule) + 1; | |
79c7ca54 | 1043 | refs = xcalloc(1, sizeof(struct ref_cache) + len); |
ce40979c | 1044 | memcpy(refs->name, submodule, len); |
e5dbf605 MH |
1045 | return refs; |
1046 | } | |
1047 | ||
4349a668 | 1048 | /* |
79c7ca54 | 1049 | * Return a pointer to a ref_cache for the specified submodule. For |
4349a668 MH |
1050 | * the main repository, use submodule==NULL. The returned structure |
1051 | * will be allocated and initialized but not necessarily populated; it | |
1052 | * should not be freed. | |
1053 | */ | |
79c7ca54 | 1054 | static struct ref_cache *get_ref_cache(const char *submodule) |
4349a668 | 1055 | { |
9da31cb0 MH |
1056 | struct ref_cache *refs; |
1057 | ||
1058 | if (!submodule || !*submodule) | |
1059 | return &ref_cache; | |
1060 | ||
1061 | for (refs = submodule_ref_caches; refs; refs = refs->next) | |
0e88c130 MH |
1062 | if (!strcmp(submodule, refs->name)) |
1063 | return refs; | |
0e88c130 | 1064 | |
79c7ca54 | 1065 | refs = create_ref_cache(submodule); |
9da31cb0 MH |
1066 | refs->next = submodule_ref_caches; |
1067 | submodule_ref_caches = refs; | |
0e88c130 | 1068 | return refs; |
4349a668 MH |
1069 | } |
1070 | ||
3feb4f0c MH |
1071 | /* The length of a peeled reference line in packed-refs, including EOL: */ |
1072 | #define PEELED_LINE_LENGTH 42 | |
1073 | ||
694b7a19 MH |
1074 | /* |
1075 | * The packed-refs header line that we write out. Perhaps other | |
1076 | * traits will be added later. The trailing space is required. | |
1077 | */ | |
1078 | static const char PACKED_REFS_HEADER[] = | |
1079 | "# pack-refs with: peeled fully-peeled \n"; | |
1080 | ||
bc5fd6d3 MH |
1081 | /* |
1082 | * Parse one line from a packed-refs file. Write the SHA1 to sha1. | |
1083 | * Return a pointer to the refname within the line (null-terminated), | |
1084 | * or NULL if there was a problem. | |
1085 | */ | |
6a49870a | 1086 | static const char *parse_ref_line(struct strbuf *line, unsigned char *sha1) |
bc5fd6d3 | 1087 | { |
6a49870a JK |
1088 | const char *ref; |
1089 | ||
bc5fd6d3 MH |
1090 | /* |
1091 | * 42: the answer to everything. | |
1092 | * | |
1093 | * In this case, it happens to be the answer to | |
1094 | * 40 (length of sha1 hex representation) | |
1095 | * +1 (space in between hex and name) | |
1096 | * +1 (newline at the end of the line) | |
1097 | */ | |
6a49870a | 1098 | if (line->len <= 42) |
bc5fd6d3 | 1099 | return NULL; |
6a49870a JK |
1100 | |
1101 | if (get_sha1_hex(line->buf, sha1) < 0) | |
bc5fd6d3 | 1102 | return NULL; |
6a49870a | 1103 | if (!isspace(line->buf[40])) |
bc5fd6d3 | 1104 | return NULL; |
6a49870a JK |
1105 | |
1106 | ref = line->buf + 41; | |
1107 | if (isspace(*ref)) | |
bc5fd6d3 | 1108 | return NULL; |
6a49870a JK |
1109 | |
1110 | if (line->buf[line->len - 1] != '\n') | |
bc5fd6d3 | 1111 | return NULL; |
6a49870a | 1112 | line->buf[--line->len] = 0; |
bc5fd6d3 | 1113 | |
6a49870a | 1114 | return ref; |
bc5fd6d3 MH |
1115 | } |
1116 | ||
c29c46fa MH |
1117 | /* |
1118 | * Read f, which is a packed-refs file, into dir. | |
1119 | * | |
1120 | * A comment line of the form "# pack-refs with: " may contain zero or | |
1121 | * more traits. We interpret the traits as follows: | |
1122 | * | |
1123 | * No traits: | |
1124 | * | |
1125 | * Probably no references are peeled. But if the file contains a | |
1126 | * peeled value for a reference, we will use it. | |
1127 | * | |
1128 | * peeled: | |
1129 | * | |
1130 | * References under "refs/tags/", if they *can* be peeled, *are* | |
1131 | * peeled in this file. References outside of "refs/tags/" are | |
1132 | * probably not peeled even if they could have been, but if we find | |
1133 | * a peeled value for such a reference we will use it. | |
1134 | * | |
1135 | * fully-peeled: | |
1136 | * | |
1137 | * All references in the file that can be peeled are peeled. | |
1138 | * Inversely (and this is more important), any references in the | |
1139 | * file for which no peeled value is recorded is not peelable. This | |
1140 | * trait should typically be written alongside "peeled" for | |
1141 | * compatibility with older clients, but we do not require it | |
1142 | * (i.e., "peeled" is a no-op if "fully-peeled" is set). | |
1143 | */ | |
d3177275 | 1144 | static void read_packed_refs(FILE *f, struct ref_dir *dir) |
f4204ab9 | 1145 | { |
e9c4c111 | 1146 | struct ref_entry *last = NULL; |
10c497aa | 1147 | struct strbuf line = STRBUF_INIT; |
c29c46fa | 1148 | enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE; |
f4204ab9 | 1149 | |
10c497aa | 1150 | while (strbuf_getwholeline(&line, f, '\n') != EOF) { |
f4204ab9 | 1151 | unsigned char sha1[20]; |
dfefa935 | 1152 | const char *refname; |
ea417833 | 1153 | const char *traits; |
f4204ab9 | 1154 | |
ea417833 | 1155 | if (skip_prefix(line.buf, "# pack-refs with:", &traits)) { |
c29c46fa MH |
1156 | if (strstr(traits, " fully-peeled ")) |
1157 | peeled = PEELED_FULLY; | |
1158 | else if (strstr(traits, " peeled ")) | |
1159 | peeled = PEELED_TAGS; | |
f4204ab9 JH |
1160 | /* perhaps other traits later as well */ |
1161 | continue; | |
1162 | } | |
1163 | ||
6a49870a | 1164 | refname = parse_ref_line(&line, sha1); |
dfefa935 | 1165 | if (refname) { |
d0f810f0 RS |
1166 | int flag = REF_ISPACKED; |
1167 | ||
1168 | if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) { | |
1169 | hashclr(sha1); | |
1170 | flag |= REF_BAD_NAME | REF_ISBROKEN; | |
1171 | } | |
1172 | last = create_ref_entry(refname, sha1, flag, 0); | |
c29c46fa | 1173 | if (peeled == PEELED_FULLY || |
59556548 | 1174 | (peeled == PEELED_TAGS && starts_with(refname, "refs/tags/"))) |
c29c46fa | 1175 | last->flag |= REF_KNOWS_PEELED; |
d3177275 | 1176 | add_ref(dir, last); |
f4204ab9 JH |
1177 | continue; |
1178 | } | |
1179 | if (last && | |
10c497aa JK |
1180 | line.buf[0] == '^' && |
1181 | line.len == PEELED_LINE_LENGTH && | |
1182 | line.buf[PEELED_LINE_LENGTH - 1] == '\n' && | |
1183 | !get_sha1_hex(line.buf + 1, sha1)) { | |
593f1bb8 | 1184 | hashcpy(last->u.value.peeled, sha1); |
c29c46fa MH |
1185 | /* |
1186 | * Regardless of what the file header said, | |
1187 | * we definitely know the value of *this* | |
1188 | * reference: | |
1189 | */ | |
1190 | last->flag |= REF_KNOWS_PEELED; | |
1191 | } | |
f4204ab9 | 1192 | } |
10c497aa JK |
1193 | |
1194 | strbuf_release(&line); | |
f4204ab9 JH |
1195 | } |
1196 | ||
2fff7812 MH |
1197 | /* |
1198 | * Get the packed_ref_cache for the specified ref_cache, creating it | |
1199 | * if necessary. | |
1200 | */ | |
1201 | static struct packed_ref_cache *get_packed_ref_cache(struct ref_cache *refs) | |
5e290ff7 | 1202 | { |
ca919930 JK |
1203 | const char *packed_refs_file; |
1204 | ||
1205 | if (*refs->name) | |
1206 | packed_refs_file = git_path_submodule(refs->name, "packed-refs"); | |
1207 | else | |
1208 | packed_refs_file = git_path("packed-refs"); | |
1209 | ||
1210 | if (refs->packed && | |
1211 | !stat_validity_check(&refs->packed->validity, packed_refs_file)) | |
1212 | clear_packed_ref_cache(refs); | |
1213 | ||
d12229f5 | 1214 | if (!refs->packed) { |
4349a668 | 1215 | FILE *f; |
0bad611b | 1216 | |
2fff7812 | 1217 | refs->packed = xcalloc(1, sizeof(*refs->packed)); |
5f5e2a88 | 1218 | acquire_packed_ref_cache(refs->packed); |
2fff7812 | 1219 | refs->packed->root = create_dir_entry(refs, "", 0, 0); |
4349a668 | 1220 | f = fopen(packed_refs_file, "r"); |
e1e22e37 | 1221 | if (f) { |
ca919930 | 1222 | stat_validity_update(&refs->packed->validity, fileno(f)); |
2fff7812 | 1223 | read_packed_refs(f, get_ref_dir(refs->packed->root)); |
e1e22e37 | 1224 | fclose(f); |
e1e22e37 | 1225 | } |
e1e22e37 | 1226 | } |
2fff7812 MH |
1227 | return refs->packed; |
1228 | } | |
1229 | ||
1230 | static struct ref_dir *get_packed_ref_dir(struct packed_ref_cache *packed_ref_cache) | |
1231 | { | |
1232 | return get_ref_dir(packed_ref_cache->root); | |
1233 | } | |
1234 | ||
1235 | static struct ref_dir *get_packed_refs(struct ref_cache *refs) | |
1236 | { | |
1237 | return get_packed_ref_dir(get_packed_ref_cache(refs)); | |
e1e22e37 LT |
1238 | } |
1239 | ||
30249ee6 MH |
1240 | void add_packed_ref(const char *refname, const unsigned char *sha1) |
1241 | { | |
9f69d297 MH |
1242 | struct packed_ref_cache *packed_ref_cache = |
1243 | get_packed_ref_cache(&ref_cache); | |
1244 | ||
1245 | if (!packed_ref_cache->lock) | |
1246 | die("internal error: packed refs not locked"); | |
1247 | add_ref(get_packed_ref_dir(packed_ref_cache), | |
9da31cb0 | 1248 | create_ref_entry(refname, sha1, REF_ISPACKED, 1)); |
30249ee6 MH |
1249 | } |
1250 | ||
abc39098 | 1251 | /* |
28e6a34e MH |
1252 | * Read the loose references from the namespace dirname into dir |
1253 | * (without recursing). dirname must end with '/'. dir must be the | |
1254 | * directory entry corresponding to dirname. | |
abc39098 | 1255 | */ |
423a1afc | 1256 | static void read_loose_refs(const char *dirname, struct ref_dir *dir) |
e1e22e37 | 1257 | { |
423a1afc | 1258 | struct ref_cache *refs = dir->ref_cache; |
d3177275 | 1259 | DIR *d; |
0bad611b | 1260 | const char *path; |
d5fdae67 | 1261 | struct dirent *de; |
abc39098 | 1262 | int dirnamelen = strlen(dirname); |
72b64b44 | 1263 | struct strbuf refname; |
0bad611b | 1264 | |
3b124823 | 1265 | if (*refs->name) |
66a3d20b | 1266 | path = git_path_submodule(refs->name, "%s", dirname); |
0bad611b | 1267 | else |
66a3d20b | 1268 | path = git_path("%s", dirname); |
0bad611b | 1269 | |
d3177275 | 1270 | d = opendir(path); |
d5fdae67 MH |
1271 | if (!d) |
1272 | return; | |
1273 | ||
66a3d20b MH |
1274 | strbuf_init(&refname, dirnamelen + 257); |
1275 | strbuf_add(&refname, dirname, dirnamelen); | |
d5fdae67 MH |
1276 | |
1277 | while ((de = readdir(d)) != NULL) { | |
1278 | unsigned char sha1[20]; | |
1279 | struct stat st; | |
1280 | int flag; | |
d5fdae67 MH |
1281 | const char *refdir; |
1282 | ||
1283 | if (de->d_name[0] == '.') | |
1284 | continue; | |
2975c770 | 1285 | if (ends_with(de->d_name, ".lock")) |
d5fdae67 | 1286 | continue; |
72b64b44 | 1287 | strbuf_addstr(&refname, de->d_name); |
d5fdae67 | 1288 | refdir = *refs->name |
72b64b44 MH |
1289 | ? git_path_submodule(refs->name, "%s", refname.buf) |
1290 | : git_path("%s", refname.buf); | |
1291 | if (stat(refdir, &st) < 0) { | |
1292 | ; /* silently ignore */ | |
1293 | } else if (S_ISDIR(st.st_mode)) { | |
abc39098 | 1294 | strbuf_addch(&refname, '/'); |
28e6a34e | 1295 | add_entry_to_dir(dir, |
b9146f51 RS |
1296 | create_dir_entry(refs, refname.buf, |
1297 | refname.len, 1)); | |
72b64b44 | 1298 | } else { |
3b124823 | 1299 | if (*refs->name) { |
f8948e2f | 1300 | hashclr(sha1); |
0bad611b | 1301 | flag = 0; |
72b64b44 | 1302 | if (resolve_gitlink_ref(refs->name, refname.buf, sha1) < 0) { |
0bad611b | 1303 | hashclr(sha1); |
98ac34b2 | 1304 | flag |= REF_ISBROKEN; |
0bad611b | 1305 | } |
7695d118 RS |
1306 | } else if (read_ref_full(refname.buf, |
1307 | RESOLVE_REF_READING, | |
1308 | sha1, &flag)) { | |
09116a1c JH |
1309 | hashclr(sha1); |
1310 | flag |= REF_ISBROKEN; | |
1311 | } | |
d0f810f0 RS |
1312 | if (check_refname_format(refname.buf, |
1313 | REFNAME_ALLOW_ONELEVEL)) { | |
1314 | hashclr(sha1); | |
1315 | flag |= REF_BAD_NAME | REF_ISBROKEN; | |
1316 | } | |
9f2fb4a3 | 1317 | add_entry_to_dir(dir, |
d0f810f0 | 1318 | create_ref_entry(refname.buf, sha1, flag, 0)); |
e1e22e37 | 1319 | } |
66a3d20b | 1320 | strbuf_setlen(&refname, dirnamelen); |
e1e22e37 | 1321 | } |
72b64b44 | 1322 | strbuf_release(&refname); |
d5fdae67 | 1323 | closedir(d); |
e1e22e37 LT |
1324 | } |
1325 | ||
d3177275 | 1326 | static struct ref_dir *get_loose_refs(struct ref_cache *refs) |
e1e22e37 | 1327 | { |
d12229f5 | 1328 | if (!refs->loose) { |
28e6a34e MH |
1329 | /* |
1330 | * Mark the top-level directory complete because we | |
1331 | * are about to read the only subdirectory that can | |
1332 | * hold references: | |
1333 | */ | |
b9146f51 | 1334 | refs->loose = create_dir_entry(refs, "", 0, 0); |
28e6a34e MH |
1335 | /* |
1336 | * Create an incomplete entry for "refs/": | |
1337 | */ | |
1338 | add_entry_to_dir(get_ref_dir(refs->loose), | |
b9146f51 | 1339 | create_dir_entry(refs, "refs/", 5, 1)); |
e1e22e37 | 1340 | } |
d7826d54 | 1341 | return get_ref_dir(refs->loose); |
e1e22e37 LT |
1342 | } |
1343 | ||
ca8db142 LT |
1344 | /* We allow "recursive" symbolic refs. Only within reason, though */ |
1345 | #define MAXDEPTH 5 | |
0ebde32c LT |
1346 | #define MAXREFLEN (1024) |
1347 | ||
e5fa45c1 JH |
1348 | /* |
1349 | * Called by resolve_gitlink_ref_recursive() after it failed to read | |
b0626608 MH |
1350 | * from the loose refs in ref_cache refs. Find <refname> in the |
1351 | * packed-refs file for the submodule. | |
e5fa45c1 | 1352 | */ |
b0626608 | 1353 | static int resolve_gitlink_packed_ref(struct ref_cache *refs, |
85be1fe3 | 1354 | const char *refname, unsigned char *sha1) |
0ebde32c | 1355 | { |
2c5c66be | 1356 | struct ref_entry *ref; |
d3177275 | 1357 | struct ref_dir *dir = get_packed_refs(refs); |
0ebde32c | 1358 | |
432ad41e | 1359 | ref = find_ref(dir, refname); |
b0626608 MH |
1360 | if (ref == NULL) |
1361 | return -1; | |
1362 | ||
50546b15 | 1363 | hashcpy(sha1, ref->u.value.sha1); |
b0626608 | 1364 | return 0; |
0ebde32c LT |
1365 | } |
1366 | ||
b0626608 | 1367 | static int resolve_gitlink_ref_recursive(struct ref_cache *refs, |
85be1fe3 | 1368 | const char *refname, unsigned char *sha1, |
dfefa935 | 1369 | int recursion) |
0ebde32c | 1370 | { |
064d51dc | 1371 | int fd, len; |
0ebde32c | 1372 | char buffer[128], *p; |
064d51dc | 1373 | char *path; |
0ebde32c | 1374 | |
064d51dc | 1375 | if (recursion > MAXDEPTH || strlen(refname) > MAXREFLEN) |
0ebde32c | 1376 | return -1; |
064d51dc MH |
1377 | path = *refs->name |
1378 | ? git_path_submodule(refs->name, "%s", refname) | |
1379 | : git_path("%s", refname); | |
1380 | fd = open(path, O_RDONLY); | |
0ebde32c | 1381 | if (fd < 0) |
b0626608 | 1382 | return resolve_gitlink_packed_ref(refs, refname, sha1); |
0ebde32c LT |
1383 | |
1384 | len = read(fd, buffer, sizeof(buffer)-1); | |
1385 | close(fd); | |
1386 | if (len < 0) | |
1387 | return -1; | |
1388 | while (len && isspace(buffer[len-1])) | |
1389 | len--; | |
1390 | buffer[len] = 0; | |
1391 | ||
1392 | /* Was it a detached head or an old-fashioned symlink? */ | |
85be1fe3 | 1393 | if (!get_sha1_hex(buffer, sha1)) |
0ebde32c LT |
1394 | return 0; |
1395 | ||
1396 | /* Symref? */ | |
1397 | if (strncmp(buffer, "ref:", 4)) | |
1398 | return -1; | |
1399 | p = buffer + 4; | |
1400 | while (isspace(*p)) | |
1401 | p++; | |
1402 | ||
064d51dc | 1403 | return resolve_gitlink_ref_recursive(refs, p, sha1, recursion+1); |
0ebde32c LT |
1404 | } |
1405 | ||
85be1fe3 | 1406 | int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sha1) |
0ebde32c LT |
1407 | { |
1408 | int len = strlen(path), retval; | |
064d51dc | 1409 | char *submodule; |
b0626608 | 1410 | struct ref_cache *refs; |
0ebde32c LT |
1411 | |
1412 | while (len && path[len-1] == '/') | |
1413 | len--; | |
1414 | if (!len) | |
1415 | return -1; | |
b0626608 MH |
1416 | submodule = xstrndup(path, len); |
1417 | refs = get_ref_cache(submodule); | |
1418 | free(submodule); | |
1419 | ||
064d51dc | 1420 | retval = resolve_gitlink_ref_recursive(refs, refname, sha1, 0); |
0ebde32c LT |
1421 | return retval; |
1422 | } | |
ca8db142 | 1423 | |
4886b89f | 1424 | /* |
63331581 MH |
1425 | * Return the ref_entry for the given refname from the packed |
1426 | * references. If it does not exist, return NULL. | |
4886b89f | 1427 | */ |
63331581 | 1428 | static struct ref_entry *get_packed_ref(const char *refname) |
c224ca7f | 1429 | { |
9da31cb0 | 1430 | return find_ref(get_packed_refs(&ref_cache), refname); |
c224ca7f MH |
1431 | } |
1432 | ||
47f534bf MH |
1433 | /* |
1434 | * A loose ref file doesn't exist; check for a packed ref. The | |
1435 | * options are forwarded from resolve_safe_unsafe(). | |
1436 | */ | |
d0f810f0 RS |
1437 | static int resolve_missing_loose_ref(const char *refname, |
1438 | int resolve_flags, | |
1439 | unsigned char *sha1, | |
1440 | int *flags) | |
47f534bf MH |
1441 | { |
1442 | struct ref_entry *entry; | |
1443 | ||
1444 | /* | |
1445 | * The loose reference file does not exist; check for a packed | |
1446 | * reference. | |
1447 | */ | |
1448 | entry = get_packed_ref(refname); | |
1449 | if (entry) { | |
1450 | hashcpy(sha1, entry->u.value.sha1); | |
7695d118 RS |
1451 | if (flags) |
1452 | *flags |= REF_ISPACKED; | |
d0f810f0 | 1453 | return 0; |
47f534bf MH |
1454 | } |
1455 | /* The reference is not a packed reference, either. */ | |
7695d118 | 1456 | if (resolve_flags & RESOLVE_REF_READING) { |
d0f810f0 RS |
1457 | errno = ENOENT; |
1458 | return -1; | |
47f534bf MH |
1459 | } else { |
1460 | hashclr(sha1); | |
d0f810f0 | 1461 | return 0; |
47f534bf MH |
1462 | } |
1463 | } | |
1464 | ||
76d70dc0 | 1465 | /* This function needs to return a meaningful errno on failure */ |
7695d118 | 1466 | const char *resolve_ref_unsafe(const char *refname, int resolve_flags, unsigned char *sha1, int *flags) |
8a65ff76 | 1467 | { |
0104ca09 HO |
1468 | int depth = MAXDEPTH; |
1469 | ssize_t len; | |
a876ed83 | 1470 | char buffer[256]; |
dfefa935 | 1471 | static char refname_buffer[256]; |
d0f810f0 | 1472 | int bad_name = 0; |
ca8db142 | 1473 | |
7695d118 RS |
1474 | if (flags) |
1475 | *flags = 0; | |
8da19775 | 1476 | |
76d70dc0 | 1477 | if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) { |
d0f810f0 RS |
1478 | if (flags) |
1479 | *flags |= REF_BAD_NAME; | |
1480 | ||
1481 | if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) || | |
1482 | !refname_is_safe(refname)) { | |
1483 | errno = EINVAL; | |
1484 | return NULL; | |
1485 | } | |
1486 | /* | |
1487 | * dwim_ref() uses REF_ISBROKEN to distinguish between | |
1488 | * missing refs and refs that were present but invalid, | |
1489 | * to complain about the latter to stderr. | |
1490 | * | |
1491 | * We don't know whether the ref exists, so don't set | |
1492 | * REF_ISBROKEN yet. | |
1493 | */ | |
1494 | bad_name = 1; | |
76d70dc0 | 1495 | } |
a876ed83 | 1496 | for (;;) { |
55956350 | 1497 | char path[PATH_MAX]; |
a876ed83 JH |
1498 | struct stat st; |
1499 | char *buf; | |
1500 | int fd; | |
8a65ff76 | 1501 | |
76d70dc0 RS |
1502 | if (--depth < 0) { |
1503 | errno = ELOOP; | |
a876ed83 | 1504 | return NULL; |
76d70dc0 | 1505 | } |
ca8db142 | 1506 | |
dfefa935 | 1507 | git_snpath(path, sizeof(path), "%s", refname); |
c224ca7f | 1508 | |
fcb7c762 MH |
1509 | /* |
1510 | * We might have to loop back here to avoid a race | |
1511 | * condition: first we lstat() the file, then we try | |
1512 | * to read it as a link or as a file. But if somebody | |
1513 | * changes the type of the file (file <-> directory | |
1514 | * <-> symlink) between the lstat() and reading, then | |
1515 | * we don't want to report that as an error but rather | |
1516 | * try again starting with the lstat(). | |
1517 | */ | |
1518 | stat_ref: | |
a876ed83 | 1519 | if (lstat(path, &st) < 0) { |
d0f810f0 RS |
1520 | if (errno != ENOENT) |
1521 | return NULL; | |
1522 | if (resolve_missing_loose_ref(refname, resolve_flags, | |
1523 | sha1, flags)) | |
a876ed83 | 1524 | return NULL; |
d0f810f0 RS |
1525 | if (bad_name) { |
1526 | hashclr(sha1); | |
1527 | if (flags) | |
1528 | *flags |= REF_ISBROKEN; | |
1529 | } | |
1530 | return refname; | |
a876ed83 | 1531 | } |
ca8db142 | 1532 | |
a876ed83 JH |
1533 | /* Follow "normalized" - ie "refs/.." symlinks by hand */ |
1534 | if (S_ISLNK(st.st_mode)) { | |
1535 | len = readlink(path, buffer, sizeof(buffer)-1); | |
fcb7c762 MH |
1536 | if (len < 0) { |
1537 | if (errno == ENOENT || errno == EINVAL) | |
1538 | /* inconsistent with lstat; retry */ | |
1539 | goto stat_ref; | |
1540 | else | |
1541 | return NULL; | |
1542 | } | |
b54cb795 | 1543 | buffer[len] = 0; |
59556548 | 1544 | if (starts_with(buffer, "refs/") && |
1f58a038 | 1545 | !check_refname_format(buffer, 0)) { |
dfefa935 MH |
1546 | strcpy(refname_buffer, buffer); |
1547 | refname = refname_buffer; | |
7695d118 RS |
1548 | if (flags) |
1549 | *flags |= REF_ISSYMREF; | |
62a2d525 JN |
1550 | if (resolve_flags & RESOLVE_REF_NO_RECURSE) { |
1551 | hashclr(sha1); | |
1552 | return refname; | |
1553 | } | |
a876ed83 JH |
1554 | continue; |
1555 | } | |
ca8db142 | 1556 | } |
a876ed83 | 1557 | |
7a21632f DS |
1558 | /* Is it a directory? */ |
1559 | if (S_ISDIR(st.st_mode)) { | |
1560 | errno = EISDIR; | |
1561 | return NULL; | |
1562 | } | |
1563 | ||
a876ed83 JH |
1564 | /* |
1565 | * Anything else, just open it and try to use it as | |
1566 | * a ref | |
1567 | */ | |
1568 | fd = open(path, O_RDONLY); | |
fcb7c762 MH |
1569 | if (fd < 0) { |
1570 | if (errno == ENOENT) | |
1571 | /* inconsistent with lstat; retry */ | |
1572 | goto stat_ref; | |
1573 | else | |
1574 | return NULL; | |
1575 | } | |
93d26e4c | 1576 | len = read_in_full(fd, buffer, sizeof(buffer)-1); |
76d70dc0 RS |
1577 | if (len < 0) { |
1578 | int save_errno = errno; | |
1579 | close(fd); | |
1580 | errno = save_errno; | |
28775050 | 1581 | return NULL; |
76d70dc0 RS |
1582 | } |
1583 | close(fd); | |
28775050 MH |
1584 | while (len && isspace(buffer[len-1])) |
1585 | len--; | |
1586 | buffer[len] = '\0'; | |
a876ed83 JH |
1587 | |
1588 | /* | |
1589 | * Is it a symbolic ref? | |
1590 | */ | |
59556548 | 1591 | if (!starts_with(buffer, "ref:")) { |
2884c06a MH |
1592 | /* |
1593 | * Please note that FETCH_HEAD has a second | |
1594 | * line containing other data. | |
1595 | */ | |
1596 | if (get_sha1_hex(buffer, sha1) || | |
1597 | (buffer[40] != '\0' && !isspace(buffer[40]))) { | |
7695d118 RS |
1598 | if (flags) |
1599 | *flags |= REF_ISBROKEN; | |
76d70dc0 | 1600 | errno = EINVAL; |
2884c06a MH |
1601 | return NULL; |
1602 | } | |
d0f810f0 RS |
1603 | if (bad_name) { |
1604 | hashclr(sha1); | |
1605 | if (flags) | |
1606 | *flags |= REF_ISBROKEN; | |
1607 | } | |
2884c06a MH |
1608 | return refname; |
1609 | } | |
7695d118 RS |
1610 | if (flags) |
1611 | *flags |= REF_ISSYMREF; | |
a876ed83 | 1612 | buf = buffer + 4; |
28775050 MH |
1613 | while (isspace(*buf)) |
1614 | buf++; | |
62a2d525 JN |
1615 | refname = strcpy(refname_buffer, buf); |
1616 | if (resolve_flags & RESOLVE_REF_NO_RECURSE) { | |
1617 | hashclr(sha1); | |
1618 | return refname; | |
1619 | } | |
313fb010 | 1620 | if (check_refname_format(buf, REFNAME_ALLOW_ONELEVEL)) { |
7695d118 RS |
1621 | if (flags) |
1622 | *flags |= REF_ISBROKEN; | |
d0f810f0 RS |
1623 | |
1624 | if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) || | |
1625 | !refname_is_safe(buf)) { | |
1626 | errno = EINVAL; | |
1627 | return NULL; | |
1628 | } | |
1629 | bad_name = 1; | |
313fb010 | 1630 | } |
8a65ff76 | 1631 | } |
a876ed83 JH |
1632 | } |
1633 | ||
7695d118 | 1634 | char *resolve_refdup(const char *ref, int resolve_flags, unsigned char *sha1, int *flags) |
96ec7b1e | 1635 | { |
092c4be7 | 1636 | return xstrdup_or_null(resolve_ref_unsafe(ref, resolve_flags, sha1, flags)); |
96ec7b1e NTND |
1637 | } |
1638 | ||
d08bae7e IL |
1639 | /* The argument to filter_refs */ |
1640 | struct ref_filter { | |
1641 | const char *pattern; | |
1642 | each_ref_fn *fn; | |
1643 | void *cb_data; | |
1644 | }; | |
1645 | ||
7695d118 | 1646 | int read_ref_full(const char *refname, int resolve_flags, unsigned char *sha1, int *flags) |
a876ed83 | 1647 | { |
7695d118 | 1648 | if (resolve_ref_unsafe(refname, resolve_flags, sha1, flags)) |
a876ed83 JH |
1649 | return 0; |
1650 | return -1; | |
8a65ff76 LT |
1651 | } |
1652 | ||
dfefa935 | 1653 | int read_ref(const char *refname, unsigned char *sha1) |
c6893323 | 1654 | { |
7695d118 | 1655 | return read_ref_full(refname, RESOLVE_REF_READING, sha1, NULL); |
c6893323 NTND |
1656 | } |
1657 | ||
bc5fd6d3 | 1658 | int ref_exists(const char *refname) |
ef06b918 | 1659 | { |
bc5fd6d3 | 1660 | unsigned char sha1[20]; |
7695d118 | 1661 | return !!resolve_ref_unsafe(refname, RESOLVE_REF_READING, sha1, NULL); |
ef06b918 JH |
1662 | } |
1663 | ||
85be1fe3 | 1664 | static int filter_refs(const char *refname, const unsigned char *sha1, int flags, |
dfefa935 | 1665 | void *data) |
d08bae7e IL |
1666 | { |
1667 | struct ref_filter *filter = (struct ref_filter *)data; | |
eb07894f | 1668 | if (wildmatch(filter->pattern, refname, 0, NULL)) |
d08bae7e | 1669 | return 0; |
85be1fe3 | 1670 | return filter->fn(refname, sha1, flags, filter->cb_data); |
d08bae7e IL |
1671 | } |
1672 | ||
68cf8703 MH |
1673 | enum peel_status { |
1674 | /* object was peeled successfully: */ | |
1675 | PEEL_PEELED = 0, | |
1676 | ||
1677 | /* | |
1678 | * object cannot be peeled because the named object (or an | |
1679 | * object referred to by a tag in the peel chain), does not | |
1680 | * exist. | |
1681 | */ | |
1682 | PEEL_INVALID = -1, | |
1683 | ||
1684 | /* object cannot be peeled because it is not a tag: */ | |
9a489f3c MH |
1685 | PEEL_NON_TAG = -2, |
1686 | ||
1687 | /* ref_entry contains no peeled value because it is a symref: */ | |
1688 | PEEL_IS_SYMREF = -3, | |
1689 | ||
1690 | /* | |
1691 | * ref_entry cannot be peeled because it is broken (i.e., the | |
1692 | * symbolic reference cannot even be resolved to an object | |
1693 | * name): | |
1694 | */ | |
1695 | PEEL_BROKEN = -4 | |
68cf8703 MH |
1696 | }; |
1697 | ||
cb2ae1c4 MH |
1698 | /* |
1699 | * Peel the named object; i.e., if the object is a tag, resolve the | |
68cf8703 MH |
1700 | * tag recursively until a non-tag is found. If successful, store the |
1701 | * result to sha1 and return PEEL_PEELED. If the object is not a tag | |
1702 | * or is not valid, return PEEL_NON_TAG or PEEL_INVALID, respectively, | |
1703 | * and leave sha1 unchanged. | |
cb2ae1c4 | 1704 | */ |
68cf8703 | 1705 | static enum peel_status peel_object(const unsigned char *name, unsigned char *sha1) |
cb2ae1c4 MH |
1706 | { |
1707 | struct object *o = lookup_unknown_object(name); | |
1708 | ||
1709 | if (o->type == OBJ_NONE) { | |
1710 | int type = sha1_object_info(name, NULL); | |
8ff226a9 | 1711 | if (type < 0 || !object_as_type(o, type, 0)) |
68cf8703 | 1712 | return PEEL_INVALID; |
cb2ae1c4 MH |
1713 | } |
1714 | ||
1715 | if (o->type != OBJ_TAG) | |
68cf8703 | 1716 | return PEEL_NON_TAG; |
cb2ae1c4 MH |
1717 | |
1718 | o = deref_tag_noverify(o); | |
1719 | if (!o) | |
68cf8703 | 1720 | return PEEL_INVALID; |
cb2ae1c4 MH |
1721 | |
1722 | hashcpy(sha1, o->sha1); | |
68cf8703 | 1723 | return PEEL_PEELED; |
cb2ae1c4 MH |
1724 | } |
1725 | ||
9a489f3c | 1726 | /* |
f85354b5 MH |
1727 | * Peel the entry (if possible) and return its new peel_status. If |
1728 | * repeel is true, re-peel the entry even if there is an old peeled | |
1729 | * value that is already stored in it. | |
694b7a19 MH |
1730 | * |
1731 | * It is OK to call this function with a packed reference entry that | |
1732 | * might be stale and might even refer to an object that has since | |
1733 | * been garbage-collected. In such a case, if the entry has | |
1734 | * REF_KNOWS_PEELED then leave the status unchanged and return | |
1735 | * PEEL_PEELED or PEEL_NON_TAG; otherwise, return PEEL_INVALID. | |
9a489f3c | 1736 | */ |
f85354b5 | 1737 | static enum peel_status peel_entry(struct ref_entry *entry, int repeel) |
9a489f3c MH |
1738 | { |
1739 | enum peel_status status; | |
1740 | ||
f85354b5 MH |
1741 | if (entry->flag & REF_KNOWS_PEELED) { |
1742 | if (repeel) { | |
1743 | entry->flag &= ~REF_KNOWS_PEELED; | |
1744 | hashclr(entry->u.value.peeled); | |
1745 | } else { | |
1746 | return is_null_sha1(entry->u.value.peeled) ? | |
1747 | PEEL_NON_TAG : PEEL_PEELED; | |
1748 | } | |
1749 | } | |
9a489f3c MH |
1750 | if (entry->flag & REF_ISBROKEN) |
1751 | return PEEL_BROKEN; | |
1752 | if (entry->flag & REF_ISSYMREF) | |
1753 | return PEEL_IS_SYMREF; | |
1754 | ||
1755 | status = peel_object(entry->u.value.sha1, entry->u.value.peeled); | |
1756 | if (status == PEEL_PEELED || status == PEEL_NON_TAG) | |
1757 | entry->flag |= REF_KNOWS_PEELED; | |
1758 | return status; | |
1759 | } | |
1760 | ||
dfefa935 | 1761 | int peel_ref(const char *refname, unsigned char *sha1) |
cf0adba7 JH |
1762 | { |
1763 | int flag; | |
1764 | unsigned char base[20]; | |
cf0adba7 | 1765 | |
dfefa935 | 1766 | if (current_ref && (current_ref->name == refname |
9a489f3c | 1767 | || !strcmp(current_ref->name, refname))) { |
f85354b5 | 1768 | if (peel_entry(current_ref, 0)) |
9a489f3c MH |
1769 | return -1; |
1770 | hashcpy(sha1, current_ref->u.value.peeled); | |
1771 | return 0; | |
0ae91be0 SP |
1772 | } |
1773 | ||
7695d118 | 1774 | if (read_ref_full(refname, RESOLVE_REF_READING, base, &flag)) |
cf0adba7 JH |
1775 | return -1; |
1776 | ||
9a489f3c MH |
1777 | /* |
1778 | * If the reference is packed, read its ref_entry from the | |
1779 | * cache in the hope that we already know its peeled value. | |
1780 | * We only try this optimization on packed references because | |
1781 | * (a) forcing the filling of the loose reference cache could | |
1782 | * be expensive and (b) loose references anyway usually do not | |
1783 | * have REF_KNOWS_PEELED. | |
1784 | */ | |
1785 | if (flag & REF_ISPACKED) { | |
f361baeb | 1786 | struct ref_entry *r = get_packed_ref(refname); |
9a489f3c | 1787 | if (r) { |
f85354b5 | 1788 | if (peel_entry(r, 0)) |
9a489f3c | 1789 | return -1; |
593f1bb8 | 1790 | hashcpy(sha1, r->u.value.peeled); |
e9c4c111 | 1791 | return 0; |
cf0adba7 | 1792 | } |
cf0adba7 JH |
1793 | } |
1794 | ||
cb2ae1c4 | 1795 | return peel_object(base, sha1); |
cf0adba7 JH |
1796 | } |
1797 | ||
bc5fd6d3 MH |
1798 | struct warn_if_dangling_data { |
1799 | FILE *fp; | |
1800 | const char *refname; | |
e6bea66d | 1801 | const struct string_list *refnames; |
bc5fd6d3 MH |
1802 | const char *msg_fmt; |
1803 | }; | |
1804 | ||
1805 | static int warn_if_dangling_symref(const char *refname, const unsigned char *sha1, | |
1806 | int flags, void *cb_data) | |
1807 | { | |
1808 | struct warn_if_dangling_data *d = cb_data; | |
1809 | const char *resolves_to; | |
1810 | unsigned char junk[20]; | |
1811 | ||
1812 | if (!(flags & REF_ISSYMREF)) | |
1813 | return 0; | |
1814 | ||
7695d118 | 1815 | resolves_to = resolve_ref_unsafe(refname, 0, junk, NULL); |
e6bea66d JL |
1816 | if (!resolves_to |
1817 | || (d->refname | |
1818 | ? strcmp(resolves_to, d->refname) | |
1819 | : !string_list_has_string(d->refnames, resolves_to))) { | |
bc5fd6d3 | 1820 | return 0; |
e6bea66d | 1821 | } |
bc5fd6d3 MH |
1822 | |
1823 | fprintf(d->fp, d->msg_fmt, refname); | |
1be65eda | 1824 | fputc('\n', d->fp); |
bc5fd6d3 MH |
1825 | return 0; |
1826 | } | |
1827 | ||
1828 | void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname) | |
1829 | { | |
1830 | struct warn_if_dangling_data data; | |
1831 | ||
1832 | data.fp = fp; | |
1833 | data.refname = refname; | |
e6bea66d JL |
1834 | data.refnames = NULL; |
1835 | data.msg_fmt = msg_fmt; | |
1836 | for_each_rawref(warn_if_dangling_symref, &data); | |
1837 | } | |
1838 | ||
1839 | void warn_dangling_symrefs(FILE *fp, const char *msg_fmt, const struct string_list *refnames) | |
1840 | { | |
1841 | struct warn_if_dangling_data data; | |
1842 | ||
1843 | data.fp = fp; | |
1844 | data.refname = NULL; | |
1845 | data.refnames = refnames; | |
bc5fd6d3 MH |
1846 | data.msg_fmt = msg_fmt; |
1847 | for_each_rawref(warn_if_dangling_symref, &data); | |
1848 | } | |
1849 | ||
fcce1703 | 1850 | /* |
65cf102b | 1851 | * Call fn for each reference in the specified ref_cache, omitting |
624cac35 MH |
1852 | * references not in the containing_dir of base. fn is called for all |
1853 | * references, including broken ones. If fn ever returns a non-zero | |
fcce1703 MH |
1854 | * value, stop the iteration and return that value; otherwise, return |
1855 | * 0. | |
1856 | */ | |
65cf102b | 1857 | static int do_for_each_entry(struct ref_cache *refs, const char *base, |
624cac35 | 1858 | each_ref_entry_fn fn, void *cb_data) |
8a65ff76 | 1859 | { |
98eeb09e JK |
1860 | struct packed_ref_cache *packed_ref_cache; |
1861 | struct ref_dir *loose_dir; | |
1862 | struct ref_dir *packed_dir; | |
933ac036 MH |
1863 | int retval = 0; |
1864 | ||
98eeb09e JK |
1865 | /* |
1866 | * We must make sure that all loose refs are read before accessing the | |
1867 | * packed-refs file; this avoids a race condition in which loose refs | |
1868 | * are migrated to the packed-refs file by a simultaneous process, but | |
1869 | * our in-memory view is from before the migration. get_packed_ref_cache() | |
1870 | * takes care of making sure our view is up to date with what is on | |
1871 | * disk. | |
1872 | */ | |
1873 | loose_dir = get_loose_refs(refs); | |
933ac036 | 1874 | if (base && *base) { |
933ac036 MH |
1875 | loose_dir = find_containing_dir(loose_dir, base, 0); |
1876 | } | |
98eeb09e JK |
1877 | if (loose_dir) |
1878 | prime_ref_dir(loose_dir); | |
1879 | ||
1880 | packed_ref_cache = get_packed_ref_cache(refs); | |
8baf2bb9 | 1881 | acquire_packed_ref_cache(packed_ref_cache); |
98eeb09e | 1882 | packed_dir = get_packed_ref_dir(packed_ref_cache); |
933ac036 MH |
1883 | if (base && *base) { |
1884 | packed_dir = find_containing_dir(packed_dir, base, 0); | |
933ac036 MH |
1885 | } |
1886 | ||
1887 | if (packed_dir && loose_dir) { | |
1888 | sort_ref_dir(packed_dir); | |
1889 | sort_ref_dir(loose_dir); | |
624cac35 MH |
1890 | retval = do_for_each_entry_in_dirs( |
1891 | packed_dir, loose_dir, fn, cb_data); | |
933ac036 MH |
1892 | } else if (packed_dir) { |
1893 | sort_ref_dir(packed_dir); | |
624cac35 MH |
1894 | retval = do_for_each_entry_in_dir( |
1895 | packed_dir, 0, fn, cb_data); | |
933ac036 MH |
1896 | } else if (loose_dir) { |
1897 | sort_ref_dir(loose_dir); | |
624cac35 MH |
1898 | retval = do_for_each_entry_in_dir( |
1899 | loose_dir, 0, fn, cb_data); | |
933ac036 MH |
1900 | } |
1901 | ||
8baf2bb9 | 1902 | release_packed_ref_cache(packed_ref_cache); |
933ac036 | 1903 | return retval; |
8a65ff76 LT |
1904 | } |
1905 | ||
624cac35 | 1906 | /* |
65cf102b | 1907 | * Call fn for each reference in the specified ref_cache for which the |
624cac35 MH |
1908 | * refname begins with base. If trim is non-zero, then trim that many |
1909 | * characters off the beginning of each refname before passing the | |
1910 | * refname to fn. flags can be DO_FOR_EACH_INCLUDE_BROKEN to include | |
1911 | * broken references in the iteration. If fn ever returns a non-zero | |
1912 | * value, stop the iteration and return that value; otherwise, return | |
1913 | * 0. | |
1914 | */ | |
65cf102b MH |
1915 | static int do_for_each_ref(struct ref_cache *refs, const char *base, |
1916 | each_ref_fn fn, int trim, int flags, void *cb_data) | |
624cac35 MH |
1917 | { |
1918 | struct ref_entry_cb data; | |
1919 | data.base = base; | |
1920 | data.trim = trim; | |
1921 | data.flags = flags; | |
1922 | data.fn = fn; | |
1923 | data.cb_data = cb_data; | |
1924 | ||
65cf102b | 1925 | return do_for_each_entry(refs, base, do_one_ref, &data); |
624cac35 MH |
1926 | } |
1927 | ||
0bad611b | 1928 | static int do_head_ref(const char *submodule, each_ref_fn fn, void *cb_data) |
723c31fe LT |
1929 | { |
1930 | unsigned char sha1[20]; | |
8da19775 JH |
1931 | int flag; |
1932 | ||
0bad611b HV |
1933 | if (submodule) { |
1934 | if (resolve_gitlink_ref(submodule, "HEAD", sha1) == 0) | |
1935 | return fn("HEAD", sha1, 0, cb_data); | |
1936 | ||
1937 | return 0; | |
1938 | } | |
1939 | ||
7695d118 | 1940 | if (!read_ref_full("HEAD", RESOLVE_REF_READING, sha1, &flag)) |
8da19775 | 1941 | return fn("HEAD", sha1, flag, cb_data); |
0bad611b | 1942 | |
2f34ba32 | 1943 | return 0; |
723c31fe LT |
1944 | } |
1945 | ||
0bad611b HV |
1946 | int head_ref(each_ref_fn fn, void *cb_data) |
1947 | { | |
1948 | return do_head_ref(NULL, fn, cb_data); | |
1949 | } | |
1950 | ||
9ef6aeb0 HV |
1951 | int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data) |
1952 | { | |
1953 | return do_head_ref(submodule, fn, cb_data); | |
1954 | } | |
1955 | ||
cb5d709f | 1956 | int for_each_ref(each_ref_fn fn, void *cb_data) |
8a65ff76 | 1957 | { |
9da31cb0 | 1958 | return do_for_each_ref(&ref_cache, "", fn, 0, 0, cb_data); |
a62be77f SE |
1959 | } |
1960 | ||
9ef6aeb0 HV |
1961 | int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data) |
1962 | { | |
65cf102b | 1963 | return do_for_each_ref(get_ref_cache(submodule), "", fn, 0, 0, cb_data); |
a62be77f SE |
1964 | } |
1965 | ||
2a8177b6 CC |
1966 | int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data) |
1967 | { | |
9da31cb0 | 1968 | return do_for_each_ref(&ref_cache, prefix, fn, strlen(prefix), 0, cb_data); |
2a8177b6 CC |
1969 | } |
1970 | ||
9ef6aeb0 HV |
1971 | int for_each_ref_in_submodule(const char *submodule, const char *prefix, |
1972 | each_ref_fn fn, void *cb_data) | |
1973 | { | |
65cf102b | 1974 | return do_for_each_ref(get_ref_cache(submodule), prefix, fn, strlen(prefix), 0, cb_data); |
2a8177b6 CC |
1975 | } |
1976 | ||
cb5d709f | 1977 | int for_each_tag_ref(each_ref_fn fn, void *cb_data) |
a62be77f | 1978 | { |
2a8177b6 | 1979 | return for_each_ref_in("refs/tags/", fn, cb_data); |
a62be77f SE |
1980 | } |
1981 | ||
9ef6aeb0 HV |
1982 | int for_each_tag_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data) |
1983 | { | |
1984 | return for_each_ref_in_submodule(submodule, "refs/tags/", fn, cb_data); | |
1985 | } | |
1986 | ||
cb5d709f | 1987 | int for_each_branch_ref(each_ref_fn fn, void *cb_data) |
a62be77f | 1988 | { |
2a8177b6 | 1989 | return for_each_ref_in("refs/heads/", fn, cb_data); |
a62be77f SE |
1990 | } |
1991 | ||
9ef6aeb0 HV |
1992 | int for_each_branch_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data) |
1993 | { | |
1994 | return for_each_ref_in_submodule(submodule, "refs/heads/", fn, cb_data); | |
1995 | } | |
1996 | ||
cb5d709f | 1997 | int for_each_remote_ref(each_ref_fn fn, void *cb_data) |
a62be77f | 1998 | { |
2a8177b6 | 1999 | return for_each_ref_in("refs/remotes/", fn, cb_data); |
f8948e2f JH |
2000 | } |
2001 | ||
9ef6aeb0 HV |
2002 | int for_each_remote_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data) |
2003 | { | |
2004 | return for_each_ref_in_submodule(submodule, "refs/remotes/", fn, cb_data); | |
2005 | } | |
2006 | ||
29268700 CC |
2007 | int for_each_replace_ref(each_ref_fn fn, void *cb_data) |
2008 | { | |
9da31cb0 | 2009 | return do_for_each_ref(&ref_cache, "refs/replace/", fn, 13, 0, cb_data); |
29268700 CC |
2010 | } |
2011 | ||
a1bea2c1 JT |
2012 | int head_ref_namespaced(each_ref_fn fn, void *cb_data) |
2013 | { | |
2014 | struct strbuf buf = STRBUF_INIT; | |
2015 | int ret = 0; | |
2016 | unsigned char sha1[20]; | |
2017 | int flag; | |
2018 | ||
2019 | strbuf_addf(&buf, "%sHEAD", get_git_namespace()); | |
7695d118 | 2020 | if (!read_ref_full(buf.buf, RESOLVE_REF_READING, sha1, &flag)) |
a1bea2c1 JT |
2021 | ret = fn(buf.buf, sha1, flag, cb_data); |
2022 | strbuf_release(&buf); | |
2023 | ||
2024 | return ret; | |
2025 | } | |
2026 | ||
2027 | int for_each_namespaced_ref(each_ref_fn fn, void *cb_data) | |
2028 | { | |
2029 | struct strbuf buf = STRBUF_INIT; | |
2030 | int ret; | |
2031 | strbuf_addf(&buf, "%srefs/", get_git_namespace()); | |
9da31cb0 | 2032 | ret = do_for_each_ref(&ref_cache, buf.buf, fn, 0, 0, cb_data); |
a1bea2c1 JT |
2033 | strbuf_release(&buf); |
2034 | return ret; | |
2035 | } | |
2036 | ||
b09fe971 IL |
2037 | int for_each_glob_ref_in(each_ref_fn fn, const char *pattern, |
2038 | const char *prefix, void *cb_data) | |
d08bae7e IL |
2039 | { |
2040 | struct strbuf real_pattern = STRBUF_INIT; | |
2041 | struct ref_filter filter; | |
d08bae7e IL |
2042 | int ret; |
2043 | ||
59556548 | 2044 | if (!prefix && !starts_with(pattern, "refs/")) |
d08bae7e | 2045 | strbuf_addstr(&real_pattern, "refs/"); |
b09fe971 IL |
2046 | else if (prefix) |
2047 | strbuf_addstr(&real_pattern, prefix); | |
d08bae7e IL |
2048 | strbuf_addstr(&real_pattern, pattern); |
2049 | ||
894a9d33 | 2050 | if (!has_glob_specials(pattern)) { |
9517e6b8 | 2051 | /* Append implied '/' '*' if not present. */ |
d08bae7e IL |
2052 | if (real_pattern.buf[real_pattern.len - 1] != '/') |
2053 | strbuf_addch(&real_pattern, '/'); | |
2054 | /* No need to check for '*', there is none. */ | |
2055 | strbuf_addch(&real_pattern, '*'); | |
2056 | } | |
2057 | ||
2058 | filter.pattern = real_pattern.buf; | |
2059 | filter.fn = fn; | |
2060 | filter.cb_data = cb_data; | |
2061 | ret = for_each_ref(filter_refs, &filter); | |
2062 | ||
2063 | strbuf_release(&real_pattern); | |
2064 | return ret; | |
2065 | } | |
2066 | ||
b09fe971 IL |
2067 | int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data) |
2068 | { | |
2069 | return for_each_glob_ref_in(fn, pattern, NULL, cb_data); | |
2070 | } | |
2071 | ||
f8948e2f JH |
2072 | int for_each_rawref(each_ref_fn fn, void *cb_data) |
2073 | { | |
9da31cb0 | 2074 | return do_for_each_ref(&ref_cache, "", fn, 0, |
f8948e2f | 2075 | DO_FOR_EACH_INCLUDE_BROKEN, cb_data); |
8a65ff76 LT |
2076 | } |
2077 | ||
4577e483 | 2078 | const char *prettify_refname(const char *name) |
a9c37a72 | 2079 | { |
a9c37a72 | 2080 | return name + ( |
59556548 CC |
2081 | starts_with(name, "refs/heads/") ? 11 : |
2082 | starts_with(name, "refs/tags/") ? 10 : | |
2083 | starts_with(name, "refs/remotes/") ? 13 : | |
a9c37a72 DB |
2084 | 0); |
2085 | } | |
2086 | ||
54457fe5 | 2087 | static const char *ref_rev_parse_rules[] = { |
79803322 SP |
2088 | "%.*s", |
2089 | "refs/%.*s", | |
2090 | "refs/tags/%.*s", | |
2091 | "refs/heads/%.*s", | |
2092 | "refs/remotes/%.*s", | |
2093 | "refs/remotes/%.*s/HEAD", | |
2094 | NULL | |
2095 | }; | |
2096 | ||
54457fe5 | 2097 | int refname_match(const char *abbrev_name, const char *full_name) |
79803322 SP |
2098 | { |
2099 | const char **p; | |
2100 | const int abbrev_name_len = strlen(abbrev_name); | |
2101 | ||
54457fe5 | 2102 | for (p = ref_rev_parse_rules; *p; p++) { |
79803322 SP |
2103 | if (!strcmp(full_name, mkpath(*p, abbrev_name_len, abbrev_name))) { |
2104 | return 1; | |
2105 | } | |
2106 | } | |
2107 | ||
2108 | return 0; | |
2109 | } | |
2110 | ||
0b1e6548 RS |
2111 | static void unlock_ref(struct ref_lock *lock) |
2112 | { | |
2113 | /* Do not free lock->lk -- atexit() still looks at them */ | |
2114 | if (lock->lk) | |
2115 | rollback_lock_file(lock->lk); | |
2116 | free(lock->ref_name); | |
2117 | free(lock->orig_ref_name); | |
2118 | free(lock); | |
2119 | } | |
2120 | ||
835e3c99 | 2121 | /* This function should make sure errno is meaningful on error */ |
e5f38ec3 | 2122 | static struct ref_lock *verify_lock(struct ref_lock *lock, |
4bd18c43 SP |
2123 | const unsigned char *old_sha1, int mustexist) |
2124 | { | |
7695d118 RS |
2125 | if (read_ref_full(lock->ref_name, |
2126 | mustexist ? RESOLVE_REF_READING : 0, | |
2127 | lock->old_sha1, NULL)) { | |
835e3c99 | 2128 | int save_errno = errno; |
434cd0cd | 2129 | error("Can't verify ref %s", lock->ref_name); |
4bd18c43 | 2130 | unlock_ref(lock); |
835e3c99 | 2131 | errno = save_errno; |
4bd18c43 SP |
2132 | return NULL; |
2133 | } | |
a89fccd2 | 2134 | if (hashcmp(lock->old_sha1, old_sha1)) { |
434cd0cd | 2135 | error("Ref %s is at %s but expected %s", lock->ref_name, |
4bd18c43 SP |
2136 | sha1_to_hex(lock->old_sha1), sha1_to_hex(old_sha1)); |
2137 | unlock_ref(lock); | |
835e3c99 | 2138 | errno = EBUSY; |
4bd18c43 SP |
2139 | return NULL; |
2140 | } | |
2141 | return lock; | |
2142 | } | |
2143 | ||
7155b727 | 2144 | static int remove_empty_directories(const char *file) |
bc7127ef JH |
2145 | { |
2146 | /* we want to create a file but there is a directory there; | |
2147 | * if that is an empty directory (or a directory that contains | |
2148 | * only empty directories), remove them. | |
2149 | */ | |
7155b727 | 2150 | struct strbuf path; |
470a91ef | 2151 | int result, save_errno; |
bc7127ef | 2152 | |
7155b727 JS |
2153 | strbuf_init(&path, 20); |
2154 | strbuf_addstr(&path, file); | |
2155 | ||
a0f4afbe | 2156 | result = remove_dir_recursively(&path, REMOVE_DIR_EMPTY_ONLY); |
470a91ef | 2157 | save_errno = errno; |
7155b727 JS |
2158 | |
2159 | strbuf_release(&path); | |
470a91ef | 2160 | errno = save_errno; |
7155b727 JS |
2161 | |
2162 | return result; | |
bc7127ef JH |
2163 | } |
2164 | ||
ff74f7f1 JH |
2165 | /* |
2166 | * *string and *len will only be substituted, and *string returned (for | |
2167 | * later free()ing) if the string passed in is a magic short-hand form | |
2168 | * to name a branch. | |
2169 | */ | |
2170 | static char *substitute_branch_name(const char **string, int *len) | |
2171 | { | |
2172 | struct strbuf buf = STRBUF_INIT; | |
cf99a761 | 2173 | int ret = interpret_branch_name(*string, *len, &buf); |
ff74f7f1 JH |
2174 | |
2175 | if (ret == *len) { | |
2176 | size_t size; | |
2177 | *string = strbuf_detach(&buf, &size); | |
2178 | *len = size; | |
2179 | return (char *)*string; | |
2180 | } | |
2181 | ||
2182 | return NULL; | |
2183 | } | |
2184 | ||
2185 | int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref) | |
2186 | { | |
2187 | char *last_branch = substitute_branch_name(&str, &len); | |
2188 | const char **p, *r; | |
2189 | int refs_found = 0; | |
2190 | ||
2191 | *ref = NULL; | |
2192 | for (p = ref_rev_parse_rules; *p; p++) { | |
2193 | char fullref[PATH_MAX]; | |
2194 | unsigned char sha1_from_ref[20]; | |
2195 | unsigned char *this_result; | |
2196 | int flag; | |
2197 | ||
2198 | this_result = refs_found ? sha1_from_ref : sha1; | |
2199 | mksnpath(fullref, sizeof(fullref), *p, len, str); | |
7695d118 RS |
2200 | r = resolve_ref_unsafe(fullref, RESOLVE_REF_READING, |
2201 | this_result, &flag); | |
ff74f7f1 JH |
2202 | if (r) { |
2203 | if (!refs_found++) | |
2204 | *ref = xstrdup(r); | |
2205 | if (!warn_ambiguous_refs) | |
2206 | break; | |
55956350 | 2207 | } else if ((flag & REF_ISSYMREF) && strcmp(fullref, "HEAD")) { |
ff74f7f1 | 2208 | warning("ignoring dangling symref %s.", fullref); |
55956350 JH |
2209 | } else if ((flag & REF_ISBROKEN) && strchr(fullref, '/')) { |
2210 | warning("ignoring broken ref %s.", fullref); | |
2211 | } | |
ff74f7f1 JH |
2212 | } |
2213 | free(last_branch); | |
2214 | return refs_found; | |
2215 | } | |
2216 | ||
2217 | int dwim_log(const char *str, int len, unsigned char *sha1, char **log) | |
2218 | { | |
2219 | char *last_branch = substitute_branch_name(&str, &len); | |
2220 | const char **p; | |
2221 | int logs_found = 0; | |
2222 | ||
2223 | *log = NULL; | |
2224 | for (p = ref_rev_parse_rules; *p; p++) { | |
ff74f7f1 JH |
2225 | unsigned char hash[20]; |
2226 | char path[PATH_MAX]; | |
2227 | const char *ref, *it; | |
2228 | ||
2229 | mksnpath(path, sizeof(path), *p, len, str); | |
7695d118 RS |
2230 | ref = resolve_ref_unsafe(path, RESOLVE_REF_READING, |
2231 | hash, NULL); | |
ff74f7f1 JH |
2232 | if (!ref) |
2233 | continue; | |
4da58835 | 2234 | if (reflog_exists(path)) |
ff74f7f1 | 2235 | it = path; |
4da58835 | 2236 | else if (strcmp(ref, path) && reflog_exists(ref)) |
ff74f7f1 JH |
2237 | it = ref; |
2238 | else | |
2239 | continue; | |
2240 | if (!logs_found++) { | |
2241 | *log = xstrdup(it); | |
2242 | hashcpy(sha1, hash); | |
2243 | } | |
2244 | if (!warn_ambiguous_refs) | |
2245 | break; | |
2246 | } | |
2247 | free(last_branch); | |
2248 | return logs_found; | |
2249 | } | |
2250 | ||
88b680ae | 2251 | /* |
3c93c847 | 2252 | * Locks a ref returning the lock on success and NULL on failure. |
88b680ae RS |
2253 | * On failure errno is set to something meaningful. |
2254 | */ | |
dfefa935 MH |
2255 | static struct ref_lock *lock_ref_sha1_basic(const char *refname, |
2256 | const unsigned char *old_sha1, | |
5fe7d825 | 2257 | const struct string_list *skip, |
dfefa935 | 2258 | int flags, int *type_p) |
4bd18c43 | 2259 | { |
434cd0cd | 2260 | char *ref_file; |
dfefa935 | 2261 | const char *orig_refname = refname; |
4bd18c43 | 2262 | struct ref_lock *lock; |
5cc3cef9 | 2263 | int last_errno = 0; |
acd3b9ec | 2264 | int type, lflags; |
4431fcc4 | 2265 | int mustexist = (old_sha1 && !is_null_sha1(old_sha1)); |
7695d118 | 2266 | int resolve_flags = 0; |
5bdd8d4a | 2267 | int missing = 0; |
c4c61c76 | 2268 | int attempts_remaining = 3; |
4bd18c43 SP |
2269 | |
2270 | lock = xcalloc(1, sizeof(struct ref_lock)); | |
2271 | lock->lock_fd = -1; | |
2272 | ||
7695d118 RS |
2273 | if (mustexist) |
2274 | resolve_flags |= RESOLVE_REF_READING; | |
d0f810f0 RS |
2275 | if (flags & REF_DELETING) { |
2276 | resolve_flags |= RESOLVE_REF_ALLOW_BAD_NAME; | |
2277 | if (flags & REF_NODEREF) | |
2278 | resolve_flags |= RESOLVE_REF_NO_RECURSE; | |
2279 | } | |
7695d118 RS |
2280 | |
2281 | refname = resolve_ref_unsafe(refname, resolve_flags, | |
2282 | lock->old_sha1, &type); | |
dfefa935 | 2283 | if (!refname && errno == EISDIR) { |
bc7127ef JH |
2284 | /* we are trying to lock foo but we used to |
2285 | * have foo/bar which now does not exist; | |
2286 | * it is normal for the empty directory 'foo' | |
2287 | * to remain. | |
2288 | */ | |
dfefa935 | 2289 | ref_file = git_path("%s", orig_refname); |
5cc3cef9 JH |
2290 | if (remove_empty_directories(ref_file)) { |
2291 | last_errno = errno; | |
dfefa935 | 2292 | error("there are still refs under '%s'", orig_refname); |
5cc3cef9 JH |
2293 | goto error_return; |
2294 | } | |
7695d118 RS |
2295 | refname = resolve_ref_unsafe(orig_refname, resolve_flags, |
2296 | lock->old_sha1, &type); | |
bc7127ef | 2297 | } |
68db31cc SV |
2298 | if (type_p) |
2299 | *type_p = type; | |
dfefa935 | 2300 | if (!refname) { |
5cc3cef9 | 2301 | last_errno = errno; |
818f477c | 2302 | error("unable to resolve reference %s: %s", |
dfefa935 | 2303 | orig_refname, strerror(errno)); |
5cc3cef9 | 2304 | goto error_return; |
4bd18c43 | 2305 | } |
5bdd8d4a | 2306 | missing = is_null_sha1(lock->old_sha1); |
c976d415 LH |
2307 | /* When the ref did not exist and we are creating it, |
2308 | * make sure there is no existing ref that is packed | |
2309 | * whose name begins with our refname, nor a ref whose | |
2310 | * name is a proper prefix of our refname. | |
2311 | */ | |
5bdd8d4a | 2312 | if (missing && |
5fe7d825 | 2313 | !is_refname_available(refname, skip, get_packed_refs(&ref_cache))) { |
f475e08e | 2314 | last_errno = ENOTDIR; |
c976d415 | 2315 | goto error_return; |
f475e08e | 2316 | } |
22a3844e | 2317 | |
c33d5174 | 2318 | lock->lk = xcalloc(1, sizeof(struct lock_file)); |
4bd18c43 | 2319 | |
e5c223e9 | 2320 | lflags = 0; |
acd3b9ec | 2321 | if (flags & REF_NODEREF) { |
dfefa935 | 2322 | refname = orig_refname; |
47ba4662 | 2323 | lflags |= LOCK_NO_DEREF; |
acd3b9ec | 2324 | } |
dfefa935 MH |
2325 | lock->ref_name = xstrdup(refname); |
2326 | lock->orig_ref_name = xstrdup(orig_refname); | |
2327 | ref_file = git_path("%s", refname); | |
5bdd8d4a | 2328 | if (missing) |
68db31cc SV |
2329 | lock->force_write = 1; |
2330 | if ((flags & REF_NODEREF) && (type & REF_ISSYMREF)) | |
2331 | lock->force_write = 1; | |
4bd18c43 | 2332 | |
c4c61c76 MH |
2333 | retry: |
2334 | switch (safe_create_leading_directories(ref_file)) { | |
2335 | case SCLD_OK: | |
2336 | break; /* success */ | |
2337 | case SCLD_VANISHED: | |
2338 | if (--attempts_remaining > 0) | |
2339 | goto retry; | |
2340 | /* fall through */ | |
2341 | default: | |
5cc3cef9 JH |
2342 | last_errno = errno; |
2343 | error("unable to create directory for %s", ref_file); | |
2344 | goto error_return; | |
2345 | } | |
4bd18c43 | 2346 | |
acd3b9ec | 2347 | lock->lock_fd = hold_lock_file_for_update(lock->lk, ref_file, lflags); |
e5c223e9 | 2348 | if (lock->lock_fd < 0) { |
06839515 | 2349 | last_errno = errno; |
e5c223e9 MH |
2350 | if (errno == ENOENT && --attempts_remaining > 0) |
2351 | /* | |
2352 | * Maybe somebody just deleted one of the | |
2353 | * directories leading to ref_file. Try | |
2354 | * again: | |
2355 | */ | |
2356 | goto retry; | |
06839515 RS |
2357 | else { |
2358 | struct strbuf err = STRBUF_INIT; | |
2359 | unable_to_lock_message(ref_file, errno, &err); | |
2360 | error("%s", err.buf); | |
33adc83d | 2361 | strbuf_release(&err); |
06839515 RS |
2362 | goto error_return; |
2363 | } | |
e5c223e9 | 2364 | } |
4bd18c43 | 2365 | return old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock; |
5cc3cef9 JH |
2366 | |
2367 | error_return: | |
2368 | unlock_ref(lock); | |
2369 | errno = last_errno; | |
2370 | return NULL; | |
4bd18c43 SP |
2371 | } |
2372 | ||
fec3137f MH |
2373 | /* |
2374 | * Write an entry to the packed-refs file for the specified refname. | |
2375 | * If peeled is non-NULL, write it as the entry's peeled value. | |
2376 | */ | |
9540ce50 | 2377 | static void write_packed_entry(FILE *fh, char *refname, unsigned char *sha1, |
fec3137f | 2378 | unsigned char *peeled) |
d66da478 | 2379 | { |
9540ce50 JK |
2380 | fprintf_or_die(fh, "%s %s\n", sha1_to_hex(sha1), refname); |
2381 | if (peeled) | |
2382 | fprintf_or_die(fh, "^%s\n", sha1_to_hex(peeled)); | |
fec3137f MH |
2383 | } |
2384 | ||
7b40d396 MH |
2385 | /* |
2386 | * An each_ref_entry_fn that writes the entry to a packed-refs file. | |
2387 | */ | |
2388 | static int write_packed_entry_fn(struct ref_entry *entry, void *cb_data) | |
2389 | { | |
7b40d396 MH |
2390 | enum peel_status peel_status = peel_entry(entry, 0); |
2391 | ||
2392 | if (peel_status != PEEL_PEELED && peel_status != PEEL_NON_TAG) | |
2393 | error("internal error: %s is not a valid packed reference!", | |
2394 | entry->name); | |
9540ce50 | 2395 | write_packed_entry(cb_data, entry->name, entry->u.value.sha1, |
7b40d396 MH |
2396 | peel_status == PEEL_PEELED ? |
2397 | entry->u.value.peeled : NULL); | |
2398 | return 0; | |
2399 | } | |
2400 | ||
447ff1bf | 2401 | /* This should return a meaningful errno on failure */ |
9f69d297 MH |
2402 | int lock_packed_refs(int flags) |
2403 | { | |
2404 | struct packed_ref_cache *packed_ref_cache; | |
2405 | ||
9f69d297 MH |
2406 | if (hold_lock_file_for_update(&packlock, git_path("packed-refs"), flags) < 0) |
2407 | return -1; | |
5d478f5c MH |
2408 | /* |
2409 | * Get the current packed-refs while holding the lock. If the | |
2410 | * packed-refs file has been modified since we last read it, | |
2411 | * this will automatically invalidate the cache and re-read | |
2412 | * the packed-refs file. | |
2413 | */ | |
9f69d297 MH |
2414 | packed_ref_cache = get_packed_ref_cache(&ref_cache); |
2415 | packed_ref_cache->lock = &packlock; | |
4f6b83e3 MH |
2416 | /* Increment the reference count to prevent it from being freed: */ |
2417 | acquire_packed_ref_cache(packed_ref_cache); | |
9f69d297 MH |
2418 | return 0; |
2419 | } | |
2420 | ||
d3f66555 RS |
2421 | /* |
2422 | * Commit the packed refs changes. | |
2423 | * On error we must make sure that errno contains a meaningful value. | |
2424 | */ | |
9f69d297 MH |
2425 | int commit_packed_refs(void) |
2426 | { | |
2427 | struct packed_ref_cache *packed_ref_cache = | |
2428 | get_packed_ref_cache(&ref_cache); | |
2429 | int error = 0; | |
d3f66555 | 2430 | int save_errno = 0; |
9540ce50 | 2431 | FILE *out; |
9f69d297 MH |
2432 | |
2433 | if (!packed_ref_cache->lock) | |
2434 | die("internal error: packed-refs not locked"); | |
9f69d297 | 2435 | |
6e578a31 | 2436 | out = fdopen_lock_file(packed_ref_cache->lock, "w"); |
9540ce50 JK |
2437 | if (!out) |
2438 | die_errno("unable to fdopen packed-refs descriptor"); | |
2439 | ||
2440 | fprintf_or_die(out, "%s", PACKED_REFS_HEADER); | |
9f69d297 | 2441 | do_for_each_entry_in_dir(get_packed_ref_dir(packed_ref_cache), |
9540ce50 | 2442 | 0, write_packed_entry_fn, out); |
9540ce50 | 2443 | |
d3f66555 RS |
2444 | if (commit_lock_file(packed_ref_cache->lock)) { |
2445 | save_errno = errno; | |
9f69d297 | 2446 | error = -1; |
d3f66555 | 2447 | } |
9f69d297 | 2448 | packed_ref_cache->lock = NULL; |
4f6b83e3 | 2449 | release_packed_ref_cache(packed_ref_cache); |
d3f66555 | 2450 | errno = save_errno; |
9f69d297 MH |
2451 | return error; |
2452 | } | |
2453 | ||
2454 | void rollback_packed_refs(void) | |
2455 | { | |
2456 | struct packed_ref_cache *packed_ref_cache = | |
2457 | get_packed_ref_cache(&ref_cache); | |
2458 | ||
2459 | if (!packed_ref_cache->lock) | |
2460 | die("internal error: packed-refs not locked"); | |
2461 | rollback_lock_file(packed_ref_cache->lock); | |
2462 | packed_ref_cache->lock = NULL; | |
4f6b83e3 | 2463 | release_packed_ref_cache(packed_ref_cache); |
9f69d297 MH |
2464 | clear_packed_ref_cache(&ref_cache); |
2465 | } | |
2466 | ||
32d462ce MH |
2467 | struct ref_to_prune { |
2468 | struct ref_to_prune *next; | |
2469 | unsigned char sha1[20]; | |
2470 | char name[FLEX_ARRAY]; | |
2471 | }; | |
2472 | ||
2473 | struct pack_refs_cb_data { | |
2474 | unsigned int flags; | |
267f9a8c | 2475 | struct ref_dir *packed_refs; |
32d462ce | 2476 | struct ref_to_prune *ref_to_prune; |
32d462ce MH |
2477 | }; |
2478 | ||
267f9a8c MH |
2479 | /* |
2480 | * An each_ref_entry_fn that is run over loose references only. If | |
2481 | * the loose reference can be packed, add an entry in the packed ref | |
2482 | * cache. If the reference should be pruned, also add it to | |
2483 | * ref_to_prune in the pack_refs_cb_data. | |
2484 | */ | |
2485 | static int pack_if_possible_fn(struct ref_entry *entry, void *cb_data) | |
32d462ce MH |
2486 | { |
2487 | struct pack_refs_cb_data *cb = cb_data; | |
f85354b5 | 2488 | enum peel_status peel_status; |
267f9a8c | 2489 | struct ref_entry *packed_entry; |
59556548 | 2490 | int is_tag_ref = starts_with(entry->name, "refs/tags/"); |
32d462ce | 2491 | |
267f9a8c MH |
2492 | /* ALWAYS pack tags */ |
2493 | if (!(cb->flags & PACK_REFS_ALL) && !is_tag_ref) | |
32d462ce MH |
2494 | return 0; |
2495 | ||
b2a8226d MH |
2496 | /* Do not pack symbolic or broken refs: */ |
2497 | if ((entry->flag & REF_ISSYMREF) || !ref_resolves_to_object(entry)) | |
2498 | return 0; | |
2499 | ||
267f9a8c | 2500 | /* Add a packed ref cache entry equivalent to the loose entry. */ |
f85354b5 | 2501 | peel_status = peel_entry(entry, 1); |
0f29920f | 2502 | if (peel_status != PEEL_PEELED && peel_status != PEEL_NON_TAG) |
f85354b5 MH |
2503 | die("internal error peeling reference %s (%s)", |
2504 | entry->name, sha1_to_hex(entry->u.value.sha1)); | |
267f9a8c MH |
2505 | packed_entry = find_ref(cb->packed_refs, entry->name); |
2506 | if (packed_entry) { | |
2507 | /* Overwrite existing packed entry with info from loose entry */ | |
2508 | packed_entry->flag = REF_ISPACKED | REF_KNOWS_PEELED; | |
2509 | hashcpy(packed_entry->u.value.sha1, entry->u.value.sha1); | |
2510 | } else { | |
2511 | packed_entry = create_ref_entry(entry->name, entry->u.value.sha1, | |
2512 | REF_ISPACKED | REF_KNOWS_PEELED, 0); | |
2513 | add_ref(cb->packed_refs, packed_entry); | |
2514 | } | |
2515 | hashcpy(packed_entry->u.value.peeled, entry->u.value.peeled); | |
32d462ce | 2516 | |
267f9a8c MH |
2517 | /* Schedule the loose reference for pruning if requested. */ |
2518 | if ((cb->flags & PACK_REFS_PRUNE)) { | |
12e77559 | 2519 | int namelen = strlen(entry->name) + 1; |
32d462ce | 2520 | struct ref_to_prune *n = xcalloc(1, sizeof(*n) + namelen); |
12e77559 MH |
2521 | hashcpy(n->sha1, entry->u.value.sha1); |
2522 | strcpy(n->name, entry->name); | |
32d462ce MH |
2523 | n->next = cb->ref_to_prune; |
2524 | cb->ref_to_prune = n; | |
2525 | } | |
d66da478 MH |
2526 | return 0; |
2527 | } | |
2528 | ||
32d462ce MH |
2529 | /* |
2530 | * Remove empty parents, but spare refs/ and immediate subdirs. | |
2531 | * Note: munges *name. | |
2532 | */ | |
2533 | static void try_remove_empty_parents(char *name) | |
2534 | { | |
2535 | char *p, *q; | |
2536 | int i; | |
2537 | p = name; | |
2538 | for (i = 0; i < 2; i++) { /* refs/{heads,tags,...}/ */ | |
2539 | while (*p && *p != '/') | |
2540 | p++; | |
2541 | /* tolerate duplicate slashes; see check_refname_format() */ | |
2542 | while (*p == '/') | |
2543 | p++; | |
2544 | } | |
2545 | for (q = p; *q; q++) | |
2546 | ; | |
2547 | while (1) { | |
2548 | while (q > p && *q != '/') | |
2549 | q--; | |
2550 | while (q > p && *(q-1) == '/') | |
2551 | q--; | |
2552 | if (q == p) | |
2553 | break; | |
2554 | *q = '\0'; | |
2555 | if (rmdir(git_path("%s", name))) | |
2556 | break; | |
2557 | } | |
2558 | } | |
2559 | ||
2560 | /* make sure nobody touched the ref, and unlink */ | |
2561 | static void prune_ref(struct ref_to_prune *r) | |
2562 | { | |
029cdb4a RS |
2563 | struct ref_transaction *transaction; |
2564 | struct strbuf err = STRBUF_INIT; | |
32d462ce | 2565 | |
88e7dff9 | 2566 | if (check_refname_format(r->name, 0)) |
cba12021 | 2567 | return; |
32d462ce | 2568 | |
029cdb4a RS |
2569 | transaction = ref_transaction_begin(&err); |
2570 | if (!transaction || | |
2571 | ref_transaction_delete(transaction, r->name, r->sha1, | |
db7516ab RS |
2572 | REF_ISPRUNING, 1, NULL, &err) || |
2573 | ref_transaction_commit(transaction, &err)) { | |
029cdb4a RS |
2574 | ref_transaction_free(transaction); |
2575 | error("%s", err.buf); | |
2576 | strbuf_release(&err); | |
2577 | return; | |
32d462ce | 2578 | } |
029cdb4a RS |
2579 | ref_transaction_free(transaction); |
2580 | strbuf_release(&err); | |
2581 | try_remove_empty_parents(r->name); | |
32d462ce MH |
2582 | } |
2583 | ||
2584 | static void prune_refs(struct ref_to_prune *r) | |
2585 | { | |
2586 | while (r) { | |
2587 | prune_ref(r); | |
2588 | r = r->next; | |
2589 | } | |
2590 | } | |
2591 | ||
32d462ce MH |
2592 | int pack_refs(unsigned int flags) |
2593 | { | |
32d462ce MH |
2594 | struct pack_refs_cb_data cbdata; |
2595 | ||
2596 | memset(&cbdata, 0, sizeof(cbdata)); | |
2597 | cbdata.flags = flags; | |
2598 | ||
9f69d297 | 2599 | lock_packed_refs(LOCK_DIE_ON_ERROR); |
267f9a8c | 2600 | cbdata.packed_refs = get_packed_refs(&ref_cache); |
32d462ce | 2601 | |
267f9a8c MH |
2602 | do_for_each_entry_in_dir(get_loose_refs(&ref_cache), 0, |
2603 | pack_if_possible_fn, &cbdata); | |
32d462ce | 2604 | |
9f69d297 | 2605 | if (commit_packed_refs()) |
32d462ce | 2606 | die_errno("unable to overwrite old ref-pack file"); |
9f69d297 | 2607 | |
32d462ce MH |
2608 | prune_refs(cbdata.ref_to_prune); |
2609 | return 0; | |
2610 | } | |
2611 | ||
7b40d396 MH |
2612 | /* |
2613 | * If entry is no longer needed in packed-refs, add it to the string | |
2614 | * list pointed to by cb_data. Reasons for deleting entries: | |
2615 | * | |
2616 | * - Entry is broken. | |
2617 | * - Entry is overridden by a loose ref. | |
2618 | * - Entry does not point at a valid object. | |
2619 | * | |
2620 | * In the first and third cases, also emit an error message because these | |
2621 | * are indications of repository corruption. | |
2622 | */ | |
2623 | static int curate_packed_ref_fn(struct ref_entry *entry, void *cb_data) | |
c0277d15 | 2624 | { |
7b40d396 | 2625 | struct string_list *refs_to_delete = cb_data; |
fec3137f | 2626 | |
ab292bc4 MH |
2627 | if (entry->flag & REF_ISBROKEN) { |
2628 | /* This shouldn't happen to packed refs. */ | |
2629 | error("%s is broken!", entry->name); | |
7b40d396 | 2630 | string_list_append(refs_to_delete, entry->name); |
c0277d15 | 2631 | return 0; |
ab292bc4 MH |
2632 | } |
2633 | if (!has_sha1_file(entry->u.value.sha1)) { | |
2634 | unsigned char sha1[20]; | |
2635 | int flags; | |
2636 | ||
7695d118 | 2637 | if (read_ref_full(entry->name, 0, sha1, &flags)) |
ab292bc4 MH |
2638 | /* We should at least have found the packed ref. */ |
2639 | die("Internal error"); | |
7b40d396 | 2640 | if ((flags & REF_ISSYMREF) || !(flags & REF_ISPACKED)) { |
ab292bc4 MH |
2641 | /* |
2642 | * This packed reference is overridden by a | |
2643 | * loose reference, so it is OK that its value | |
2644 | * is no longer valid; for example, it might | |
2645 | * refer to an object that has been garbage | |
2646 | * collected. For this purpose we don't even | |
2647 | * care whether the loose reference itself is | |
2648 | * invalid, broken, symbolic, etc. Silently | |
7b40d396 | 2649 | * remove the packed reference. |
ab292bc4 | 2650 | */ |
7b40d396 | 2651 | string_list_append(refs_to_delete, entry->name); |
ab292bc4 | 2652 | return 0; |
7b40d396 | 2653 | } |
ab292bc4 MH |
2654 | /* |
2655 | * There is no overriding loose reference, so the fact | |
2656 | * that this reference doesn't refer to a valid object | |
2657 | * indicates some kind of repository corruption. | |
2658 | * Report the problem, then omit the reference from | |
2659 | * the output. | |
2660 | */ | |
2661 | error("%s does not point to a valid object!", entry->name); | |
7b40d396 | 2662 | string_list_append(refs_to_delete, entry->name); |
ab292bc4 MH |
2663 | return 0; |
2664 | } | |
2665 | ||
d66da478 MH |
2666 | return 0; |
2667 | } | |
2668 | ||
4a45b2f3 | 2669 | int repack_without_refs(struct string_list *refnames, struct strbuf *err) |
c0277d15 | 2670 | { |
7618fd80 | 2671 | struct ref_dir *packed; |
7b40d396 | 2672 | struct string_list refs_to_delete = STRING_LIST_INIT_DUP; |
4a45b2f3 MH |
2673 | struct string_list_item *refname, *ref_to_delete; |
2674 | int ret, needs_repacking = 0, removed = 0; | |
61cee0db | 2675 | |
5a603b04 JN |
2676 | assert(err); |
2677 | ||
61cee0db | 2678 | /* Look for a packed ref */ |
4a45b2f3 MH |
2679 | for_each_string_list_item(refname, refnames) { |
2680 | if (get_packed_ref(refname->string)) { | |
2681 | needs_repacking = 1; | |
61cee0db | 2682 | break; |
4a45b2f3 MH |
2683 | } |
2684 | } | |
7618fd80 | 2685 | |
61cee0db | 2686 | /* Avoid locking if we have nothing to do */ |
4a45b2f3 | 2687 | if (!needs_repacking) |
61cee0db | 2688 | return 0; /* no refname exists in packed refs */ |
7618fd80 | 2689 | |
9f69d297 | 2690 | if (lock_packed_refs(0)) { |
5a603b04 JN |
2691 | unable_to_lock_message(git_path("packed-refs"), errno, err); |
2692 | return -1; | |
1b018fd9 | 2693 | } |
9da31cb0 | 2694 | packed = get_packed_refs(&ref_cache); |
7b40d396 | 2695 | |
61cee0db | 2696 | /* Remove refnames from the cache */ |
4a45b2f3 MH |
2697 | for_each_string_list_item(refname, refnames) |
2698 | if (remove_entry(packed, refname->string) != -1) | |
61cee0db BK |
2699 | removed = 1; |
2700 | if (!removed) { | |
506a760d | 2701 | /* |
61cee0db | 2702 | * All packed entries disappeared while we were |
506a760d MH |
2703 | * acquiring the lock. |
2704 | */ | |
9f69d297 | 2705 | rollback_packed_refs(); |
506a760d MH |
2706 | return 0; |
2707 | } | |
7b40d396 | 2708 | |
61cee0db | 2709 | /* Remove any other accumulated cruft */ |
7b40d396 MH |
2710 | do_for_each_entry_in_dir(packed, 0, curate_packed_ref_fn, &refs_to_delete); |
2711 | for_each_string_list_item(ref_to_delete, &refs_to_delete) { | |
2712 | if (remove_entry(packed, ref_to_delete->string) == -1) | |
2713 | die("internal error"); | |
2714 | } | |
2715 | ||
61cee0db | 2716 | /* Write what remains */ |
60bca085 | 2717 | ret = commit_packed_refs(); |
5a603b04 | 2718 | if (ret) |
60bca085 RS |
2719 | strbuf_addf(err, "unable to overwrite old ref-pack file: %s", |
2720 | strerror(errno)); | |
2721 | return ret; | |
c0277d15 JH |
2722 | } |
2723 | ||
dbdcac7d | 2724 | static int delete_ref_loose(struct ref_lock *lock, int flag, struct strbuf *err) |
2ddb5d17 | 2725 | { |
5a603b04 JN |
2726 | assert(err); |
2727 | ||
045a476f | 2728 | if (!(flag & REF_ISPACKED) || flag & REF_ISSYMREF) { |
91f1f191 MH |
2729 | /* |
2730 | * loose. The loose file name is the same as the | |
2731 | * lockfile name, minus ".lock": | |
2732 | */ | |
ec38b4e4 | 2733 | char *loose_filename = get_locked_file_path(lock->lk); |
dbdcac7d | 2734 | int res = unlink_or_msg(loose_filename, err); |
91f1f191 | 2735 | free(loose_filename); |
dbdcac7d | 2736 | if (res) |
2ddb5d17 | 2737 | return 1; |
c0277d15 | 2738 | } |
2ddb5d17 BK |
2739 | return 0; |
2740 | } | |
2741 | ||
eca35a25 | 2742 | int delete_ref(const char *refname, const unsigned char *sha1, int delopt) |
c0277d15 | 2743 | { |
7521cc46 RS |
2744 | struct ref_transaction *transaction; |
2745 | struct strbuf err = STRBUF_INIT; | |
c0277d15 | 2746 | |
7521cc46 RS |
2747 | transaction = ref_transaction_begin(&err); |
2748 | if (!transaction || | |
2749 | ref_transaction_delete(transaction, refname, sha1, delopt, | |
db7516ab RS |
2750 | sha1 && !is_null_sha1(sha1), NULL, &err) || |
2751 | ref_transaction_commit(transaction, &err)) { | |
7521cc46 RS |
2752 | error("%s", err.buf); |
2753 | ref_transaction_free(transaction); | |
2754 | strbuf_release(&err); | |
c0277d15 | 2755 | return 1; |
7521cc46 RS |
2756 | } |
2757 | ref_transaction_free(transaction); | |
2758 | strbuf_release(&err); | |
2759 | return 0; | |
4bd18c43 SP |
2760 | } |
2761 | ||
765c2258 PH |
2762 | /* |
2763 | * People using contrib's git-new-workdir have .git/logs/refs -> | |
2764 | * /some/other/path/.git/logs/refs, and that may live on another device. | |
2765 | * | |
2766 | * IOW, to avoid cross device rename errors, the temporary renamed log must | |
2767 | * live into logs/refs. | |
2768 | */ | |
2769 | #define TMP_RENAMED_LOG "logs/refs/.tmp-renamed-log" | |
2770 | ||
fa59ae79 MH |
2771 | static int rename_tmp_log(const char *newrefname) |
2772 | { | |
f1e9e9a4 | 2773 | int attempts_remaining = 4; |
ae4a283e MH |
2774 | |
2775 | retry: | |
08f555cb MH |
2776 | switch (safe_create_leading_directories(git_path("logs/%s", newrefname))) { |
2777 | case SCLD_OK: | |
2778 | break; /* success */ | |
2779 | case SCLD_VANISHED: | |
2780 | if (--attempts_remaining > 0) | |
2781 | goto retry; | |
2782 | /* fall through */ | |
2783 | default: | |
fa59ae79 MH |
2784 | error("unable to create directory for %s", newrefname); |
2785 | return -1; | |
2786 | } | |
2787 | ||
fa59ae79 | 2788 | if (rename(git_path(TMP_RENAMED_LOG), git_path("logs/%s", newrefname))) { |
f1e9e9a4 | 2789 | if ((errno==EISDIR || errno==ENOTDIR) && --attempts_remaining > 0) { |
fa59ae79 MH |
2790 | /* |
2791 | * rename(a, b) when b is an existing | |
2792 | * directory ought to result in ISDIR, but | |
2793 | * Solaris 5.8 gives ENOTDIR. Sheesh. | |
2794 | */ | |
2795 | if (remove_empty_directories(git_path("logs/%s", newrefname))) { | |
2796 | error("Directory not empty: logs/%s", newrefname); | |
2797 | return -1; | |
2798 | } | |
2799 | goto retry; | |
ae4a283e MH |
2800 | } else if (errno == ENOENT && --attempts_remaining > 0) { |
2801 | /* | |
2802 | * Maybe another process just deleted one of | |
2803 | * the directories in the path to newrefname. | |
2804 | * Try again from the beginning. | |
2805 | */ | |
2806 | goto retry; | |
fa59ae79 MH |
2807 | } else { |
2808 | error("unable to move logfile "TMP_RENAMED_LOG" to logs/%s: %s", | |
2809 | newrefname, strerror(errno)); | |
2810 | return -1; | |
2811 | } | |
2812 | } | |
2813 | return 0; | |
2814 | } | |
2815 | ||
5fe7d825 RS |
2816 | static int rename_ref_available(const char *oldname, const char *newname) |
2817 | { | |
2818 | struct string_list skip = STRING_LIST_INIT_NODUP; | |
2819 | int ret; | |
2820 | ||
2821 | string_list_insert(&skip, oldname); | |
2822 | ret = is_refname_available(newname, &skip, get_packed_refs(&ref_cache)) | |
2823 | && is_refname_available(newname, &skip, get_loose_refs(&ref_cache)); | |
2824 | string_list_clear(&skip, 0); | |
2825 | return ret; | |
2826 | } | |
2827 | ||
aae383db RS |
2828 | static int write_ref_sha1(struct ref_lock *lock, const unsigned char *sha1, |
2829 | const char *logmsg); | |
2830 | ||
dfefa935 | 2831 | int rename_ref(const char *oldrefname, const char *newrefname, const char *logmsg) |
c976d415 | 2832 | { |
c976d415 LH |
2833 | unsigned char sha1[20], orig_sha1[20]; |
2834 | int flag = 0, logmoved = 0; | |
2835 | struct ref_lock *lock; | |
c976d415 | 2836 | struct stat loginfo; |
dfefa935 | 2837 | int log = !lstat(git_path("logs/%s", oldrefname), &loginfo); |
eca35a25 | 2838 | const char *symref = NULL; |
c976d415 | 2839 | |
450d4c0f | 2840 | if (log && S_ISLNK(loginfo.st_mode)) |
dfefa935 | 2841 | return error("reflog for %s is a symlink", oldrefname); |
c976d415 | 2842 | |
7695d118 RS |
2843 | symref = resolve_ref_unsafe(oldrefname, RESOLVE_REF_READING, |
2844 | orig_sha1, &flag); | |
eca35a25 | 2845 | if (flag & REF_ISSYMREF) |
fa58186c | 2846 | return error("refname %s is a symbolic ref, renaming it is not supported", |
dfefa935 | 2847 | oldrefname); |
eca35a25 | 2848 | if (!symref) |
dfefa935 | 2849 | return error("refname %s not found", oldrefname); |
c976d415 | 2850 | |
5fe7d825 | 2851 | if (!rename_ref_available(oldrefname, newrefname)) |
c976d415 LH |
2852 | return 1; |
2853 | ||
dfefa935 | 2854 | if (log && rename(git_path("logs/%s", oldrefname), git_path(TMP_RENAMED_LOG))) |
765c2258 | 2855 | return error("unable to move logfile logs/%s to "TMP_RENAMED_LOG": %s", |
dfefa935 | 2856 | oldrefname, strerror(errno)); |
c976d415 | 2857 | |
dfefa935 MH |
2858 | if (delete_ref(oldrefname, orig_sha1, REF_NODEREF)) { |
2859 | error("unable to delete old %s", oldrefname); | |
c976d415 LH |
2860 | goto rollback; |
2861 | } | |
2862 | ||
7695d118 | 2863 | if (!read_ref_full(newrefname, RESOLVE_REF_READING, sha1, NULL) && |
dfefa935 | 2864 | delete_ref(newrefname, sha1, REF_NODEREF)) { |
c976d415 | 2865 | if (errno==EISDIR) { |
dfefa935 MH |
2866 | if (remove_empty_directories(git_path("%s", newrefname))) { |
2867 | error("Directory not empty: %s", newrefname); | |
c976d415 LH |
2868 | goto rollback; |
2869 | } | |
2870 | } else { | |
dfefa935 | 2871 | error("unable to delete existing %s", newrefname); |
c976d415 LH |
2872 | goto rollback; |
2873 | } | |
2874 | } | |
2875 | ||
fa59ae79 | 2876 | if (log && rename_tmp_log(newrefname)) |
c976d415 | 2877 | goto rollback; |
c976d415 | 2878 | |
c976d415 LH |
2879 | logmoved = log; |
2880 | ||
5fe7d825 | 2881 | lock = lock_ref_sha1_basic(newrefname, NULL, NULL, 0, NULL); |
c976d415 | 2882 | if (!lock) { |
dfefa935 | 2883 | error("unable to lock %s for update", newrefname); |
c976d415 LH |
2884 | goto rollback; |
2885 | } | |
c976d415 LH |
2886 | lock->force_write = 1; |
2887 | hashcpy(lock->old_sha1, orig_sha1); | |
678d0f4c | 2888 | if (write_ref_sha1(lock, orig_sha1, logmsg)) { |
dfefa935 | 2889 | error("unable to write current sha1 into %s", newrefname); |
c976d415 LH |
2890 | goto rollback; |
2891 | } | |
2892 | ||
2893 | return 0; | |
2894 | ||
2895 | rollback: | |
5fe7d825 | 2896 | lock = lock_ref_sha1_basic(oldrefname, NULL, NULL, 0, NULL); |
c976d415 | 2897 | if (!lock) { |
dfefa935 | 2898 | error("unable to lock %s for rollback", oldrefname); |
c976d415 LH |
2899 | goto rollbacklog; |
2900 | } | |
2901 | ||
2902 | lock->force_write = 1; | |
2903 | flag = log_all_ref_updates; | |
2904 | log_all_ref_updates = 0; | |
2905 | if (write_ref_sha1(lock, orig_sha1, NULL)) | |
dfefa935 | 2906 | error("unable to write current sha1 into %s", oldrefname); |
c976d415 LH |
2907 | log_all_ref_updates = flag; |
2908 | ||
2909 | rollbacklog: | |
dfefa935 | 2910 | if (logmoved && rename(git_path("logs/%s", newrefname), git_path("logs/%s", oldrefname))) |
c976d415 | 2911 | error("unable to restore logfile %s from %s: %s", |
dfefa935 | 2912 | oldrefname, newrefname, strerror(errno)); |
c976d415 | 2913 | if (!logmoved && log && |
dfefa935 | 2914 | rename(git_path(TMP_RENAMED_LOG), git_path("logs/%s", oldrefname))) |
765c2258 | 2915 | error("unable to restore logfile %s from "TMP_RENAMED_LOG": %s", |
dfefa935 | 2916 | oldrefname, strerror(errno)); |
c976d415 LH |
2917 | |
2918 | return 1; | |
2919 | } | |
2920 | ||
0b1e6548 | 2921 | static int close_ref(struct ref_lock *lock) |
b531394d BC |
2922 | { |
2923 | if (close_lock_file(lock->lk)) | |
2924 | return -1; | |
2925 | lock->lock_fd = -1; | |
2926 | return 0; | |
2927 | } | |
2928 | ||
0b1e6548 | 2929 | static int commit_ref(struct ref_lock *lock) |
b531394d BC |
2930 | { |
2931 | if (commit_lock_file(lock->lk)) | |
2932 | return -1; | |
2933 | lock->lock_fd = -1; | |
2934 | return 0; | |
2935 | } | |
2936 | ||
0ec29a47 JH |
2937 | /* |
2938 | * copy the reflog message msg to buf, which has been allocated sufficiently | |
2939 | * large, while cleaning up the whitespaces. Especially, convert LF to space, | |
2940 | * because reflog file is one line per entry. | |
2941 | */ | |
2942 | static int copy_msg(char *buf, const char *msg) | |
2943 | { | |
2944 | char *cp = buf; | |
2945 | char c; | |
2946 | int wasspace = 1; | |
2947 | ||
2948 | *cp++ = '\t'; | |
2949 | while ((c = *msg++)) { | |
2950 | if (wasspace && isspace(c)) | |
2951 | continue; | |
2952 | wasspace = isspace(c); | |
2953 | if (wasspace) | |
2954 | c = ' '; | |
2955 | *cp++ = c; | |
2956 | } | |
2957 | while (buf < cp && isspace(cp[-1])) | |
2958 | cp--; | |
2959 | *cp++ = '\n'; | |
2960 | return cp - buf; | |
2961 | } | |
2962 | ||
bd3b02da | 2963 | /* This function must set a meaningful errno on failure */ |
dfefa935 | 2964 | int log_ref_setup(const char *refname, char *logfile, int bufsize) |
6de08ae6 | 2965 | { |
859c3017 | 2966 | int logfd, oflags = O_APPEND | O_WRONLY; |
9a13f0b7 | 2967 | |
dfefa935 | 2968 | git_snpath(logfile, bufsize, "logs/%s", refname); |
4057deb5 | 2969 | if (log_all_ref_updates && |
59556548 CC |
2970 | (starts_with(refname, "refs/heads/") || |
2971 | starts_with(refname, "refs/remotes/") || | |
2972 | starts_with(refname, "refs/notes/") || | |
dfefa935 | 2973 | !strcmp(refname, "HEAD"))) { |
bd3b02da RS |
2974 | if (safe_create_leading_directories(logfile) < 0) { |
2975 | int save_errno = errno; | |
2976 | error("unable to create directory for %s", logfile); | |
2977 | errno = save_errno; | |
2978 | return -1; | |
2979 | } | |
6de08ae6 SP |
2980 | oflags |= O_CREAT; |
2981 | } | |
2982 | ||
157aaea5 | 2983 | logfd = open(logfile, oflags, 0666); |
6de08ae6 | 2984 | if (logfd < 0) { |
9233887c | 2985 | if (!(oflags & O_CREAT) && (errno == ENOENT || errno == EISDIR)) |
6de08ae6 | 2986 | return 0; |
3b463c3f | 2987 | |
9233887c | 2988 | if (errno == EISDIR) { |
157aaea5 | 2989 | if (remove_empty_directories(logfile)) { |
bd3b02da RS |
2990 | int save_errno = errno; |
2991 | error("There are still logs under '%s'", | |
2992 | logfile); | |
2993 | errno = save_errno; | |
2994 | return -1; | |
3b463c3f | 2995 | } |
157aaea5 | 2996 | logfd = open(logfile, oflags, 0666); |
3b463c3f JH |
2997 | } |
2998 | ||
bd3b02da RS |
2999 | if (logfd < 0) { |
3000 | int save_errno = errno; | |
3001 | error("Unable to append to %s: %s", logfile, | |
3002 | strerror(errno)); | |
3003 | errno = save_errno; | |
3004 | return -1; | |
3005 | } | |
6de08ae6 SP |
3006 | } |
3007 | ||
157aaea5 | 3008 | adjust_shared_perm(logfile); |
859c3017 EM |
3009 | close(logfd); |
3010 | return 0; | |
3011 | } | |
443b92b6 | 3012 | |
2c6207ab RS |
3013 | static int log_ref_write_fd(int fd, const unsigned char *old_sha1, |
3014 | const unsigned char *new_sha1, | |
3015 | const char *committer, const char *msg) | |
3016 | { | |
3017 | int msglen, written; | |
3018 | unsigned maxlen, len; | |
3019 | char *logrec; | |
3020 | ||
3021 | msglen = msg ? strlen(msg) : 0; | |
3022 | maxlen = strlen(committer) + msglen + 100; | |
3023 | logrec = xmalloc(maxlen); | |
3024 | len = sprintf(logrec, "%s %s %s\n", | |
3025 | sha1_to_hex(old_sha1), | |
3026 | sha1_to_hex(new_sha1), | |
3027 | committer); | |
3028 | if (msglen) | |
3029 | len += copy_msg(logrec + len - 1, msg) - 1; | |
3030 | ||
3031 | written = len <= maxlen ? write_in_full(fd, logrec, len) : -1; | |
3032 | free(logrec); | |
3033 | if (written != len) | |
3034 | return -1; | |
3035 | ||
3036 | return 0; | |
3037 | } | |
3038 | ||
dfefa935 | 3039 | static int log_ref_write(const char *refname, const unsigned char *old_sha1, |
859c3017 EM |
3040 | const unsigned char *new_sha1, const char *msg) |
3041 | { | |
2c6207ab | 3042 | int logfd, result, oflags = O_APPEND | O_WRONLY; |
157aaea5 | 3043 | char log_file[PATH_MAX]; |
859c3017 EM |
3044 | |
3045 | if (log_all_ref_updates < 0) | |
3046 | log_all_ref_updates = !is_bare_repository(); | |
3047 | ||
dfefa935 | 3048 | result = log_ref_setup(refname, log_file, sizeof(log_file)); |
859c3017 EM |
3049 | if (result) |
3050 | return result; | |
3051 | ||
3052 | logfd = open(log_file, oflags); | |
3053 | if (logfd < 0) | |
3054 | return 0; | |
2c6207ab RS |
3055 | result = log_ref_write_fd(logfd, old_sha1, new_sha1, |
3056 | git_committer_info(0), msg); | |
3057 | if (result) { | |
dc615de8 RS |
3058 | int save_errno = errno; |
3059 | close(logfd); | |
3060 | error("Unable to append to %s", log_file); | |
3061 | errno = save_errno; | |
3062 | return -1; | |
3063 | } | |
3064 | if (close(logfd)) { | |
3065 | int save_errno = errno; | |
3066 | error("Unable to append to %s", log_file); | |
3067 | errno = save_errno; | |
3068 | return -1; | |
3069 | } | |
6de08ae6 SP |
3070 | return 0; |
3071 | } | |
3072 | ||
e7e0f26e | 3073 | int is_branch(const char *refname) |
c3b0dec5 | 3074 | { |
59556548 | 3075 | return !strcmp(refname, "HEAD") || starts_with(refname, "refs/heads/"); |
c3b0dec5 LT |
3076 | } |
3077 | ||
aae383db RS |
3078 | /* |
3079 | * Write sha1 into the ref specified by the lock. Make sure that errno | |
3080 | * is sane on error. | |
3081 | */ | |
3082 | static int write_ref_sha1(struct ref_lock *lock, | |
4bd18c43 SP |
3083 | const unsigned char *sha1, const char *logmsg) |
3084 | { | |
3085 | static char term = '\n'; | |
c3b0dec5 | 3086 | struct object *o; |
4bd18c43 | 3087 | |
dc615de8 RS |
3088 | if (!lock) { |
3089 | errno = EINVAL; | |
95fc7512 | 3090 | return -1; |
dc615de8 | 3091 | } |
a89fccd2 | 3092 | if (!lock->force_write && !hashcmp(lock->old_sha1, sha1)) { |
4bd18c43 SP |
3093 | unlock_ref(lock); |
3094 | return 0; | |
95fc7512 | 3095 | } |
c3b0dec5 LT |
3096 | o = parse_object(sha1); |
3097 | if (!o) { | |
7be8b3ba | 3098 | error("Trying to write ref %s with nonexistent object %s", |
c3b0dec5 LT |
3099 | lock->ref_name, sha1_to_hex(sha1)); |
3100 | unlock_ref(lock); | |
dc615de8 | 3101 | errno = EINVAL; |
c3b0dec5 LT |
3102 | return -1; |
3103 | } | |
3104 | if (o->type != OBJ_COMMIT && is_branch(lock->ref_name)) { | |
3105 | error("Trying to write non-commit object %s to branch %s", | |
3106 | sha1_to_hex(sha1), lock->ref_name); | |
3107 | unlock_ref(lock); | |
dc615de8 | 3108 | errno = EINVAL; |
c3b0dec5 LT |
3109 | return -1; |
3110 | } | |
93822c22 | 3111 | if (write_in_full(lock->lock_fd, sha1_to_hex(sha1), 40) != 40 || |
dc615de8 RS |
3112 | write_in_full(lock->lock_fd, &term, 1) != 1 || |
3113 | close_ref(lock) < 0) { | |
3114 | int save_errno = errno; | |
cf6950d3 | 3115 | error("Couldn't write %s", lock->lk->filename.buf); |
4bd18c43 | 3116 | unlock_ref(lock); |
dc615de8 | 3117 | errno = save_errno; |
4bd18c43 SP |
3118 | return -1; |
3119 | } | |
9da31cb0 | 3120 | clear_loose_ref_cache(&ref_cache); |
bd104db1 NP |
3121 | if (log_ref_write(lock->ref_name, lock->old_sha1, sha1, logmsg) < 0 || |
3122 | (strcmp(lock->ref_name, lock->orig_ref_name) && | |
3123 | log_ref_write(lock->orig_ref_name, lock->old_sha1, sha1, logmsg) < 0)) { | |
6de08ae6 SP |
3124 | unlock_ref(lock); |
3125 | return -1; | |
3126 | } | |
605fac8b NP |
3127 | if (strcmp(lock->orig_ref_name, "HEAD") != 0) { |
3128 | /* | |
3129 | * Special hack: If a branch is updated directly and HEAD | |
3130 | * points to it (may happen on the remote side of a push | |
3131 | * for example) then logically the HEAD reflog should be | |
3132 | * updated too. | |
3133 | * A generic solution implies reverse symref information, | |
3134 | * but finding all symrefs pointing to the given branch | |
3135 | * would be rather costly for this rare event (the direct | |
3136 | * update of a branch) to be worth it. So let's cheat and | |
3137 | * check with HEAD only which should cover 99% of all usage | |
3138 | * scenarios (even 100% of the default ones). | |
3139 | */ | |
3140 | unsigned char head_sha1[20]; | |
3141 | int head_flag; | |
3142 | const char *head_ref; | |
7695d118 RS |
3143 | head_ref = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, |
3144 | head_sha1, &head_flag); | |
605fac8b NP |
3145 | if (head_ref && (head_flag & REF_ISSYMREF) && |
3146 | !strcmp(head_ref, lock->ref_name)) | |
3147 | log_ref_write("HEAD", lock->old_sha1, sha1, logmsg); | |
3148 | } | |
b531394d | 3149 | if (commit_ref(lock)) { |
434cd0cd | 3150 | error("Couldn't set %s", lock->ref_name); |
4bd18c43 SP |
3151 | unlock_ref(lock); |
3152 | return -1; | |
3153 | } | |
4bd18c43 SP |
3154 | unlock_ref(lock); |
3155 | return 0; | |
95fc7512 | 3156 | } |
d556fae2 | 3157 | |
8b5157e4 NP |
3158 | int create_symref(const char *ref_target, const char *refs_heads_master, |
3159 | const char *logmsg) | |
41b625b0 NP |
3160 | { |
3161 | const char *lockpath; | |
3162 | char ref[1000]; | |
3163 | int fd, len, written; | |
a4f34cbb | 3164 | char *git_HEAD = git_pathdup("%s", ref_target); |
8b5157e4 NP |
3165 | unsigned char old_sha1[20], new_sha1[20]; |
3166 | ||
3167 | if (logmsg && read_ref(ref_target, old_sha1)) | |
3168 | hashclr(old_sha1); | |
41b625b0 | 3169 | |
d48744d1 JH |
3170 | if (safe_create_leading_directories(git_HEAD) < 0) |
3171 | return error("unable to create directory for %s", git_HEAD); | |
3172 | ||
41b625b0 NP |
3173 | #ifndef NO_SYMLINK_HEAD |
3174 | if (prefer_symlink_refs) { | |
3175 | unlink(git_HEAD); | |
3176 | if (!symlink(refs_heads_master, git_HEAD)) | |
8b5157e4 | 3177 | goto done; |
41b625b0 NP |
3178 | fprintf(stderr, "no symlink - falling back to symbolic ref\n"); |
3179 | } | |
3180 | #endif | |
3181 | ||
3182 | len = snprintf(ref, sizeof(ref), "ref: %s\n", refs_heads_master); | |
3183 | if (sizeof(ref) <= len) { | |
3184 | error("refname too long: %s", refs_heads_master); | |
47fc52e2 | 3185 | goto error_free_return; |
41b625b0 NP |
3186 | } |
3187 | lockpath = mkpath("%s.lock", git_HEAD); | |
3188 | fd = open(lockpath, O_CREAT | O_EXCL | O_WRONLY, 0666); | |
3189 | if (fd < 0) { | |
3190 | error("Unable to open %s for writing", lockpath); | |
47fc52e2 | 3191 | goto error_free_return; |
41b625b0 NP |
3192 | } |
3193 | written = write_in_full(fd, ref, len); | |
91c8d590 | 3194 | if (close(fd) != 0 || written != len) { |
41b625b0 | 3195 | error("Unable to write to %s", lockpath); |
47fc52e2 | 3196 | goto error_unlink_return; |
41b625b0 NP |
3197 | } |
3198 | if (rename(lockpath, git_HEAD) < 0) { | |
41b625b0 | 3199 | error("Unable to create %s", git_HEAD); |
47fc52e2 | 3200 | goto error_unlink_return; |
41b625b0 NP |
3201 | } |
3202 | if (adjust_shared_perm(git_HEAD)) { | |
41b625b0 | 3203 | error("Unable to fix permissions on %s", lockpath); |
47fc52e2 | 3204 | error_unlink_return: |
691f1a28 | 3205 | unlink_or_warn(lockpath); |
47fc52e2 JH |
3206 | error_free_return: |
3207 | free(git_HEAD); | |
3208 | return -1; | |
41b625b0 | 3209 | } |
8b5157e4 | 3210 | |
ee96d11b | 3211 | #ifndef NO_SYMLINK_HEAD |
8b5157e4 | 3212 | done: |
ee96d11b | 3213 | #endif |
8b5157e4 NP |
3214 | if (logmsg && !read_ref(refs_heads_master, new_sha1)) |
3215 | log_ref_write(ref_target, old_sha1, new_sha1, logmsg); | |
3216 | ||
47fc52e2 | 3217 | free(git_HEAD); |
41b625b0 NP |
3218 | return 0; |
3219 | } | |
3220 | ||
4207ed28 RS |
3221 | struct read_ref_at_cb { |
3222 | const char *refname; | |
3223 | unsigned long at_time; | |
3224 | int cnt; | |
3225 | int reccnt; | |
3226 | unsigned char *sha1; | |
3227 | int found_it; | |
3228 | ||
3229 | unsigned char osha1[20]; | |
3230 | unsigned char nsha1[20]; | |
3231 | int tz; | |
3232 | unsigned long date; | |
3233 | char **msg; | |
3234 | unsigned long *cutoff_time; | |
3235 | int *cutoff_tz; | |
3236 | int *cutoff_cnt; | |
3237 | }; | |
3238 | ||
3239 | static int read_ref_at_ent(unsigned char *osha1, unsigned char *nsha1, | |
3240 | const char *email, unsigned long timestamp, int tz, | |
3241 | const char *message, void *cb_data) | |
3242 | { | |
3243 | struct read_ref_at_cb *cb = cb_data; | |
3244 | ||
3245 | cb->reccnt++; | |
3246 | cb->tz = tz; | |
3247 | cb->date = timestamp; | |
3248 | ||
3249 | if (timestamp <= cb->at_time || cb->cnt == 0) { | |
3250 | if (cb->msg) | |
3251 | *cb->msg = xstrdup(message); | |
3252 | if (cb->cutoff_time) | |
3253 | *cb->cutoff_time = timestamp; | |
3254 | if (cb->cutoff_tz) | |
3255 | *cb->cutoff_tz = tz; | |
3256 | if (cb->cutoff_cnt) | |
3257 | *cb->cutoff_cnt = cb->reccnt - 1; | |
3258 | /* | |
3259 | * we have not yet updated cb->[n|o]sha1 so they still | |
3260 | * hold the values for the previous record. | |
3261 | */ | |
3262 | if (!is_null_sha1(cb->osha1)) { | |
3263 | hashcpy(cb->sha1, nsha1); | |
3264 | if (hashcmp(cb->osha1, nsha1)) | |
3265 | warning("Log for ref %s has gap after %s.", | |
3266 | cb->refname, show_date(cb->date, cb->tz, DATE_RFC2822)); | |
3267 | } | |
3268 | else if (cb->date == cb->at_time) | |
3269 | hashcpy(cb->sha1, nsha1); | |
3270 | else if (hashcmp(nsha1, cb->sha1)) | |
3271 | warning("Log for ref %s unexpectedly ended on %s.", | |
3272 | cb->refname, show_date(cb->date, cb->tz, | |
3273 | DATE_RFC2822)); | |
3274 | hashcpy(cb->osha1, osha1); | |
3275 | hashcpy(cb->nsha1, nsha1); | |
3276 | cb->found_it = 1; | |
3277 | return 1; | |
3278 | } | |
3279 | hashcpy(cb->osha1, osha1); | |
3280 | hashcpy(cb->nsha1, nsha1); | |
3281 | if (cb->cnt > 0) | |
3282 | cb->cnt--; | |
3283 | return 0; | |
3284 | } | |
3285 | ||
3286 | static int read_ref_at_ent_oldest(unsigned char *osha1, unsigned char *nsha1, | |
3287 | const char *email, unsigned long timestamp, | |
3288 | int tz, const char *message, void *cb_data) | |
3289 | { | |
3290 | struct read_ref_at_cb *cb = cb_data; | |
3291 | ||
3292 | if (cb->msg) | |
3293 | *cb->msg = xstrdup(message); | |
3294 | if (cb->cutoff_time) | |
3295 | *cb->cutoff_time = timestamp; | |
3296 | if (cb->cutoff_tz) | |
3297 | *cb->cutoff_tz = tz; | |
3298 | if (cb->cutoff_cnt) | |
3299 | *cb->cutoff_cnt = cb->reccnt; | |
3300 | hashcpy(cb->sha1, osha1); | |
3301 | if (is_null_sha1(cb->sha1)) | |
3302 | hashcpy(cb->sha1, nsha1); | |
3303 | /* We just want the first entry */ | |
3304 | return 1; | |
16d7cc90 JH |
3305 | } |
3306 | ||
c41a87dd | 3307 | int read_ref_at(const char *refname, unsigned int flags, unsigned long at_time, int cnt, |
dfefa935 MH |
3308 | unsigned char *sha1, char **msg, |
3309 | unsigned long *cutoff_time, int *cutoff_tz, int *cutoff_cnt) | |
d556fae2 | 3310 | { |
4207ed28 | 3311 | struct read_ref_at_cb cb; |
d556fae2 | 3312 | |
4207ed28 RS |
3313 | memset(&cb, 0, sizeof(cb)); |
3314 | cb.refname = refname; | |
3315 | cb.at_time = at_time; | |
3316 | cb.cnt = cnt; | |
3317 | cb.msg = msg; | |
3318 | cb.cutoff_time = cutoff_time; | |
3319 | cb.cutoff_tz = cutoff_tz; | |
3320 | cb.cutoff_cnt = cutoff_cnt; | |
3321 | cb.sha1 = sha1; | |
3322 | ||
3323 | for_each_reflog_ent_reverse(refname, read_ref_at_ent, &cb); | |
3324 | ||
c41a87dd DA |
3325 | if (!cb.reccnt) { |
3326 | if (flags & GET_SHA1_QUIETLY) | |
3327 | exit(128); | |
3328 | else | |
3329 | die("Log for %s is empty.", refname); | |
3330 | } | |
4207ed28 RS |
3331 | if (cb.found_it) |
3332 | return 0; | |
3333 | ||
3334 | for_each_reflog_ent(refname, read_ref_at_ent_oldest, &cb); | |
d556fae2 | 3335 | |
16d7cc90 | 3336 | return 1; |
d556fae2 | 3337 | } |
2ff81662 | 3338 | |
4da58835 RS |
3339 | int reflog_exists(const char *refname) |
3340 | { | |
3341 | struct stat st; | |
3342 | ||
3343 | return !lstat(git_path("logs/%s", refname), &st) && | |
3344 | S_ISREG(st.st_mode); | |
3345 | } | |
3346 | ||
3347 | int delete_reflog(const char *refname) | |
3348 | { | |
3349 | return remove_path(git_path("logs/%s", refname)); | |
3350 | } | |
3351 | ||
9a7a183b JH |
3352 | static int show_one_reflog_ent(struct strbuf *sb, each_reflog_ent_fn fn, void *cb_data) |
3353 | { | |
3354 | unsigned char osha1[20], nsha1[20]; | |
3355 | char *email_end, *message; | |
3356 | unsigned long timestamp; | |
3357 | int tz; | |
3358 | ||
3359 | /* old SP new SP name <email> SP time TAB msg LF */ | |
3360 | if (sb->len < 83 || sb->buf[sb->len - 1] != '\n' || | |
3361 | get_sha1_hex(sb->buf, osha1) || sb->buf[40] != ' ' || | |
3362 | get_sha1_hex(sb->buf + 41, nsha1) || sb->buf[81] != ' ' || | |
3363 | !(email_end = strchr(sb->buf + 82, '>')) || | |
3364 | email_end[1] != ' ' || | |
3365 | !(timestamp = strtoul(email_end + 2, &message, 10)) || | |
3366 | !message || message[0] != ' ' || | |
3367 | (message[1] != '+' && message[1] != '-') || | |
3368 | !isdigit(message[2]) || !isdigit(message[3]) || | |
3369 | !isdigit(message[4]) || !isdigit(message[5])) | |
3370 | return 0; /* corrupt? */ | |
3371 | email_end[1] = '\0'; | |
3372 | tz = strtol(message + 1, NULL, 10); | |
3373 | if (message[6] != '\t') | |
3374 | message += 6; | |
3375 | else | |
3376 | message += 7; | |
3377 | return fn(osha1, nsha1, sb->buf + 82, timestamp, tz, message, cb_data); | |
3378 | } | |
3379 | ||
98f85ff4 JH |
3380 | static char *find_beginning_of_line(char *bob, char *scan) |
3381 | { | |
3382 | while (bob < scan && *(--scan) != '\n') | |
3383 | ; /* keep scanning backwards */ | |
3384 | /* | |
3385 | * Return either beginning of the buffer, or LF at the end of | |
3386 | * the previous line. | |
3387 | */ | |
3388 | return scan; | |
3389 | } | |
3390 | ||
3391 | int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn, void *cb_data) | |
2ff81662 | 3392 | { |
8ca78803 | 3393 | struct strbuf sb = STRBUF_INIT; |
98f85ff4 JH |
3394 | FILE *logfp; |
3395 | long pos; | |
3396 | int ret = 0, at_tail = 1; | |
2ff81662 | 3397 | |
7ae07c1b | 3398 | logfp = fopen(git_path("logs/%s", refname), "r"); |
2ff81662 | 3399 | if (!logfp) |
883d60fa | 3400 | return -1; |
101d15e0 | 3401 | |
98f85ff4 JH |
3402 | /* Jump to the end */ |
3403 | if (fseek(logfp, 0, SEEK_END) < 0) | |
3404 | return error("cannot seek back reflog for %s: %s", | |
3405 | refname, strerror(errno)); | |
3406 | pos = ftell(logfp); | |
3407 | while (!ret && 0 < pos) { | |
3408 | int cnt; | |
3409 | size_t nread; | |
3410 | char buf[BUFSIZ]; | |
3411 | char *endp, *scanp; | |
3412 | ||
3413 | /* Fill next block from the end */ | |
3414 | cnt = (sizeof(buf) < pos) ? sizeof(buf) : pos; | |
3415 | if (fseek(logfp, pos - cnt, SEEK_SET)) | |
3416 | return error("cannot seek back reflog for %s: %s", | |
3417 | refname, strerror(errno)); | |
3418 | nread = fread(buf, cnt, 1, logfp); | |
e4ca819a | 3419 | if (nread != 1) |
98f85ff4 JH |
3420 | return error("cannot read %d bytes from reflog for %s: %s", |
3421 | cnt, refname, strerror(errno)); | |
3422 | pos -= cnt; | |
3423 | ||
3424 | scanp = endp = buf + cnt; | |
3425 | if (at_tail && scanp[-1] == '\n') | |
3426 | /* Looking at the final LF at the end of the file */ | |
3427 | scanp--; | |
3428 | at_tail = 0; | |
3429 | ||
3430 | while (buf < scanp) { | |
3431 | /* | |
3432 | * terminating LF of the previous line, or the beginning | |
3433 | * of the buffer. | |
3434 | */ | |
3435 | char *bp; | |
3436 | ||
3437 | bp = find_beginning_of_line(buf, scanp); | |
3438 | ||
e5e73ff2 | 3439 | if (*bp == '\n') { |
98f85ff4 | 3440 | /* |
e5e73ff2 JK |
3441 | * The newline is the end of the previous line, |
3442 | * so we know we have complete line starting | |
3443 | * at (bp + 1). Prefix it onto any prior data | |
3444 | * we collected for the line and process it. | |
98f85ff4 JH |
3445 | */ |
3446 | strbuf_splice(&sb, 0, 0, bp + 1, endp - (bp + 1)); | |
3447 | scanp = bp; | |
3448 | endp = bp + 1; | |
e5e73ff2 JK |
3449 | ret = show_one_reflog_ent(&sb, fn, cb_data); |
3450 | strbuf_reset(&sb); | |
3451 | if (ret) | |
3452 | break; | |
3453 | } else if (!pos) { | |
3454 | /* | |
3455 | * We are at the start of the buffer, and the | |
3456 | * start of the file; there is no previous | |
3457 | * line, and we have everything for this one. | |
3458 | * Process it, and we can end the loop. | |
3459 | */ | |
3460 | strbuf_splice(&sb, 0, 0, buf, endp - buf); | |
3461 | ret = show_one_reflog_ent(&sb, fn, cb_data); | |
3462 | strbuf_reset(&sb); | |
3463 | break; | |
98f85ff4 | 3464 | } |
e5e73ff2 JK |
3465 | |
3466 | if (bp == buf) { | |
3467 | /* | |
3468 | * We are at the start of the buffer, and there | |
3469 | * is more file to read backwards. Which means | |
3470 | * we are in the middle of a line. Note that we | |
3471 | * may get here even if *bp was a newline; that | |
3472 | * just means we are at the exact end of the | |
3473 | * previous line, rather than some spot in the | |
3474 | * middle. | |
3475 | * | |
3476 | * Save away what we have to be combined with | |
3477 | * the data from the next read. | |
3478 | */ | |
3479 | strbuf_splice(&sb, 0, 0, buf, endp - buf); | |
98f85ff4 | 3480 | break; |
e5e73ff2 | 3481 | } |
9d33f7c2 | 3482 | } |
101d15e0 | 3483 | |
2ff81662 | 3484 | } |
98f85ff4 | 3485 | if (!ret && sb.len) |
69216bf7 | 3486 | die("BUG: reverse reflog parser had leftover data"); |
98f85ff4 | 3487 | |
2ff81662 | 3488 | fclose(logfp); |
8ca78803 | 3489 | strbuf_release(&sb); |
2266bf27 | 3490 | return ret; |
2ff81662 | 3491 | } |
e29cb53a | 3492 | |
dfefa935 | 3493 | int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn, void *cb_data) |
101d15e0 | 3494 | { |
98f85ff4 JH |
3495 | FILE *logfp; |
3496 | struct strbuf sb = STRBUF_INIT; | |
3497 | int ret = 0; | |
3498 | ||
3499 | logfp = fopen(git_path("logs/%s", refname), "r"); | |
3500 | if (!logfp) | |
3501 | return -1; | |
101d15e0 | 3502 | |
98f85ff4 JH |
3503 | while (!ret && !strbuf_getwholeline(&sb, logfp, '\n')) |
3504 | ret = show_one_reflog_ent(&sb, fn, cb_data); | |
3505 | fclose(logfp); | |
3506 | strbuf_release(&sb); | |
3507 | return ret; | |
3508 | } | |
989c0e5d MH |
3509 | /* |
3510 | * Call fn for each reflog in the namespace indicated by name. name | |
3511 | * must be empty or end with '/'. Name will be used as a scratch | |
3512 | * space, but its contents will be restored before return. | |
3513 | */ | |
3514 | static int do_for_each_reflog(struct strbuf *name, each_ref_fn fn, void *cb_data) | |
eb8381c8 | 3515 | { |
989c0e5d | 3516 | DIR *d = opendir(git_path("logs/%s", name->buf)); |
fcee5a14 | 3517 | int retval = 0; |
93c603fc | 3518 | struct dirent *de; |
989c0e5d | 3519 | int oldlen = name->len; |
eb8381c8 | 3520 | |
93c603fc | 3521 | if (!d) |
989c0e5d | 3522 | return name->len ? errno : 0; |
eb8381c8 | 3523 | |
93c603fc MH |
3524 | while ((de = readdir(d)) != NULL) { |
3525 | struct stat st; | |
eb8381c8 | 3526 | |
93c603fc MH |
3527 | if (de->d_name[0] == '.') |
3528 | continue; | |
2975c770 | 3529 | if (ends_with(de->d_name, ".lock")) |
93c603fc | 3530 | continue; |
989c0e5d MH |
3531 | strbuf_addstr(name, de->d_name); |
3532 | if (stat(git_path("logs/%s", name->buf), &st) < 0) { | |
3533 | ; /* silently ignore */ | |
93c603fc | 3534 | } else { |
eb8381c8 | 3535 | if (S_ISDIR(st.st_mode)) { |
989c0e5d MH |
3536 | strbuf_addch(name, '/'); |
3537 | retval = do_for_each_reflog(name, fn, cb_data); | |
eb8381c8 NP |
3538 | } else { |
3539 | unsigned char sha1[20]; | |
7695d118 | 3540 | if (read_ref_full(name->buf, 0, sha1, NULL)) |
989c0e5d | 3541 | retval = error("bad ref for %s", name->buf); |
eb8381c8 | 3542 | else |
989c0e5d | 3543 | retval = fn(name->buf, sha1, 0, cb_data); |
eb8381c8 NP |
3544 | } |
3545 | if (retval) | |
3546 | break; | |
3547 | } | |
989c0e5d | 3548 | strbuf_setlen(name, oldlen); |
eb8381c8 | 3549 | } |
93c603fc | 3550 | closedir(d); |
eb8381c8 NP |
3551 | return retval; |
3552 | } | |
3553 | ||
3554 | int for_each_reflog(each_ref_fn fn, void *cb_data) | |
3555 | { | |
989c0e5d MH |
3556 | int retval; |
3557 | struct strbuf name; | |
3558 | strbuf_init(&name, PATH_MAX); | |
3559 | retval = do_for_each_reflog(&name, fn, cb_data); | |
3560 | strbuf_release(&name); | |
3561 | return retval; | |
eb8381c8 | 3562 | } |
3d9f037c | 3563 | |
b5c8ea2a MH |
3564 | /** |
3565 | * Information needed for a single ref update. Set new_sha1 to the | |
3566 | * new value or to zero to delete the ref. To check the old value | |
3567 | * while locking the ref, set have_old to 1 and set old_sha1 to the | |
3568 | * value or to zero to ensure the ref does not exist before update. | |
3569 | */ | |
3570 | struct ref_update { | |
b5c8ea2a MH |
3571 | unsigned char new_sha1[20]; |
3572 | unsigned char old_sha1[20]; | |
3573 | int flags; /* REF_NODEREF? */ | |
3574 | int have_old; /* 1 if old_sha1 is valid, 0 otherwise */ | |
81c960e4 | 3575 | struct ref_lock *lock; |
84178db7 | 3576 | int type; |
db7516ab | 3577 | char *msg; |
88615910 | 3578 | const char refname[FLEX_ARRAY]; |
b5c8ea2a MH |
3579 | }; |
3580 | ||
2bdc785f RS |
3581 | /* |
3582 | * Transaction states. | |
3583 | * OPEN: The transaction is in a valid state and can accept new updates. | |
3584 | * An OPEN transaction can be committed. | |
3585 | * CLOSED: A closed transaction is no longer active and no other operations | |
3586 | * than free can be used on it in this state. | |
3587 | * A transaction can either become closed by successfully committing | |
3588 | * an active transaction or if there is a failure while building | |
3589 | * the transaction thus rendering it failed/inactive. | |
3590 | */ | |
3591 | enum ref_transaction_state { | |
3592 | REF_TRANSACTION_OPEN = 0, | |
3593 | REF_TRANSACTION_CLOSED = 1 | |
3594 | }; | |
3595 | ||
caa4046c MH |
3596 | /* |
3597 | * Data structure for holding a reference transaction, which can | |
3598 | * consist of checks and updates to multiple references, carried out | |
3599 | * as atomically as possible. This structure is opaque to callers. | |
3600 | */ | |
3601 | struct ref_transaction { | |
3602 | struct ref_update **updates; | |
3603 | size_t alloc; | |
3604 | size_t nr; | |
2bdc785f | 3605 | enum ref_transaction_state state; |
caa4046c MH |
3606 | }; |
3607 | ||
93a644ea | 3608 | struct ref_transaction *ref_transaction_begin(struct strbuf *err) |
caa4046c | 3609 | { |
5a603b04 JN |
3610 | assert(err); |
3611 | ||
caa4046c MH |
3612 | return xcalloc(1, sizeof(struct ref_transaction)); |
3613 | } | |
3614 | ||
026bd1d3 | 3615 | void ref_transaction_free(struct ref_transaction *transaction) |
caa4046c MH |
3616 | { |
3617 | int i; | |
3618 | ||
1b07255c RS |
3619 | if (!transaction) |
3620 | return; | |
3621 | ||
db7516ab RS |
3622 | for (i = 0; i < transaction->nr; i++) { |
3623 | free(transaction->updates[i]->msg); | |
88615910 | 3624 | free(transaction->updates[i]); |
db7516ab | 3625 | } |
caa4046c MH |
3626 | free(transaction->updates); |
3627 | free(transaction); | |
3628 | } | |
3629 | ||
caa4046c MH |
3630 | static struct ref_update *add_update(struct ref_transaction *transaction, |
3631 | const char *refname) | |
3632 | { | |
88615910 MH |
3633 | size_t len = strlen(refname); |
3634 | struct ref_update *update = xcalloc(1, sizeof(*update) + len + 1); | |
caa4046c | 3635 | |
88615910 | 3636 | strcpy((char *)update->refname, refname); |
caa4046c MH |
3637 | ALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc); |
3638 | transaction->updates[transaction->nr++] = update; | |
3639 | return update; | |
3640 | } | |
3641 | ||
8e34800e RS |
3642 | int ref_transaction_update(struct ref_transaction *transaction, |
3643 | const char *refname, | |
3644 | const unsigned char *new_sha1, | |
3645 | const unsigned char *old_sha1, | |
db7516ab | 3646 | int flags, int have_old, const char *msg, |
8e34800e | 3647 | struct strbuf *err) |
caa4046c | 3648 | { |
8e34800e | 3649 | struct ref_update *update; |
caa4046c | 3650 | |
5a603b04 JN |
3651 | assert(err); |
3652 | ||
2bdc785f RS |
3653 | if (transaction->state != REF_TRANSACTION_OPEN) |
3654 | die("BUG: update called for transaction that is not open"); | |
3655 | ||
8e34800e RS |
3656 | if (have_old && !old_sha1) |
3657 | die("BUG: have_old is true but old_sha1 is NULL"); | |
caa4046c | 3658 | |
d0f810f0 RS |
3659 | if (!is_null_sha1(new_sha1) && |
3660 | check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) { | |
3661 | strbuf_addf(err, "refusing to update ref with bad name %s", | |
3662 | refname); | |
3663 | return -1; | |
3664 | } | |
3665 | ||
8e34800e | 3666 | update = add_update(transaction, refname); |
caa4046c MH |
3667 | hashcpy(update->new_sha1, new_sha1); |
3668 | update->flags = flags; | |
3669 | update->have_old = have_old; | |
3670 | if (have_old) | |
3671 | hashcpy(update->old_sha1, old_sha1); | |
db7516ab RS |
3672 | if (msg) |
3673 | update->msg = xstrdup(msg); | |
8e34800e | 3674 | return 0; |
caa4046c MH |
3675 | } |
3676 | ||
b416af5b RS |
3677 | int ref_transaction_create(struct ref_transaction *transaction, |
3678 | const char *refname, | |
3679 | const unsigned char *new_sha1, | |
db7516ab | 3680 | int flags, const char *msg, |
b416af5b | 3681 | struct strbuf *err) |
caa4046c | 3682 | { |
bc9f2925 RS |
3683 | return ref_transaction_update(transaction, refname, new_sha1, |
3684 | null_sha1, flags, 1, msg, err); | |
caa4046c MH |
3685 | } |
3686 | ||
8c8bdc0d RS |
3687 | int ref_transaction_delete(struct ref_transaction *transaction, |
3688 | const char *refname, | |
3689 | const unsigned char *old_sha1, | |
db7516ab | 3690 | int flags, int have_old, const char *msg, |
8c8bdc0d | 3691 | struct strbuf *err) |
caa4046c | 3692 | { |
a785d3f7 RS |
3693 | return ref_transaction_update(transaction, refname, null_sha1, |
3694 | old_sha1, flags, have_old, msg, err); | |
caa4046c MH |
3695 | } |
3696 | ||
4738a333 BK |
3697 | int update_ref(const char *action, const char *refname, |
3698 | const unsigned char *sha1, const unsigned char *oldval, | |
3699 | int flags, enum action_on_err onerr) | |
3700 | { | |
b4d75ac1 RS |
3701 | struct ref_transaction *t; |
3702 | struct strbuf err = STRBUF_INIT; | |
3703 | ||
3704 | t = ref_transaction_begin(&err); | |
3705 | if (!t || | |
3706 | ref_transaction_update(t, refname, sha1, oldval, flags, | |
db7516ab RS |
3707 | !!oldval, action, &err) || |
3708 | ref_transaction_commit(t, &err)) { | |
b4d75ac1 RS |
3709 | const char *str = "update_ref failed for ref '%s': %s"; |
3710 | ||
3711 | ref_transaction_free(t); | |
3712 | switch (onerr) { | |
3713 | case UPDATE_REFS_MSG_ON_ERR: | |
3714 | error(str, refname, err.buf); | |
3715 | break; | |
3716 | case UPDATE_REFS_DIE_ON_ERR: | |
3717 | die(str, refname, err.buf); | |
3718 | break; | |
3719 | case UPDATE_REFS_QUIET_ON_ERR: | |
3720 | break; | |
3721 | } | |
3722 | strbuf_release(&err); | |
4738a333 | 3723 | return 1; |
b4d75ac1 RS |
3724 | } |
3725 | strbuf_release(&err); | |
3726 | ref_transaction_free(t); | |
3727 | return 0; | |
4738a333 BK |
3728 | } |
3729 | ||
98aee92d BK |
3730 | static int ref_update_compare(const void *r1, const void *r2) |
3731 | { | |
3732 | const struct ref_update * const *u1 = r1; | |
3733 | const struct ref_update * const *u2 = r2; | |
5524e241 | 3734 | return strcmp((*u1)->refname, (*u2)->refname); |
98aee92d BK |
3735 | } |
3736 | ||
3737 | static int ref_update_reject_duplicates(struct ref_update **updates, int n, | |
01319837 | 3738 | struct strbuf *err) |
98aee92d BK |
3739 | { |
3740 | int i; | |
5a603b04 JN |
3741 | |
3742 | assert(err); | |
3743 | ||
98aee92d | 3744 | for (i = 1; i < n; i++) |
5524e241 | 3745 | if (!strcmp(updates[i - 1]->refname, updates[i]->refname)) { |
5a603b04 JN |
3746 | strbuf_addf(err, |
3747 | "Multiple updates for ref '%s' not allowed.", | |
3748 | updates[i]->refname); | |
98aee92d BK |
3749 | return 1; |
3750 | } | |
3751 | return 0; | |
3752 | } | |
3753 | ||
b5c8ea2a | 3754 | int ref_transaction_commit(struct ref_transaction *transaction, |
db7516ab | 3755 | struct strbuf *err) |
98aee92d | 3756 | { |
4a45b2f3 | 3757 | int ret = 0, i; |
b5c8ea2a | 3758 | int n = transaction->nr; |
6a402338 | 3759 | struct ref_update **updates = transaction->updates; |
4a45b2f3 MH |
3760 | struct string_list refs_to_delete = STRING_LIST_INIT_NODUP; |
3761 | struct string_list_item *ref_to_delete; | |
98aee92d | 3762 | |
5a603b04 JN |
3763 | assert(err); |
3764 | ||
2bdc785f RS |
3765 | if (transaction->state != REF_TRANSACTION_OPEN) |
3766 | die("BUG: commit called for transaction that is not open"); | |
3767 | ||
3768 | if (!n) { | |
3769 | transaction->state = REF_TRANSACTION_CLOSED; | |
98aee92d | 3770 | return 0; |
2bdc785f | 3771 | } |
98aee92d | 3772 | |
98aee92d | 3773 | /* Copy, sort, and reject duplicate refs */ |
98aee92d | 3774 | qsort(updates, n, sizeof(*updates), ref_update_compare); |
28e6a97e RS |
3775 | if (ref_update_reject_duplicates(updates, n, err)) { |
3776 | ret = TRANSACTION_GENERIC_ERROR; | |
98aee92d | 3777 | goto cleanup; |
28e6a97e | 3778 | } |
98aee92d BK |
3779 | |
3780 | /* Acquire all locks while verifying old values */ | |
3781 | for (i = 0; i < n; i++) { | |
cb198d21 | 3782 | struct ref_update *update = updates[i]; |
62a2d525 | 3783 | int flags = update->flags; |
cb198d21 | 3784 | |
62a2d525 JN |
3785 | if (is_null_sha1(update->new_sha1)) |
3786 | flags |= REF_DELETING; | |
5d94a1b0 RS |
3787 | update->lock = lock_ref_sha1_basic(update->refname, |
3788 | (update->have_old ? | |
3789 | update->old_sha1 : | |
3790 | NULL), | |
5fe7d825 | 3791 | NULL, |
62a2d525 | 3792 | flags, |
5d94a1b0 | 3793 | &update->type); |
81c960e4 | 3794 | if (!update->lock) { |
28e6a97e RS |
3795 | ret = (errno == ENOTDIR) |
3796 | ? TRANSACTION_NAME_CONFLICT | |
3797 | : TRANSACTION_GENERIC_ERROR; | |
5a603b04 JN |
3798 | strbuf_addf(err, "Cannot lock the ref '%s'.", |
3799 | update->refname); | |
98aee92d BK |
3800 | goto cleanup; |
3801 | } | |
3802 | } | |
3803 | ||
3804 | /* Perform updates first so live commits remain referenced */ | |
cb198d21 MH |
3805 | for (i = 0; i < n; i++) { |
3806 | struct ref_update *update = updates[i]; | |
3807 | ||
3808 | if (!is_null_sha1(update->new_sha1)) { | |
28e6a97e RS |
3809 | if (write_ref_sha1(update->lock, update->new_sha1, |
3810 | update->msg)) { | |
3811 | update->lock = NULL; /* freed by write_ref_sha1 */ | |
5a603b04 JN |
3812 | strbuf_addf(err, "Cannot update the ref '%s'.", |
3813 | update->refname); | |
28e6a97e | 3814 | ret = TRANSACTION_GENERIC_ERROR; |
98aee92d | 3815 | goto cleanup; |
04ad6223 | 3816 | } |
28e6a97e | 3817 | update->lock = NULL; /* freed by write_ref_sha1 */ |
98aee92d | 3818 | } |
cb198d21 | 3819 | } |
98aee92d BK |
3820 | |
3821 | /* Perform deletes now that updates are safely completed */ | |
81c960e4 MH |
3822 | for (i = 0; i < n; i++) { |
3823 | struct ref_update *update = updates[i]; | |
3824 | ||
3825 | if (update->lock) { | |
65732845 | 3826 | if (delete_ref_loose(update->lock, update->type, err)) { |
28e6a97e | 3827 | ret = TRANSACTION_GENERIC_ERROR; |
65732845 JN |
3828 | goto cleanup; |
3829 | } | |
28e6a97e | 3830 | |
029cdb4a | 3831 | if (!(update->flags & REF_ISPRUNING)) |
4a45b2f3 MH |
3832 | string_list_append(&refs_to_delete, |
3833 | update->lock->ref_name); | |
98aee92d | 3834 | } |
81c960e4 MH |
3835 | } |
3836 | ||
4a45b2f3 | 3837 | if (repack_without_refs(&refs_to_delete, err)) { |
28e6a97e | 3838 | ret = TRANSACTION_GENERIC_ERROR; |
65732845 JN |
3839 | goto cleanup; |
3840 | } | |
4a45b2f3 MH |
3841 | for_each_string_list_item(ref_to_delete, &refs_to_delete) |
3842 | unlink_or_warn(git_path("logs/%s", ref_to_delete->string)); | |
98aee92d BK |
3843 | clear_loose_ref_cache(&ref_cache); |
3844 | ||
3845 | cleanup: | |
2bdc785f RS |
3846 | transaction->state = REF_TRANSACTION_CLOSED; |
3847 | ||
98aee92d | 3848 | for (i = 0; i < n; i++) |
81c960e4 MH |
3849 | if (updates[i]->lock) |
3850 | unlock_ref(updates[i]->lock); | |
4a45b2f3 | 3851 | string_list_clear(&refs_to_delete, 0); |
caa4046c MH |
3852 | return ret; |
3853 | } | |
3854 | ||
dfefa935 | 3855 | char *shorten_unambiguous_ref(const char *refname, int strict) |
7c2b3029 JK |
3856 | { |
3857 | int i; | |
3858 | static char **scanf_fmts; | |
3859 | static int nr_rules; | |
3860 | char *short_name; | |
3861 | ||
7c2b3029 | 3862 | if (!nr_rules) { |
4346663a MH |
3863 | /* |
3864 | * Pre-generate scanf formats from ref_rev_parse_rules[]. | |
3865 | * Generate a format suitable for scanf from a | |
3866 | * ref_rev_parse_rules rule by interpolating "%s" at the | |
3867 | * location of the "%.*s". | |
3868 | */ | |
7c2b3029 | 3869 | size_t total_len = 0; |
84d5633f | 3870 | size_t offset = 0; |
7c2b3029 JK |
3871 | |
3872 | /* the rule list is NULL terminated, count them first */ | |
a4165851 | 3873 | for (nr_rules = 0; ref_rev_parse_rules[nr_rules]; nr_rules++) |
7902fe03 MH |
3874 | /* -2 for strlen("%.*s") - strlen("%s"); +1 for NUL */ |
3875 | total_len += strlen(ref_rev_parse_rules[nr_rules]) - 2 + 1; | |
7c2b3029 JK |
3876 | |
3877 | scanf_fmts = xmalloc(nr_rules * sizeof(char *) + total_len); | |
3878 | ||
84d5633f | 3879 | offset = 0; |
7c2b3029 | 3880 | for (i = 0; i < nr_rules; i++) { |
4346663a | 3881 | assert(offset < total_len); |
84d5633f | 3882 | scanf_fmts[i] = (char *)&scanf_fmts[nr_rules] + offset; |
4346663a MH |
3883 | offset += snprintf(scanf_fmts[i], total_len - offset, |
3884 | ref_rev_parse_rules[i], 2, "%s") + 1; | |
7c2b3029 JK |
3885 | } |
3886 | } | |
3887 | ||
3888 | /* bail out if there are no rules */ | |
3889 | if (!nr_rules) | |
dfefa935 | 3890 | return xstrdup(refname); |
7c2b3029 | 3891 | |
dfefa935 MH |
3892 | /* buffer for scanf result, at most refname must fit */ |
3893 | short_name = xstrdup(refname); | |
7c2b3029 JK |
3894 | |
3895 | /* skip first rule, it will always match */ | |
3896 | for (i = nr_rules - 1; i > 0 ; --i) { | |
3897 | int j; | |
6e7b3309 | 3898 | int rules_to_fail = i; |
7c2b3029 JK |
3899 | int short_name_len; |
3900 | ||
dfefa935 | 3901 | if (1 != sscanf(refname, scanf_fmts[i], short_name)) |
7c2b3029 JK |
3902 | continue; |
3903 | ||
3904 | short_name_len = strlen(short_name); | |
3905 | ||
6e7b3309 BW |
3906 | /* |
3907 | * in strict mode, all (except the matched one) rules | |
3908 | * must fail to resolve to a valid non-ambiguous ref | |
3909 | */ | |
3910 | if (strict) | |
3911 | rules_to_fail = nr_rules; | |
3912 | ||
7c2b3029 JK |
3913 | /* |
3914 | * check if the short name resolves to a valid ref, | |
3915 | * but use only rules prior to the matched one | |
3916 | */ | |
6e7b3309 | 3917 | for (j = 0; j < rules_to_fail; j++) { |
7c2b3029 | 3918 | const char *rule = ref_rev_parse_rules[j]; |
7c2b3029 JK |
3919 | char refname[PATH_MAX]; |
3920 | ||
6e7b3309 BW |
3921 | /* skip matched rule */ |
3922 | if (i == j) | |
3923 | continue; | |
3924 | ||
7c2b3029 JK |
3925 | /* |
3926 | * the short name is ambiguous, if it resolves | |
3927 | * (with this previous rule) to a valid ref | |
3928 | * read_ref() returns 0 on success | |
3929 | */ | |
3930 | mksnpath(refname, sizeof(refname), | |
3931 | rule, short_name_len, short_name); | |
c6893323 | 3932 | if (ref_exists(refname)) |
7c2b3029 JK |
3933 | break; |
3934 | } | |
3935 | ||
3936 | /* | |
3937 | * short name is non-ambiguous if all previous rules | |
3938 | * haven't resolved to a valid ref | |
3939 | */ | |
6e7b3309 | 3940 | if (j == rules_to_fail) |
7c2b3029 JK |
3941 | return short_name; |
3942 | } | |
3943 | ||
3944 | free(short_name); | |
dfefa935 | 3945 | return xstrdup(refname); |
7c2b3029 | 3946 | } |
daebaa78 JH |
3947 | |
3948 | static struct string_list *hide_refs; | |
3949 | ||
3950 | int parse_hide_refs_config(const char *var, const char *value, const char *section) | |
3951 | { | |
3952 | if (!strcmp("transfer.hiderefs", var) || | |
3953 | /* NEEDSWORK: use parse_config_key() once both are merged */ | |
59556548 | 3954 | (starts_with(var, section) && var[strlen(section)] == '.' && |
daebaa78 JH |
3955 | !strcmp(var + strlen(section), ".hiderefs"))) { |
3956 | char *ref; | |
3957 | int len; | |
3958 | ||
3959 | if (!value) | |
3960 | return config_error_nonbool(var); | |
3961 | ref = xstrdup(value); | |
3962 | len = strlen(ref); | |
3963 | while (len && ref[len - 1] == '/') | |
3964 | ref[--len] = '\0'; | |
3965 | if (!hide_refs) { | |
3966 | hide_refs = xcalloc(1, sizeof(*hide_refs)); | |
3967 | hide_refs->strdup_strings = 1; | |
3968 | } | |
3969 | string_list_append(hide_refs, ref); | |
3970 | } | |
3971 | return 0; | |
3972 | } | |
3973 | ||
3974 | int ref_is_hidden(const char *refname) | |
3975 | { | |
3976 | struct string_list_item *item; | |
3977 | ||
3978 | if (!hide_refs) | |
3979 | return 0; | |
3980 | for_each_string_list_item(item, hide_refs) { | |
3981 | int len; | |
59556548 | 3982 | if (!starts_with(refname, item->string)) |
daebaa78 JH |
3983 | continue; |
3984 | len = strlen(item->string); | |
3985 | if (!refname[len] || refname[len] == '/') | |
3986 | return 1; | |
3987 | } | |
3988 | return 0; | |
3989 | } | |
fa5b1830 MH |
3990 | |
3991 | struct expire_reflog_cb { | |
3992 | unsigned int flags; | |
3993 | reflog_expiry_should_prune_fn *should_prune_fn; | |
3994 | void *policy_cb; | |
3995 | FILE *newlog; | |
3996 | unsigned char last_kept_sha1[20]; | |
3997 | }; | |
3998 | ||
3999 | static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1, | |
4000 | const char *email, unsigned long timestamp, int tz, | |
4001 | const char *message, void *cb_data) | |
4002 | { | |
4003 | struct expire_reflog_cb *cb = cb_data; | |
4004 | struct expire_reflog_policy_cb *policy_cb = cb->policy_cb; | |
4005 | ||
4006 | if (cb->flags & EXPIRE_REFLOGS_REWRITE) | |
4007 | osha1 = cb->last_kept_sha1; | |
4008 | ||
4009 | if ((*cb->should_prune_fn)(osha1, nsha1, email, timestamp, tz, | |
4010 | message, policy_cb)) { | |
4011 | if (!cb->newlog) | |
4012 | printf("would prune %s", message); | |
4013 | else if (cb->flags & EXPIRE_REFLOGS_VERBOSE) | |
4014 | printf("prune %s", message); | |
4015 | } else { | |
4016 | if (cb->newlog) { | |
c653e034 | 4017 | fprintf(cb->newlog, "%s %s %s %lu %+05d\t%s", |
fa5b1830 | 4018 | sha1_to_hex(osha1), sha1_to_hex(nsha1), |
c653e034 | 4019 | email, timestamp, tz, message); |
fa5b1830 MH |
4020 | hashcpy(cb->last_kept_sha1, nsha1); |
4021 | } | |
4022 | if (cb->flags & EXPIRE_REFLOGS_VERBOSE) | |
4023 | printf("keep %s", message); | |
4024 | } | |
4025 | return 0; | |
4026 | } | |
4027 | ||
4028 | int reflog_expire(const char *refname, const unsigned char *sha1, | |
4029 | unsigned int flags, | |
4030 | reflog_expiry_prepare_fn prepare_fn, | |
4031 | reflog_expiry_should_prune_fn should_prune_fn, | |
4032 | reflog_expiry_cleanup_fn cleanup_fn, | |
4033 | void *policy_cb_data) | |
4034 | { | |
4035 | static struct lock_file reflog_lock; | |
4036 | struct expire_reflog_cb cb; | |
4037 | struct ref_lock *lock; | |
4038 | char *log_file; | |
4039 | int status = 0; | |
4040 | ||
4041 | memset(&cb, 0, sizeof(cb)); | |
4042 | cb.flags = flags; | |
4043 | cb.policy_cb = policy_cb_data; | |
4044 | cb.should_prune_fn = should_prune_fn; | |
4045 | ||
4046 | /* | |
4047 | * The reflog file is locked by holding the lock on the | |
4048 | * reference itself, plus we might need to update the | |
4049 | * reference if --updateref was specified: | |
4050 | */ | |
31e07f76 | 4051 | lock = lock_ref_sha1_basic(refname, sha1, NULL, 0, NULL); |
fa5b1830 MH |
4052 | if (!lock) |
4053 | return error("cannot lock ref '%s'", refname); | |
4054 | if (!reflog_exists(refname)) { | |
4055 | unlock_ref(lock); | |
4056 | return 0; | |
4057 | } | |
4058 | ||
4059 | log_file = git_pathdup("logs/%s", refname); | |
4060 | if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) { | |
4061 | /* | |
4062 | * Even though holding $GIT_DIR/logs/$reflog.lock has | |
4063 | * no locking implications, we use the lock_file | |
4064 | * machinery here anyway because it does a lot of the | |
4065 | * work we need, including cleaning up if the program | |
4066 | * exits unexpectedly. | |
4067 | */ | |
4068 | if (hold_lock_file_for_update(&reflog_lock, log_file, 0) < 0) { | |
4069 | struct strbuf err = STRBUF_INIT; | |
4070 | unable_to_lock_message(log_file, errno, &err); | |
4071 | error("%s", err.buf); | |
4072 | strbuf_release(&err); | |
4073 | goto failure; | |
4074 | } | |
4075 | cb.newlog = fdopen_lock_file(&reflog_lock, "w"); | |
4076 | if (!cb.newlog) { | |
4077 | error("cannot fdopen %s (%s)", | |
4078 | reflog_lock.filename.buf, strerror(errno)); | |
4079 | goto failure; | |
4080 | } | |
4081 | } | |
4082 | ||
4083 | (*prepare_fn)(refname, sha1, cb.policy_cb); | |
4084 | for_each_reflog_ent(refname, expire_reflog_ent, &cb); | |
4085 | (*cleanup_fn)(cb.policy_cb); | |
4086 | ||
4087 | if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) { | |
4088 | if (close_lock_file(&reflog_lock)) { | |
4089 | status |= error("couldn't write %s: %s", log_file, | |
4090 | strerror(errno)); | |
4091 | } else if ((flags & EXPIRE_REFLOGS_UPDATE_REF) && | |
4092 | (write_in_full(lock->lock_fd, | |
4093 | sha1_to_hex(cb.last_kept_sha1), 40) != 40 || | |
4094 | write_str_in_full(lock->lock_fd, "\n") != 1 || | |
4095 | close_ref(lock) < 0)) { | |
4096 | status |= error("couldn't write %s", | |
4097 | lock->lk->filename.buf); | |
4098 | rollback_lock_file(&reflog_lock); | |
4099 | } else if (commit_lock_file(&reflog_lock)) { | |
4100 | status |= error("unable to commit reflog '%s' (%s)", | |
4101 | log_file, strerror(errno)); | |
4102 | } else if ((flags & EXPIRE_REFLOGS_UPDATE_REF) && commit_ref(lock)) { | |
4103 | status |= error("couldn't set %s", lock->ref_name); | |
4104 | } | |
4105 | } | |
4106 | free(log_file); | |
4107 | unlock_ref(lock); | |
4108 | return status; | |
4109 | ||
4110 | failure: | |
4111 | rollback_lock_file(&reflog_lock); | |
4112 | free(log_file); | |
4113 | unlock_ref(lock); | |
4114 | return -1; | |
4115 | } |