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