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