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