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