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