]>
Commit | Line | Data |
---|---|---|
7bd9bcf3 | 1 | #include "../cache.h" |
b2141fc1 | 2 | #include "../config.h" |
7bd9bcf3 MH |
3 | #include "../refs.h" |
4 | #include "refs-internal.h" | |
958f9646 | 5 | #include "ref-cache.h" |
67be7c5a | 6 | #include "packed-backend.h" |
3bc581b9 | 7 | #include "../iterator.h" |
2880d16f | 8 | #include "../dir-iterator.h" |
7bd9bcf3 MH |
9 | #include "../lockfile.h" |
10 | #include "../object.h" | |
11 | #include "../dir.h" | |
12 | ||
5ac95fee MH |
13 | /* |
14 | * This backend uses the following flags in `ref_update::flags` for | |
15 | * internal bookkeeping purposes. Their numerical values must not | |
91774afc | 16 | * conflict with REF_NO_DEREF, REF_FORCE_CREATE_REFLOG, REF_HAVE_NEW, |
5ac95fee MH |
17 | * REF_HAVE_OLD, or REF_ISPRUNING, which are also stored in |
18 | * `ref_update::flags`. | |
19 | */ | |
20 | ||
21 | /* | |
22 | * Used as a flag in ref_update::flags when a loose ref is being | |
91774afc | 23 | * pruned. This flag must only be used when REF_NO_DEREF is set. |
5ac95fee MH |
24 | */ |
25 | #define REF_ISPRUNING (1 << 4) | |
26 | ||
27 | /* | |
28 | * Flag passed to lock_ref_sha1_basic() telling it to tolerate broken | |
29 | * refs (i.e., because the reference is about to be deleted anyway). | |
30 | */ | |
31 | #define REF_DELETING (1 << 5) | |
32 | ||
33 | /* | |
34 | * Used as a flag in ref_update::flags when the lockfile needs to be | |
35 | * committed. | |
36 | */ | |
37 | #define REF_NEEDS_COMMIT (1 << 6) | |
38 | ||
39 | /* | |
40 | * Used as a flag in ref_update::flags when we want to log a ref | |
41 | * update but not actually perform it. This is used when a symbolic | |
42 | * ref update is split up. | |
43 | */ | |
44 | #define REF_LOG_ONLY (1 << 7) | |
45 | ||
46 | /* | |
47 | * Used as a flag in ref_update::flags when the ref_update was via an | |
48 | * update to HEAD. | |
49 | */ | |
50 | #define REF_UPDATE_VIA_HEAD (1 << 8) | |
51 | ||
52 | /* | |
53 | * Used as a flag in ref_update::flags when the loose reference has | |
54 | * been deleted. | |
55 | */ | |
56 | #define REF_DELETED_LOOSE (1 << 9) | |
57 | ||
7bd9bcf3 MH |
58 | struct ref_lock { |
59 | char *ref_name; | |
ee4d8e45 | 60 | struct lock_file lk; |
7bd9bcf3 MH |
61 | struct object_id old_oid; |
62 | }; | |
63 | ||
7bd9bcf3 MH |
64 | /* |
65 | * Future: need to be in "struct repository" | |
66 | * when doing a full libification. | |
67 | */ | |
00eebe35 MH |
68 | struct files_ref_store { |
69 | struct ref_store base; | |
9e7ec634 | 70 | unsigned int store_flags; |
32c597e7 | 71 | |
f57f37e2 NTND |
72 | char *gitdir; |
73 | char *gitcommondir; | |
33dfb9f3 | 74 | |
7c22bc8a | 75 | struct ref_cache *loose; |
55c6bc37 | 76 | |
e0cc8ac8 | 77 | struct ref_store *packed_ref_store; |
00eebe35 | 78 | }; |
7bd9bcf3 | 79 | |
65a0a8e5 | 80 | static void clear_loose_ref_cache(struct files_ref_store *refs) |
7bd9bcf3 MH |
81 | { |
82 | if (refs->loose) { | |
7c22bc8a | 83 | free_ref_cache(refs->loose); |
7bd9bcf3 MH |
84 | refs->loose = NULL; |
85 | } | |
86 | } | |
87 | ||
a2d5156c JK |
88 | /* |
89 | * Create a new submodule ref cache and add it to the internal | |
90 | * set of caches. | |
91 | */ | |
9e7ec634 NTND |
92 | static struct ref_store *files_ref_store_create(const char *gitdir, |
93 | unsigned int flags) | |
7bd9bcf3 | 94 | { |
00eebe35 MH |
95 | struct files_ref_store *refs = xcalloc(1, sizeof(*refs)); |
96 | struct ref_store *ref_store = (struct ref_store *)refs; | |
f57f37e2 | 97 | struct strbuf sb = STRBUF_INIT; |
7bd9bcf3 | 98 | |
fbfd0a29 | 99 | base_ref_store_init(ref_store, &refs_be_files); |
9e7ec634 | 100 | refs->store_flags = flags; |
7bd9bcf3 | 101 | |
f57f37e2 NTND |
102 | refs->gitdir = xstrdup(gitdir); |
103 | get_common_dir_noenv(&sb, gitdir); | |
104 | refs->gitcommondir = strbuf_detach(&sb, NULL); | |
105 | strbuf_addf(&sb, "%s/packed-refs", refs->gitcommondir); | |
e0d48397 MH |
106 | refs->packed_ref_store = packed_ref_store_create(sb.buf, flags); |
107 | strbuf_release(&sb); | |
7bd9bcf3 | 108 | |
00eebe35 | 109 | return ref_store; |
a2d5156c | 110 | } |
7bd9bcf3 | 111 | |
32c597e7 | 112 | /* |
9e7ec634 NTND |
113 | * Die if refs is not the main ref store. caller is used in any |
114 | * necessary error messages. | |
32c597e7 MH |
115 | */ |
116 | static void files_assert_main_repository(struct files_ref_store *refs, | |
117 | const char *caller) | |
118 | { | |
9e7ec634 NTND |
119 | if (refs->store_flags & REF_STORE_MAIN) |
120 | return; | |
121 | ||
122 | die("BUG: operation %s only allowed for main ref store", caller); | |
32c597e7 MH |
123 | } |
124 | ||
a2d5156c | 125 | /* |
00eebe35 | 126 | * Downcast ref_store to files_ref_store. Die if ref_store is not a |
9e7ec634 NTND |
127 | * files_ref_store. required_flags is compared with ref_store's |
128 | * store_flags to ensure the ref_store has all required capabilities. | |
129 | * "caller" is used in any necessary error messages. | |
a2d5156c | 130 | */ |
9e7ec634 NTND |
131 | static struct files_ref_store *files_downcast(struct ref_store *ref_store, |
132 | unsigned int required_flags, | |
133 | const char *caller) | |
a2d5156c | 134 | { |
32c597e7 MH |
135 | struct files_ref_store *refs; |
136 | ||
00eebe35 MH |
137 | if (ref_store->be != &refs_be_files) |
138 | die("BUG: ref_store is type \"%s\" not \"files\" in %s", | |
139 | ref_store->be->name, caller); | |
2eed2780 | 140 | |
32c597e7 MH |
141 | refs = (struct files_ref_store *)ref_store; |
142 | ||
9e7ec634 NTND |
143 | if ((refs->store_flags & required_flags) != required_flags) |
144 | die("BUG: operation %s requires abilities 0x%x, but only have 0x%x", | |
145 | caller, required_flags, refs->store_flags); | |
2eed2780 | 146 | |
32c597e7 | 147 | return refs; |
7bd9bcf3 MH |
148 | } |
149 | ||
802de3da NTND |
150 | static void files_reflog_path(struct files_ref_store *refs, |
151 | struct strbuf *sb, | |
152 | const char *refname) | |
153 | { | |
f57f37e2 NTND |
154 | switch (ref_type(refname)) { |
155 | case REF_TYPE_PER_WORKTREE: | |
156 | case REF_TYPE_PSEUDOREF: | |
157 | strbuf_addf(sb, "%s/logs/%s", refs->gitdir, refname); | |
158 | break; | |
159 | case REF_TYPE_NORMAL: | |
160 | strbuf_addf(sb, "%s/logs/%s", refs->gitcommondir, refname); | |
161 | break; | |
162 | default: | |
163 | die("BUG: unknown ref type %d of ref %s", | |
164 | ref_type(refname), refname); | |
165 | } | |
802de3da NTND |
166 | } |
167 | ||
19e02f4f NTND |
168 | static void files_ref_path(struct files_ref_store *refs, |
169 | struct strbuf *sb, | |
170 | const char *refname) | |
171 | { | |
f57f37e2 NTND |
172 | switch (ref_type(refname)) { |
173 | case REF_TYPE_PER_WORKTREE: | |
174 | case REF_TYPE_PSEUDOREF: | |
175 | strbuf_addf(sb, "%s/%s", refs->gitdir, refname); | |
176 | break; | |
177 | case REF_TYPE_NORMAL: | |
178 | strbuf_addf(sb, "%s/%s", refs->gitcommondir, refname); | |
179 | break; | |
180 | default: | |
181 | die("BUG: unknown ref type %d of ref %s", | |
182 | ref_type(refname), refname); | |
183 | } | |
19e02f4f NTND |
184 | } |
185 | ||
7bd9bcf3 MH |
186 | /* |
187 | * Read the loose references from the namespace dirname into dir | |
188 | * (without recursing). dirname must end with '/'. dir must be the | |
189 | * directory entry corresponding to dirname. | |
190 | */ | |
df308759 MH |
191 | static void loose_fill_ref_dir(struct ref_store *ref_store, |
192 | struct ref_dir *dir, const char *dirname) | |
7bd9bcf3 | 193 | { |
df308759 MH |
194 | struct files_ref_store *refs = |
195 | files_downcast(ref_store, REF_STORE_READ, "fill_ref_dir"); | |
7bd9bcf3 MH |
196 | DIR *d; |
197 | struct dirent *de; | |
198 | int dirnamelen = strlen(dirname); | |
199 | struct strbuf refname; | |
200 | struct strbuf path = STRBUF_INIT; | |
201 | size_t path_baselen; | |
202 | ||
19e02f4f | 203 | files_ref_path(refs, &path, dirname); |
7bd9bcf3 MH |
204 | path_baselen = path.len; |
205 | ||
206 | d = opendir(path.buf); | |
207 | if (!d) { | |
208 | strbuf_release(&path); | |
209 | return; | |
210 | } | |
211 | ||
212 | strbuf_init(&refname, dirnamelen + 257); | |
213 | strbuf_add(&refname, dirname, dirnamelen); | |
214 | ||
215 | while ((de = readdir(d)) != NULL) { | |
4417df8c | 216 | struct object_id oid; |
7bd9bcf3 MH |
217 | struct stat st; |
218 | int flag; | |
219 | ||
220 | if (de->d_name[0] == '.') | |
221 | continue; | |
222 | if (ends_with(de->d_name, ".lock")) | |
223 | continue; | |
224 | strbuf_addstr(&refname, de->d_name); | |
225 | strbuf_addstr(&path, de->d_name); | |
226 | if (stat(path.buf, &st) < 0) { | |
227 | ; /* silently ignore */ | |
228 | } else if (S_ISDIR(st.st_mode)) { | |
229 | strbuf_addch(&refname, '/'); | |
230 | add_entry_to_dir(dir, | |
e00d1a4f | 231 | create_dir_entry(dir->cache, refname.buf, |
7bd9bcf3 MH |
232 | refname.len, 1)); |
233 | } else { | |
7d2df051 | 234 | if (!refs_resolve_ref_unsafe(&refs->base, |
3c0cb0cb MH |
235 | refname.buf, |
236 | RESOLVE_REF_READING, | |
49e61479 | 237 | &oid, &flag)) { |
4417df8c | 238 | oidclr(&oid); |
7bd9bcf3 | 239 | flag |= REF_ISBROKEN; |
4417df8c | 240 | } else if (is_null_oid(&oid)) { |
7bd9bcf3 MH |
241 | /* |
242 | * It is so astronomically unlikely | |
243 | * that NULL_SHA1 is the SHA-1 of an | |
244 | * actual object that we consider its | |
245 | * appearance in a loose reference | |
246 | * file to be repo corruption | |
247 | * (probably due to a software bug). | |
248 | */ | |
249 | flag |= REF_ISBROKEN; | |
250 | } | |
251 | ||
252 | if (check_refname_format(refname.buf, | |
253 | REFNAME_ALLOW_ONELEVEL)) { | |
254 | if (!refname_is_safe(refname.buf)) | |
255 | die("loose refname is dangerous: %s", refname.buf); | |
4417df8c | 256 | oidclr(&oid); |
7bd9bcf3 MH |
257 | flag |= REF_BAD_NAME | REF_ISBROKEN; |
258 | } | |
259 | add_entry_to_dir(dir, | |
c1da06c6 | 260 | create_ref_entry(refname.buf, &oid, flag)); |
7bd9bcf3 MH |
261 | } |
262 | strbuf_setlen(&refname, dirnamelen); | |
263 | strbuf_setlen(&path, path_baselen); | |
264 | } | |
265 | strbuf_release(&refname); | |
266 | strbuf_release(&path); | |
267 | closedir(d); | |
e3bf2989 MH |
268 | |
269 | /* | |
270 | * Manually add refs/bisect, which, being per-worktree, might | |
271 | * not appear in the directory listing for refs/ in the main | |
272 | * repo. | |
273 | */ | |
274 | if (!strcmp(dirname, "refs/")) { | |
275 | int pos = search_ref_dir(dir, "refs/bisect/", 12); | |
276 | ||
277 | if (pos < 0) { | |
278 | struct ref_entry *child_entry = create_dir_entry( | |
279 | dir->cache, "refs/bisect/", 12, 1); | |
280 | add_entry_to_dir(dir, child_entry); | |
281 | } | |
282 | } | |
7bd9bcf3 MH |
283 | } |
284 | ||
a714b19c | 285 | static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs) |
7bd9bcf3 MH |
286 | { |
287 | if (!refs->loose) { | |
288 | /* | |
289 | * Mark the top-level directory complete because we | |
290 | * are about to read the only subdirectory that can | |
291 | * hold references: | |
292 | */ | |
df308759 | 293 | refs->loose = create_ref_cache(&refs->base, loose_fill_ref_dir); |
7c22bc8a MH |
294 | |
295 | /* We're going to fill the top level ourselves: */ | |
296 | refs->loose->root->flag &= ~REF_INCOMPLETE; | |
297 | ||
7bd9bcf3 | 298 | /* |
7c22bc8a MH |
299 | * Add an incomplete entry for "refs/" (to be filled |
300 | * lazily): | |
7bd9bcf3 | 301 | */ |
7c22bc8a | 302 | add_entry_to_dir(get_ref_dir(refs->loose->root), |
e00d1a4f | 303 | create_dir_entry(refs->loose, "refs/", 5, 1)); |
7bd9bcf3 | 304 | } |
a714b19c | 305 | return refs->loose; |
7bd9bcf3 MH |
306 | } |
307 | ||
e1e33b72 | 308 | static int files_read_raw_ref(struct ref_store *ref_store, |
99afe91a | 309 | const char *refname, struct object_id *oid, |
e1e33b72 | 310 | struct strbuf *referent, unsigned int *type) |
7bd9bcf3 | 311 | { |
4308651c | 312 | struct files_ref_store *refs = |
9e7ec634 | 313 | files_downcast(ref_store, REF_STORE_READ, "read_raw_ref"); |
42a38cf7 MH |
314 | struct strbuf sb_contents = STRBUF_INIT; |
315 | struct strbuf sb_path = STRBUF_INIT; | |
7048653a DT |
316 | const char *path; |
317 | const char *buf; | |
99afe91a | 318 | const char *p; |
7048653a DT |
319 | struct stat st; |
320 | int fd; | |
42a38cf7 MH |
321 | int ret = -1; |
322 | int save_errno; | |
e8c42cb9 | 323 | int remaining_retries = 3; |
7bd9bcf3 | 324 | |
fa96ea1b | 325 | *type = 0; |
42a38cf7 | 326 | strbuf_reset(&sb_path); |
34c7ad8f | 327 | |
19e02f4f | 328 | files_ref_path(refs, &sb_path, refname); |
34c7ad8f | 329 | |
42a38cf7 | 330 | path = sb_path.buf; |
7bd9bcf3 | 331 | |
7048653a DT |
332 | stat_ref: |
333 | /* | |
334 | * We might have to loop back here to avoid a race | |
335 | * condition: first we lstat() the file, then we try | |
336 | * to read it as a link or as a file. But if somebody | |
337 | * changes the type of the file (file <-> directory | |
338 | * <-> symlink) between the lstat() and reading, then | |
339 | * we don't want to report that as an error but rather | |
340 | * try again starting with the lstat(). | |
e8c42cb9 JK |
341 | * |
342 | * We'll keep a count of the retries, though, just to avoid | |
343 | * any confusing situation sending us into an infinite loop. | |
7048653a | 344 | */ |
7bd9bcf3 | 345 | |
e8c42cb9 JK |
346 | if (remaining_retries-- <= 0) |
347 | goto out; | |
348 | ||
7048653a DT |
349 | if (lstat(path, &st) < 0) { |
350 | if (errno != ENOENT) | |
42a38cf7 | 351 | goto out; |
e0cc8ac8 | 352 | if (refs_read_raw_ref(refs->packed_ref_store, refname, |
99afe91a | 353 | oid, referent, type)) { |
7048653a | 354 | errno = ENOENT; |
42a38cf7 | 355 | goto out; |
7bd9bcf3 | 356 | } |
42a38cf7 MH |
357 | ret = 0; |
358 | goto out; | |
7bd9bcf3 | 359 | } |
7bd9bcf3 | 360 | |
7048653a DT |
361 | /* Follow "normalized" - ie "refs/.." symlinks by hand */ |
362 | if (S_ISLNK(st.st_mode)) { | |
42a38cf7 MH |
363 | strbuf_reset(&sb_contents); |
364 | if (strbuf_readlink(&sb_contents, path, 0) < 0) { | |
7048653a | 365 | if (errno == ENOENT || errno == EINVAL) |
7bd9bcf3 MH |
366 | /* inconsistent with lstat; retry */ |
367 | goto stat_ref; | |
368 | else | |
42a38cf7 | 369 | goto out; |
7bd9bcf3 | 370 | } |
42a38cf7 MH |
371 | if (starts_with(sb_contents.buf, "refs/") && |
372 | !check_refname_format(sb_contents.buf, 0)) { | |
92b38093 | 373 | strbuf_swap(&sb_contents, referent); |
3a0b6b9a | 374 | *type |= REF_ISSYMREF; |
42a38cf7 MH |
375 | ret = 0; |
376 | goto out; | |
7bd9bcf3 | 377 | } |
3f7bd767 JK |
378 | /* |
379 | * It doesn't look like a refname; fall through to just | |
380 | * treating it like a non-symlink, and reading whatever it | |
381 | * points to. | |
382 | */ | |
7048653a | 383 | } |
7bd9bcf3 | 384 | |
7048653a DT |
385 | /* Is it a directory? */ |
386 | if (S_ISDIR(st.st_mode)) { | |
e167a567 MH |
387 | /* |
388 | * Even though there is a directory where the loose | |
389 | * ref is supposed to be, there could still be a | |
390 | * packed ref: | |
391 | */ | |
e0cc8ac8 | 392 | if (refs_read_raw_ref(refs->packed_ref_store, refname, |
99afe91a | 393 | oid, referent, type)) { |
e167a567 MH |
394 | errno = EISDIR; |
395 | goto out; | |
396 | } | |
397 | ret = 0; | |
42a38cf7 | 398 | goto out; |
7048653a DT |
399 | } |
400 | ||
401 | /* | |
402 | * Anything else, just open it and try to use it as | |
403 | * a ref | |
404 | */ | |
405 | fd = open(path, O_RDONLY); | |
406 | if (fd < 0) { | |
3f7bd767 | 407 | if (errno == ENOENT && !S_ISLNK(st.st_mode)) |
7048653a DT |
408 | /* inconsistent with lstat; retry */ |
409 | goto stat_ref; | |
410 | else | |
42a38cf7 | 411 | goto out; |
7048653a | 412 | } |
42a38cf7 MH |
413 | strbuf_reset(&sb_contents); |
414 | if (strbuf_read(&sb_contents, fd, 256) < 0) { | |
7048653a DT |
415 | int save_errno = errno; |
416 | close(fd); | |
417 | errno = save_errno; | |
42a38cf7 | 418 | goto out; |
7048653a DT |
419 | } |
420 | close(fd); | |
42a38cf7 MH |
421 | strbuf_rtrim(&sb_contents); |
422 | buf = sb_contents.buf; | |
7048653a DT |
423 | if (starts_with(buf, "ref:")) { |
424 | buf += 4; | |
7bd9bcf3 MH |
425 | while (isspace(*buf)) |
426 | buf++; | |
7048653a | 427 | |
92b38093 MH |
428 | strbuf_reset(referent); |
429 | strbuf_addstr(referent, buf); | |
3a0b6b9a | 430 | *type |= REF_ISSYMREF; |
42a38cf7 MH |
431 | ret = 0; |
432 | goto out; | |
7bd9bcf3 | 433 | } |
7bd9bcf3 | 434 | |
7048653a DT |
435 | /* |
436 | * Please note that FETCH_HEAD has additional | |
437 | * data after the sha. | |
438 | */ | |
99afe91a | 439 | if (parse_oid_hex(buf, oid, &p) || |
440 | (*p != '\0' && !isspace(*p))) { | |
3a0b6b9a | 441 | *type |= REF_ISBROKEN; |
7048653a | 442 | errno = EINVAL; |
42a38cf7 | 443 | goto out; |
7048653a DT |
444 | } |
445 | ||
42a38cf7 | 446 | ret = 0; |
7bd9bcf3 | 447 | |
42a38cf7 MH |
448 | out: |
449 | save_errno = errno; | |
7bd9bcf3 MH |
450 | strbuf_release(&sb_path); |
451 | strbuf_release(&sb_contents); | |
42a38cf7 | 452 | errno = save_errno; |
7bd9bcf3 MH |
453 | return ret; |
454 | } | |
455 | ||
8415d247 MH |
456 | static void unlock_ref(struct ref_lock *lock) |
457 | { | |
ee4d8e45 | 458 | rollback_lock_file(&lock->lk); |
8415d247 | 459 | free(lock->ref_name); |
8415d247 MH |
460 | free(lock); |
461 | } | |
462 | ||
92b1551b MH |
463 | /* |
464 | * Lock refname, without following symrefs, and set *lock_p to point | |
465 | * at a newly-allocated lock object. Fill in lock->old_oid, referent, | |
466 | * and type similarly to read_raw_ref(). | |
467 | * | |
468 | * The caller must verify that refname is a "safe" reference name (in | |
469 | * the sense of refname_is_safe()) before calling this function. | |
470 | * | |
471 | * If the reference doesn't already exist, verify that refname doesn't | |
472 | * have a D/F conflict with any existing references. extras and skip | |
524a9fdb | 473 | * are passed to refs_verify_refname_available() for this check. |
92b1551b MH |
474 | * |
475 | * If mustexist is not set and the reference is not found or is | |
476 | * broken, lock the reference anyway but clear sha1. | |
477 | * | |
478 | * Return 0 on success. On failure, write an error message to err and | |
479 | * return TRANSACTION_NAME_CONFLICT or TRANSACTION_GENERIC_ERROR. | |
480 | * | |
481 | * Implementation note: This function is basically | |
482 | * | |
483 | * lock reference | |
484 | * read_raw_ref() | |
485 | * | |
486 | * but it includes a lot more code to | |
487 | * - Deal with possible races with other processes | |
524a9fdb | 488 | * - Avoid calling refs_verify_refname_available() when it can be |
92b1551b MH |
489 | * avoided, namely if we were successfully able to read the ref |
490 | * - Generate informative error messages in the case of failure | |
491 | */ | |
f7b0a987 MH |
492 | static int lock_raw_ref(struct files_ref_store *refs, |
493 | const char *refname, int mustexist, | |
92b1551b MH |
494 | const struct string_list *extras, |
495 | const struct string_list *skip, | |
496 | struct ref_lock **lock_p, | |
497 | struct strbuf *referent, | |
498 | unsigned int *type, | |
499 | struct strbuf *err) | |
500 | { | |
501 | struct ref_lock *lock; | |
502 | struct strbuf ref_file = STRBUF_INIT; | |
503 | int attempts_remaining = 3; | |
504 | int ret = TRANSACTION_GENERIC_ERROR; | |
505 | ||
506 | assert(err); | |
32c597e7 | 507 | files_assert_main_repository(refs, "lock_raw_ref"); |
f7b0a987 | 508 | |
92b1551b MH |
509 | *type = 0; |
510 | ||
511 | /* First lock the file so it can't change out from under us. */ | |
512 | ||
513 | *lock_p = lock = xcalloc(1, sizeof(*lock)); | |
514 | ||
515 | lock->ref_name = xstrdup(refname); | |
19e02f4f | 516 | files_ref_path(refs, &ref_file, refname); |
92b1551b MH |
517 | |
518 | retry: | |
519 | switch (safe_create_leading_directories(ref_file.buf)) { | |
520 | case SCLD_OK: | |
521 | break; /* success */ | |
522 | case SCLD_EXISTS: | |
523 | /* | |
524 | * Suppose refname is "refs/foo/bar". We just failed | |
525 | * to create the containing directory, "refs/foo", | |
526 | * because there was a non-directory in the way. This | |
527 | * indicates a D/F conflict, probably because of | |
528 | * another reference such as "refs/foo". There is no | |
529 | * reason to expect this error to be transitory. | |
530 | */ | |
7d2df051 NTND |
531 | if (refs_verify_refname_available(&refs->base, refname, |
532 | extras, skip, err)) { | |
92b1551b MH |
533 | if (mustexist) { |
534 | /* | |
535 | * To the user the relevant error is | |
536 | * that the "mustexist" reference is | |
537 | * missing: | |
538 | */ | |
539 | strbuf_reset(err); | |
540 | strbuf_addf(err, "unable to resolve reference '%s'", | |
541 | refname); | |
542 | } else { | |
543 | /* | |
544 | * The error message set by | |
524a9fdb MH |
545 | * refs_verify_refname_available() is |
546 | * OK. | |
92b1551b MH |
547 | */ |
548 | ret = TRANSACTION_NAME_CONFLICT; | |
549 | } | |
550 | } else { | |
551 | /* | |
552 | * The file that is in the way isn't a loose | |
553 | * reference. Report it as a low-level | |
554 | * failure. | |
555 | */ | |
556 | strbuf_addf(err, "unable to create lock file %s.lock; " | |
557 | "non-directory in the way", | |
558 | ref_file.buf); | |
559 | } | |
560 | goto error_return; | |
561 | case SCLD_VANISHED: | |
562 | /* Maybe another process was tidying up. Try again. */ | |
563 | if (--attempts_remaining > 0) | |
564 | goto retry; | |
565 | /* fall through */ | |
566 | default: | |
567 | strbuf_addf(err, "unable to create directory for %s", | |
568 | ref_file.buf); | |
569 | goto error_return; | |
570 | } | |
571 | ||
4ff0f01c | 572 | if (hold_lock_file_for_update_timeout( |
ee4d8e45 | 573 | &lock->lk, ref_file.buf, LOCK_NO_DEREF, |
4ff0f01c | 574 | get_files_ref_lock_timeout_ms()) < 0) { |
92b1551b MH |
575 | if (errno == ENOENT && --attempts_remaining > 0) { |
576 | /* | |
577 | * Maybe somebody just deleted one of the | |
578 | * directories leading to ref_file. Try | |
579 | * again: | |
580 | */ | |
581 | goto retry; | |
582 | } else { | |
583 | unable_to_lock_message(ref_file.buf, errno, err); | |
584 | goto error_return; | |
585 | } | |
586 | } | |
587 | ||
588 | /* | |
589 | * Now we hold the lock and can read the reference without | |
590 | * fear that its value will change. | |
591 | */ | |
592 | ||
f7b0a987 | 593 | if (files_read_raw_ref(&refs->base, refname, |
99afe91a | 594 | &lock->old_oid, referent, type)) { |
92b1551b MH |
595 | if (errno == ENOENT) { |
596 | if (mustexist) { | |
597 | /* Garden variety missing reference. */ | |
598 | strbuf_addf(err, "unable to resolve reference '%s'", | |
599 | refname); | |
600 | goto error_return; | |
601 | } else { | |
602 | /* | |
603 | * Reference is missing, but that's OK. We | |
604 | * know that there is not a conflict with | |
605 | * another loose reference because | |
606 | * (supposing that we are trying to lock | |
607 | * reference "refs/foo/bar"): | |
608 | * | |
609 | * - We were successfully able to create | |
610 | * the lockfile refs/foo/bar.lock, so we | |
611 | * know there cannot be a loose reference | |
612 | * named "refs/foo". | |
613 | * | |
614 | * - We got ENOENT and not EISDIR, so we | |
615 | * know that there cannot be a loose | |
616 | * reference named "refs/foo/bar/baz". | |
617 | */ | |
618 | } | |
619 | } else if (errno == EISDIR) { | |
620 | /* | |
621 | * There is a directory in the way. It might have | |
622 | * contained references that have been deleted. If | |
623 | * we don't require that the reference already | |
624 | * exists, try to remove the directory so that it | |
625 | * doesn't cause trouble when we want to rename the | |
626 | * lockfile into place later. | |
627 | */ | |
628 | if (mustexist) { | |
629 | /* Garden variety missing reference. */ | |
630 | strbuf_addf(err, "unable to resolve reference '%s'", | |
631 | refname); | |
632 | goto error_return; | |
633 | } else if (remove_dir_recursively(&ref_file, | |
634 | REMOVE_DIR_EMPTY_ONLY)) { | |
b05855b5 MH |
635 | if (refs_verify_refname_available( |
636 | &refs->base, refname, | |
637 | extras, skip, err)) { | |
92b1551b MH |
638 | /* |
639 | * The error message set by | |
640 | * verify_refname_available() is OK. | |
641 | */ | |
642 | ret = TRANSACTION_NAME_CONFLICT; | |
643 | goto error_return; | |
644 | } else { | |
645 | /* | |
646 | * We can't delete the directory, | |
647 | * but we also don't know of any | |
648 | * references that it should | |
649 | * contain. | |
650 | */ | |
651 | strbuf_addf(err, "there is a non-empty directory '%s' " | |
652 | "blocking reference '%s'", | |
653 | ref_file.buf, refname); | |
654 | goto error_return; | |
655 | } | |
656 | } | |
657 | } else if (errno == EINVAL && (*type & REF_ISBROKEN)) { | |
658 | strbuf_addf(err, "unable to resolve reference '%s': " | |
659 | "reference broken", refname); | |
660 | goto error_return; | |
661 | } else { | |
662 | strbuf_addf(err, "unable to resolve reference '%s': %s", | |
663 | refname, strerror(errno)); | |
664 | goto error_return; | |
665 | } | |
666 | ||
667 | /* | |
668 | * If the ref did not exist and we are creating it, | |
8ec617c8 MH |
669 | * make sure there is no existing packed ref that |
670 | * conflicts with refname: | |
92b1551b | 671 | */ |
524a9fdb | 672 | if (refs_verify_refname_available( |
8ec617c8 | 673 | refs->packed_ref_store, refname, |
524a9fdb | 674 | extras, skip, err)) |
92b1551b | 675 | goto error_return; |
92b1551b MH |
676 | } |
677 | ||
678 | ret = 0; | |
679 | goto out; | |
680 | ||
681 | error_return: | |
682 | unlock_ref(lock); | |
683 | *lock_p = NULL; | |
684 | ||
685 | out: | |
686 | strbuf_release(&ref_file); | |
687 | return ret; | |
688 | } | |
689 | ||
3bc581b9 MH |
690 | struct files_ref_iterator { |
691 | struct ref_iterator base; | |
692 | ||
3bc581b9 MH |
693 | struct ref_iterator *iter0; |
694 | unsigned int flags; | |
695 | }; | |
7bd9bcf3 | 696 | |
3bc581b9 MH |
697 | static int files_ref_iterator_advance(struct ref_iterator *ref_iterator) |
698 | { | |
699 | struct files_ref_iterator *iter = | |
700 | (struct files_ref_iterator *)ref_iterator; | |
701 | int ok; | |
7bd9bcf3 | 702 | |
3bc581b9 | 703 | while ((ok = ref_iterator_advance(iter->iter0)) == ITER_OK) { |
0c09ec07 DT |
704 | if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY && |
705 | ref_type(iter->iter0->refname) != REF_TYPE_PER_WORKTREE) | |
706 | continue; | |
707 | ||
3bc581b9 MH |
708 | if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) && |
709 | !ref_resolves_to_object(iter->iter0->refname, | |
710 | iter->iter0->oid, | |
711 | iter->iter0->flags)) | |
712 | continue; | |
713 | ||
714 | iter->base.refname = iter->iter0->refname; | |
715 | iter->base.oid = iter->iter0->oid; | |
716 | iter->base.flags = iter->iter0->flags; | |
717 | return ITER_OK; | |
7bd9bcf3 MH |
718 | } |
719 | ||
3bc581b9 MH |
720 | iter->iter0 = NULL; |
721 | if (ref_iterator_abort(ref_iterator) != ITER_DONE) | |
722 | ok = ITER_ERROR; | |
723 | ||
724 | return ok; | |
7bd9bcf3 MH |
725 | } |
726 | ||
3bc581b9 MH |
727 | static int files_ref_iterator_peel(struct ref_iterator *ref_iterator, |
728 | struct object_id *peeled) | |
7bd9bcf3 | 729 | { |
3bc581b9 MH |
730 | struct files_ref_iterator *iter = |
731 | (struct files_ref_iterator *)ref_iterator; | |
93770590 | 732 | |
3bc581b9 MH |
733 | return ref_iterator_peel(iter->iter0, peeled); |
734 | } | |
735 | ||
736 | static int files_ref_iterator_abort(struct ref_iterator *ref_iterator) | |
737 | { | |
738 | struct files_ref_iterator *iter = | |
739 | (struct files_ref_iterator *)ref_iterator; | |
740 | int ok = ITER_DONE; | |
741 | ||
742 | if (iter->iter0) | |
743 | ok = ref_iterator_abort(iter->iter0); | |
744 | ||
3bc581b9 MH |
745 | base_ref_iterator_free(ref_iterator); |
746 | return ok; | |
747 | } | |
748 | ||
749 | static struct ref_iterator_vtable files_ref_iterator_vtable = { | |
750 | files_ref_iterator_advance, | |
751 | files_ref_iterator_peel, | |
752 | files_ref_iterator_abort | |
753 | }; | |
754 | ||
1a769003 | 755 | static struct ref_iterator *files_ref_iterator_begin( |
37b6f6d5 | 756 | struct ref_store *ref_store, |
3bc581b9 MH |
757 | const char *prefix, unsigned int flags) |
758 | { | |
9e7ec634 | 759 | struct files_ref_store *refs; |
8738a8a4 | 760 | struct ref_iterator *loose_iter, *packed_iter, *overlay_iter; |
3bc581b9 MH |
761 | struct files_ref_iterator *iter; |
762 | struct ref_iterator *ref_iterator; | |
0a0865b8 | 763 | unsigned int required_flags = REF_STORE_READ; |
3bc581b9 | 764 | |
0a0865b8 MH |
765 | if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN)) |
766 | required_flags |= REF_STORE_ODB; | |
3bc581b9 | 767 | |
0a0865b8 | 768 | refs = files_downcast(ref_store, required_flags, "ref_iterator_begin"); |
9e7ec634 | 769 | |
3bc581b9 MH |
770 | /* |
771 | * We must make sure that all loose refs are read before | |
772 | * accessing the packed-refs file; this avoids a race | |
773 | * condition if loose refs are migrated to the packed-refs | |
774 | * file by a simultaneous process, but our in-memory view is | |
775 | * from before the migration. We ensure this as follows: | |
059ae35a MH |
776 | * First, we call start the loose refs iteration with its |
777 | * `prime_ref` argument set to true. This causes the loose | |
778 | * references in the subtree to be pre-read into the cache. | |
779 | * (If they've already been read, that's OK; we only need to | |
780 | * guarantee that they're read before the packed refs, not | |
781 | * *how much* before.) After that, we call | |
38b86e81 MH |
782 | * packed_ref_iterator_begin(), which internally checks |
783 | * whether the packed-ref cache is up to date with what is on | |
784 | * disk, and re-reads it if not. | |
3bc581b9 MH |
785 | */ |
786 | ||
059ae35a MH |
787 | loose_iter = cache_ref_iterator_begin(get_loose_ref_cache(refs), |
788 | prefix, 1); | |
3bc581b9 | 789 | |
38b86e81 MH |
790 | /* |
791 | * The packed-refs file might contain broken references, for | |
792 | * example an old version of a reference that points at an | |
793 | * object that has since been garbage-collected. This is OK as | |
794 | * long as there is a corresponding loose reference that | |
795 | * overrides it, and we don't want to emit an error message in | |
796 | * this case. So ask the packed_ref_store for all of its | |
797 | * references, and (if needed) do our own check for broken | |
798 | * ones in files_ref_iterator_advance(), after we have merged | |
799 | * the packed and loose references. | |
800 | */ | |
e0cc8ac8 MH |
801 | packed_iter = refs_ref_iterator_begin( |
802 | refs->packed_ref_store, prefix, 0, | |
38b86e81 | 803 | DO_FOR_EACH_INCLUDE_BROKEN); |
3bc581b9 | 804 | |
8738a8a4 MH |
805 | overlay_iter = overlay_ref_iterator_begin(loose_iter, packed_iter); |
806 | ||
807 | iter = xcalloc(1, sizeof(*iter)); | |
808 | ref_iterator = &iter->base; | |
809 | base_ref_iterator_init(ref_iterator, &files_ref_iterator_vtable, | |
810 | overlay_iter->ordered); | |
811 | iter->iter0 = overlay_iter; | |
3bc581b9 MH |
812 | iter->flags = flags; |
813 | ||
814 | return ref_iterator; | |
7bd9bcf3 MH |
815 | } |
816 | ||
7bd9bcf3 | 817 | /* |
4f01e508 | 818 | * Verify that the reference locked by lock has the value old_oid |
819 | * (unless it is NULL). Fail if the reference doesn't exist and | |
820 | * mustexist is set. Return 0 on success. On error, write an error | |
821 | * message to err, set errno, and return a negative value. | |
7bd9bcf3 | 822 | */ |
2f40e954 | 823 | static int verify_lock(struct ref_store *ref_store, struct ref_lock *lock, |
4f01e508 | 824 | const struct object_id *old_oid, int mustexist, |
7bd9bcf3 MH |
825 | struct strbuf *err) |
826 | { | |
827 | assert(err); | |
828 | ||
2f40e954 NTND |
829 | if (refs_read_ref_full(ref_store, lock->ref_name, |
830 | mustexist ? RESOLVE_REF_READING : 0, | |
34c290a6 | 831 | &lock->old_oid, NULL)) { |
4f01e508 | 832 | if (old_oid) { |
6294dcb4 | 833 | int save_errno = errno; |
0568c8e9 | 834 | strbuf_addf(err, "can't verify ref '%s'", lock->ref_name); |
6294dcb4 JK |
835 | errno = save_errno; |
836 | return -1; | |
837 | } else { | |
c368dde9 | 838 | oidclr(&lock->old_oid); |
6294dcb4 JK |
839 | return 0; |
840 | } | |
7bd9bcf3 | 841 | } |
4f01e508 | 842 | if (old_oid && oidcmp(&lock->old_oid, old_oid)) { |
0568c8e9 | 843 | strbuf_addf(err, "ref '%s' is at %s but expected %s", |
7bd9bcf3 | 844 | lock->ref_name, |
c368dde9 | 845 | oid_to_hex(&lock->old_oid), |
4f01e508 | 846 | oid_to_hex(old_oid)); |
7bd9bcf3 MH |
847 | errno = EBUSY; |
848 | return -1; | |
849 | } | |
850 | return 0; | |
851 | } | |
852 | ||
853 | static int remove_empty_directories(struct strbuf *path) | |
854 | { | |
855 | /* | |
856 | * we want to create a file but there is a directory there; | |
857 | * if that is an empty directory (or a directory that contains | |
858 | * only empty directories), remove them. | |
859 | */ | |
860 | return remove_dir_recursively(path, REMOVE_DIR_EMPTY_ONLY); | |
861 | } | |
862 | ||
3b5d3c98 MH |
863 | static int create_reflock(const char *path, void *cb) |
864 | { | |
865 | struct lock_file *lk = cb; | |
866 | ||
4ff0f01c MH |
867 | return hold_lock_file_for_update_timeout( |
868 | lk, path, LOCK_NO_DEREF, | |
869 | get_files_ref_lock_timeout_ms()) < 0 ? -1 : 0; | |
3b5d3c98 MH |
870 | } |
871 | ||
7bd9bcf3 MH |
872 | /* |
873 | * Locks a ref returning the lock on success and NULL on failure. | |
874 | * On failure errno is set to something meaningful. | |
875 | */ | |
4f01e508 | 876 | static struct ref_lock *lock_ref_oid_basic(struct files_ref_store *refs, |
877 | const char *refname, | |
878 | const struct object_id *old_oid, | |
879 | const struct string_list *extras, | |
880 | const struct string_list *skip, | |
881 | unsigned int flags, int *type, | |
882 | struct strbuf *err) | |
7bd9bcf3 MH |
883 | { |
884 | struct strbuf ref_file = STRBUF_INIT; | |
7bd9bcf3 MH |
885 | struct ref_lock *lock; |
886 | int last_errno = 0; | |
4f01e508 | 887 | int mustexist = (old_oid && !is_null_oid(old_oid)); |
7a418f3a | 888 | int resolve_flags = RESOLVE_REF_NO_RECURSE; |
7a418f3a | 889 | int resolved; |
7bd9bcf3 | 890 | |
4f01e508 | 891 | files_assert_main_repository(refs, "lock_ref_oid_basic"); |
7bd9bcf3 MH |
892 | assert(err); |
893 | ||
894 | lock = xcalloc(1, sizeof(struct ref_lock)); | |
895 | ||
896 | if (mustexist) | |
897 | resolve_flags |= RESOLVE_REF_READING; | |
2859dcd4 | 898 | if (flags & REF_DELETING) |
7bd9bcf3 | 899 | resolve_flags |= RESOLVE_REF_ALLOW_BAD_NAME; |
7bd9bcf3 | 900 | |
19e02f4f | 901 | files_ref_path(refs, &ref_file, refname); |
2f40e954 NTND |
902 | resolved = !!refs_resolve_ref_unsafe(&refs->base, |
903 | refname, resolve_flags, | |
49e61479 | 904 | &lock->old_oid, type); |
7a418f3a | 905 | if (!resolved && errno == EISDIR) { |
7bd9bcf3 MH |
906 | /* |
907 | * we are trying to lock foo but we used to | |
908 | * have foo/bar which now does not exist; | |
909 | * it is normal for the empty directory 'foo' | |
910 | * to remain. | |
911 | */ | |
7a418f3a | 912 | if (remove_empty_directories(&ref_file)) { |
7bd9bcf3 | 913 | last_errno = errno; |
b05855b5 MH |
914 | if (!refs_verify_refname_available( |
915 | &refs->base, | |
916 | refname, extras, skip, err)) | |
7bd9bcf3 | 917 | strbuf_addf(err, "there are still refs under '%s'", |
7a418f3a | 918 | refname); |
7bd9bcf3 MH |
919 | goto error_return; |
920 | } | |
2f40e954 NTND |
921 | resolved = !!refs_resolve_ref_unsafe(&refs->base, |
922 | refname, resolve_flags, | |
49e61479 | 923 | &lock->old_oid, type); |
7bd9bcf3 | 924 | } |
7a418f3a | 925 | if (!resolved) { |
7bd9bcf3 MH |
926 | last_errno = errno; |
927 | if (last_errno != ENOTDIR || | |
b05855b5 MH |
928 | !refs_verify_refname_available(&refs->base, refname, |
929 | extras, skip, err)) | |
0568c8e9 | 930 | strbuf_addf(err, "unable to resolve reference '%s': %s", |
7a418f3a | 931 | refname, strerror(last_errno)); |
7bd9bcf3 MH |
932 | |
933 | goto error_return; | |
934 | } | |
2859dcd4 | 935 | |
7bd9bcf3 MH |
936 | /* |
937 | * If the ref did not exist and we are creating it, make sure | |
938 | * there is no existing packed ref whose name begins with our | |
939 | * refname, nor a packed ref whose name is a proper prefix of | |
940 | * our refname. | |
941 | */ | |
942 | if (is_null_oid(&lock->old_oid) && | |
8ec617c8 | 943 | refs_verify_refname_available(refs->packed_ref_store, refname, |
524a9fdb | 944 | extras, skip, err)) { |
7bd9bcf3 MH |
945 | last_errno = ENOTDIR; |
946 | goto error_return; | |
947 | } | |
948 | ||
7bd9bcf3 | 949 | lock->ref_name = xstrdup(refname); |
7bd9bcf3 | 950 | |
ee4d8e45 | 951 | if (raceproof_create_file(ref_file.buf, create_reflock, &lock->lk)) { |
7bd9bcf3 | 952 | last_errno = errno; |
3b5d3c98 | 953 | unable_to_lock_message(ref_file.buf, errno, err); |
7bd9bcf3 MH |
954 | goto error_return; |
955 | } | |
956 | ||
4f01e508 | 957 | if (verify_lock(&refs->base, lock, old_oid, mustexist, err)) { |
7bd9bcf3 MH |
958 | last_errno = errno; |
959 | goto error_return; | |
960 | } | |
961 | goto out; | |
962 | ||
963 | error_return: | |
964 | unlock_ref(lock); | |
965 | lock = NULL; | |
966 | ||
967 | out: | |
968 | strbuf_release(&ref_file); | |
7bd9bcf3 MH |
969 | errno = last_errno; |
970 | return lock; | |
971 | } | |
972 | ||
7bd9bcf3 MH |
973 | struct ref_to_prune { |
974 | struct ref_to_prune *next; | |
49e99588 | 975 | struct object_id oid; |
7bd9bcf3 MH |
976 | char name[FLEX_ARRAY]; |
977 | }; | |
978 | ||
a8f0db2d MH |
979 | enum { |
980 | REMOVE_EMPTY_PARENTS_REF = 0x01, | |
981 | REMOVE_EMPTY_PARENTS_REFLOG = 0x02 | |
982 | }; | |
983 | ||
7bd9bcf3 | 984 | /* |
a8f0db2d MH |
985 | * Remove empty parent directories associated with the specified |
986 | * reference and/or its reflog, but spare [logs/]refs/ and immediate | |
987 | * subdirs. flags is a combination of REMOVE_EMPTY_PARENTS_REF and/or | |
988 | * REMOVE_EMPTY_PARENTS_REFLOG. | |
7bd9bcf3 | 989 | */ |
802de3da NTND |
990 | static void try_remove_empty_parents(struct files_ref_store *refs, |
991 | const char *refname, | |
992 | unsigned int flags) | |
7bd9bcf3 | 993 | { |
8bdaecb4 | 994 | struct strbuf buf = STRBUF_INIT; |
e9dcc305 | 995 | struct strbuf sb = STRBUF_INIT; |
7bd9bcf3 MH |
996 | char *p, *q; |
997 | int i; | |
8bdaecb4 MH |
998 | |
999 | strbuf_addstr(&buf, refname); | |
1000 | p = buf.buf; | |
7bd9bcf3 MH |
1001 | for (i = 0; i < 2; i++) { /* refs/{heads,tags,...}/ */ |
1002 | while (*p && *p != '/') | |
1003 | p++; | |
1004 | /* tolerate duplicate slashes; see check_refname_format() */ | |
1005 | while (*p == '/') | |
1006 | p++; | |
1007 | } | |
8bdaecb4 | 1008 | q = buf.buf + buf.len; |
a8f0db2d | 1009 | while (flags & (REMOVE_EMPTY_PARENTS_REF | REMOVE_EMPTY_PARENTS_REFLOG)) { |
7bd9bcf3 MH |
1010 | while (q > p && *q != '/') |
1011 | q--; | |
1012 | while (q > p && *(q-1) == '/') | |
1013 | q--; | |
1014 | if (q == p) | |
1015 | break; | |
8bdaecb4 | 1016 | strbuf_setlen(&buf, q - buf.buf); |
e9dcc305 NTND |
1017 | |
1018 | strbuf_reset(&sb); | |
19e02f4f | 1019 | files_ref_path(refs, &sb, buf.buf); |
e9dcc305 | 1020 | if ((flags & REMOVE_EMPTY_PARENTS_REF) && rmdir(sb.buf)) |
a8f0db2d | 1021 | flags &= ~REMOVE_EMPTY_PARENTS_REF; |
e9dcc305 NTND |
1022 | |
1023 | strbuf_reset(&sb); | |
802de3da | 1024 | files_reflog_path(refs, &sb, buf.buf); |
e9dcc305 | 1025 | if ((flags & REMOVE_EMPTY_PARENTS_REFLOG) && rmdir(sb.buf)) |
a8f0db2d | 1026 | flags &= ~REMOVE_EMPTY_PARENTS_REFLOG; |
7bd9bcf3 | 1027 | } |
8bdaecb4 | 1028 | strbuf_release(&buf); |
e9dcc305 | 1029 | strbuf_release(&sb); |
7bd9bcf3 MH |
1030 | } |
1031 | ||
1032 | /* make sure nobody touched the ref, and unlink */ | |
2f40e954 | 1033 | static void prune_ref(struct files_ref_store *refs, struct ref_to_prune *r) |
7bd9bcf3 MH |
1034 | { |
1035 | struct ref_transaction *transaction; | |
1036 | struct strbuf err = STRBUF_INIT; | |
b00f3cfa | 1037 | int ret = -1; |
7bd9bcf3 MH |
1038 | |
1039 | if (check_refname_format(r->name, 0)) | |
1040 | return; | |
1041 | ||
2f40e954 | 1042 | transaction = ref_store_transaction_begin(&refs->base, &err); |
b00f3cfa MH |
1043 | if (!transaction) |
1044 | goto cleanup; | |
1045 | ref_transaction_add_update( | |
1046 | transaction, r->name, | |
91774afc | 1047 | REF_NO_DEREF | REF_HAVE_NEW | REF_HAVE_OLD | REF_ISPRUNING, |
b00f3cfa MH |
1048 | &null_oid, &r->oid, NULL); |
1049 | if (ref_transaction_commit(transaction, &err)) | |
1050 | goto cleanup; | |
1051 | ||
1052 | ret = 0; | |
1053 | ||
1054 | cleanup: | |
1055 | if (ret) | |
7bd9bcf3 | 1056 | error("%s", err.buf); |
7bd9bcf3 | 1057 | strbuf_release(&err); |
b00f3cfa MH |
1058 | ref_transaction_free(transaction); |
1059 | return; | |
7bd9bcf3 MH |
1060 | } |
1061 | ||
22b09cdf MH |
1062 | /* |
1063 | * Prune the loose versions of the references in the linked list | |
1064 | * `*refs_to_prune`, freeing the entries in the list as we go. | |
1065 | */ | |
1066 | static void prune_refs(struct files_ref_store *refs, struct ref_to_prune **refs_to_prune) | |
7bd9bcf3 | 1067 | { |
22b09cdf MH |
1068 | while (*refs_to_prune) { |
1069 | struct ref_to_prune *r = *refs_to_prune; | |
1070 | *refs_to_prune = r->next; | |
2f40e954 | 1071 | prune_ref(refs, r); |
22b09cdf | 1072 | free(r); |
7bd9bcf3 MH |
1073 | } |
1074 | } | |
1075 | ||
531cc4a5 MH |
1076 | /* |
1077 | * Return true if the specified reference should be packed. | |
1078 | */ | |
1079 | static int should_pack_ref(const char *refname, | |
1080 | const struct object_id *oid, unsigned int ref_flags, | |
1081 | unsigned int pack_flags) | |
1082 | { | |
1083 | /* Do not pack per-worktree refs: */ | |
1084 | if (ref_type(refname) != REF_TYPE_NORMAL) | |
1085 | return 0; | |
1086 | ||
1087 | /* Do not pack non-tags unless PACK_REFS_ALL is set: */ | |
1088 | if (!(pack_flags & PACK_REFS_ALL) && !starts_with(refname, "refs/tags/")) | |
1089 | return 0; | |
1090 | ||
1091 | /* Do not pack symbolic refs: */ | |
1092 | if (ref_flags & REF_ISSYMREF) | |
1093 | return 0; | |
1094 | ||
1095 | /* Do not pack broken refs: */ | |
1096 | if (!ref_resolves_to_object(refname, oid, ref_flags)) | |
1097 | return 0; | |
1098 | ||
1099 | return 1; | |
1100 | } | |
1101 | ||
8231527e | 1102 | static int files_pack_refs(struct ref_store *ref_store, unsigned int flags) |
7bd9bcf3 | 1103 | { |
00eebe35 | 1104 | struct files_ref_store *refs = |
9e7ec634 NTND |
1105 | files_downcast(ref_store, REF_STORE_WRITE | REF_STORE_ODB, |
1106 | "pack_refs"); | |
50c2d855 | 1107 | struct ref_iterator *iter; |
50c2d855 MH |
1108 | int ok; |
1109 | struct ref_to_prune *refs_to_prune = NULL; | |
3478983b | 1110 | struct strbuf err = STRBUF_INIT; |
27d03d04 MH |
1111 | struct ref_transaction *transaction; |
1112 | ||
1113 | transaction = ref_store_transaction_begin(refs->packed_ref_store, &err); | |
1114 | if (!transaction) | |
1115 | return -1; | |
7bd9bcf3 | 1116 | |
c8bed835 | 1117 | packed_refs_lock(refs->packed_ref_store, LOCK_DIE_ON_ERROR, &err); |
50c2d855 MH |
1118 | |
1119 | iter = cache_ref_iterator_begin(get_loose_ref_cache(refs), NULL, 0); | |
1120 | while ((ok = ref_iterator_advance(iter)) == ITER_OK) { | |
1121 | /* | |
1122 | * If the loose reference can be packed, add an entry | |
1123 | * in the packed ref cache. If the reference should be | |
1124 | * pruned, also add it to refs_to_prune. | |
1125 | */ | |
531cc4a5 MH |
1126 | if (!should_pack_ref(iter->refname, iter->oid, iter->flags, |
1127 | flags)) | |
50c2d855 MH |
1128 | continue; |
1129 | ||
1130 | /* | |
27d03d04 MH |
1131 | * Add a reference creation for this reference to the |
1132 | * packed-refs transaction: | |
50c2d855 | 1133 | */ |
27d03d04 | 1134 | if (ref_transaction_update(transaction, iter->refname, |
89f3bbdd | 1135 | iter->oid, NULL, |
91774afc | 1136 | REF_NO_DEREF, NULL, &err)) |
27d03d04 MH |
1137 | die("failure preparing to create packed reference %s: %s", |
1138 | iter->refname, err.buf); | |
50c2d855 MH |
1139 | |
1140 | /* Schedule the loose reference for pruning if requested. */ | |
1141 | if ((flags & PACK_REFS_PRUNE)) { | |
1142 | struct ref_to_prune *n; | |
1143 | FLEX_ALLOC_STR(n, name, iter->refname); | |
49e99588 | 1144 | oidcpy(&n->oid, iter->oid); |
50c2d855 MH |
1145 | n->next = refs_to_prune; |
1146 | refs_to_prune = n; | |
1147 | } | |
1148 | } | |
1149 | if (ok != ITER_DONE) | |
1150 | die("error while iterating over references"); | |
7bd9bcf3 | 1151 | |
27d03d04 MH |
1152 | if (ref_transaction_commit(transaction, &err)) |
1153 | die("unable to write new packed-refs: %s", err.buf); | |
1154 | ||
1155 | ref_transaction_free(transaction); | |
1156 | ||
42c7f7ff | 1157 | packed_refs_unlock(refs->packed_ref_store); |
7bd9bcf3 | 1158 | |
22b09cdf | 1159 | prune_refs(refs, &refs_to_prune); |
3478983b | 1160 | strbuf_release(&err); |
7bd9bcf3 MH |
1161 | return 0; |
1162 | } | |
1163 | ||
64da4199 | 1164 | static int files_delete_refs(struct ref_store *ref_store, const char *msg, |
a27dcf89 | 1165 | struct string_list *refnames, unsigned int flags) |
7bd9bcf3 | 1166 | { |
0a95ac5f | 1167 | struct files_ref_store *refs = |
9e7ec634 | 1168 | files_downcast(ref_store, REF_STORE_WRITE, "delete_refs"); |
7bd9bcf3 MH |
1169 | struct strbuf err = STRBUF_INIT; |
1170 | int i, result = 0; | |
1171 | ||
1172 | if (!refnames->nr) | |
1173 | return 0; | |
1174 | ||
e5cc7d7d MH |
1175 | if (packed_refs_lock(refs->packed_ref_store, 0, &err)) |
1176 | goto error; | |
7bd9bcf3 | 1177 | |
2fb330ca | 1178 | if (refs_delete_refs(refs->packed_ref_store, msg, refnames, flags)) { |
e5cc7d7d MH |
1179 | packed_refs_unlock(refs->packed_ref_store); |
1180 | goto error; | |
7bd9bcf3 MH |
1181 | } |
1182 | ||
e5cc7d7d MH |
1183 | packed_refs_unlock(refs->packed_ref_store); |
1184 | ||
7bd9bcf3 MH |
1185 | for (i = 0; i < refnames->nr; i++) { |
1186 | const char *refname = refnames->items[i].string; | |
1187 | ||
64da4199 | 1188 | if (refs_delete_ref(&refs->base, msg, refname, NULL, flags)) |
7bd9bcf3 MH |
1189 | result |= error(_("could not remove reference %s"), refname); |
1190 | } | |
1191 | ||
7bd9bcf3 MH |
1192 | strbuf_release(&err); |
1193 | return result; | |
e5cc7d7d MH |
1194 | |
1195 | error: | |
1196 | /* | |
1197 | * If we failed to rewrite the packed-refs file, then it is | |
1198 | * unsafe to try to remove loose refs, because doing so might | |
1199 | * expose an obsolete packed value for a reference that might | |
1200 | * even point at an object that has been garbage collected. | |
1201 | */ | |
1202 | if (refnames->nr == 1) | |
1203 | error(_("could not delete reference %s: %s"), | |
1204 | refnames->items[0].string, err.buf); | |
1205 | else | |
1206 | error(_("could not delete references: %s"), err.buf); | |
1207 | ||
1208 | strbuf_release(&err); | |
1209 | return -1; | |
7bd9bcf3 MH |
1210 | } |
1211 | ||
1212 | /* | |
1213 | * People using contrib's git-new-workdir have .git/logs/refs -> | |
1214 | * /some/other/path/.git/logs/refs, and that may live on another device. | |
1215 | * | |
1216 | * IOW, to avoid cross device rename errors, the temporary renamed log must | |
1217 | * live into logs/refs. | |
1218 | */ | |
a5c1efd6 | 1219 | #define TMP_RENAMED_LOG "refs/.tmp-renamed-log" |
7bd9bcf3 | 1220 | |
e9dcc305 NTND |
1221 | struct rename_cb { |
1222 | const char *tmp_renamed_log; | |
1223 | int true_errno; | |
1224 | }; | |
1225 | ||
1226 | static int rename_tmp_log_callback(const char *path, void *cb_data) | |
7bd9bcf3 | 1227 | { |
e9dcc305 | 1228 | struct rename_cb *cb = cb_data; |
7bd9bcf3 | 1229 | |
e9dcc305 | 1230 | if (rename(cb->tmp_renamed_log, path)) { |
6a7f3631 MH |
1231 | /* |
1232 | * rename(a, b) when b is an existing directory ought | |
1233 | * to result in ISDIR, but Solaris 5.8 gives ENOTDIR. | |
1234 | * Sheesh. Record the true errno for error reporting, | |
1235 | * but report EISDIR to raceproof_create_file() so | |
1236 | * that it knows to retry. | |
1237 | */ | |
e9dcc305 | 1238 | cb->true_errno = errno; |
6a7f3631 MH |
1239 | if (errno == ENOTDIR) |
1240 | errno = EISDIR; | |
1241 | return -1; | |
1242 | } else { | |
1243 | return 0; | |
7bd9bcf3 | 1244 | } |
6a7f3631 | 1245 | } |
7bd9bcf3 | 1246 | |
802de3da | 1247 | static int rename_tmp_log(struct files_ref_store *refs, const char *newrefname) |
6a7f3631 | 1248 | { |
e9dcc305 NTND |
1249 | struct strbuf path = STRBUF_INIT; |
1250 | struct strbuf tmp = STRBUF_INIT; | |
1251 | struct rename_cb cb; | |
1252 | int ret; | |
6a7f3631 | 1253 | |
802de3da NTND |
1254 | files_reflog_path(refs, &path, newrefname); |
1255 | files_reflog_path(refs, &tmp, TMP_RENAMED_LOG); | |
e9dcc305 NTND |
1256 | cb.tmp_renamed_log = tmp.buf; |
1257 | ret = raceproof_create_file(path.buf, rename_tmp_log_callback, &cb); | |
6a7f3631 MH |
1258 | if (ret) { |
1259 | if (errno == EISDIR) | |
e9dcc305 | 1260 | error("directory not empty: %s", path.buf); |
6a7f3631 | 1261 | else |
990c98d2 | 1262 | error("unable to move logfile %s to %s: %s", |
e9dcc305 NTND |
1263 | tmp.buf, path.buf, |
1264 | strerror(cb.true_errno)); | |
7bd9bcf3 | 1265 | } |
6a7f3631 | 1266 | |
e9dcc305 NTND |
1267 | strbuf_release(&path); |
1268 | strbuf_release(&tmp); | |
7bd9bcf3 MH |
1269 | return ret; |
1270 | } | |
1271 | ||
7bd9bcf3 | 1272 | static int write_ref_to_lockfile(struct ref_lock *lock, |
4417df8c | 1273 | const struct object_id *oid, struct strbuf *err); |
f18a7892 MH |
1274 | static int commit_ref_update(struct files_ref_store *refs, |
1275 | struct ref_lock *lock, | |
4417df8c | 1276 | const struct object_id *oid, const char *logmsg, |
5d9b2de4 | 1277 | struct strbuf *err); |
7bd9bcf3 | 1278 | |
52d59cc6 | 1279 | static int files_copy_or_rename_ref(struct ref_store *ref_store, |
9b6b40d9 | 1280 | const char *oldrefname, const char *newrefname, |
52d59cc6 | 1281 | const char *logmsg, int copy) |
7bd9bcf3 | 1282 | { |
9b6b40d9 | 1283 | struct files_ref_store *refs = |
9e7ec634 | 1284 | files_downcast(ref_store, REF_STORE_WRITE, "rename_ref"); |
4417df8c | 1285 | struct object_id oid, orig_oid; |
7bd9bcf3 MH |
1286 | int flag = 0, logmoved = 0; |
1287 | struct ref_lock *lock; | |
1288 | struct stat loginfo; | |
e9dcc305 NTND |
1289 | struct strbuf sb_oldref = STRBUF_INIT; |
1290 | struct strbuf sb_newref = STRBUF_INIT; | |
1291 | struct strbuf tmp_renamed_log = STRBUF_INIT; | |
1292 | int log, ret; | |
7bd9bcf3 MH |
1293 | struct strbuf err = STRBUF_INIT; |
1294 | ||
802de3da NTND |
1295 | files_reflog_path(refs, &sb_oldref, oldrefname); |
1296 | files_reflog_path(refs, &sb_newref, newrefname); | |
1297 | files_reflog_path(refs, &tmp_renamed_log, TMP_RENAMED_LOG); | |
e9dcc305 NTND |
1298 | |
1299 | log = !lstat(sb_oldref.buf, &loginfo); | |
0a3f07d6 NTND |
1300 | if (log && S_ISLNK(loginfo.st_mode)) { |
1301 | ret = error("reflog for %s is a symlink", oldrefname); | |
1302 | goto out; | |
1303 | } | |
7bd9bcf3 | 1304 | |
2f40e954 NTND |
1305 | if (!refs_resolve_ref_unsafe(&refs->base, oldrefname, |
1306 | RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE, | |
49e61479 | 1307 | &orig_oid, &flag)) { |
0a3f07d6 NTND |
1308 | ret = error("refname %s not found", oldrefname); |
1309 | goto out; | |
1310 | } | |
e711b1af | 1311 | |
0a3f07d6 | 1312 | if (flag & REF_ISSYMREF) { |
52d59cc6 SD |
1313 | if (copy) |
1314 | ret = error("refname %s is a symbolic ref, copying it is not supported", | |
1315 | oldrefname); | |
1316 | else | |
1317 | ret = error("refname %s is a symbolic ref, renaming it is not supported", | |
1318 | oldrefname); | |
0a3f07d6 NTND |
1319 | goto out; |
1320 | } | |
7d2df051 | 1321 | if (!refs_rename_ref_available(&refs->base, oldrefname, newrefname)) { |
0a3f07d6 NTND |
1322 | ret = 1; |
1323 | goto out; | |
1324 | } | |
7bd9bcf3 | 1325 | |
52d59cc6 | 1326 | if (!copy && log && rename(sb_oldref.buf, tmp_renamed_log.buf)) { |
a5c1efd6 | 1327 | ret = error("unable to move logfile logs/%s to logs/"TMP_RENAMED_LOG": %s", |
0a3f07d6 NTND |
1328 | oldrefname, strerror(errno)); |
1329 | goto out; | |
1330 | } | |
7bd9bcf3 | 1331 | |
52d59cc6 SD |
1332 | if (copy && log && copy_file(tmp_renamed_log.buf, sb_oldref.buf, 0644)) { |
1333 | ret = error("unable to copy logfile logs/%s to logs/"TMP_RENAMED_LOG": %s", | |
1334 | oldrefname, strerror(errno)); | |
1335 | goto out; | |
1336 | } | |
1337 | ||
1338 | if (!copy && refs_delete_ref(&refs->base, logmsg, oldrefname, | |
91774afc | 1339 | &orig_oid, REF_NO_DEREF)) { |
7bd9bcf3 MH |
1340 | error("unable to delete old %s", oldrefname); |
1341 | goto rollback; | |
1342 | } | |
1343 | ||
12fd3496 | 1344 | /* |
4417df8c | 1345 | * Since we are doing a shallow lookup, oid is not the |
1346 | * correct value to pass to delete_ref as old_oid. But that | |
1347 | * doesn't matter, because an old_oid check wouldn't add to | |
12fd3496 DT |
1348 | * the safety anyway; we want to delete the reference whatever |
1349 | * its current value. | |
1350 | */ | |
52d59cc6 | 1351 | if (!copy && !refs_read_ref_full(&refs->base, newrefname, |
2f40e954 | 1352 | RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE, |
34c290a6 | 1353 | &oid, NULL) && |
2f40e954 | 1354 | refs_delete_ref(&refs->base, NULL, newrefname, |
91774afc | 1355 | NULL, REF_NO_DEREF)) { |
58364324 | 1356 | if (errno == EISDIR) { |
7bd9bcf3 MH |
1357 | struct strbuf path = STRBUF_INIT; |
1358 | int result; | |
1359 | ||
19e02f4f | 1360 | files_ref_path(refs, &path, newrefname); |
7bd9bcf3 MH |
1361 | result = remove_empty_directories(&path); |
1362 | strbuf_release(&path); | |
1363 | ||
1364 | if (result) { | |
1365 | error("Directory not empty: %s", newrefname); | |
1366 | goto rollback; | |
1367 | } | |
1368 | } else { | |
1369 | error("unable to delete existing %s", newrefname); | |
1370 | goto rollback; | |
1371 | } | |
1372 | } | |
1373 | ||
802de3da | 1374 | if (log && rename_tmp_log(refs, newrefname)) |
7bd9bcf3 MH |
1375 | goto rollback; |
1376 | ||
1377 | logmoved = log; | |
1378 | ||
4f01e508 | 1379 | lock = lock_ref_oid_basic(refs, newrefname, NULL, NULL, NULL, |
91774afc | 1380 | REF_NO_DEREF, NULL, &err); |
7bd9bcf3 | 1381 | if (!lock) { |
52d59cc6 SD |
1382 | if (copy) |
1383 | error("unable to copy '%s' to '%s': %s", oldrefname, newrefname, err.buf); | |
1384 | else | |
1385 | error("unable to rename '%s' to '%s': %s", oldrefname, newrefname, err.buf); | |
7bd9bcf3 MH |
1386 | strbuf_release(&err); |
1387 | goto rollback; | |
1388 | } | |
4417df8c | 1389 | oidcpy(&lock->old_oid, &orig_oid); |
7bd9bcf3 | 1390 | |
4417df8c | 1391 | if (write_ref_to_lockfile(lock, &orig_oid, &err) || |
1392 | commit_ref_update(refs, lock, &orig_oid, logmsg, &err)) { | |
7bd9bcf3 MH |
1393 | error("unable to write current sha1 into %s: %s", newrefname, err.buf); |
1394 | strbuf_release(&err); | |
1395 | goto rollback; | |
1396 | } | |
1397 | ||
0a3f07d6 NTND |
1398 | ret = 0; |
1399 | goto out; | |
7bd9bcf3 MH |
1400 | |
1401 | rollback: | |
4f01e508 | 1402 | lock = lock_ref_oid_basic(refs, oldrefname, NULL, NULL, NULL, |
91774afc | 1403 | REF_NO_DEREF, NULL, &err); |
7bd9bcf3 MH |
1404 | if (!lock) { |
1405 | error("unable to lock %s for rollback: %s", oldrefname, err.buf); | |
1406 | strbuf_release(&err); | |
1407 | goto rollbacklog; | |
1408 | } | |
1409 | ||
1410 | flag = log_all_ref_updates; | |
341fb286 | 1411 | log_all_ref_updates = LOG_REFS_NONE; |
4417df8c | 1412 | if (write_ref_to_lockfile(lock, &orig_oid, &err) || |
1413 | commit_ref_update(refs, lock, &orig_oid, NULL, &err)) { | |
7bd9bcf3 MH |
1414 | error("unable to write current sha1 into %s: %s", oldrefname, err.buf); |
1415 | strbuf_release(&err); | |
1416 | } | |
1417 | log_all_ref_updates = flag; | |
1418 | ||
1419 | rollbacklog: | |
e9dcc305 | 1420 | if (logmoved && rename(sb_newref.buf, sb_oldref.buf)) |
7bd9bcf3 MH |
1421 | error("unable to restore logfile %s from %s: %s", |
1422 | oldrefname, newrefname, strerror(errno)); | |
1423 | if (!logmoved && log && | |
e9dcc305 | 1424 | rename(tmp_renamed_log.buf, sb_oldref.buf)) |
a5c1efd6 | 1425 | error("unable to restore logfile %s from logs/"TMP_RENAMED_LOG": %s", |
7bd9bcf3 | 1426 | oldrefname, strerror(errno)); |
0a3f07d6 NTND |
1427 | ret = 1; |
1428 | out: | |
e9dcc305 NTND |
1429 | strbuf_release(&sb_newref); |
1430 | strbuf_release(&sb_oldref); | |
1431 | strbuf_release(&tmp_renamed_log); | |
1432 | ||
0a3f07d6 | 1433 | return ret; |
7bd9bcf3 MH |
1434 | } |
1435 | ||
52d59cc6 SD |
1436 | static int files_rename_ref(struct ref_store *ref_store, |
1437 | const char *oldrefname, const char *newrefname, | |
1438 | const char *logmsg) | |
1439 | { | |
1440 | return files_copy_or_rename_ref(ref_store, oldrefname, | |
1441 | newrefname, logmsg, 0); | |
1442 | } | |
1443 | ||
1444 | static int files_copy_ref(struct ref_store *ref_store, | |
1445 | const char *oldrefname, const char *newrefname, | |
1446 | const char *logmsg) | |
1447 | { | |
1448 | return files_copy_or_rename_ref(ref_store, oldrefname, | |
1449 | newrefname, logmsg, 1); | |
1450 | } | |
1451 | ||
83a3069a | 1452 | static int close_ref_gently(struct ref_lock *lock) |
7bd9bcf3 | 1453 | { |
ee4d8e45 | 1454 | if (close_lock_file_gently(&lock->lk)) |
7bd9bcf3 MH |
1455 | return -1; |
1456 | return 0; | |
1457 | } | |
1458 | ||
1459 | static int commit_ref(struct ref_lock *lock) | |
1460 | { | |
ee4d8e45 | 1461 | char *path = get_locked_file_path(&lock->lk); |
5387c0d8 MH |
1462 | struct stat st; |
1463 | ||
1464 | if (!lstat(path, &st) && S_ISDIR(st.st_mode)) { | |
1465 | /* | |
1466 | * There is a directory at the path we want to rename | |
1467 | * the lockfile to. Hopefully it is empty; try to | |
1468 | * delete it. | |
1469 | */ | |
1470 | size_t len = strlen(path); | |
1471 | struct strbuf sb_path = STRBUF_INIT; | |
1472 | ||
1473 | strbuf_attach(&sb_path, path, len, len); | |
1474 | ||
1475 | /* | |
1476 | * If this fails, commit_lock_file() will also fail | |
1477 | * and will report the problem. | |
1478 | */ | |
1479 | remove_empty_directories(&sb_path); | |
1480 | strbuf_release(&sb_path); | |
1481 | } else { | |
1482 | free(path); | |
1483 | } | |
1484 | ||
ee4d8e45 | 1485 | if (commit_lock_file(&lock->lk)) |
7bd9bcf3 MH |
1486 | return -1; |
1487 | return 0; | |
1488 | } | |
1489 | ||
1fb0c809 MH |
1490 | static int open_or_create_logfile(const char *path, void *cb) |
1491 | { | |
1492 | int *fd = cb; | |
1493 | ||
1494 | *fd = open(path, O_APPEND | O_WRONLY | O_CREAT, 0666); | |
1495 | return (*fd < 0) ? -1 : 0; | |
1496 | } | |
1497 | ||
7bd9bcf3 | 1498 | /* |
4533e534 MH |
1499 | * Create a reflog for a ref. If force_create = 0, only create the |
1500 | * reflog for certain refs (those for which should_autocreate_reflog | |
1501 | * returns non-zero). Otherwise, create it regardless of the reference | |
1502 | * name. If the logfile already existed or was created, return 0 and | |
1503 | * set *logfd to the file descriptor opened for appending to the file. | |
1504 | * If no logfile exists and we decided not to create one, return 0 and | |
1505 | * set *logfd to -1. On failure, fill in *err, set *logfd to -1, and | |
1506 | * return -1. | |
7bd9bcf3 | 1507 | */ |
802de3da NTND |
1508 | static int log_ref_setup(struct files_ref_store *refs, |
1509 | const char *refname, int force_create, | |
4533e534 | 1510 | int *logfd, struct strbuf *err) |
7bd9bcf3 | 1511 | { |
802de3da NTND |
1512 | struct strbuf logfile_sb = STRBUF_INIT; |
1513 | char *logfile; | |
1514 | ||
1515 | files_reflog_path(refs, &logfile_sb, refname); | |
1516 | logfile = strbuf_detach(&logfile_sb, NULL); | |
7bd9bcf3 | 1517 | |
7bd9bcf3 | 1518 | if (force_create || should_autocreate_reflog(refname)) { |
4533e534 | 1519 | if (raceproof_create_file(logfile, open_or_create_logfile, logfd)) { |
1fb0c809 MH |
1520 | if (errno == ENOENT) |
1521 | strbuf_addf(err, "unable to create directory for '%s': " | |
4533e534 | 1522 | "%s", logfile, strerror(errno)); |
1fb0c809 MH |
1523 | else if (errno == EISDIR) |
1524 | strbuf_addf(err, "there are still logs under '%s'", | |
4533e534 | 1525 | logfile); |
1fb0c809 | 1526 | else |
854bda6b | 1527 | strbuf_addf(err, "unable to append to '%s': %s", |
4533e534 | 1528 | logfile, strerror(errno)); |
7bd9bcf3 | 1529 | |
4533e534 | 1530 | goto error; |
7bd9bcf3 | 1531 | } |
854bda6b | 1532 | } else { |
4533e534 | 1533 | *logfd = open(logfile, O_APPEND | O_WRONLY, 0666); |
e404f459 | 1534 | if (*logfd < 0) { |
854bda6b MH |
1535 | if (errno == ENOENT || errno == EISDIR) { |
1536 | /* | |
1537 | * The logfile doesn't already exist, | |
1538 | * but that is not an error; it only | |
1539 | * means that we won't write log | |
1540 | * entries to it. | |
1541 | */ | |
1542 | ; | |
1543 | } else { | |
1544 | strbuf_addf(err, "unable to append to '%s': %s", | |
4533e534 MH |
1545 | logfile, strerror(errno)); |
1546 | goto error; | |
854bda6b | 1547 | } |
7bd9bcf3 MH |
1548 | } |
1549 | } | |
1550 | ||
e404f459 | 1551 | if (*logfd >= 0) |
4533e534 | 1552 | adjust_shared_perm(logfile); |
854bda6b | 1553 | |
4533e534 | 1554 | free(logfile); |
7bd9bcf3 | 1555 | return 0; |
7bd9bcf3 | 1556 | |
4533e534 MH |
1557 | error: |
1558 | free(logfile); | |
1559 | return -1; | |
7bd9bcf3 | 1560 | } |
7bd9bcf3 | 1561 | |
e3688bd6 DT |
1562 | static int files_create_reflog(struct ref_store *ref_store, |
1563 | const char *refname, int force_create, | |
1564 | struct strbuf *err) | |
7bd9bcf3 | 1565 | { |
802de3da | 1566 | struct files_ref_store *refs = |
9e7ec634 | 1567 | files_downcast(ref_store, REF_STORE_WRITE, "create_reflog"); |
e404f459 | 1568 | int fd; |
7bd9bcf3 | 1569 | |
802de3da | 1570 | if (log_ref_setup(refs, refname, force_create, &fd, err)) |
4533e534 MH |
1571 | return -1; |
1572 | ||
e404f459 MH |
1573 | if (fd >= 0) |
1574 | close(fd); | |
4533e534 MH |
1575 | |
1576 | return 0; | |
7bd9bcf3 MH |
1577 | } |
1578 | ||
4417df8c | 1579 | static int log_ref_write_fd(int fd, const struct object_id *old_oid, |
1580 | const struct object_id *new_oid, | |
7bd9bcf3 MH |
1581 | const char *committer, const char *msg) |
1582 | { | |
1583 | int msglen, written; | |
1584 | unsigned maxlen, len; | |
1585 | char *logrec; | |
1586 | ||
1587 | msglen = msg ? strlen(msg) : 0; | |
1588 | maxlen = strlen(committer) + msglen + 100; | |
1589 | logrec = xmalloc(maxlen); | |
1590 | len = xsnprintf(logrec, maxlen, "%s %s %s\n", | |
4417df8c | 1591 | oid_to_hex(old_oid), |
1592 | oid_to_hex(new_oid), | |
7bd9bcf3 MH |
1593 | committer); |
1594 | if (msglen) | |
1595 | len += copy_reflog_msg(logrec + len - 1, msg) - 1; | |
1596 | ||
1597 | written = len <= maxlen ? write_in_full(fd, logrec, len) : -1; | |
1598 | free(logrec); | |
564bde9a | 1599 | if (written < 0) |
7bd9bcf3 MH |
1600 | return -1; |
1601 | ||
1602 | return 0; | |
1603 | } | |
1604 | ||
802de3da | 1605 | static int files_log_ref_write(struct files_ref_store *refs, |
4417df8c | 1606 | const char *refname, const struct object_id *old_oid, |
1607 | const struct object_id *new_oid, const char *msg, | |
11f8457f | 1608 | int flags, struct strbuf *err) |
7bd9bcf3 | 1609 | { |
e404f459 | 1610 | int logfd, result; |
7bd9bcf3 | 1611 | |
341fb286 CW |
1612 | if (log_all_ref_updates == LOG_REFS_UNSET) |
1613 | log_all_ref_updates = is_bare_repository() ? LOG_REFS_NONE : LOG_REFS_NORMAL; | |
7bd9bcf3 | 1614 | |
802de3da NTND |
1615 | result = log_ref_setup(refs, refname, |
1616 | flags & REF_FORCE_CREATE_REFLOG, | |
4533e534 | 1617 | &logfd, err); |
7bd9bcf3 MH |
1618 | |
1619 | if (result) | |
1620 | return result; | |
1621 | ||
7bd9bcf3 MH |
1622 | if (logfd < 0) |
1623 | return 0; | |
4417df8c | 1624 | result = log_ref_write_fd(logfd, old_oid, new_oid, |
7bd9bcf3 MH |
1625 | git_committer_info(0), msg); |
1626 | if (result) { | |
e9dcc305 | 1627 | struct strbuf sb = STRBUF_INIT; |
87b21e05 MH |
1628 | int save_errno = errno; |
1629 | ||
802de3da | 1630 | files_reflog_path(refs, &sb, refname); |
87b21e05 | 1631 | strbuf_addf(err, "unable to append to '%s': %s", |
e9dcc305 NTND |
1632 | sb.buf, strerror(save_errno)); |
1633 | strbuf_release(&sb); | |
7bd9bcf3 MH |
1634 | close(logfd); |
1635 | return -1; | |
1636 | } | |
1637 | if (close(logfd)) { | |
e9dcc305 | 1638 | struct strbuf sb = STRBUF_INIT; |
87b21e05 MH |
1639 | int save_errno = errno; |
1640 | ||
802de3da | 1641 | files_reflog_path(refs, &sb, refname); |
87b21e05 | 1642 | strbuf_addf(err, "unable to append to '%s': %s", |
e9dcc305 NTND |
1643 | sb.buf, strerror(save_errno)); |
1644 | strbuf_release(&sb); | |
7bd9bcf3 MH |
1645 | return -1; |
1646 | } | |
1647 | return 0; | |
1648 | } | |
1649 | ||
7bd9bcf3 MH |
1650 | /* |
1651 | * Write sha1 into the open lockfile, then close the lockfile. On | |
1652 | * errors, rollback the lockfile, fill in *err and | |
1653 | * return -1. | |
1654 | */ | |
1655 | static int write_ref_to_lockfile(struct ref_lock *lock, | |
4417df8c | 1656 | const struct object_id *oid, struct strbuf *err) |
7bd9bcf3 MH |
1657 | { |
1658 | static char term = '\n'; | |
1659 | struct object *o; | |
1660 | int fd; | |
1661 | ||
c251c83d | 1662 | o = parse_object(oid); |
7bd9bcf3 MH |
1663 | if (!o) { |
1664 | strbuf_addf(err, | |
0568c8e9 | 1665 | "trying to write ref '%s' with nonexistent object %s", |
4417df8c | 1666 | lock->ref_name, oid_to_hex(oid)); |
7bd9bcf3 MH |
1667 | unlock_ref(lock); |
1668 | return -1; | |
1669 | } | |
1670 | if (o->type != OBJ_COMMIT && is_branch(lock->ref_name)) { | |
1671 | strbuf_addf(err, | |
0568c8e9 | 1672 | "trying to write non-commit object %s to branch '%s'", |
4417df8c | 1673 | oid_to_hex(oid), lock->ref_name); |
7bd9bcf3 MH |
1674 | unlock_ref(lock); |
1675 | return -1; | |
1676 | } | |
ee4d8e45 | 1677 | fd = get_lock_file_fd(&lock->lk); |
06f46f23 JK |
1678 | if (write_in_full(fd, oid_to_hex(oid), GIT_SHA1_HEXSZ) < 0 || |
1679 | write_in_full(fd, &term, 1) < 0 || | |
83a3069a | 1680 | close_ref_gently(lock) < 0) { |
7bd9bcf3 | 1681 | strbuf_addf(err, |
ee4d8e45 | 1682 | "couldn't write '%s'", get_lock_file_path(&lock->lk)); |
7bd9bcf3 MH |
1683 | unlock_ref(lock); |
1684 | return -1; | |
1685 | } | |
1686 | return 0; | |
1687 | } | |
1688 | ||
1689 | /* | |
1690 | * Commit a change to a loose reference that has already been written | |
1691 | * to the loose reference lockfile. Also update the reflogs if | |
1692 | * necessary, using the specified lockmsg (which can be NULL). | |
1693 | */ | |
f18a7892 MH |
1694 | static int commit_ref_update(struct files_ref_store *refs, |
1695 | struct ref_lock *lock, | |
4417df8c | 1696 | const struct object_id *oid, const char *logmsg, |
5d9b2de4 | 1697 | struct strbuf *err) |
7bd9bcf3 | 1698 | { |
32c597e7 | 1699 | files_assert_main_repository(refs, "commit_ref_update"); |
00eebe35 MH |
1700 | |
1701 | clear_loose_ref_cache(refs); | |
802de3da | 1702 | if (files_log_ref_write(refs, lock->ref_name, |
4417df8c | 1703 | &lock->old_oid, oid, |
81b1b6d4 | 1704 | logmsg, 0, err)) { |
7bd9bcf3 | 1705 | char *old_msg = strbuf_detach(err, NULL); |
0568c8e9 | 1706 | strbuf_addf(err, "cannot update the ref '%s': %s", |
7bd9bcf3 MH |
1707 | lock->ref_name, old_msg); |
1708 | free(old_msg); | |
1709 | unlock_ref(lock); | |
1710 | return -1; | |
1711 | } | |
7a418f3a MH |
1712 | |
1713 | if (strcmp(lock->ref_name, "HEAD") != 0) { | |
7bd9bcf3 MH |
1714 | /* |
1715 | * Special hack: If a branch is updated directly and HEAD | |
1716 | * points to it (may happen on the remote side of a push | |
1717 | * for example) then logically the HEAD reflog should be | |
1718 | * updated too. | |
1719 | * A generic solution implies reverse symref information, | |
1720 | * but finding all symrefs pointing to the given branch | |
1721 | * would be rather costly for this rare event (the direct | |
1722 | * update of a branch) to be worth it. So let's cheat and | |
1723 | * check with HEAD only which should cover 99% of all usage | |
1724 | * scenarios (even 100% of the default ones). | |
1725 | */ | |
7bd9bcf3 MH |
1726 | int head_flag; |
1727 | const char *head_ref; | |
7a418f3a | 1728 | |
2f40e954 NTND |
1729 | head_ref = refs_resolve_ref_unsafe(&refs->base, "HEAD", |
1730 | RESOLVE_REF_READING, | |
e691b027 | 1731 | NULL, &head_flag); |
7bd9bcf3 MH |
1732 | if (head_ref && (head_flag & REF_ISSYMREF) && |
1733 | !strcmp(head_ref, lock->ref_name)) { | |
1734 | struct strbuf log_err = STRBUF_INIT; | |
802de3da | 1735 | if (files_log_ref_write(refs, "HEAD", |
4417df8c | 1736 | &lock->old_oid, oid, |
802de3da | 1737 | logmsg, 0, &log_err)) { |
7bd9bcf3 MH |
1738 | error("%s", log_err.buf); |
1739 | strbuf_release(&log_err); | |
1740 | } | |
1741 | } | |
1742 | } | |
7a418f3a | 1743 | |
7bd9bcf3 | 1744 | if (commit_ref(lock)) { |
0568c8e9 | 1745 | strbuf_addf(err, "couldn't set '%s'", lock->ref_name); |
7bd9bcf3 MH |
1746 | unlock_ref(lock); |
1747 | return -1; | |
1748 | } | |
1749 | ||
1750 | unlock_ref(lock); | |
1751 | return 0; | |
1752 | } | |
1753 | ||
370e5ad6 | 1754 | static int create_ref_symlink(struct ref_lock *lock, const char *target) |
7bd9bcf3 | 1755 | { |
370e5ad6 | 1756 | int ret = -1; |
7bd9bcf3 | 1757 | #ifndef NO_SYMLINK_HEAD |
ee4d8e45 | 1758 | char *ref_path = get_locked_file_path(&lock->lk); |
370e5ad6 JK |
1759 | unlink(ref_path); |
1760 | ret = symlink(target, ref_path); | |
1761 | free(ref_path); | |
1762 | ||
1763 | if (ret) | |
7bd9bcf3 | 1764 | fprintf(stderr, "no symlink - falling back to symbolic ref\n"); |
7bd9bcf3 | 1765 | #endif |
370e5ad6 JK |
1766 | return ret; |
1767 | } | |
7bd9bcf3 | 1768 | |
802de3da NTND |
1769 | static void update_symref_reflog(struct files_ref_store *refs, |
1770 | struct ref_lock *lock, const char *refname, | |
370e5ad6 JK |
1771 | const char *target, const char *logmsg) |
1772 | { | |
1773 | struct strbuf err = STRBUF_INIT; | |
4417df8c | 1774 | struct object_id new_oid; |
2f40e954 NTND |
1775 | if (logmsg && |
1776 | !refs_read_ref_full(&refs->base, target, | |
34c290a6 | 1777 | RESOLVE_REF_READING, &new_oid, NULL) && |
4417df8c | 1778 | files_log_ref_write(refs, refname, &lock->old_oid, |
1779 | &new_oid, logmsg, 0, &err)) { | |
7bd9bcf3 MH |
1780 | error("%s", err.buf); |
1781 | strbuf_release(&err); | |
1782 | } | |
370e5ad6 | 1783 | } |
7bd9bcf3 | 1784 | |
802de3da NTND |
1785 | static int create_symref_locked(struct files_ref_store *refs, |
1786 | struct ref_lock *lock, const char *refname, | |
370e5ad6 JK |
1787 | const char *target, const char *logmsg) |
1788 | { | |
1789 | if (prefer_symlink_refs && !create_ref_symlink(lock, target)) { | |
802de3da | 1790 | update_symref_reflog(refs, lock, refname, target, logmsg); |
370e5ad6 JK |
1791 | return 0; |
1792 | } | |
1793 | ||
ee4d8e45 | 1794 | if (!fdopen_lock_file(&lock->lk, "w")) |
370e5ad6 | 1795 | return error("unable to fdopen %s: %s", |
ee4d8e45 | 1796 | lock->lk.tempfile->filename.buf, strerror(errno)); |
370e5ad6 | 1797 | |
802de3da | 1798 | update_symref_reflog(refs, lock, refname, target, logmsg); |
396da8f7 | 1799 | |
370e5ad6 | 1800 | /* no error check; commit_ref will check ferror */ |
ee4d8e45 | 1801 | fprintf(lock->lk.tempfile->fp, "ref: %s\n", target); |
370e5ad6 JK |
1802 | if (commit_ref(lock) < 0) |
1803 | return error("unable to write symref for %s: %s", refname, | |
1804 | strerror(errno)); | |
7bd9bcf3 MH |
1805 | return 0; |
1806 | } | |
1807 | ||
284689ba MH |
1808 | static int files_create_symref(struct ref_store *ref_store, |
1809 | const char *refname, const char *target, | |
1810 | const char *logmsg) | |
370e5ad6 | 1811 | { |
7eb27cdf | 1812 | struct files_ref_store *refs = |
9e7ec634 | 1813 | files_downcast(ref_store, REF_STORE_WRITE, "create_symref"); |
370e5ad6 JK |
1814 | struct strbuf err = STRBUF_INIT; |
1815 | struct ref_lock *lock; | |
1816 | int ret; | |
1817 | ||
4f01e508 | 1818 | lock = lock_ref_oid_basic(refs, refname, NULL, |
91774afc | 1819 | NULL, NULL, REF_NO_DEREF, NULL, |
4f01e508 | 1820 | &err); |
370e5ad6 JK |
1821 | if (!lock) { |
1822 | error("%s", err.buf); | |
1823 | strbuf_release(&err); | |
1824 | return -1; | |
1825 | } | |
1826 | ||
802de3da | 1827 | ret = create_symref_locked(refs, lock, refname, target, logmsg); |
370e5ad6 JK |
1828 | unlock_ref(lock); |
1829 | return ret; | |
1830 | } | |
1831 | ||
e3688bd6 DT |
1832 | static int files_reflog_exists(struct ref_store *ref_store, |
1833 | const char *refname) | |
7bd9bcf3 | 1834 | { |
802de3da | 1835 | struct files_ref_store *refs = |
9e7ec634 | 1836 | files_downcast(ref_store, REF_STORE_READ, "reflog_exists"); |
e9dcc305 | 1837 | struct strbuf sb = STRBUF_INIT; |
7bd9bcf3 | 1838 | struct stat st; |
e9dcc305 | 1839 | int ret; |
7bd9bcf3 | 1840 | |
802de3da | 1841 | files_reflog_path(refs, &sb, refname); |
e9dcc305 NTND |
1842 | ret = !lstat(sb.buf, &st) && S_ISREG(st.st_mode); |
1843 | strbuf_release(&sb); | |
1844 | return ret; | |
7bd9bcf3 MH |
1845 | } |
1846 | ||
e3688bd6 DT |
1847 | static int files_delete_reflog(struct ref_store *ref_store, |
1848 | const char *refname) | |
7bd9bcf3 | 1849 | { |
802de3da | 1850 | struct files_ref_store *refs = |
9e7ec634 | 1851 | files_downcast(ref_store, REF_STORE_WRITE, "delete_reflog"); |
e9dcc305 NTND |
1852 | struct strbuf sb = STRBUF_INIT; |
1853 | int ret; | |
1854 | ||
802de3da | 1855 | files_reflog_path(refs, &sb, refname); |
e9dcc305 NTND |
1856 | ret = remove_path(sb.buf); |
1857 | strbuf_release(&sb); | |
1858 | return ret; | |
7bd9bcf3 MH |
1859 | } |
1860 | ||
1861 | static int show_one_reflog_ent(struct strbuf *sb, each_reflog_ent_fn fn, void *cb_data) | |
1862 | { | |
9461d272 | 1863 | struct object_id ooid, noid; |
7bd9bcf3 | 1864 | char *email_end, *message; |
dddbad72 | 1865 | timestamp_t timestamp; |
7bd9bcf3 | 1866 | int tz; |
43bc3b6c | 1867 | const char *p = sb->buf; |
7bd9bcf3 MH |
1868 | |
1869 | /* old SP new SP name <email> SP time TAB msg LF */ | |
43bc3b6c | 1870 | if (!sb->len || sb->buf[sb->len - 1] != '\n' || |
1871 | parse_oid_hex(p, &ooid, &p) || *p++ != ' ' || | |
1872 | parse_oid_hex(p, &noid, &p) || *p++ != ' ' || | |
1873 | !(email_end = strchr(p, '>')) || | |
7bd9bcf3 | 1874 | email_end[1] != ' ' || |
1aeb7e75 | 1875 | !(timestamp = parse_timestamp(email_end + 2, &message, 10)) || |
7bd9bcf3 MH |
1876 | !message || message[0] != ' ' || |
1877 | (message[1] != '+' && message[1] != '-') || | |
1878 | !isdigit(message[2]) || !isdigit(message[3]) || | |
1879 | !isdigit(message[4]) || !isdigit(message[5])) | |
1880 | return 0; /* corrupt? */ | |
1881 | email_end[1] = '\0'; | |
1882 | tz = strtol(message + 1, NULL, 10); | |
1883 | if (message[6] != '\t') | |
1884 | message += 6; | |
1885 | else | |
1886 | message += 7; | |
43bc3b6c | 1887 | return fn(&ooid, &noid, p, timestamp, tz, message, cb_data); |
7bd9bcf3 MH |
1888 | } |
1889 | ||
1890 | static char *find_beginning_of_line(char *bob, char *scan) | |
1891 | { | |
1892 | while (bob < scan && *(--scan) != '\n') | |
1893 | ; /* keep scanning backwards */ | |
1894 | /* | |
1895 | * Return either beginning of the buffer, or LF at the end of | |
1896 | * the previous line. | |
1897 | */ | |
1898 | return scan; | |
1899 | } | |
1900 | ||
e3688bd6 DT |
1901 | static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store, |
1902 | const char *refname, | |
1903 | each_reflog_ent_fn fn, | |
1904 | void *cb_data) | |
7bd9bcf3 | 1905 | { |
802de3da | 1906 | struct files_ref_store *refs = |
9e7ec634 NTND |
1907 | files_downcast(ref_store, REF_STORE_READ, |
1908 | "for_each_reflog_ent_reverse"); | |
7bd9bcf3 MH |
1909 | struct strbuf sb = STRBUF_INIT; |
1910 | FILE *logfp; | |
1911 | long pos; | |
1912 | int ret = 0, at_tail = 1; | |
1913 | ||
802de3da | 1914 | files_reflog_path(refs, &sb, refname); |
e9dcc305 NTND |
1915 | logfp = fopen(sb.buf, "r"); |
1916 | strbuf_release(&sb); | |
7bd9bcf3 MH |
1917 | if (!logfp) |
1918 | return -1; | |
1919 | ||
1920 | /* Jump to the end */ | |
1921 | if (fseek(logfp, 0, SEEK_END) < 0) | |
be686f03 RS |
1922 | ret = error("cannot seek back reflog for %s: %s", |
1923 | refname, strerror(errno)); | |
7bd9bcf3 MH |
1924 | pos = ftell(logfp); |
1925 | while (!ret && 0 < pos) { | |
1926 | int cnt; | |
1927 | size_t nread; | |
1928 | char buf[BUFSIZ]; | |
1929 | char *endp, *scanp; | |
1930 | ||
1931 | /* Fill next block from the end */ | |
1932 | cnt = (sizeof(buf) < pos) ? sizeof(buf) : pos; | |
be686f03 RS |
1933 | if (fseek(logfp, pos - cnt, SEEK_SET)) { |
1934 | ret = error("cannot seek back reflog for %s: %s", | |
1935 | refname, strerror(errno)); | |
1936 | break; | |
1937 | } | |
7bd9bcf3 | 1938 | nread = fread(buf, cnt, 1, logfp); |
be686f03 RS |
1939 | if (nread != 1) { |
1940 | ret = error("cannot read %d bytes from reflog for %s: %s", | |
1941 | cnt, refname, strerror(errno)); | |
1942 | break; | |
1943 | } | |
7bd9bcf3 MH |
1944 | pos -= cnt; |
1945 | ||
1946 | scanp = endp = buf + cnt; | |
1947 | if (at_tail && scanp[-1] == '\n') | |
1948 | /* Looking at the final LF at the end of the file */ | |
1949 | scanp--; | |
1950 | at_tail = 0; | |
1951 | ||
1952 | while (buf < scanp) { | |
1953 | /* | |
1954 | * terminating LF of the previous line, or the beginning | |
1955 | * of the buffer. | |
1956 | */ | |
1957 | char *bp; | |
1958 | ||
1959 | bp = find_beginning_of_line(buf, scanp); | |
1960 | ||
1961 | if (*bp == '\n') { | |
1962 | /* | |
1963 | * The newline is the end of the previous line, | |
1964 | * so we know we have complete line starting | |
1965 | * at (bp + 1). Prefix it onto any prior data | |
1966 | * we collected for the line and process it. | |
1967 | */ | |
1968 | strbuf_splice(&sb, 0, 0, bp + 1, endp - (bp + 1)); | |
1969 | scanp = bp; | |
1970 | endp = bp + 1; | |
1971 | ret = show_one_reflog_ent(&sb, fn, cb_data); | |
1972 | strbuf_reset(&sb); | |
1973 | if (ret) | |
1974 | break; | |
1975 | } else if (!pos) { | |
1976 | /* | |
1977 | * We are at the start of the buffer, and the | |
1978 | * start of the file; there is no previous | |
1979 | * line, and we have everything for this one. | |
1980 | * Process it, and we can end the loop. | |
1981 | */ | |
1982 | strbuf_splice(&sb, 0, 0, buf, endp - buf); | |
1983 | ret = show_one_reflog_ent(&sb, fn, cb_data); | |
1984 | strbuf_reset(&sb); | |
1985 | break; | |
1986 | } | |
1987 | ||
1988 | if (bp == buf) { | |
1989 | /* | |
1990 | * We are at the start of the buffer, and there | |
1991 | * is more file to read backwards. Which means | |
1992 | * we are in the middle of a line. Note that we | |
1993 | * may get here even if *bp was a newline; that | |
1994 | * just means we are at the exact end of the | |
1995 | * previous line, rather than some spot in the | |
1996 | * middle. | |
1997 | * | |
1998 | * Save away what we have to be combined with | |
1999 | * the data from the next read. | |
2000 | */ | |
2001 | strbuf_splice(&sb, 0, 0, buf, endp - buf); | |
2002 | break; | |
2003 | } | |
2004 | } | |
2005 | ||
2006 | } | |
2007 | if (!ret && sb.len) | |
2008 | die("BUG: reverse reflog parser had leftover data"); | |
2009 | ||
2010 | fclose(logfp); | |
2011 | strbuf_release(&sb); | |
2012 | return ret; | |
2013 | } | |
2014 | ||
e3688bd6 DT |
2015 | static int files_for_each_reflog_ent(struct ref_store *ref_store, |
2016 | const char *refname, | |
2017 | each_reflog_ent_fn fn, void *cb_data) | |
7bd9bcf3 | 2018 | { |
802de3da | 2019 | struct files_ref_store *refs = |
9e7ec634 NTND |
2020 | files_downcast(ref_store, REF_STORE_READ, |
2021 | "for_each_reflog_ent"); | |
7bd9bcf3 MH |
2022 | FILE *logfp; |
2023 | struct strbuf sb = STRBUF_INIT; | |
2024 | int ret = 0; | |
2025 | ||
802de3da | 2026 | files_reflog_path(refs, &sb, refname); |
e9dcc305 NTND |
2027 | logfp = fopen(sb.buf, "r"); |
2028 | strbuf_release(&sb); | |
7bd9bcf3 MH |
2029 | if (!logfp) |
2030 | return -1; | |
2031 | ||
2032 | while (!ret && !strbuf_getwholeline(&sb, logfp, '\n')) | |
2033 | ret = show_one_reflog_ent(&sb, fn, cb_data); | |
2034 | fclose(logfp); | |
2035 | strbuf_release(&sb); | |
2036 | return ret; | |
2037 | } | |
7bd9bcf3 | 2038 | |
2880d16f MH |
2039 | struct files_reflog_iterator { |
2040 | struct ref_iterator base; | |
7bd9bcf3 | 2041 | |
2f40e954 | 2042 | struct ref_store *ref_store; |
2880d16f MH |
2043 | struct dir_iterator *dir_iterator; |
2044 | struct object_id oid; | |
2045 | }; | |
7bd9bcf3 | 2046 | |
2880d16f MH |
2047 | static int files_reflog_iterator_advance(struct ref_iterator *ref_iterator) |
2048 | { | |
2049 | struct files_reflog_iterator *iter = | |
2050 | (struct files_reflog_iterator *)ref_iterator; | |
2051 | struct dir_iterator *diter = iter->dir_iterator; | |
2052 | int ok; | |
2053 | ||
2054 | while ((ok = dir_iterator_advance(diter)) == ITER_OK) { | |
2055 | int flags; | |
2056 | ||
2057 | if (!S_ISREG(diter->st.st_mode)) | |
7bd9bcf3 | 2058 | continue; |
2880d16f MH |
2059 | if (diter->basename[0] == '.') |
2060 | continue; | |
2061 | if (ends_with(diter->basename, ".lock")) | |
7bd9bcf3 | 2062 | continue; |
7bd9bcf3 | 2063 | |
2f40e954 NTND |
2064 | if (refs_read_ref_full(iter->ref_store, |
2065 | diter->relative_path, 0, | |
34c290a6 | 2066 | &iter->oid, &flags)) { |
2880d16f MH |
2067 | error("bad ref for %s", diter->path.buf); |
2068 | continue; | |
7bd9bcf3 | 2069 | } |
2880d16f MH |
2070 | |
2071 | iter->base.refname = diter->relative_path; | |
2072 | iter->base.oid = &iter->oid; | |
2073 | iter->base.flags = flags; | |
2074 | return ITER_OK; | |
7bd9bcf3 | 2075 | } |
2880d16f MH |
2076 | |
2077 | iter->dir_iterator = NULL; | |
2078 | if (ref_iterator_abort(ref_iterator) == ITER_ERROR) | |
2079 | ok = ITER_ERROR; | |
2080 | return ok; | |
2081 | } | |
2082 | ||
2083 | static int files_reflog_iterator_peel(struct ref_iterator *ref_iterator, | |
2084 | struct object_id *peeled) | |
2085 | { | |
2086 | die("BUG: ref_iterator_peel() called for reflog_iterator"); | |
2087 | } | |
2088 | ||
2089 | static int files_reflog_iterator_abort(struct ref_iterator *ref_iterator) | |
2090 | { | |
2091 | struct files_reflog_iterator *iter = | |
2092 | (struct files_reflog_iterator *)ref_iterator; | |
2093 | int ok = ITER_DONE; | |
2094 | ||
2095 | if (iter->dir_iterator) | |
2096 | ok = dir_iterator_abort(iter->dir_iterator); | |
2097 | ||
2098 | base_ref_iterator_free(ref_iterator); | |
2099 | return ok; | |
2100 | } | |
2101 | ||
2102 | static struct ref_iterator_vtable files_reflog_iterator_vtable = { | |
2103 | files_reflog_iterator_advance, | |
2104 | files_reflog_iterator_peel, | |
2105 | files_reflog_iterator_abort | |
2106 | }; | |
2107 | ||
944b4e30 NTND |
2108 | static struct ref_iterator *reflog_iterator_begin(struct ref_store *ref_store, |
2109 | const char *gitdir) | |
2880d16f MH |
2110 | { |
2111 | struct files_reflog_iterator *iter = xcalloc(1, sizeof(*iter)); | |
2112 | struct ref_iterator *ref_iterator = &iter->base; | |
e9dcc305 | 2113 | struct strbuf sb = STRBUF_INIT; |
2880d16f | 2114 | |
8738a8a4 | 2115 | base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable, 0); |
944b4e30 | 2116 | strbuf_addf(&sb, "%s/logs", gitdir); |
e9dcc305 | 2117 | iter->dir_iterator = dir_iterator_begin(sb.buf); |
2f40e954 | 2118 | iter->ref_store = ref_store; |
e9dcc305 | 2119 | strbuf_release(&sb); |
944b4e30 | 2120 | |
2880d16f | 2121 | return ref_iterator; |
7bd9bcf3 MH |
2122 | } |
2123 | ||
944b4e30 NTND |
2124 | static enum iterator_selection reflog_iterator_select( |
2125 | struct ref_iterator *iter_worktree, | |
2126 | struct ref_iterator *iter_common, | |
2127 | void *cb_data) | |
2128 | { | |
2129 | if (iter_worktree) { | |
2130 | /* | |
2131 | * We're a bit loose here. We probably should ignore | |
2132 | * common refs if they are accidentally added as | |
2133 | * per-worktree refs. | |
2134 | */ | |
2135 | return ITER_SELECT_0; | |
2136 | } else if (iter_common) { | |
2137 | if (ref_type(iter_common->refname) == REF_TYPE_NORMAL) | |
2138 | return ITER_SELECT_1; | |
2139 | ||
2140 | /* | |
2141 | * The main ref store may contain main worktree's | |
2142 | * per-worktree refs, which should be ignored | |
2143 | */ | |
2144 | return ITER_SKIP_1; | |
2145 | } else | |
2146 | return ITER_DONE; | |
2147 | } | |
2148 | ||
2149 | static struct ref_iterator *files_reflog_iterator_begin(struct ref_store *ref_store) | |
2150 | { | |
2151 | struct files_ref_store *refs = | |
2152 | files_downcast(ref_store, REF_STORE_READ, | |
2153 | "reflog_iterator_begin"); | |
2154 | ||
2155 | if (!strcmp(refs->gitdir, refs->gitcommondir)) { | |
2156 | return reflog_iterator_begin(ref_store, refs->gitcommondir); | |
2157 | } else { | |
2158 | return merge_ref_iterator_begin( | |
1a2e1a76 | 2159 | 0, |
944b4e30 NTND |
2160 | reflog_iterator_begin(ref_store, refs->gitdir), |
2161 | reflog_iterator_begin(ref_store, refs->gitcommondir), | |
2162 | reflog_iterator_select, refs); | |
2163 | } | |
2164 | } | |
2165 | ||
165056b2 | 2166 | /* |
92b1551b MH |
2167 | * If update is a direct update of head_ref (the reference pointed to |
2168 | * by HEAD), then add an extra REF_LOG_ONLY update for HEAD. | |
2169 | */ | |
2170 | static int split_head_update(struct ref_update *update, | |
2171 | struct ref_transaction *transaction, | |
2172 | const char *head_ref, | |
2173 | struct string_list *affected_refnames, | |
2174 | struct strbuf *err) | |
2175 | { | |
2176 | struct string_list_item *item; | |
2177 | struct ref_update *new_update; | |
2178 | ||
2179 | if ((update->flags & REF_LOG_ONLY) || | |
2180 | (update->flags & REF_ISPRUNING) || | |
2181 | (update->flags & REF_UPDATE_VIA_HEAD)) | |
2182 | return 0; | |
2183 | ||
2184 | if (strcmp(update->refname, head_ref)) | |
2185 | return 0; | |
2186 | ||
2187 | /* | |
2188 | * First make sure that HEAD is not already in the | |
276d0e35 | 2189 | * transaction. This check is O(lg N) in the transaction |
92b1551b MH |
2190 | * size, but it happens at most once per transaction. |
2191 | */ | |
276d0e35 | 2192 | if (string_list_has_string(affected_refnames, "HEAD")) { |
92b1551b MH |
2193 | /* An entry already existed */ |
2194 | strbuf_addf(err, | |
2195 | "multiple updates for 'HEAD' (including one " | |
2196 | "via its referent '%s') are not allowed", | |
2197 | update->refname); | |
2198 | return TRANSACTION_NAME_CONFLICT; | |
2199 | } | |
2200 | ||
2201 | new_update = ref_transaction_add_update( | |
2202 | transaction, "HEAD", | |
91774afc | 2203 | update->flags | REF_LOG_ONLY | REF_NO_DEREF, |
89f3bbdd | 2204 | &update->new_oid, &update->old_oid, |
92b1551b MH |
2205 | update->msg); |
2206 | ||
276d0e35 MÅ |
2207 | /* |
2208 | * Add "HEAD". This insertion is O(N) in the transaction | |
2209 | * size, but it happens at most once per transaction. | |
2210 | * Add new_update->refname instead of a literal "HEAD". | |
2211 | */ | |
2212 | if (strcmp(new_update->refname, "HEAD")) | |
2213 | BUG("%s unexpectedly not 'HEAD'", new_update->refname); | |
2214 | item = string_list_insert(affected_refnames, new_update->refname); | |
92b1551b MH |
2215 | item->util = new_update; |
2216 | ||
2217 | return 0; | |
2218 | } | |
2219 | ||
2220 | /* | |
2221 | * update is for a symref that points at referent and doesn't have | |
91774afc MH |
2222 | * REF_NO_DEREF set. Split it into two updates: |
2223 | * - The original update, but with REF_LOG_ONLY and REF_NO_DEREF set | |
92b1551b MH |
2224 | * - A new, separate update for the referent reference |
2225 | * Note that the new update will itself be subject to splitting when | |
2226 | * the iteration gets to it. | |
2227 | */ | |
fcc42ea0 MH |
2228 | static int split_symref_update(struct files_ref_store *refs, |
2229 | struct ref_update *update, | |
92b1551b MH |
2230 | const char *referent, |
2231 | struct ref_transaction *transaction, | |
2232 | struct string_list *affected_refnames, | |
2233 | struct strbuf *err) | |
2234 | { | |
2235 | struct string_list_item *item; | |
2236 | struct ref_update *new_update; | |
2237 | unsigned int new_flags; | |
2238 | ||
2239 | /* | |
2240 | * First make sure that referent is not already in the | |
c299468b | 2241 | * transaction. This check is O(lg N) in the transaction |
92b1551b MH |
2242 | * size, but it happens at most once per symref in a |
2243 | * transaction. | |
2244 | */ | |
c299468b MÅ |
2245 | if (string_list_has_string(affected_refnames, referent)) { |
2246 | /* An entry already exists */ | |
92b1551b MH |
2247 | strbuf_addf(err, |
2248 | "multiple updates for '%s' (including one " | |
2249 | "via symref '%s') are not allowed", | |
2250 | referent, update->refname); | |
2251 | return TRANSACTION_NAME_CONFLICT; | |
2252 | } | |
2253 | ||
2254 | new_flags = update->flags; | |
2255 | if (!strcmp(update->refname, "HEAD")) { | |
2256 | /* | |
2257 | * Record that the new update came via HEAD, so that | |
2258 | * when we process it, split_head_update() doesn't try | |
2259 | * to add another reflog update for HEAD. Note that | |
2260 | * this bit will be propagated if the new_update | |
2261 | * itself needs to be split. | |
2262 | */ | |
2263 | new_flags |= REF_UPDATE_VIA_HEAD; | |
2264 | } | |
2265 | ||
2266 | new_update = ref_transaction_add_update( | |
2267 | transaction, referent, new_flags, | |
89f3bbdd | 2268 | &update->new_oid, &update->old_oid, |
92b1551b MH |
2269 | update->msg); |
2270 | ||
6e30b2f6 MH |
2271 | new_update->parent_update = update; |
2272 | ||
2273 | /* | |
2274 | * Change the symbolic ref update to log only. Also, it | |
2275 | * doesn't need to check its old SHA-1 value, as that will be | |
2276 | * done when new_update is processed. | |
2277 | */ | |
91774afc | 2278 | update->flags |= REF_LOG_ONLY | REF_NO_DEREF; |
6e30b2f6 | 2279 | update->flags &= ~REF_HAVE_OLD; |
92b1551b | 2280 | |
c299468b MÅ |
2281 | /* |
2282 | * Add the referent. This insertion is O(N) in the transaction | |
2283 | * size, but it happens at most once per symref in a | |
2284 | * transaction. Make sure to add new_update->refname, which will | |
2285 | * be valid as long as affected_refnames is in use, and NOT | |
2286 | * referent, which might soon be freed by our caller. | |
2287 | */ | |
2288 | item = string_list_insert(affected_refnames, new_update->refname); | |
2289 | if (item->util) | |
2290 | BUG("%s unexpectedly found in affected_refnames", | |
2291 | new_update->refname); | |
92b1551b MH |
2292 | item->util = new_update; |
2293 | ||
2294 | return 0; | |
2295 | } | |
2296 | ||
6e30b2f6 MH |
2297 | /* |
2298 | * Return the refname under which update was originally requested. | |
2299 | */ | |
2300 | static const char *original_update_refname(struct ref_update *update) | |
2301 | { | |
2302 | while (update->parent_update) | |
2303 | update = update->parent_update; | |
2304 | ||
2305 | return update->refname; | |
2306 | } | |
2307 | ||
e3f51039 MH |
2308 | /* |
2309 | * Check whether the REF_HAVE_OLD and old_oid values stored in update | |
2310 | * are consistent with oid, which is the reference's current value. If | |
2311 | * everything is OK, return 0; otherwise, write an error message to | |
2312 | * err and return -1. | |
2313 | */ | |
2314 | static int check_old_oid(struct ref_update *update, struct object_id *oid, | |
2315 | struct strbuf *err) | |
2316 | { | |
2317 | if (!(update->flags & REF_HAVE_OLD) || | |
98491298 | 2318 | !oidcmp(oid, &update->old_oid)) |
e3f51039 MH |
2319 | return 0; |
2320 | ||
98491298 | 2321 | if (is_null_oid(&update->old_oid)) |
e3f51039 MH |
2322 | strbuf_addf(err, "cannot lock ref '%s': " |
2323 | "reference already exists", | |
2324 | original_update_refname(update)); | |
2325 | else if (is_null_oid(oid)) | |
2326 | strbuf_addf(err, "cannot lock ref '%s': " | |
2327 | "reference is missing but expected %s", | |
2328 | original_update_refname(update), | |
98491298 | 2329 | oid_to_hex(&update->old_oid)); |
e3f51039 MH |
2330 | else |
2331 | strbuf_addf(err, "cannot lock ref '%s': " | |
2332 | "is at %s but expected %s", | |
2333 | original_update_refname(update), | |
2334 | oid_to_hex(oid), | |
98491298 | 2335 | oid_to_hex(&update->old_oid)); |
e3f51039 MH |
2336 | |
2337 | return -1; | |
2338 | } | |
2339 | ||
92b1551b MH |
2340 | /* |
2341 | * Prepare for carrying out update: | |
2342 | * - Lock the reference referred to by update. | |
2343 | * - Read the reference under lock. | |
2344 | * - Check that its old SHA-1 value (if specified) is correct, and in | |
2345 | * any case record it in update->lock->old_oid for later use when | |
2346 | * writing the reflog. | |
91774afc | 2347 | * - If it is a symref update without REF_NO_DEREF, split it up into a |
92b1551b MH |
2348 | * REF_LOG_ONLY update of the symref and add a separate update for |
2349 | * the referent to transaction. | |
2350 | * - If it is an update of head_ref, add a corresponding REF_LOG_ONLY | |
2351 | * update of HEAD. | |
165056b2 | 2352 | */ |
b3bbbc5c MH |
2353 | static int lock_ref_for_update(struct files_ref_store *refs, |
2354 | struct ref_update *update, | |
165056b2 | 2355 | struct ref_transaction *transaction, |
92b1551b | 2356 | const char *head_ref, |
165056b2 MH |
2357 | struct string_list *affected_refnames, |
2358 | struct strbuf *err) | |
2359 | { | |
92b1551b MH |
2360 | struct strbuf referent = STRBUF_INIT; |
2361 | int mustexist = (update->flags & REF_HAVE_OLD) && | |
98491298 | 2362 | !is_null_oid(&update->old_oid); |
851e1fbd | 2363 | int ret = 0; |
92b1551b | 2364 | struct ref_lock *lock; |
165056b2 | 2365 | |
32c597e7 | 2366 | files_assert_main_repository(refs, "lock_ref_for_update"); |
b3bbbc5c | 2367 | |
98491298 | 2368 | if ((update->flags & REF_HAVE_NEW) && is_null_oid(&update->new_oid)) |
165056b2 | 2369 | update->flags |= REF_DELETING; |
92b1551b MH |
2370 | |
2371 | if (head_ref) { | |
2372 | ret = split_head_update(update, transaction, head_ref, | |
2373 | affected_refnames, err); | |
2374 | if (ret) | |
851e1fbd | 2375 | goto out; |
92b1551b MH |
2376 | } |
2377 | ||
f7b0a987 | 2378 | ret = lock_raw_ref(refs, update->refname, mustexist, |
92b1551b | 2379 | affected_refnames, NULL, |
7d618264 | 2380 | &lock, &referent, |
92b1551b | 2381 | &update->type, err); |
92b1551b | 2382 | if (ret) { |
165056b2 MH |
2383 | char *reason; |
2384 | ||
165056b2 MH |
2385 | reason = strbuf_detach(err, NULL); |
2386 | strbuf_addf(err, "cannot lock ref '%s': %s", | |
e3f51039 | 2387 | original_update_refname(update), reason); |
165056b2 | 2388 | free(reason); |
851e1fbd | 2389 | goto out; |
165056b2 | 2390 | } |
92b1551b | 2391 | |
7d618264 | 2392 | update->backend_data = lock; |
92b1551b | 2393 | |
8169d0d0 | 2394 | if (update->type & REF_ISSYMREF) { |
91774afc | 2395 | if (update->flags & REF_NO_DEREF) { |
6e30b2f6 MH |
2396 | /* |
2397 | * We won't be reading the referent as part of | |
2398 | * the transaction, so we have to read it here | |
2399 | * to record and possibly check old_sha1: | |
2400 | */ | |
2f40e954 NTND |
2401 | if (refs_read_ref_full(&refs->base, |
2402 | referent.buf, 0, | |
34c290a6 | 2403 | &lock->old_oid, NULL)) { |
6e30b2f6 MH |
2404 | if (update->flags & REF_HAVE_OLD) { |
2405 | strbuf_addf(err, "cannot lock ref '%s': " | |
e3f51039 MH |
2406 | "error reading reference", |
2407 | original_update_refname(update)); | |
3f5ef95b | 2408 | ret = TRANSACTION_GENERIC_ERROR; |
851e1fbd | 2409 | goto out; |
6e30b2f6 | 2410 | } |
e3f51039 | 2411 | } else if (check_old_oid(update, &lock->old_oid, err)) { |
851e1fbd MÅ |
2412 | ret = TRANSACTION_GENERIC_ERROR; |
2413 | goto out; | |
8169d0d0 | 2414 | } |
6e30b2f6 MH |
2415 | } else { |
2416 | /* | |
2417 | * Create a new update for the reference this | |
2418 | * symref is pointing at. Also, we will record | |
2419 | * and verify old_sha1 for this update as part | |
2420 | * of processing the split-off update, so we | |
2421 | * don't have to do it here. | |
2422 | */ | |
fcc42ea0 MH |
2423 | ret = split_symref_update(refs, update, |
2424 | referent.buf, transaction, | |
92b1551b MH |
2425 | affected_refnames, err); |
2426 | if (ret) | |
851e1fbd | 2427 | goto out; |
92b1551b | 2428 | } |
6e30b2f6 MH |
2429 | } else { |
2430 | struct ref_update *parent_update; | |
8169d0d0 | 2431 | |
851e1fbd MÅ |
2432 | if (check_old_oid(update, &lock->old_oid, err)) { |
2433 | ret = TRANSACTION_GENERIC_ERROR; | |
2434 | goto out; | |
2435 | } | |
e3f51039 | 2436 | |
6e30b2f6 MH |
2437 | /* |
2438 | * If this update is happening indirectly because of a | |
2439 | * symref update, record the old SHA-1 in the parent | |
2440 | * update: | |
2441 | */ | |
2442 | for (parent_update = update->parent_update; | |
2443 | parent_update; | |
2444 | parent_update = parent_update->parent_update) { | |
7d618264 DT |
2445 | struct ref_lock *parent_lock = parent_update->backend_data; |
2446 | oidcpy(&parent_lock->old_oid, &lock->old_oid); | |
6e30b2f6 | 2447 | } |
92b1551b MH |
2448 | } |
2449 | ||
165056b2 MH |
2450 | if ((update->flags & REF_HAVE_NEW) && |
2451 | !(update->flags & REF_DELETING) && | |
2452 | !(update->flags & REF_LOG_ONLY)) { | |
92b1551b | 2453 | if (!(update->type & REF_ISSYMREF) && |
98491298 | 2454 | !oidcmp(&lock->old_oid, &update->new_oid)) { |
165056b2 MH |
2455 | /* |
2456 | * The reference already has the desired | |
2457 | * value, so we don't need to write it. | |
2458 | */ | |
4417df8c | 2459 | } else if (write_ref_to_lockfile(lock, &update->new_oid, |
165056b2 MH |
2460 | err)) { |
2461 | char *write_err = strbuf_detach(err, NULL); | |
2462 | ||
2463 | /* | |
2464 | * The lock was freed upon failure of | |
2465 | * write_ref_to_lockfile(): | |
2466 | */ | |
7d618264 | 2467 | update->backend_data = NULL; |
165056b2 | 2468 | strbuf_addf(err, |
e3f51039 | 2469 | "cannot update ref '%s': %s", |
165056b2 MH |
2470 | update->refname, write_err); |
2471 | free(write_err); | |
851e1fbd MÅ |
2472 | ret = TRANSACTION_GENERIC_ERROR; |
2473 | goto out; | |
165056b2 MH |
2474 | } else { |
2475 | update->flags |= REF_NEEDS_COMMIT; | |
2476 | } | |
2477 | } | |
2478 | if (!(update->flags & REF_NEEDS_COMMIT)) { | |
2479 | /* | |
2480 | * We didn't call write_ref_to_lockfile(), so | |
2481 | * the lockfile is still open. Close it to | |
2482 | * free up the file descriptor: | |
2483 | */ | |
83a3069a | 2484 | if (close_ref_gently(lock)) { |
165056b2 MH |
2485 | strbuf_addf(err, "couldn't close '%s.lock'", |
2486 | update->refname); | |
851e1fbd MÅ |
2487 | ret = TRANSACTION_GENERIC_ERROR; |
2488 | goto out; | |
165056b2 MH |
2489 | } |
2490 | } | |
851e1fbd MÅ |
2491 | |
2492 | out: | |
2493 | strbuf_release(&referent); | |
2494 | return ret; | |
165056b2 MH |
2495 | } |
2496 | ||
dc39e099 MH |
2497 | struct files_transaction_backend_data { |
2498 | struct ref_transaction *packed_transaction; | |
2499 | int packed_refs_locked; | |
2500 | }; | |
2501 | ||
c0ca9357 MH |
2502 | /* |
2503 | * Unlock any references in `transaction` that are still locked, and | |
2504 | * mark the transaction closed. | |
2505 | */ | |
dc39e099 MH |
2506 | static void files_transaction_cleanup(struct files_ref_store *refs, |
2507 | struct ref_transaction *transaction) | |
c0ca9357 MH |
2508 | { |
2509 | size_t i; | |
dc39e099 MH |
2510 | struct files_transaction_backend_data *backend_data = |
2511 | transaction->backend_data; | |
2512 | struct strbuf err = STRBUF_INIT; | |
c0ca9357 MH |
2513 | |
2514 | for (i = 0; i < transaction->nr; i++) { | |
2515 | struct ref_update *update = transaction->updates[i]; | |
2516 | struct ref_lock *lock = update->backend_data; | |
2517 | ||
2518 | if (lock) { | |
2519 | unlock_ref(lock); | |
2520 | update->backend_data = NULL; | |
2521 | } | |
2522 | } | |
2523 | ||
dc39e099 MH |
2524 | if (backend_data->packed_transaction && |
2525 | ref_transaction_abort(backend_data->packed_transaction, &err)) { | |
2526 | error("error aborting transaction: %s", err.buf); | |
2527 | strbuf_release(&err); | |
2528 | } | |
2529 | ||
2530 | if (backend_data->packed_refs_locked) | |
2531 | packed_refs_unlock(refs->packed_ref_store); | |
2532 | ||
2533 | free(backend_data); | |
2534 | ||
c0ca9357 MH |
2535 | transaction->state = REF_TRANSACTION_CLOSED; |
2536 | } | |
2537 | ||
30173b88 MH |
2538 | static int files_transaction_prepare(struct ref_store *ref_store, |
2539 | struct ref_transaction *transaction, | |
2540 | struct strbuf *err) | |
7bd9bcf3 | 2541 | { |
00eebe35 | 2542 | struct files_ref_store *refs = |
9e7ec634 | 2543 | files_downcast(ref_store, REF_STORE_WRITE, |
30173b88 | 2544 | "ref_transaction_prepare"); |
43a2dfde MH |
2545 | size_t i; |
2546 | int ret = 0; | |
7bd9bcf3 | 2547 | struct string_list affected_refnames = STRING_LIST_INIT_NODUP; |
92b1551b MH |
2548 | char *head_ref = NULL; |
2549 | int head_type; | |
dc39e099 MH |
2550 | struct files_transaction_backend_data *backend_data; |
2551 | struct ref_transaction *packed_transaction = NULL; | |
7bd9bcf3 MH |
2552 | |
2553 | assert(err); | |
2554 | ||
c0ca9357 MH |
2555 | if (!transaction->nr) |
2556 | goto cleanup; | |
7bd9bcf3 | 2557 | |
dc39e099 MH |
2558 | backend_data = xcalloc(1, sizeof(*backend_data)); |
2559 | transaction->backend_data = backend_data; | |
2560 | ||
92b1551b MH |
2561 | /* |
2562 | * Fail if a refname appears more than once in the | |
2563 | * transaction. (If we end up splitting up any updates using | |
2564 | * split_symref_update() or split_head_update(), those | |
2565 | * functions will check that the new updates don't have the | |
62c72d1f | 2566 | * same refname as any existing ones.) Also fail if any of the |
91774afc | 2567 | * updates use REF_ISPRUNING without REF_NO_DEREF. |
92b1551b MH |
2568 | */ |
2569 | for (i = 0; i < transaction->nr; i++) { | |
2570 | struct ref_update *update = transaction->updates[i]; | |
2571 | struct string_list_item *item = | |
2572 | string_list_append(&affected_refnames, update->refname); | |
2573 | ||
62c72d1f | 2574 | if ((update->flags & REF_ISPRUNING) && |
91774afc MH |
2575 | !(update->flags & REF_NO_DEREF)) |
2576 | BUG("REF_ISPRUNING set without REF_NO_DEREF"); | |
62c72d1f | 2577 | |
92b1551b MH |
2578 | /* |
2579 | * We store a pointer to update in item->util, but at | |
2580 | * the moment we never use the value of this field | |
2581 | * except to check whether it is non-NULL. | |
2582 | */ | |
2583 | item->util = update; | |
2584 | } | |
7bd9bcf3 MH |
2585 | string_list_sort(&affected_refnames); |
2586 | if (ref_update_reject_duplicates(&affected_refnames, err)) { | |
2587 | ret = TRANSACTION_GENERIC_ERROR; | |
2588 | goto cleanup; | |
2589 | } | |
2590 | ||
92b1551b MH |
2591 | /* |
2592 | * Special hack: If a branch is updated directly and HEAD | |
2593 | * points to it (may happen on the remote side of a push | |
2594 | * for example) then logically the HEAD reflog should be | |
2595 | * updated too. | |
2596 | * | |
2597 | * A generic solution would require reverse symref lookups, | |
2598 | * but finding all symrefs pointing to a given branch would be | |
2599 | * rather costly for this rare event (the direct update of a | |
2600 | * branch) to be worth it. So let's cheat and check with HEAD | |
2601 | * only, which should cover 99% of all usage scenarios (even | |
2602 | * 100% of the default ones). | |
2603 | * | |
2604 | * So if HEAD is a symbolic reference, then record the name of | |
2605 | * the reference that it points to. If we see an update of | |
2606 | * head_ref within the transaction, then split_head_update() | |
2607 | * arranges for the reflog of HEAD to be updated, too. | |
2608 | */ | |
2f40e954 NTND |
2609 | head_ref = refs_resolve_refdup(ref_store, "HEAD", |
2610 | RESOLVE_REF_NO_RECURSE, | |
872ccb2c | 2611 | NULL, &head_type); |
92b1551b MH |
2612 | |
2613 | if (head_ref && !(head_type & REF_ISSYMREF)) { | |
6a83d902 | 2614 | FREE_AND_NULL(head_ref); |
92b1551b MH |
2615 | } |
2616 | ||
7bd9bcf3 MH |
2617 | /* |
2618 | * Acquire all locks, verify old values if provided, check | |
2619 | * that new values are valid, and write new values to the | |
2620 | * lockfiles, ready to be activated. Only keep one lockfile | |
2621 | * open at a time to avoid running out of file descriptors. | |
30173b88 MH |
2622 | * Note that lock_ref_for_update() might append more updates |
2623 | * to the transaction. | |
7bd9bcf3 | 2624 | */ |
efe47281 MH |
2625 | for (i = 0; i < transaction->nr; i++) { |
2626 | struct ref_update *update = transaction->updates[i]; | |
7bd9bcf3 | 2627 | |
b3bbbc5c MH |
2628 | ret = lock_ref_for_update(refs, update, transaction, |
2629 | head_ref, &affected_refnames, err); | |
165056b2 | 2630 | if (ret) |
da5267f1 | 2631 | goto cleanup; |
dc39e099 MH |
2632 | |
2633 | if (update->flags & REF_DELETING && | |
2634 | !(update->flags & REF_LOG_ONLY) && | |
2635 | !(update->flags & REF_ISPRUNING)) { | |
2636 | /* | |
2637 | * This reference has to be deleted from | |
2638 | * packed-refs if it exists there. | |
2639 | */ | |
2640 | if (!packed_transaction) { | |
2641 | packed_transaction = ref_store_transaction_begin( | |
2642 | refs->packed_ref_store, err); | |
2643 | if (!packed_transaction) { | |
2644 | ret = TRANSACTION_GENERIC_ERROR; | |
2645 | goto cleanup; | |
2646 | } | |
2647 | ||
2648 | backend_data->packed_transaction = | |
2649 | packed_transaction; | |
2650 | } | |
2651 | ||
2652 | ref_transaction_add_update( | |
2653 | packed_transaction, update->refname, | |
91774afc | 2654 | REF_HAVE_NEW | REF_NO_DEREF, |
b0ca4110 | 2655 | &update->new_oid, NULL, |
dc39e099 MH |
2656 | NULL); |
2657 | } | |
2658 | } | |
2659 | ||
2660 | if (packed_transaction) { | |
2661 | if (packed_refs_lock(refs->packed_ref_store, 0, err)) { | |
2662 | ret = TRANSACTION_GENERIC_ERROR; | |
2663 | goto cleanup; | |
2664 | } | |
2665 | backend_data->packed_refs_locked = 1; | |
2666 | ret = ref_transaction_prepare(packed_transaction, err); | |
30173b88 MH |
2667 | } |
2668 | ||
2669 | cleanup: | |
2670 | free(head_ref); | |
2671 | string_list_clear(&affected_refnames, 0); | |
2672 | ||
2673 | if (ret) | |
dc39e099 | 2674 | files_transaction_cleanup(refs, transaction); |
30173b88 MH |
2675 | else |
2676 | transaction->state = REF_TRANSACTION_PREPARED; | |
2677 | ||
2678 | return ret; | |
2679 | } | |
2680 | ||
2681 | static int files_transaction_finish(struct ref_store *ref_store, | |
2682 | struct ref_transaction *transaction, | |
2683 | struct strbuf *err) | |
2684 | { | |
2685 | struct files_ref_store *refs = | |
2686 | files_downcast(ref_store, 0, "ref_transaction_finish"); | |
2687 | size_t i; | |
2688 | int ret = 0; | |
30173b88 | 2689 | struct strbuf sb = STRBUF_INIT; |
dc39e099 MH |
2690 | struct files_transaction_backend_data *backend_data; |
2691 | struct ref_transaction *packed_transaction; | |
2692 | ||
30173b88 MH |
2693 | |
2694 | assert(err); | |
2695 | ||
2696 | if (!transaction->nr) { | |
2697 | transaction->state = REF_TRANSACTION_CLOSED; | |
2698 | return 0; | |
7bd9bcf3 | 2699 | } |
7bd9bcf3 | 2700 | |
dc39e099 MH |
2701 | backend_data = transaction->backend_data; |
2702 | packed_transaction = backend_data->packed_transaction; | |
2703 | ||
7bd9bcf3 | 2704 | /* Perform updates first so live commits remain referenced */ |
efe47281 MH |
2705 | for (i = 0; i < transaction->nr; i++) { |
2706 | struct ref_update *update = transaction->updates[i]; | |
7d618264 | 2707 | struct ref_lock *lock = update->backend_data; |
7bd9bcf3 | 2708 | |
d99aa884 DT |
2709 | if (update->flags & REF_NEEDS_COMMIT || |
2710 | update->flags & REF_LOG_ONLY) { | |
802de3da NTND |
2711 | if (files_log_ref_write(refs, |
2712 | lock->ref_name, | |
4417df8c | 2713 | &lock->old_oid, |
2714 | &update->new_oid, | |
81b1b6d4 MH |
2715 | update->msg, update->flags, |
2716 | err)) { | |
92b1551b MH |
2717 | char *old_msg = strbuf_detach(err, NULL); |
2718 | ||
2719 | strbuf_addf(err, "cannot update the ref '%s': %s", | |
2720 | lock->ref_name, old_msg); | |
2721 | free(old_msg); | |
2722 | unlock_ref(lock); | |
7d618264 | 2723 | update->backend_data = NULL; |
7bd9bcf3 MH |
2724 | ret = TRANSACTION_GENERIC_ERROR; |
2725 | goto cleanup; | |
7bd9bcf3 MH |
2726 | } |
2727 | } | |
7bd9bcf3 | 2728 | if (update->flags & REF_NEEDS_COMMIT) { |
00eebe35 | 2729 | clear_loose_ref_cache(refs); |
92b1551b MH |
2730 | if (commit_ref(lock)) { |
2731 | strbuf_addf(err, "couldn't set '%s'", lock->ref_name); | |
2732 | unlock_ref(lock); | |
7d618264 | 2733 | update->backend_data = NULL; |
7bd9bcf3 MH |
2734 | ret = TRANSACTION_GENERIC_ERROR; |
2735 | goto cleanup; | |
7bd9bcf3 MH |
2736 | } |
2737 | } | |
2738 | } | |
dc39e099 | 2739 | |
5e00a6c8 MH |
2740 | /* |
2741 | * Now that updates are safely completed, we can perform | |
2742 | * deletes. First delete the reflogs of any references that | |
2743 | * will be deleted, since (in the unexpected event of an | |
2744 | * error) leaving a reference without a reflog is less bad | |
2745 | * than leaving a reflog without a reference (the latter is a | |
2746 | * mildly invalid repository state): | |
2747 | */ | |
2748 | for (i = 0; i < transaction->nr; i++) { | |
2749 | struct ref_update *update = transaction->updates[i]; | |
2750 | if (update->flags & REF_DELETING && | |
2751 | !(update->flags & REF_LOG_ONLY) && | |
2752 | !(update->flags & REF_ISPRUNING)) { | |
2753 | strbuf_reset(&sb); | |
2754 | files_reflog_path(refs, &sb, update->refname); | |
2755 | if (!unlink_or_warn(sb.buf)) | |
2756 | try_remove_empty_parents(refs, update->refname, | |
2757 | REMOVE_EMPTY_PARENTS_REFLOG); | |
2758 | } | |
2759 | } | |
2760 | ||
dc39e099 MH |
2761 | /* |
2762 | * Perform deletes now that updates are safely completed. | |
2763 | * | |
2764 | * First delete any packed versions of the references, while | |
2765 | * retaining the packed-refs lock: | |
2766 | */ | |
2767 | if (packed_transaction) { | |
2768 | ret = ref_transaction_commit(packed_transaction, err); | |
2769 | ref_transaction_free(packed_transaction); | |
2770 | packed_transaction = NULL; | |
2771 | backend_data->packed_transaction = NULL; | |
2772 | if (ret) | |
2773 | goto cleanup; | |
2774 | } | |
2775 | ||
2776 | /* Now delete the loose versions of the references: */ | |
efe47281 MH |
2777 | for (i = 0; i < transaction->nr; i++) { |
2778 | struct ref_update *update = transaction->updates[i]; | |
7d618264 | 2779 | struct ref_lock *lock = update->backend_data; |
7bd9bcf3 | 2780 | |
d99aa884 DT |
2781 | if (update->flags & REF_DELETING && |
2782 | !(update->flags & REF_LOG_ONLY)) { | |
ce0af24d MH |
2783 | if (!(update->type & REF_ISPACKED) || |
2784 | update->type & REF_ISSYMREF) { | |
2785 | /* It is a loose reference. */ | |
e9dcc305 | 2786 | strbuf_reset(&sb); |
19e02f4f | 2787 | files_ref_path(refs, &sb, lock->ref_name); |
e9dcc305 | 2788 | if (unlink_or_msg(sb.buf, err)) { |
ce0af24d MH |
2789 | ret = TRANSACTION_GENERIC_ERROR; |
2790 | goto cleanup; | |
2791 | } | |
44639777 | 2792 | update->flags |= REF_DELETED_LOOSE; |
7bd9bcf3 | 2793 | } |
7bd9bcf3 MH |
2794 | } |
2795 | } | |
2796 | ||
00eebe35 | 2797 | clear_loose_ref_cache(refs); |
7bd9bcf3 MH |
2798 | |
2799 | cleanup: | |
dc39e099 | 2800 | files_transaction_cleanup(refs, transaction); |
7bd9bcf3 | 2801 | |
44639777 MH |
2802 | for (i = 0; i < transaction->nr; i++) { |
2803 | struct ref_update *update = transaction->updates[i]; | |
44639777 MH |
2804 | |
2805 | if (update->flags & REF_DELETED_LOOSE) { | |
2806 | /* | |
2807 | * The loose reference was deleted. Delete any | |
2808 | * empty parent directories. (Note that this | |
2809 | * can only work because we have already | |
2810 | * removed the lockfile.) | |
2811 | */ | |
802de3da | 2812 | try_remove_empty_parents(refs, update->refname, |
44639777 MH |
2813 | REMOVE_EMPTY_PARENTS_REF); |
2814 | } | |
2815 | } | |
2816 | ||
30173b88 | 2817 | strbuf_release(&sb); |
7bd9bcf3 MH |
2818 | return ret; |
2819 | } | |
2820 | ||
30173b88 MH |
2821 | static int files_transaction_abort(struct ref_store *ref_store, |
2822 | struct ref_transaction *transaction, | |
2823 | struct strbuf *err) | |
2824 | { | |
dc39e099 MH |
2825 | struct files_ref_store *refs = |
2826 | files_downcast(ref_store, 0, "ref_transaction_abort"); | |
2827 | ||
2828 | files_transaction_cleanup(refs, transaction); | |
30173b88 MH |
2829 | return 0; |
2830 | } | |
2831 | ||
7bd9bcf3 MH |
2832 | static int ref_present(const char *refname, |
2833 | const struct object_id *oid, int flags, void *cb_data) | |
2834 | { | |
2835 | struct string_list *affected_refnames = cb_data; | |
2836 | ||
2837 | return string_list_has_string(affected_refnames, refname); | |
2838 | } | |
2839 | ||
fc681463 DT |
2840 | static int files_initial_transaction_commit(struct ref_store *ref_store, |
2841 | struct ref_transaction *transaction, | |
2842 | struct strbuf *err) | |
7bd9bcf3 | 2843 | { |
d99825ab | 2844 | struct files_ref_store *refs = |
9e7ec634 NTND |
2845 | files_downcast(ref_store, REF_STORE_WRITE, |
2846 | "initial_ref_transaction_commit"); | |
43a2dfde MH |
2847 | size_t i; |
2848 | int ret = 0; | |
7bd9bcf3 | 2849 | struct string_list affected_refnames = STRING_LIST_INIT_NODUP; |
1444bfe0 | 2850 | struct ref_transaction *packed_transaction = NULL; |
7bd9bcf3 MH |
2851 | |
2852 | assert(err); | |
2853 | ||
2854 | if (transaction->state != REF_TRANSACTION_OPEN) | |
2855 | die("BUG: commit called for transaction that is not open"); | |
2856 | ||
2857 | /* Fail if a refname appears more than once in the transaction: */ | |
efe47281 MH |
2858 | for (i = 0; i < transaction->nr; i++) |
2859 | string_list_append(&affected_refnames, | |
2860 | transaction->updates[i]->refname); | |
7bd9bcf3 MH |
2861 | string_list_sort(&affected_refnames); |
2862 | if (ref_update_reject_duplicates(&affected_refnames, err)) { | |
2863 | ret = TRANSACTION_GENERIC_ERROR; | |
2864 | goto cleanup; | |
2865 | } | |
2866 | ||
2867 | /* | |
2868 | * It's really undefined to call this function in an active | |
2869 | * repository or when there are existing references: we are | |
2870 | * only locking and changing packed-refs, so (1) any | |
2871 | * simultaneous processes might try to change a reference at | |
2872 | * the same time we do, and (2) any existing loose versions of | |
2873 | * the references that we are setting would have precedence | |
2874 | * over our values. But some remote helpers create the remote | |
2875 | * "HEAD" and "master" branches before calling this function, | |
2876 | * so here we really only check that none of the references | |
2877 | * that we are creating already exists. | |
2878 | */ | |
2f40e954 NTND |
2879 | if (refs_for_each_rawref(&refs->base, ref_present, |
2880 | &affected_refnames)) | |
7bd9bcf3 MH |
2881 | die("BUG: initial ref transaction called with existing refs"); |
2882 | ||
1444bfe0 MH |
2883 | packed_transaction = ref_store_transaction_begin(refs->packed_ref_store, err); |
2884 | if (!packed_transaction) { | |
2885 | ret = TRANSACTION_GENERIC_ERROR; | |
2886 | goto cleanup; | |
2887 | } | |
2888 | ||
efe47281 MH |
2889 | for (i = 0; i < transaction->nr; i++) { |
2890 | struct ref_update *update = transaction->updates[i]; | |
7bd9bcf3 MH |
2891 | |
2892 | if ((update->flags & REF_HAVE_OLD) && | |
98491298 | 2893 | !is_null_oid(&update->old_oid)) |
7bd9bcf3 | 2894 | die("BUG: initial ref transaction with old_sha1 set"); |
7d2df051 NTND |
2895 | if (refs_verify_refname_available(&refs->base, update->refname, |
2896 | &affected_refnames, NULL, | |
2897 | err)) { | |
7bd9bcf3 MH |
2898 | ret = TRANSACTION_NAME_CONFLICT; |
2899 | goto cleanup; | |
2900 | } | |
1444bfe0 MH |
2901 | |
2902 | /* | |
2903 | * Add a reference creation for this reference to the | |
2904 | * packed-refs transaction: | |
2905 | */ | |
2906 | ref_transaction_add_update(packed_transaction, update->refname, | |
2907 | update->flags & ~REF_HAVE_OLD, | |
89f3bbdd | 2908 | &update->new_oid, &update->old_oid, |
1444bfe0 | 2909 | NULL); |
7bd9bcf3 MH |
2910 | } |
2911 | ||
c8bed835 | 2912 | if (packed_refs_lock(refs->packed_ref_store, 0, err)) { |
7bd9bcf3 MH |
2913 | ret = TRANSACTION_GENERIC_ERROR; |
2914 | goto cleanup; | |
2915 | } | |
2916 | ||
1444bfe0 | 2917 | if (initial_ref_transaction_commit(packed_transaction, err)) { |
7bd9bcf3 MH |
2918 | ret = TRANSACTION_GENERIC_ERROR; |
2919 | goto cleanup; | |
2920 | } | |
2921 | ||
2922 | cleanup: | |
1444bfe0 MH |
2923 | if (packed_transaction) |
2924 | ref_transaction_free(packed_transaction); | |
42c7f7ff | 2925 | packed_refs_unlock(refs->packed_ref_store); |
7bd9bcf3 MH |
2926 | transaction->state = REF_TRANSACTION_CLOSED; |
2927 | string_list_clear(&affected_refnames, 0); | |
2928 | return ret; | |
2929 | } | |
2930 | ||
2931 | struct expire_reflog_cb { | |
2932 | unsigned int flags; | |
2933 | reflog_expiry_should_prune_fn *should_prune_fn; | |
2934 | void *policy_cb; | |
2935 | FILE *newlog; | |
9461d272 | 2936 | struct object_id last_kept_oid; |
7bd9bcf3 MH |
2937 | }; |
2938 | ||
9461d272 | 2939 | static int expire_reflog_ent(struct object_id *ooid, struct object_id *noid, |
dddbad72 | 2940 | const char *email, timestamp_t timestamp, int tz, |
7bd9bcf3 MH |
2941 | const char *message, void *cb_data) |
2942 | { | |
2943 | struct expire_reflog_cb *cb = cb_data; | |
2944 | struct expire_reflog_policy_cb *policy_cb = cb->policy_cb; | |
2945 | ||
2946 | if (cb->flags & EXPIRE_REFLOGS_REWRITE) | |
9461d272 | 2947 | ooid = &cb->last_kept_oid; |
7bd9bcf3 | 2948 | |
4322478a | 2949 | if ((*cb->should_prune_fn)(ooid, noid, email, timestamp, tz, |
7bd9bcf3 MH |
2950 | message, policy_cb)) { |
2951 | if (!cb->newlog) | |
2952 | printf("would prune %s", message); | |
2953 | else if (cb->flags & EXPIRE_REFLOGS_VERBOSE) | |
2954 | printf("prune %s", message); | |
2955 | } else { | |
2956 | if (cb->newlog) { | |
cb71f8bd | 2957 | fprintf(cb->newlog, "%s %s %s %"PRItime" %+05d\t%s", |
9461d272 | 2958 | oid_to_hex(ooid), oid_to_hex(noid), |
7bd9bcf3 | 2959 | email, timestamp, tz, message); |
9461d272 | 2960 | oidcpy(&cb->last_kept_oid, noid); |
7bd9bcf3 MH |
2961 | } |
2962 | if (cb->flags & EXPIRE_REFLOGS_VERBOSE) | |
2963 | printf("keep %s", message); | |
2964 | } | |
2965 | return 0; | |
2966 | } | |
2967 | ||
e3688bd6 | 2968 | static int files_reflog_expire(struct ref_store *ref_store, |
0155f710 | 2969 | const char *refname, const struct object_id *oid, |
e3688bd6 DT |
2970 | unsigned int flags, |
2971 | reflog_expiry_prepare_fn prepare_fn, | |
2972 | reflog_expiry_should_prune_fn should_prune_fn, | |
2973 | reflog_expiry_cleanup_fn cleanup_fn, | |
2974 | void *policy_cb_data) | |
7bd9bcf3 | 2975 | { |
7eb27cdf | 2976 | struct files_ref_store *refs = |
9e7ec634 | 2977 | files_downcast(ref_store, REF_STORE_WRITE, "reflog_expire"); |
7bd9bcf3 MH |
2978 | static struct lock_file reflog_lock; |
2979 | struct expire_reflog_cb cb; | |
2980 | struct ref_lock *lock; | |
802de3da | 2981 | struct strbuf log_file_sb = STRBUF_INIT; |
7bd9bcf3 MH |
2982 | char *log_file; |
2983 | int status = 0; | |
2984 | int type; | |
2985 | struct strbuf err = STRBUF_INIT; | |
2986 | ||
2987 | memset(&cb, 0, sizeof(cb)); | |
2988 | cb.flags = flags; | |
2989 | cb.policy_cb = policy_cb_data; | |
2990 | cb.should_prune_fn = should_prune_fn; | |
2991 | ||
2992 | /* | |
2993 | * The reflog file is locked by holding the lock on the | |
2994 | * reference itself, plus we might need to update the | |
2995 | * reference if --updateref was specified: | |
2996 | */ | |
4f01e508 | 2997 | lock = lock_ref_oid_basic(refs, refname, oid, |
91774afc | 2998 | NULL, NULL, REF_NO_DEREF, |
4f01e508 | 2999 | &type, &err); |
7bd9bcf3 MH |
3000 | if (!lock) { |
3001 | error("cannot lock ref '%s': %s", refname, err.buf); | |
3002 | strbuf_release(&err); | |
3003 | return -1; | |
3004 | } | |
2f40e954 | 3005 | if (!refs_reflog_exists(ref_store, refname)) { |
7bd9bcf3 MH |
3006 | unlock_ref(lock); |
3007 | return 0; | |
3008 | } | |
3009 | ||
802de3da NTND |
3010 | files_reflog_path(refs, &log_file_sb, refname); |
3011 | log_file = strbuf_detach(&log_file_sb, NULL); | |
7bd9bcf3 MH |
3012 | if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) { |
3013 | /* | |
3014 | * Even though holding $GIT_DIR/logs/$reflog.lock has | |
3015 | * no locking implications, we use the lock_file | |
3016 | * machinery here anyway because it does a lot of the | |
3017 | * work we need, including cleaning up if the program | |
3018 | * exits unexpectedly. | |
3019 | */ | |
3020 | if (hold_lock_file_for_update(&reflog_lock, log_file, 0) < 0) { | |
3021 | struct strbuf err = STRBUF_INIT; | |
3022 | unable_to_lock_message(log_file, errno, &err); | |
3023 | error("%s", err.buf); | |
3024 | strbuf_release(&err); | |
3025 | goto failure; | |
3026 | } | |
3027 | cb.newlog = fdopen_lock_file(&reflog_lock, "w"); | |
3028 | if (!cb.newlog) { | |
3029 | error("cannot fdopen %s (%s)", | |
3030 | get_lock_file_path(&reflog_lock), strerror(errno)); | |
3031 | goto failure; | |
3032 | } | |
3033 | } | |
3034 | ||
0155f710 | 3035 | (*prepare_fn)(refname, oid, cb.policy_cb); |
2f40e954 | 3036 | refs_for_each_reflog_ent(ref_store, refname, expire_reflog_ent, &cb); |
7bd9bcf3 MH |
3037 | (*cleanup_fn)(cb.policy_cb); |
3038 | ||
3039 | if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) { | |
3040 | /* | |
3041 | * It doesn't make sense to adjust a reference pointed | |
3042 | * to by a symbolic ref based on expiring entries in | |
3043 | * the symbolic reference's reflog. Nor can we update | |
3044 | * a reference if there are no remaining reflog | |
3045 | * entries. | |
3046 | */ | |
3047 | int update = (flags & EXPIRE_REFLOGS_UPDATE_REF) && | |
3048 | !(type & REF_ISSYMREF) && | |
9461d272 | 3049 | !is_null_oid(&cb.last_kept_oid); |
7bd9bcf3 | 3050 | |
83a3069a | 3051 | if (close_lock_file_gently(&reflog_lock)) { |
7bd9bcf3 MH |
3052 | status |= error("couldn't write %s: %s", log_file, |
3053 | strerror(errno)); | |
83a3069a | 3054 | rollback_lock_file(&reflog_lock); |
7bd9bcf3 | 3055 | } else if (update && |
ee4d8e45 | 3056 | (write_in_full(get_lock_file_fd(&lock->lk), |
06f46f23 | 3057 | oid_to_hex(&cb.last_kept_oid), GIT_SHA1_HEXSZ) < 0 || |
88780c37 | 3058 | write_str_in_full(get_lock_file_fd(&lock->lk), "\n") < 0 || |
83a3069a | 3059 | close_ref_gently(lock) < 0)) { |
7bd9bcf3 | 3060 | status |= error("couldn't write %s", |
ee4d8e45 | 3061 | get_lock_file_path(&lock->lk)); |
7bd9bcf3 MH |
3062 | rollback_lock_file(&reflog_lock); |
3063 | } else if (commit_lock_file(&reflog_lock)) { | |
e0048d3e | 3064 | status |= error("unable to write reflog '%s' (%s)", |
7bd9bcf3 MH |
3065 | log_file, strerror(errno)); |
3066 | } else if (update && commit_ref(lock)) { | |
3067 | status |= error("couldn't set %s", lock->ref_name); | |
3068 | } | |
3069 | } | |
3070 | free(log_file); | |
3071 | unlock_ref(lock); | |
3072 | return status; | |
3073 | ||
3074 | failure: | |
3075 | rollback_lock_file(&reflog_lock); | |
3076 | free(log_file); | |
3077 | unlock_ref(lock); | |
3078 | return -1; | |
3079 | } | |
3dce444f | 3080 | |
6fb5acfd DT |
3081 | static int files_init_db(struct ref_store *ref_store, struct strbuf *err) |
3082 | { | |
19e02f4f | 3083 | struct files_ref_store *refs = |
9e7ec634 | 3084 | files_downcast(ref_store, REF_STORE_WRITE, "init_db"); |
e9dcc305 NTND |
3085 | struct strbuf sb = STRBUF_INIT; |
3086 | ||
6fb5acfd DT |
3087 | /* |
3088 | * Create .git/refs/{heads,tags} | |
3089 | */ | |
19e02f4f | 3090 | files_ref_path(refs, &sb, "refs/heads"); |
e9dcc305 NTND |
3091 | safe_create_dir(sb.buf, 1); |
3092 | ||
3093 | strbuf_reset(&sb); | |
19e02f4f | 3094 | files_ref_path(refs, &sb, "refs/tags"); |
e9dcc305 NTND |
3095 | safe_create_dir(sb.buf, 1); |
3096 | ||
3097 | strbuf_release(&sb); | |
6fb5acfd DT |
3098 | return 0; |
3099 | } | |
3100 | ||
3dce444f RS |
3101 | struct ref_storage_be refs_be_files = { |
3102 | NULL, | |
00eebe35 | 3103 | "files", |
127b42a1 | 3104 | files_ref_store_create, |
6fb5acfd | 3105 | files_init_db, |
30173b88 MH |
3106 | files_transaction_prepare, |
3107 | files_transaction_finish, | |
3108 | files_transaction_abort, | |
fc681463 | 3109 | files_initial_transaction_commit, |
e1e33b72 | 3110 | |
8231527e | 3111 | files_pack_refs, |
284689ba | 3112 | files_create_symref, |
a27dcf89 | 3113 | files_delete_refs, |
9b6b40d9 | 3114 | files_rename_ref, |
52d59cc6 | 3115 | files_copy_ref, |
8231527e | 3116 | |
1a769003 | 3117 | files_ref_iterator_begin, |
62665823 | 3118 | files_read_raw_ref, |
e3688bd6 DT |
3119 | |
3120 | files_reflog_iterator_begin, | |
3121 | files_for_each_reflog_ent, | |
3122 | files_for_each_reflog_ent_reverse, | |
3123 | files_reflog_exists, | |
3124 | files_create_reflog, | |
3125 | files_delete_reflog, | |
3126 | files_reflog_expire | |
3dce444f | 3127 | }; |