]> git.ipfire.org Git - thirdparty/git.git/blame - refs/files-backend.c
The third batch
[thirdparty/git.git] / refs / files-backend.c
CommitLineData
673af418 1#define USE_THE_REPOSITORY_VARIABLE
41f43b82 2#define DISABLE_SIGN_COMPARE_WARNINGS
673af418 3
bc5c5ec0 4#include "../git-compat-util.h"
c9f03f38 5#include "../abspath.h"
8e2e8a33 6#include "../config.h"
d5fff46f 7#include "../copy.h"
32a8f510 8#include "../environment.h"
f394e093 9#include "../gettext.h"
d1cbe1e6 10#include "../hash.h"
41771fa4 11#include "../hex.h"
a7600b84 12#include "../fsck.h"
7bd9bcf3 13#include "../refs.h"
9a20b889 14#include "../repo-settings.h"
7bd9bcf3 15#include "refs-internal.h"
958f9646 16#include "ref-cache.h"
67be7c5a 17#include "packed-backend.h"
b5fa6081 18#include "../ident.h"
3bc581b9 19#include "../iterator.h"
2880d16f 20#include "../dir-iterator.h"
7bd9bcf3
MH
21#include "../lockfile.h"
22#include "../object.h"
c339932b 23#include "../path.h"
7bd9bcf3 24#include "../dir.h"
fb9c2d27 25#include "../chdir-notify.h"
e38da487 26#include "../setup.h"
7c78d819 27#include "../worktree.h"
d5ebb50d 28#include "../wrapper.h"
d48be35c 29#include "../write-or-die.h"
826ae79f 30#include "../revision.h"
cbc882ea 31#include <wildmatch.h>
7bd9bcf3 32
5ac95fee
MH
33/*
34 * This backend uses the following flags in `ref_update::flags` for
35 * internal bookkeeping purposes. Their numerical values must not
91774afc 36 * conflict with REF_NO_DEREF, REF_FORCE_CREATE_REFLOG, REF_HAVE_NEW,
0464d0a1 37 * or REF_HAVE_OLD, which are also stored in `ref_update::flags`.
5ac95fee
MH
38 */
39
40/*
41 * Used as a flag in ref_update::flags when a loose ref is being
91774afc 42 * pruned. This flag must only be used when REF_NO_DEREF is set.
5ac95fee 43 */
acedcde7 44#define REF_IS_PRUNING (1 << 4)
5ac95fee
MH
45
46/*
47 * Flag passed to lock_ref_sha1_basic() telling it to tolerate broken
48 * refs (i.e., because the reference is about to be deleted anyway).
49 */
50#define REF_DELETING (1 << 5)
51
52/*
53 * Used as a flag in ref_update::flags when the lockfile needs to be
54 * committed.
55 */
56#define REF_NEEDS_COMMIT (1 << 6)
57
5ac95fee
MH
58/*
59 * Used as a flag in ref_update::flags when the ref_update was via an
60 * update to HEAD.
61 */
62#define REF_UPDATE_VIA_HEAD (1 << 8)
63
64/*
5f03e512
WC
65 * Used as a flag in ref_update::flags when a reference has been
66 * deleted and the ref's parent directories may need cleanup.
5ac95fee 67 */
5f03e512 68#define REF_DELETED_RMDIR (1 << 9)
5ac95fee 69
211fa8b2
PS
70/*
71 * Used to indicate that the reflog-only update has been created via
72 * `split_head_update()`.
73 */
74#define REF_LOG_VIA_SPLIT (1 << 14)
75
7bd9bcf3
MH
76struct ref_lock {
77 char *ref_name;
ee4d8e45 78 struct lock_file lk;
7bd9bcf3 79 struct object_id old_oid;
611986f3 80 unsigned int count; /* track users of the lock (ref update + reflog updates) */
7bd9bcf3
MH
81};
82
00eebe35
MH
83struct files_ref_store {
84 struct ref_store base;
9e7ec634 85 unsigned int store_flags;
32c597e7 86
f57f37e2 87 char *gitcommondir;
eafb1264 88 enum log_refs_config log_all_ref_updates;
8e2e8a33 89 int prefer_symlink_refs;
33dfb9f3 90
7c22bc8a 91 struct ref_cache *loose;
55c6bc37 92
e0cc8ac8 93 struct ref_store *packed_ref_store;
00eebe35 94};
7bd9bcf3 95
65a0a8e5 96static void clear_loose_ref_cache(struct files_ref_store *refs)
7bd9bcf3
MH
97{
98 if (refs->loose) {
7c22bc8a 99 free_ref_cache(refs->loose);
7bd9bcf3
MH
100 refs->loose = NULL;
101 }
102}
103
a2d5156c
JK
104/*
105 * Create a new submodule ref cache and add it to the internal
106 * set of caches.
107 */
1febabff
PS
108static struct ref_store *files_ref_store_init(struct repository *repo,
109 const char *gitdir,
110 unsigned int flags)
7bd9bcf3 111{
00eebe35
MH
112 struct files_ref_store *refs = xcalloc(1, sizeof(*refs));
113 struct ref_store *ref_store = (struct ref_store *)refs;
f57f37e2 114 struct strbuf sb = STRBUF_INIT;
7bd9bcf3 115
f9f7fd3b 116 base_ref_store_init(ref_store, repo, gitdir, &refs_be_files);
9e7ec634 117 refs->store_flags = flags;
f57f37e2
NTND
118 get_common_dir_noenv(&sb, gitdir);
119 refs->gitcommondir = strbuf_detach(&sb, NULL);
99f0d97b 120 refs->packed_ref_store =
1febabff 121 packed_ref_store_init(repo, refs->gitcommondir, flags);
eafb1264 122 refs->log_all_ref_updates = repo_settings_get_log_all_ref_updates(repo);
8e2e8a33 123 repo_config_get_bool(repo, "core.prefersymlinkrefs", &refs->prefer_symlink_refs);
7bd9bcf3 124
5085aef4 125 chdir_notify_reparent("files-backend $GIT_DIR", &refs->base.gitdir);
fb9c2d27
JK
126 chdir_notify_reparent("files-backend $GIT_COMMONDIR",
127 &refs->gitcommondir);
128
00eebe35 129 return ref_store;
a2d5156c 130}
7bd9bcf3 131
32c597e7 132/*
9e7ec634
NTND
133 * Die if refs is not the main ref store. caller is used in any
134 * necessary error messages.
32c597e7
MH
135 */
136static void files_assert_main_repository(struct files_ref_store *refs,
137 const char *caller)
138{
9e7ec634
NTND
139 if (refs->store_flags & REF_STORE_MAIN)
140 return;
141
033abf97 142 BUG("operation %s only allowed for main ref store", caller);
32c597e7
MH
143}
144
a2d5156c 145/*
00eebe35 146 * Downcast ref_store to files_ref_store. Die if ref_store is not a
9e7ec634
NTND
147 * files_ref_store. required_flags is compared with ref_store's
148 * store_flags to ensure the ref_store has all required capabilities.
149 * "caller" is used in any necessary error messages.
a2d5156c 150 */
9e7ec634
NTND
151static struct files_ref_store *files_downcast(struct ref_store *ref_store,
152 unsigned int required_flags,
153 const char *caller)
a2d5156c 154{
32c597e7
MH
155 struct files_ref_store *refs;
156
00eebe35 157 if (ref_store->be != &refs_be_files)
033abf97 158 BUG("ref_store is type \"%s\" not \"files\" in %s",
00eebe35 159 ref_store->be->name, caller);
2eed2780 160
32c597e7
MH
161 refs = (struct files_ref_store *)ref_store;
162
9e7ec634 163 if ((refs->store_flags & required_flags) != required_flags)
033abf97 164 BUG("operation %s requires abilities 0x%x, but only have 0x%x",
9e7ec634 165 caller, required_flags, refs->store_flags);
2eed2780 166
32c597e7 167 return refs;
7bd9bcf3
MH
168}
169
71c871b4
PS
170static void files_ref_store_release(struct ref_store *ref_store)
171{
172 struct files_ref_store *refs = files_downcast(ref_store, 0, "release");
173 free_ref_cache(refs->loose);
174 free(refs->gitcommondir);
175 ref_store_release(refs->packed_ref_store);
e2e373ba 176 free(refs->packed_ref_store);
71c871b4
PS
177}
178
802de3da
NTND
179static void files_reflog_path(struct files_ref_store *refs,
180 struct strbuf *sb,
181 const char *refname)
182{
71e54734
HWN
183 const char *bare_refname;
184 const char *wtname;
185 int wtname_len;
186 enum ref_worktree_type wt_type = parse_worktree_ref(
187 refname, &wtname, &wtname_len, &bare_refname);
188
189 switch (wt_type) {
190 case REF_WORKTREE_CURRENT:
5085aef4 191 strbuf_addf(sb, "%s/logs/%s", refs->base.gitdir, refname);
f57f37e2 192 break;
71e54734
HWN
193 case REF_WORKTREE_SHARED:
194 case REF_WORKTREE_MAIN:
195 strbuf_addf(sb, "%s/logs/%s", refs->gitcommondir, bare_refname);
46c0eb58 196 break;
71e54734
HWN
197 case REF_WORKTREE_OTHER:
198 strbuf_addf(sb, "%s/worktrees/%.*s/logs/%s", refs->gitcommondir,
199 wtname_len, wtname, bare_refname);
f57f37e2
NTND
200 break;
201 default:
71e54734 202 BUG("unknown ref type %d of ref %s", wt_type, refname);
f57f37e2 203 }
802de3da
NTND
204}
205
19e02f4f
NTND
206static void files_ref_path(struct files_ref_store *refs,
207 struct strbuf *sb,
208 const char *refname)
209{
71e54734
HWN
210 const char *bare_refname;
211 const char *wtname;
212 int wtname_len;
213 enum ref_worktree_type wt_type = parse_worktree_ref(
214 refname, &wtname, &wtname_len, &bare_refname);
215 switch (wt_type) {
216 case REF_WORKTREE_CURRENT:
5085aef4 217 strbuf_addf(sb, "%s/%s", refs->base.gitdir, refname);
f57f37e2 218 break;
71e54734
HWN
219 case REF_WORKTREE_OTHER:
220 strbuf_addf(sb, "%s/worktrees/%.*s/%s", refs->gitcommondir,
221 wtname_len, wtname, bare_refname);
222 break;
223 case REF_WORKTREE_SHARED:
224 case REF_WORKTREE_MAIN:
225 strbuf_addf(sb, "%s/%s", refs->gitcommondir, bare_refname);
f57f37e2
NTND
226 break;
227 default:
71e54734 228 BUG("unknown ref type %d of ref %s", wt_type, refname);
f57f37e2 229 }
19e02f4f
NTND
230}
231
09e65645 232/*
b9317d55 233 * Manually add refs/bisect, refs/rewritten and refs/worktree, which, being
09e65645
NTND
234 * per-worktree, might not appear in the directory listing for
235 * refs/ in the main repo.
236 */
237static void add_per_worktree_entries_to_dir(struct ref_dir *dir, const char *dirname)
238{
b9317d55 239 const char *prefixes[] = { "refs/bisect/", "refs/worktree/", "refs/rewritten/" };
90d31ff5 240 int ip;
09e65645
NTND
241
242 if (strcmp(dirname, "refs/"))
243 return;
244
90d31ff5
NTND
245 for (ip = 0; ip < ARRAY_SIZE(prefixes); ip++) {
246 const char *prefix = prefixes[ip];
247 int prefix_len = strlen(prefix);
248 struct ref_entry *child_entry;
249 int pos;
09e65645 250
90d31ff5
NTND
251 pos = search_ref_dir(dir, prefix, prefix_len);
252 if (pos >= 0)
253 continue;
750036c8 254 child_entry = create_dir_entry(dir->cache, prefix, prefix_len);
09e65645
NTND
255 add_entry_to_dir(dir, child_entry);
256 }
257}
258
f768296c
KN
259static void loose_fill_ref_dir_regular_file(struct files_ref_store *refs,
260 const char *refname,
261 struct ref_dir *dir)
262{
263 struct object_id oid;
264 int flag;
cfd97152
JC
265 const char *referent = refs_resolve_ref_unsafe(&refs->base,
266 refname,
267 RESOLVE_REF_READING,
268 &oid, &flag);
f768296c 269
cfd97152 270 if (!referent) {
a6ebc2c6 271 oidclr(&oid, refs->base.repo->hash_algo);
f768296c
KN
272 flag |= REF_ISBROKEN;
273 } else if (is_null_oid(&oid)) {
274 /*
275 * It is so astronomically unlikely
276 * that null_oid is the OID of an
277 * actual object that we consider its
278 * appearance in a loose reference
279 * file to be repo corruption
280 * (probably due to a software bug).
281 */
282 flag |= REF_ISBROKEN;
283 }
284
285 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
286 if (!refname_is_safe(refname))
287 die("loose refname is dangerous: %s", refname);
a6ebc2c6 288 oidclr(&oid, refs->base.repo->hash_algo);
f768296c
KN
289 flag |= REF_BAD_NAME | REF_ISBROKEN;
290 }
cfd97152
JC
291
292 if (!(flag & REF_ISSYMREF))
293 referent = NULL;
294
295 add_entry_to_dir(dir, create_ref_entry(refname, referent, &oid, flag));
f768296c
KN
296}
297
7bd9bcf3
MH
298/*
299 * Read the loose references from the namespace dirname into dir
300 * (without recursing). dirname must end with '/'. dir must be the
301 * directory entry corresponding to dirname.
302 */
df308759
MH
303static void loose_fill_ref_dir(struct ref_store *ref_store,
304 struct ref_dir *dir, const char *dirname)
7bd9bcf3 305{
df308759
MH
306 struct files_ref_store *refs =
307 files_downcast(ref_store, REF_STORE_READ, "fill_ref_dir");
7bd9bcf3
MH
308 DIR *d;
309 struct dirent *de;
310 int dirnamelen = strlen(dirname);
311 struct strbuf refname;
312 struct strbuf path = STRBUF_INIT;
7bd9bcf3 313
19e02f4f 314 files_ref_path(refs, &path, dirname);
7bd9bcf3
MH
315
316 d = opendir(path.buf);
317 if (!d) {
318 strbuf_release(&path);
319 return;
320 }
321
322 strbuf_init(&refname, dirnamelen + 257);
323 strbuf_add(&refname, dirname, dirnamelen);
324
325 while ((de = readdir(d)) != NULL) {
2cdb7961 326 unsigned char dtype;
7bd9bcf3
MH
327
328 if (de->d_name[0] == '.')
329 continue;
330 if (ends_with(de->d_name, ".lock"))
331 continue;
332 strbuf_addstr(&refname, de->d_name);
2cdb7961
VD
333
334 dtype = get_dtype(de, &path, 1);
335 if (dtype == DT_DIR) {
7bd9bcf3
MH
336 strbuf_addch(&refname, '/');
337 add_entry_to_dir(dir,
e00d1a4f 338 create_dir_entry(dir->cache, refname.buf,
750036c8 339 refname.len));
2cdb7961 340 } else if (dtype == DT_REG) {
f768296c 341 loose_fill_ref_dir_regular_file(refs, refname.buf, dir);
7bd9bcf3
MH
342 }
343 strbuf_setlen(&refname, dirnamelen);
7bd9bcf3
MH
344 }
345 strbuf_release(&refname);
346 strbuf_release(&path);
347 closedir(d);
e3bf2989 348
09e65645 349 add_per_worktree_entries_to_dir(dir, dirname);
7bd9bcf3
MH
350}
351
120b6717
PS
352static int for_each_root_ref(struct files_ref_store *refs,
353 int (*cb)(const char *refname, void *cb_data),
354 void *cb_data)
d0f00c1a 355{
d0f00c1a 356 struct strbuf path = STRBUF_INIT, refname = STRBUF_INIT;
66275a63 357 const char *dirname = refs->loose->root->name;
d0f00c1a
KN
358 struct dirent *de;
359 size_t dirnamelen;
120b6717 360 int ret;
d0f00c1a
KN
361 DIR *d;
362
363 files_ref_path(refs, &path, dirname);
364
365 d = opendir(path.buf);
366 if (!d) {
367 strbuf_release(&path);
120b6717 368 return -1;
d0f00c1a
KN
369 }
370
371 strbuf_addstr(&refname, dirname);
372 dirnamelen = refname.len;
373
374 while ((de = readdir(d)) != NULL) {
375 unsigned char dtype;
376
377 if (de->d_name[0] == '.')
378 continue;
379 if (ends_with(de->d_name, ".lock"))
380 continue;
381 strbuf_addstr(&refname, de->d_name);
382
383 dtype = get_dtype(de, &path, 1);
120b6717
PS
384 if (dtype == DT_REG && is_root_ref(de->d_name)) {
385 ret = cb(refname.buf, cb_data);
386 if (ret)
387 goto done;
388 }
d0f00c1a
KN
389
390 strbuf_setlen(&refname, dirnamelen);
391 }
120b6717
PS
392
393 ret = 0;
394
395done:
d0f00c1a
KN
396 strbuf_release(&refname);
397 strbuf_release(&path);
398 closedir(d);
120b6717
PS
399 return ret;
400}
401
402struct fill_root_ref_data {
403 struct files_ref_store *refs;
404 struct ref_dir *dir;
405};
406
407static int fill_root_ref(const char *refname, void *cb_data)
408{
409 struct fill_root_ref_data *data = cb_data;
410 loose_fill_ref_dir_regular_file(data->refs, refname, data->dir);
411 return 0;
412}
413
414/*
415 * Add root refs to the ref dir by parsing the directory for any files which
416 * follow the root ref syntax.
417 */
418static void add_root_refs(struct files_ref_store *refs,
419 struct ref_dir *dir)
420{
421 struct fill_root_ref_data data = {
422 .refs = refs,
423 .dir = dir,
424 };
425
426 for_each_root_ref(refs, fill_root_ref, &data);
d0f00c1a
KN
427}
428
429static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs,
430 unsigned int flags)
7bd9bcf3
MH
431{
432 if (!refs->loose) {
d0f00c1a
KN
433 struct ref_dir *dir;
434
7bd9bcf3
MH
435 /*
436 * Mark the top-level directory complete because we
437 * are about to read the only subdirectory that can
438 * hold references:
439 */
df308759 440 refs->loose = create_ref_cache(&refs->base, loose_fill_ref_dir);
7c22bc8a
MH
441
442 /* We're going to fill the top level ourselves: */
443 refs->loose->root->flag &= ~REF_INCOMPLETE;
444
d0f00c1a
KN
445 dir = get_ref_dir(refs->loose->root);
446
447 if (flags & DO_FOR_EACH_INCLUDE_ROOT_REFS)
66275a63 448 add_root_refs(refs, dir);
d0f00c1a 449
7bd9bcf3 450 /*
7c22bc8a
MH
451 * Add an incomplete entry for "refs/" (to be filled
452 * lazily):
7bd9bcf3 453 */
d0f00c1a 454 add_entry_to_dir(dir, create_dir_entry(refs->loose, "refs/", 5));
7bd9bcf3 455 }
a714b19c 456 return refs->loose;
7bd9bcf3
MH
457}
458
0a7b3870
PS
459static int read_ref_internal(struct ref_store *ref_store, const char *refname,
460 struct object_id *oid, struct strbuf *referent,
461 unsigned int *type, int *failure_errno, int skip_packed_refs)
7bd9bcf3 462{
4308651c 463 struct files_ref_store *refs =
9e7ec634 464 files_downcast(ref_store, REF_STORE_READ, "read_raw_ref");
42a38cf7
MH
465 struct strbuf sb_contents = STRBUF_INIT;
466 struct strbuf sb_path = STRBUF_INIT;
7048653a
DT
467 const char *path;
468 const char *buf;
469 struct stat st;
470 int fd;
42a38cf7 471 int ret = -1;
e8c42cb9 472 int remaining_retries = 3;
df3458e9 473 int myerr = 0;
7bd9bcf3 474
fa96ea1b 475 *type = 0;
42a38cf7 476 strbuf_reset(&sb_path);
34c7ad8f 477
19e02f4f 478 files_ref_path(refs, &sb_path, refname);
34c7ad8f 479
42a38cf7 480 path = sb_path.buf;
7bd9bcf3 481
7048653a
DT
482stat_ref:
483 /*
484 * We might have to loop back here to avoid a race
485 * condition: first we lstat() the file, then we try
486 * to read it as a link or as a file. But if somebody
487 * changes the type of the file (file <-> directory
488 * <-> symlink) between the lstat() and reading, then
489 * we don't want to report that as an error but rather
490 * try again starting with the lstat().
e8c42cb9
JK
491 *
492 * We'll keep a count of the retries, though, just to avoid
493 * any confusing situation sending us into an infinite loop.
7048653a 494 */
7bd9bcf3 495
e8c42cb9
JK
496 if (remaining_retries-- <= 0)
497 goto out;
498
7048653a 499 if (lstat(path, &st) < 0) {
8b72fea7 500 int ignore_errno;
df3458e9 501 myerr = errno;
0a7b3870 502 if (myerr != ENOENT || skip_packed_refs)
42a38cf7 503 goto out;
8b72fea7
HWN
504 if (refs_read_raw_ref(refs->packed_ref_store, refname, oid,
505 referent, type, &ignore_errno)) {
df3458e9 506 myerr = ENOENT;
42a38cf7 507 goto out;
7bd9bcf3 508 }
42a38cf7
MH
509 ret = 0;
510 goto out;
7bd9bcf3 511 }
7bd9bcf3 512
7048653a
DT
513 /* Follow "normalized" - ie "refs/.." symlinks by hand */
514 if (S_ISLNK(st.st_mode)) {
42a38cf7 515 strbuf_reset(&sb_contents);
765b496d 516 if (strbuf_readlink(&sb_contents, path, st.st_size) < 0) {
df3458e9 517 myerr = errno;
df3458e9 518 if (myerr == ENOENT || myerr == EINVAL)
7bd9bcf3
MH
519 /* inconsistent with lstat; retry */
520 goto stat_ref;
521 else
42a38cf7 522 goto out;
7bd9bcf3 523 }
42a38cf7
MH
524 if (starts_with(sb_contents.buf, "refs/") &&
525 !check_refname_format(sb_contents.buf, 0)) {
92b38093 526 strbuf_swap(&sb_contents, referent);
3a0b6b9a 527 *type |= REF_ISSYMREF;
42a38cf7
MH
528 ret = 0;
529 goto out;
7bd9bcf3 530 }
3f7bd767
JK
531 /*
532 * It doesn't look like a refname; fall through to just
533 * treating it like a non-symlink, and reading whatever it
534 * points to.
535 */
7048653a 536 }
7bd9bcf3 537
7048653a
DT
538 /* Is it a directory? */
539 if (S_ISDIR(st.st_mode)) {
8b72fea7 540 int ignore_errno;
e167a567
MH
541 /*
542 * Even though there is a directory where the loose
543 * ref is supposed to be, there could still be a
544 * packed ref:
545 */
0a7b3870
PS
546 if (skip_packed_refs ||
547 refs_read_raw_ref(refs->packed_ref_store, refname, oid,
8b72fea7 548 referent, type, &ignore_errno)) {
df3458e9 549 myerr = EISDIR;
e167a567
MH
550 goto out;
551 }
552 ret = 0;
42a38cf7 553 goto out;
7048653a
DT
554 }
555
556 /*
557 * Anything else, just open it and try to use it as
558 * a ref
559 */
560 fd = open(path, O_RDONLY);
561 if (fd < 0) {
df3458e9
HWN
562 myerr = errno;
563 if (myerr == ENOENT && !S_ISLNK(st.st_mode))
7048653a
DT
564 /* inconsistent with lstat; retry */
565 goto stat_ref;
566 else
42a38cf7 567 goto out;
7048653a 568 }
42a38cf7
MH
569 strbuf_reset(&sb_contents);
570 if (strbuf_read(&sb_contents, fd, 256) < 0) {
df3458e9 571 myerr = errno;
7048653a 572 close(fd);
42a38cf7 573 goto out;
7048653a
DT
574 }
575 close(fd);
42a38cf7
MH
576 strbuf_rtrim(&sb_contents);
577 buf = sb_contents.buf;
e39620f0 578
080b068f 579 ret = parse_loose_ref_contents(ref_store->repo->hash_algo, buf,
1c0e2a00 580 oid, referent, type, NULL, &myerr);
e39620f0
HWN
581
582out:
df3458e9
HWN
583 if (ret && !myerr)
584 BUG("returning non-zero %d, should have set myerr!", ret);
585 *failure_errno = myerr;
586
e39620f0
HWN
587 strbuf_release(&sb_path);
588 strbuf_release(&sb_contents);
cac15b3f 589 errno = 0;
e39620f0
HWN
590 return ret;
591}
592
0a7b3870
PS
593static int files_read_raw_ref(struct ref_store *ref_store, const char *refname,
594 struct object_id *oid, struct strbuf *referent,
595 unsigned int *type, int *failure_errno)
596{
597 return read_ref_internal(ref_store, refname, oid, referent, type, failure_errno, 0);
598}
599
600static int files_read_symbolic_ref(struct ref_store *ref_store, const char *refname,
601 struct strbuf *referent)
602{
603 struct object_id oid;
604 int failure_errno, ret;
605 unsigned int type;
606
607 ret = read_ref_internal(ref_store, refname, &oid, referent, &type, &failure_errno, 1);
8102d10f
BF
608 if (!ret && !(type & REF_ISSYMREF))
609 return NOT_A_SYMREF;
610 return ret;
0a7b3870
PS
611}
612
080b068f
PS
613int parse_loose_ref_contents(const struct git_hash_algo *algop,
614 const char *buf, struct object_id *oid,
df3458e9 615 struct strbuf *referent, unsigned int *type,
1c0e2a00 616 const char **trailing, int *failure_errno)
e39620f0
HWN
617{
618 const char *p;
145136a9 619 if (skip_prefix(buf, "ref:", &buf)) {
7bd9bcf3
MH
620 while (isspace(*buf))
621 buf++;
7048653a 622
92b38093
MH
623 strbuf_reset(referent);
624 strbuf_addstr(referent, buf);
3a0b6b9a 625 *type |= REF_ISSYMREF;
e39620f0 626 return 0;
7bd9bcf3 627 }
7bd9bcf3 628
7048653a 629 /*
e39620f0 630 * FETCH_HEAD has additional data after the sha.
7048653a 631 */
080b068f 632 if (parse_oid_hex_algop(buf, oid, &p, algop) ||
99afe91a 633 (*p != '\0' && !isspace(*p))) {
3a0b6b9a 634 *type |= REF_ISBROKEN;
df3458e9 635 *failure_errno = EINVAL;
e39620f0 636 return -1;
7048653a 637 }
1c0e2a00 638
639 if (trailing)
640 *trailing = p;
641
e39620f0 642 return 0;
7bd9bcf3
MH
643}
644
8415d247
MH
645static void unlock_ref(struct ref_lock *lock)
646{
611986f3
KN
647 lock->count--;
648 if (!lock->count) {
649 rollback_lock_file(&lock->lk);
650 free(lock->ref_name);
651 free(lock);
652 }
8415d247
MH
653}
654
3c070632
KN
655/*
656 * Check if the transaction has another update with a case-insensitive refname
657 * match.
658 *
659 * If the update is part of the transaction, we only check up to that index.
660 * Further updates are expected to call this function to match previous indices.
661 */
662static bool transaction_has_case_conflicting_update(struct ref_transaction *transaction,
663 struct ref_update *update)
664{
665 for (size_t i = 0; i < transaction->nr; i++) {
666 if (transaction->updates[i] == update)
667 break;
668
669 if (!strcasecmp(transaction->updates[i]->refname, update->refname))
670 return true;
671 }
672 return false;
673}
674
92b1551b
MH
675/*
676 * Lock refname, without following symrefs, and set *lock_p to point
677 * at a newly-allocated lock object. Fill in lock->old_oid, referent,
678 * and type similarly to read_raw_ref().
679 *
680 * The caller must verify that refname is a "safe" reference name (in
681 * the sense of refname_is_safe()) before calling this function.
682 *
683 * If the reference doesn't already exist, verify that refname doesn't
684 * have a D/F conflict with any existing references. extras and skip
524a9fdb 685 * are passed to refs_verify_refname_available() for this check.
92b1551b
MH
686 *
687 * If mustexist is not set and the reference is not found or is
78fb4579 688 * broken, lock the reference anyway but clear old_oid.
92b1551b
MH
689 *
690 * Return 0 on success. On failure, write an error message to err and
76e760b9 691 * return REF_TRANSACTION_ERROR_NAME_CONFLICT or REF_TRANSACTION_ERROR_GENERIC.
92b1551b
MH
692 *
693 * Implementation note: This function is basically
694 *
695 * lock reference
696 * read_raw_ref()
697 *
698 * but it includes a lot more code to
699 * - Deal with possible races with other processes
524a9fdb 700 * - Avoid calling refs_verify_refname_available() when it can be
92b1551b
MH
701 * avoided, namely if we were successfully able to read the ref
702 * - Generate informative error messages in the case of failure
703 */
76e760b9 704static enum ref_transaction_error lock_raw_ref(struct files_ref_store *refs,
3c070632 705 struct ref_transaction *transaction,
31726bb9 706 size_t update_idx,
76e760b9
KN
707 int mustexist,
708 struct string_list *refnames_to_check,
76e760b9
KN
709 struct ref_lock **lock_p,
710 struct strbuf *referent,
76e760b9
KN
711 struct strbuf *err)
712{
713 enum ref_transaction_error ret = REF_TRANSACTION_ERROR_GENERIC;
3c070632
KN
714 struct ref_update *update = transaction->updates[update_idx];
715 const struct string_list *extras = &transaction->refnames;
31726bb9
KN
716 const char *refname = update->refname;
717 unsigned int *type = &update->type;
92b1551b
MH
718 struct ref_lock *lock;
719 struct strbuf ref_file = STRBUF_INIT;
720 int attempts_remaining = 3;
5b12e16b 721 int failure_errno;
92b1551b
MH
722
723 assert(err);
32c597e7 724 files_assert_main_repository(refs, "lock_raw_ref");
f7b0a987 725
92b1551b
MH
726 *type = 0;
727
728 /* First lock the file so it can't change out from under us. */
729
ca56dadb 730 *lock_p = CALLOC_ARRAY(lock, 1);
92b1551b
MH
731
732 lock->ref_name = xstrdup(refname);
611986f3 733 lock->count = 1;
19e02f4f 734 files_ref_path(refs, &ref_file, refname);
92b1551b
MH
735
736retry:
1a99fe80 737 switch (safe_create_leading_directories(the_repository, ref_file.buf)) {
92b1551b
MH
738 case SCLD_OK:
739 break; /* success */
740 case SCLD_EXISTS:
741 /*
742 * Suppose refname is "refs/foo/bar". We just failed
743 * to create the containing directory, "refs/foo",
744 * because there was a non-directory in the way. This
745 * indicates a D/F conflict, probably because of
746 * another reference such as "refs/foo". There is no
747 * reason to expect this error to be transitory.
748 */
7d2df051 749 if (refs_verify_refname_available(&refs->base, refname,
e4929cdf 750 extras, NULL, 0, err)) {
92b1551b
MH
751 if (mustexist) {
752 /*
753 * To the user the relevant error is
754 * that the "mustexist" reference is
755 * missing:
756 */
757 strbuf_reset(err);
758 strbuf_addf(err, "unable to resolve reference '%s'",
759 refname);
76e760b9 760 ret = REF_TRANSACTION_ERROR_NONEXISTENT_REF;
92b1551b
MH
761 } else {
762 /*
763 * The error message set by
524a9fdb
MH
764 * refs_verify_refname_available() is
765 * OK.
92b1551b 766 */
76e760b9 767 ret = REF_TRANSACTION_ERROR_NAME_CONFLICT;
92b1551b
MH
768 }
769 } else {
770 /*
771 * The file that is in the way isn't a loose
772 * reference. Report it as a low-level
773 * failure.
774 */
775 strbuf_addf(err, "unable to create lock file %s.lock; "
776 "non-directory in the way",
777 ref_file.buf);
778 }
779 goto error_return;
780 case SCLD_VANISHED:
781 /* Maybe another process was tidying up. Try again. */
782 if (--attempts_remaining > 0)
783 goto retry;
784 /* fall through */
785 default:
786 strbuf_addf(err, "unable to create directory for %s",
787 ref_file.buf);
788 goto error_return;
789 }
790
4ff0f01c 791 if (hold_lock_file_for_update_timeout(
ee4d8e45 792 &lock->lk, ref_file.buf, LOCK_NO_DEREF,
4ff0f01c 793 get_files_ref_lock_timeout_ms()) < 0) {
5b12e16b
HWN
794 int myerr = errno;
795 errno = 0;
796 if (myerr == ENOENT && --attempts_remaining > 0) {
92b1551b
MH
797 /*
798 * Maybe somebody just deleted one of the
799 * directories leading to ref_file. Try
800 * again:
801 */
802 goto retry;
803 } else {
5b12e16b 804 unable_to_lock_message(ref_file.buf, myerr, err);
9b62a67b
KN
805 if (myerr == EEXIST) {
806 if (ignore_case &&
807 transaction_has_case_conflicting_update(transaction, update)) {
808 /*
809 * In case-insensitive filesystems, ensure that conflicts within a
810 * given transaction are handled. Pre-existing refs on a
811 * case-insensitive system will be overridden without any issue.
812 */
813 ret = REF_TRANSACTION_ERROR_CASE_CONFLICT;
814 } else {
815 /*
816 * Pre-existing case-conflicting reference locks should also be
817 * specially categorized to avoid failing all batched updates.
818 */
819 ret = REF_TRANSACTION_ERROR_CREATE_EXISTS;
820 }
821 }
822
92b1551b
MH
823 goto error_return;
824 }
825 }
826
827 /*
828 * Now we hold the lock and can read the reference without
829 * fear that its value will change.
830 */
831
5b12e16b
HWN
832 if (files_read_raw_ref(&refs->base, refname, &lock->old_oid, referent,
833 type, &failure_errno)) {
31726bb9
KN
834 struct string_list_item *item;
835
5b12e16b 836 if (failure_errno == ENOENT) {
92b1551b
MH
837 if (mustexist) {
838 /* Garden variety missing reference. */
839 strbuf_addf(err, "unable to resolve reference '%s'",
840 refname);
76e760b9 841 ret = REF_TRANSACTION_ERROR_NONEXISTENT_REF;
92b1551b
MH
842 goto error_return;
843 } else {
844 /*
845 * Reference is missing, but that's OK. We
846 * know that there is not a conflict with
847 * another loose reference because
848 * (supposing that we are trying to lock
849 * reference "refs/foo/bar"):
850 *
851 * - We were successfully able to create
852 * the lockfile refs/foo/bar.lock, so we
853 * know there cannot be a loose reference
854 * named "refs/foo".
855 *
856 * - We got ENOENT and not EISDIR, so we
857 * know that there cannot be a loose
858 * reference named "refs/foo/bar/baz".
859 */
860 }
5b12e16b 861 } else if (failure_errno == EISDIR) {
92b1551b
MH
862 /*
863 * There is a directory in the way. It might have
864 * contained references that have been deleted. If
865 * we don't require that the reference already
866 * exists, try to remove the directory so that it
867 * doesn't cause trouble when we want to rename the
868 * lockfile into place later.
869 */
870 if (mustexist) {
871 /* Garden variety missing reference. */
872 strbuf_addf(err, "unable to resolve reference '%s'",
873 refname);
76e760b9 874 ret = REF_TRANSACTION_ERROR_NONEXISTENT_REF;
92b1551b
MH
875 goto error_return;
876 } else if (remove_dir_recursively(&ref_file,
877 REMOVE_DIR_EMPTY_ONLY)) {
948b2ab0 878 ret = REF_TRANSACTION_ERROR_NAME_CONFLICT;
b05855b5
MH
879 if (refs_verify_refname_available(
880 &refs->base, refname,
e4929cdf 881 extras, NULL, 0, err)) {
92b1551b
MH
882 /*
883 * The error message set by
884 * verify_refname_available() is OK.
885 */
92b1551b
MH
886 goto error_return;
887 } else {
888 /*
948b2ab0
KN
889 * Directory conflicts can occur if there
890 * is an existing lock file in the directory
891 * or if the filesystem is case-insensitive
892 * and the directory contains a valid reference
893 * but conflicts with the update.
92b1551b
MH
894 */
895 strbuf_addf(err, "there is a non-empty directory '%s' "
896 "blocking reference '%s'",
897 ref_file.buf, refname);
898 goto error_return;
899 }
900 }
5b12e16b 901 } else if (failure_errno == EINVAL && (*type & REF_ISBROKEN)) {
92b1551b
MH
902 strbuf_addf(err, "unable to resolve reference '%s': "
903 "reference broken", refname);
904 goto error_return;
905 } else {
906 strbuf_addf(err, "unable to resolve reference '%s': %s",
5b12e16b 907 refname, strerror(failure_errno));
92b1551b
MH
908 goto error_return;
909 }
910
911 /*
6c90726b
PS
912 * If the ref did not exist and we are creating it, we have to
913 * make sure there is no existing packed ref that conflicts
914 * with refname. This check is deferred so that we can batch it.
770f389b
KN
915 *
916 * For case-insensitive filesystems, we should also check for F/D
917 * conflicts between 'foo' and 'Foo/bar'. So let's lowercase
918 * the refname.
92b1551b 919 */
770f389b
KN
920 if (ignore_case) {
921 struct strbuf lower = STRBUF_INIT;
922
923 strbuf_addstr(&lower, refname);
924 strbuf_tolower(&lower);
925
926 item = string_list_append_nodup(refnames_to_check,
927 strbuf_detach(&lower, NULL));
928 } else {
929 item = string_list_append(refnames_to_check, refname);
930 }
931
31726bb9
KN
932 item->util = xmalloc(sizeof(update_idx));
933 memcpy(item->util, &update_idx, sizeof(update_idx));
92b1551b
MH
934 }
935
936 ret = 0;
937 goto out;
938
939error_return:
940 unlock_ref(lock);
941 *lock_p = NULL;
942
943out:
944 strbuf_release(&ref_file);
945 return ret;
946}
947
3bc581b9
MH
948struct files_ref_iterator {
949 struct ref_iterator base;
950
3bc581b9 951 struct ref_iterator *iter0;
9bc45a28 952 struct repository *repo;
3bc581b9
MH
953 unsigned int flags;
954};
7bd9bcf3 955
3bc581b9
MH
956static int files_ref_iterator_advance(struct ref_iterator *ref_iterator)
957{
958 struct files_ref_iterator *iter =
959 (struct files_ref_iterator *)ref_iterator;
960 int ok;
7bd9bcf3 961
3bc581b9 962 while ((ok = ref_iterator_advance(iter->iter0)) == ITER_OK) {
0c09ec07 963 if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&
89baa52d 964 parse_worktree_ref(iter->iter0->ref.name, NULL, NULL,
71e54734 965 NULL) != REF_WORKTREE_CURRENT)
0c09ec07
DT
966 continue;
967
8dccb224 968 if ((iter->flags & DO_FOR_EACH_OMIT_DANGLING_SYMREFS) &&
89baa52d
PS
969 (iter->iter0->ref.flags & REF_ISSYMREF) &&
970 (iter->iter0->ref.flags & REF_ISBROKEN))
8dccb224
JK
971 continue;
972
3bc581b9 973 if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
89baa52d 974 !ref_resolves_to_object(iter->iter0->ref.name,
9bc45a28 975 iter->repo,
89baa52d
PS
976 iter->iter0->ref.oid,
977 iter->iter0->ref.flags))
3bc581b9
MH
978 continue;
979
89baa52d 980 iter->base.ref = iter->iter0->ref;
cfd97152 981
3bc581b9 982 return ITER_OK;
7bd9bcf3
MH
983 }
984
3bc581b9 985 return ok;
7bd9bcf3
MH
986}
987
a95da5c8 988static int files_ref_iterator_seek(struct ref_iterator *ref_iterator,
2b4648b9 989 const char *refname, unsigned int flags)
a95da5c8
PS
990{
991 struct files_ref_iterator *iter =
992 (struct files_ref_iterator *)ref_iterator;
2b4648b9 993 return ref_iterator_seek(iter->iter0, refname, flags);
a95da5c8
PS
994}
995
cec2b6f5 996static void files_ref_iterator_release(struct ref_iterator *ref_iterator)
3bc581b9
MH
997{
998 struct files_ref_iterator *iter =
999 (struct files_ref_iterator *)ref_iterator;
cec2b6f5 1000 ref_iterator_free(iter->iter0);
3bc581b9
MH
1001}
1002
1003static struct ref_iterator_vtable files_ref_iterator_vtable = {
e2f8acb6 1004 .advance = files_ref_iterator_advance,
a95da5c8 1005 .seek = files_ref_iterator_seek,
cec2b6f5 1006 .release = files_ref_iterator_release,
3bc581b9
MH
1007};
1008
1a769003 1009static struct ref_iterator *files_ref_iterator_begin(
37b6f6d5 1010 struct ref_store *ref_store,
b269ac53
TB
1011 const char *prefix, const char **exclude_patterns,
1012 unsigned int flags)
3bc581b9 1013{
9e7ec634 1014 struct files_ref_store *refs;
8738a8a4 1015 struct ref_iterator *loose_iter, *packed_iter, *overlay_iter;
3bc581b9
MH
1016 struct files_ref_iterator *iter;
1017 struct ref_iterator *ref_iterator;
0a0865b8 1018 unsigned int required_flags = REF_STORE_READ;
3bc581b9 1019
0a0865b8
MH
1020 if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN))
1021 required_flags |= REF_STORE_ODB;
3bc581b9 1022
0a0865b8 1023 refs = files_downcast(ref_store, required_flags, "ref_iterator_begin");
9e7ec634 1024
3bc581b9
MH
1025 /*
1026 * We must make sure that all loose refs are read before
1027 * accessing the packed-refs file; this avoids a race
1028 * condition if loose refs are migrated to the packed-refs
1029 * file by a simultaneous process, but our in-memory view is
1030 * from before the migration. We ensure this as follows:
059ae35a
MH
1031 * First, we call start the loose refs iteration with its
1032 * `prime_ref` argument set to true. This causes the loose
1033 * references in the subtree to be pre-read into the cache.
1034 * (If they've already been read, that's OK; we only need to
1035 * guarantee that they're read before the packed refs, not
1036 * *how much* before.) After that, we call
38b86e81
MH
1037 * packed_ref_iterator_begin(), which internally checks
1038 * whether the packed-ref cache is up to date with what is on
1039 * disk, and re-reads it if not.
3bc581b9
MH
1040 */
1041
d0f00c1a 1042 loose_iter = cache_ref_iterator_begin(get_loose_ref_cache(refs, flags),
8788195c 1043 prefix, ref_store->repo, 1);
3bc581b9 1044
38b86e81
MH
1045 /*
1046 * The packed-refs file might contain broken references, for
1047 * example an old version of a reference that points at an
1048 * object that has since been garbage-collected. This is OK as
1049 * long as there is a corresponding loose reference that
1050 * overrides it, and we don't want to emit an error message in
1051 * this case. So ask the packed_ref_store for all of its
1052 * references, and (if needed) do our own check for broken
1053 * ones in files_ref_iterator_advance(), after we have merged
1054 * the packed and loose references.
1055 */
e0cc8ac8 1056 packed_iter = refs_ref_iterator_begin(
b269ac53 1057 refs->packed_ref_store, prefix, exclude_patterns, 0,
38b86e81 1058 DO_FOR_EACH_INCLUDE_BROKEN);
3bc581b9 1059
8738a8a4
MH
1060 overlay_iter = overlay_ref_iterator_begin(loose_iter, packed_iter);
1061
ca56dadb 1062 CALLOC_ARRAY(iter, 1);
8738a8a4 1063 ref_iterator = &iter->base;
5e01d838 1064 base_ref_iterator_init(ref_iterator, &files_ref_iterator_vtable);
8738a8a4 1065 iter->iter0 = overlay_iter;
9bc45a28 1066 iter->repo = ref_store->repo;
3bc581b9
MH
1067 iter->flags = flags;
1068
1069 return ref_iterator;
7bd9bcf3
MH
1070}
1071
3fa2e91d
ÆAB
1072/*
1073 * Callback function for raceproof_create_file(). This function is
1074 * expected to do something that makes dirname(path) permanent despite
1075 * the fact that other processes might be cleaning up empty
1076 * directories at the same time. Usually it will create a file named
1077 * path, but alternatively it could create another file in that
1078 * directory, or even chdir() into that directory. The function should
1079 * return 0 if the action was completed successfully. On error, it
1080 * should return a nonzero result and set errno.
1081 * raceproof_create_file() treats two errno values specially:
1082 *
1083 * - ENOENT -- dirname(path) does not exist. In this case,
1084 * raceproof_create_file() tries creating dirname(path)
1085 * (and any parent directories, if necessary) and calls
1086 * the function again.
1087 *
1088 * - EISDIR -- the file already exists and is a directory. In this
1089 * case, raceproof_create_file() removes the directory if
1090 * it is empty (and recursively any empty directories that
1091 * it contains) and calls the function again.
1092 *
1093 * Any other errno causes raceproof_create_file() to fail with the
1094 * callback's return value and errno.
1095 *
1096 * Obviously, this function should be OK with being called again if it
1097 * fails with ENOENT or EISDIR. In other scenarios it will not be
1098 * called again.
1099 */
1100typedef int create_file_fn(const char *path, void *cb);
1101
1102/*
1103 * Create a file in dirname(path) by calling fn, creating leading
1104 * directories if necessary. Retry a few times in case we are racing
1105 * with another process that is trying to clean up the directory that
1106 * contains path. See the documentation for create_file_fn for more
1107 * details.
1108 *
1109 * Return the value and set the errno that resulted from the most
1110 * recent call of fn. fn is always called at least once, and will be
1111 * called more than once if it returns ENOENT or EISDIR.
1112 */
1113static int raceproof_create_file(const char *path, create_file_fn fn, void *cb)
1114{
1115 /*
1116 * The number of times we will try to remove empty directories
1117 * in the way of path. This is only 1 because if another
1118 * process is racily creating directories that conflict with
1119 * us, we don't want to fight against them.
1120 */
1121 int remove_directories_remaining = 1;
1122
1123 /*
1124 * The number of times that we will try to create the
1125 * directories containing path. We are willing to attempt this
1126 * more than once, because another process could be trying to
1127 * clean up empty directories at the same time as we are
1128 * trying to create them.
1129 */
1130 int create_directories_remaining = 3;
1131
1132 /* A scratch copy of path, filled lazily if we need it: */
1133 struct strbuf path_copy = STRBUF_INIT;
1134
1135 int ret, save_errno;
1136
1137 /* Sanity check: */
1138 assert(*path);
1139
1140retry_fn:
1141 ret = fn(path, cb);
1142 save_errno = errno;
1143 if (!ret)
1144 goto out;
1145
1146 if (errno == EISDIR && remove_directories_remaining-- > 0) {
1147 /*
1148 * A directory is in the way. Maybe it is empty; try
1149 * to remove it:
1150 */
1151 if (!path_copy.len)
1152 strbuf_addstr(&path_copy, path);
1153
1154 if (!remove_dir_recursively(&path_copy, REMOVE_DIR_EMPTY_ONLY))
1155 goto retry_fn;
1156 } else if (errno == ENOENT && create_directories_remaining-- > 0) {
1157 /*
1158 * Maybe the containing directory didn't exist, or
1159 * maybe it was just deleted by a process that is
1160 * racing with us to clean up empty directories. Try
1161 * to create it:
1162 */
1163 enum scld_error scld_result;
1164
1165 if (!path_copy.len)
1166 strbuf_addstr(&path_copy, path);
1167
1168 do {
1a99fe80 1169 scld_result = safe_create_leading_directories(the_repository, path_copy.buf);
3fa2e91d
ÆAB
1170 if (scld_result == SCLD_OK)
1171 goto retry_fn;
1172 } while (scld_result == SCLD_VANISHED && create_directories_remaining-- > 0);
1173 }
1174
1175out:
1176 strbuf_release(&path_copy);
1177 errno = save_errno;
1178 return ret;
1179}
1180
7bd9bcf3
MH
1181static int remove_empty_directories(struct strbuf *path)
1182{
1183 /*
1184 * we want to create a file but there is a directory there;
1185 * if that is an empty directory (or a directory that contains
1186 * only empty directories), remove them.
1187 */
1188 return remove_dir_recursively(path, REMOVE_DIR_EMPTY_ONLY);
1189}
1190
3b5d3c98
MH
1191static int create_reflock(const char *path, void *cb)
1192{
1193 struct lock_file *lk = cb;
1194
4ff0f01c
MH
1195 return hold_lock_file_for_update_timeout(
1196 lk, path, LOCK_NO_DEREF,
1197 get_files_ref_lock_timeout_ms()) < 0 ? -1 : 0;
3b5d3c98
MH
1198}
1199
7bd9bcf3
MH
1200/*
1201 * Locks a ref returning the lock on success and NULL on failure.
7bd9bcf3 1202 */
4f01e508 1203static struct ref_lock *lock_ref_oid_basic(struct files_ref_store *refs,
52106430 1204 const char *refname,
4f01e508 1205 struct strbuf *err)
7bd9bcf3
MH
1206{
1207 struct strbuf ref_file = STRBUF_INIT;
7bd9bcf3 1208 struct ref_lock *lock;
7bd9bcf3 1209
4f01e508 1210 files_assert_main_repository(refs, "lock_ref_oid_basic");
7bd9bcf3
MH
1211 assert(err);
1212
ca56dadb 1213 CALLOC_ARRAY(lock, 1);
7bd9bcf3 1214
19e02f4f 1215 files_ref_path(refs, &ref_file, refname);
2859dcd4 1216
7bd9bcf3
MH
1217 /*
1218 * If the ref did not exist and we are creating it, make sure
1219 * there is no existing packed ref whose name begins with our
1220 * refname, nor a packed ref whose name is a proper prefix of
1221 * our refname.
1222 */
1223 if (is_null_oid(&lock->old_oid) &&
8ec617c8 1224 refs_verify_refname_available(refs->packed_ref_store, refname,
e4929cdf 1225 NULL, NULL, 0, err))
7bd9bcf3 1226 goto error_return;
7bd9bcf3 1227
7bd9bcf3 1228 lock->ref_name = xstrdup(refname);
611986f3 1229 lock->count = 1;
7bd9bcf3 1230
ee4d8e45 1231 if (raceproof_create_file(ref_file.buf, create_reflock, &lock->lk)) {
3b5d3c98 1232 unable_to_lock_message(ref_file.buf, errno, err);
7bd9bcf3
MH
1233 goto error_return;
1234 }
1235
f1da24ca 1236 if (!refs_resolve_ref_unsafe(&refs->base, lock->ref_name, 0,
ce14de03 1237 &lock->old_oid, NULL))
a6ebc2c6 1238 oidclr(&lock->old_oid, refs->base.repo->hash_algo);
7bd9bcf3
MH
1239 goto out;
1240
1241 error_return:
1242 unlock_ref(lock);
1243 lock = NULL;
1244
1245 out:
1246 strbuf_release(&ref_file);
7bd9bcf3
MH
1247 return lock;
1248}
1249
7bd9bcf3
MH
1250struct ref_to_prune {
1251 struct ref_to_prune *next;
49e99588 1252 struct object_id oid;
7bd9bcf3
MH
1253 char name[FLEX_ARRAY];
1254};
1255
a8f0db2d
MH
1256enum {
1257 REMOVE_EMPTY_PARENTS_REF = 0x01,
1258 REMOVE_EMPTY_PARENTS_REFLOG = 0x02
1259};
1260
7bd9bcf3 1261/*
a8f0db2d
MH
1262 * Remove empty parent directories associated with the specified
1263 * reference and/or its reflog, but spare [logs/]refs/ and immediate
1264 * subdirs. flags is a combination of REMOVE_EMPTY_PARENTS_REF and/or
1265 * REMOVE_EMPTY_PARENTS_REFLOG.
7bd9bcf3 1266 */
802de3da
NTND
1267static void try_remove_empty_parents(struct files_ref_store *refs,
1268 const char *refname,
1269 unsigned int flags)
7bd9bcf3 1270{
8bdaecb4 1271 struct strbuf buf = STRBUF_INIT;
e9dcc305 1272 struct strbuf sb = STRBUF_INIT;
7bd9bcf3
MH
1273 char *p, *q;
1274 int i;
8bdaecb4
MH
1275
1276 strbuf_addstr(&buf, refname);
1277 p = buf.buf;
7bd9bcf3
MH
1278 for (i = 0; i < 2; i++) { /* refs/{heads,tags,...}/ */
1279 while (*p && *p != '/')
1280 p++;
1281 /* tolerate duplicate slashes; see check_refname_format() */
1282 while (*p == '/')
1283 p++;
1284 }
8bdaecb4 1285 q = buf.buf + buf.len;
a8f0db2d 1286 while (flags & (REMOVE_EMPTY_PARENTS_REF | REMOVE_EMPTY_PARENTS_REFLOG)) {
7bd9bcf3
MH
1287 while (q > p && *q != '/')
1288 q--;
1289 while (q > p && *(q-1) == '/')
1290 q--;
1291 if (q == p)
1292 break;
8bdaecb4 1293 strbuf_setlen(&buf, q - buf.buf);
e9dcc305
NTND
1294
1295 strbuf_reset(&sb);
19e02f4f 1296 files_ref_path(refs, &sb, buf.buf);
e9dcc305 1297 if ((flags & REMOVE_EMPTY_PARENTS_REF) && rmdir(sb.buf))
a8f0db2d 1298 flags &= ~REMOVE_EMPTY_PARENTS_REF;
e9dcc305
NTND
1299
1300 strbuf_reset(&sb);
802de3da 1301 files_reflog_path(refs, &sb, buf.buf);
e9dcc305 1302 if ((flags & REMOVE_EMPTY_PARENTS_REFLOG) && rmdir(sb.buf))
a8f0db2d 1303 flags &= ~REMOVE_EMPTY_PARENTS_REFLOG;
7bd9bcf3 1304 }
8bdaecb4 1305 strbuf_release(&buf);
e9dcc305 1306 strbuf_release(&sb);
7bd9bcf3
MH
1307}
1308
1309/* make sure nobody touched the ref, and unlink */
2f40e954 1310static void prune_ref(struct files_ref_store *refs, struct ref_to_prune *r)
7bd9bcf3
MH
1311{
1312 struct ref_transaction *transaction;
1313 struct strbuf err = STRBUF_INIT;
b00f3cfa 1314 int ret = -1;
7bd9bcf3
MH
1315
1316 if (check_refname_format(r->name, 0))
1317 return;
1318
a0efef14 1319 transaction = ref_store_transaction_begin(&refs->base, 0, &err);
b00f3cfa
MH
1320 if (!transaction)
1321 goto cleanup;
1322 ref_transaction_add_update(
1323 transaction, r->name,
acedcde7 1324 REF_NO_DEREF | REF_HAVE_NEW | REF_HAVE_OLD | REF_IS_PRUNING,
7d70b29c 1325 null_oid(the_hash_algo), &r->oid, NULL, NULL, NULL, NULL);
b00f3cfa
MH
1326 if (ref_transaction_commit(transaction, &err))
1327 goto cleanup;
1328
1329 ret = 0;
1330
1331cleanup:
1332 if (ret)
7bd9bcf3 1333 error("%s", err.buf);
7bd9bcf3 1334 strbuf_release(&err);
b00f3cfa
MH
1335 ref_transaction_free(transaction);
1336 return;
7bd9bcf3
MH
1337}
1338
22b09cdf
MH
1339/*
1340 * Prune the loose versions of the references in the linked list
1341 * `*refs_to_prune`, freeing the entries in the list as we go.
1342 */
1343static void prune_refs(struct files_ref_store *refs, struct ref_to_prune **refs_to_prune)
7bd9bcf3 1344{
22b09cdf
MH
1345 while (*refs_to_prune) {
1346 struct ref_to_prune *r = *refs_to_prune;
1347 *refs_to_prune = r->next;
2f40e954 1348 prune_ref(refs, r);
22b09cdf 1349 free(r);
7bd9bcf3
MH
1350 }
1351}
1352
531cc4a5
MH
1353/*
1354 * Return true if the specified reference should be packed.
1355 */
c9e9723e 1356static int should_pack_ref(struct files_ref_store *refs,
89baa52d 1357 const struct reference *ref,
2cd99d98 1358 struct refs_optimize_opts *opts)
531cc4a5 1359{
4fe42f32
JC
1360 struct string_list_item *item;
1361
531cc4a5 1362 /* Do not pack per-worktree refs: */
89baa52d 1363 if (parse_worktree_ref(ref->name, NULL, NULL, NULL) !=
71e54734 1364 REF_WORKTREE_SHARED)
531cc4a5
MH
1365 return 0;
1366
531cc4a5 1367 /* Do not pack symbolic refs: */
89baa52d 1368 if (ref->flags & REF_ISSYMREF)
531cc4a5
MH
1369 return 0;
1370
1371 /* Do not pack broken refs: */
89baa52d 1372 if (!ref_resolves_to_object(ref->name, refs->base.repo, ref->oid, ref->flags))
531cc4a5
MH
1373 return 0;
1374
89baa52d 1375 if (ref_excluded(opts->exclusions, ref->name))
4fe42f32
JC
1376 return 0;
1377
1378 for_each_string_list_item(item, opts->includes)
89baa52d 1379 if (!wildmatch(item->string, ref->name, 0))
4fe42f32
JC
1380 return 1;
1381
1382 return 0;
531cc4a5
MH
1383}
1384
c3459ae9 1385static int should_pack_refs(struct files_ref_store *refs,
2cd99d98 1386 struct refs_optimize_opts *opts)
c3459ae9
PS
1387{
1388 struct ref_iterator *iter;
1389 size_t packed_size;
1390 size_t refcount = 0;
1391 size_t limit;
1392 int ret;
1393
2cd99d98 1394 if (!(opts->flags & REFS_OPTIMIZE_AUTO))
c3459ae9
PS
1395 return 1;
1396
1397 ret = packed_refs_size(refs->packed_ref_store, &packed_size);
1398 if (ret < 0)
1399 die("cannot determine packed-refs size");
1400
1401 /*
1402 * Packing loose references into the packed-refs file scales with the
1403 * number of references we're about to write. We thus decide whether we
1404 * repack refs by weighing the current size of the packed-refs file
1405 * against the number of loose references. This is done such that we do
1406 * not repack too often on repositories with a huge number of
1407 * references, where we can expect a lot of churn in the number of
1408 * references.
1409 *
1410 * As a heuristic, we repack if the number of loose references in the
1411 * repository exceeds `log2(nr_packed_refs) * 5`, where we estimate
1412 * `nr_packed_refs = packed_size / 100`, which scales as following:
1413 *
1414 * - 1kB ~ 10 packed refs: 16 refs
1415 * - 10kB ~ 100 packed refs: 33 refs
1416 * - 100kB ~ 1k packed refs: 49 refs
1417 * - 1MB ~ 10k packed refs: 66 refs
1418 * - 10MB ~ 100k packed refs: 82 refs
1419 * - 100MB ~ 1m packed refs: 99 refs
1420 *
1421 * We thus allow roughly 16 additional loose refs per factor of ten of
1422 * packed refs. This heuristic may be tweaked in the future, but should
1423 * serve as a sufficiently good first iteration.
1424 */
1425 limit = log2u(packed_size / 100) * 5;
1426 if (limit < 16)
1427 limit = 16;
1428
1429 iter = cache_ref_iterator_begin(get_loose_ref_cache(refs, 0), NULL,
1430 refs->base.repo, 0);
1431 while ((ret = ref_iterator_advance(iter)) == ITER_OK) {
89baa52d 1432 if (should_pack_ref(refs, &iter->ref, opts))
c3459ae9
PS
1433 refcount++;
1434 if (refcount >= limit) {
cec2b6f5 1435 ref_iterator_free(iter);
c3459ae9
PS
1436 return 1;
1437 }
1438 }
1439
1440 if (ret != ITER_DONE)
1441 die("error while iterating over references");
1442
cec2b6f5 1443 ref_iterator_free(iter);
c3459ae9
PS
1444 return 0;
1445}
1446
9b93ab8a 1447static int files_optimize(struct ref_store *ref_store,
2cd99d98 1448 struct refs_optimize_opts *opts)
7bd9bcf3 1449{
00eebe35 1450 struct files_ref_store *refs =
9e7ec634
NTND
1451 files_downcast(ref_store, REF_STORE_WRITE | REF_STORE_ODB,
1452 "pack_refs");
50c2d855 1453 struct ref_iterator *iter;
50c2d855
MH
1454 int ok;
1455 struct ref_to_prune *refs_to_prune = NULL;
3478983b 1456 struct strbuf err = STRBUF_INIT;
27d03d04
MH
1457 struct ref_transaction *transaction;
1458
c3459ae9
PS
1459 if (!should_pack_refs(refs, opts))
1460 return 0;
1461
a0efef14
PS
1462 transaction = ref_store_transaction_begin(refs->packed_ref_store,
1463 0, &err);
27d03d04
MH
1464 if (!transaction)
1465 return -1;
7bd9bcf3 1466
c8bed835 1467 packed_refs_lock(refs->packed_ref_store, LOCK_DIE_ON_ERROR, &err);
50c2d855 1468
d0f00c1a 1469 iter = cache_ref_iterator_begin(get_loose_ref_cache(refs, 0), NULL,
c9e9723e 1470 refs->base.repo, 0);
50c2d855
MH
1471 while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
1472 /*
1473 * If the loose reference can be packed, add an entry
1474 * in the packed ref cache. If the reference should be
1475 * pruned, also add it to refs_to_prune.
1476 */
89baa52d 1477 if (!should_pack_ref(refs, &iter->ref, opts))
50c2d855
MH
1478 continue;
1479
1480 /*
27d03d04
MH
1481 * Add a reference creation for this reference to the
1482 * packed-refs transaction:
50c2d855 1483 */
89baa52d
PS
1484 if (ref_transaction_update(transaction, iter->ref.name,
1485 iter->ref.oid, NULL, NULL, NULL,
91774afc 1486 REF_NO_DEREF, NULL, &err))
27d03d04 1487 die("failure preparing to create packed reference %s: %s",
89baa52d 1488 iter->ref.name, err.buf);
50c2d855
MH
1489
1490 /* Schedule the loose reference for pruning if requested. */
2cd99d98 1491 if ((opts->flags & REFS_OPTIMIZE_PRUNE)) {
50c2d855 1492 struct ref_to_prune *n;
89baa52d
PS
1493 FLEX_ALLOC_STR(n, name, iter->ref.name);
1494 oidcpy(&n->oid, iter->ref.oid);
50c2d855
MH
1495 n->next = refs_to_prune;
1496 refs_to_prune = n;
1497 }
1498 }
1499 if (ok != ITER_DONE)
1500 die("error while iterating over references");
7bd9bcf3 1501
27d03d04
MH
1502 if (ref_transaction_commit(transaction, &err))
1503 die("unable to write new packed-refs: %s", err.buf);
1504
1505 ref_transaction_free(transaction);
1506
42c7f7ff 1507 packed_refs_unlock(refs->packed_ref_store);
7bd9bcf3 1508
22b09cdf 1509 prune_refs(refs, &refs_to_prune);
cec2b6f5 1510 ref_iterator_free(iter);
3478983b 1511 strbuf_release(&err);
7bd9bcf3
MH
1512 return 0;
1513}
1514
f6c5ca38
KN
1515static int files_optimize_required(struct ref_store *ref_store,
1516 struct refs_optimize_opts *opts,
1517 bool *required)
1518{
1519 struct files_ref_store *refs = files_downcast(ref_store, REF_STORE_READ,
1520 "optimize_required");
1521 *required = should_pack_refs(refs, opts);
1522 return 0;
1523}
1524
7bd9bcf3
MH
1525/*
1526 * People using contrib's git-new-workdir have .git/logs/refs ->
1527 * /some/other/path/.git/logs/refs, and that may live on another device.
1528 *
1529 * IOW, to avoid cross device rename errors, the temporary renamed log must
1530 * live into logs/refs.
1531 */
a5c1efd6 1532#define TMP_RENAMED_LOG "refs/.tmp-renamed-log"
7bd9bcf3 1533
e9dcc305
NTND
1534struct rename_cb {
1535 const char *tmp_renamed_log;
1536 int true_errno;
1537};
1538
1539static int rename_tmp_log_callback(const char *path, void *cb_data)
7bd9bcf3 1540{
e9dcc305 1541 struct rename_cb *cb = cb_data;
7bd9bcf3 1542
e9dcc305 1543 if (rename(cb->tmp_renamed_log, path)) {
6a7f3631
MH
1544 /*
1545 * rename(a, b) when b is an existing directory ought
1546 * to result in ISDIR, but Solaris 5.8 gives ENOTDIR.
1547 * Sheesh. Record the true errno for error reporting,
1548 * but report EISDIR to raceproof_create_file() so
1549 * that it knows to retry.
1550 */
e9dcc305 1551 cb->true_errno = errno;
6a7f3631
MH
1552 if (errno == ENOTDIR)
1553 errno = EISDIR;
1554 return -1;
1555 } else {
1556 return 0;
7bd9bcf3 1557 }
6a7f3631 1558}
7bd9bcf3 1559
802de3da 1560static int rename_tmp_log(struct files_ref_store *refs, const char *newrefname)
6a7f3631 1561{
e9dcc305
NTND
1562 struct strbuf path = STRBUF_INIT;
1563 struct strbuf tmp = STRBUF_INIT;
1564 struct rename_cb cb;
1565 int ret;
6a7f3631 1566
802de3da
NTND
1567 files_reflog_path(refs, &path, newrefname);
1568 files_reflog_path(refs, &tmp, TMP_RENAMED_LOG);
e9dcc305
NTND
1569 cb.tmp_renamed_log = tmp.buf;
1570 ret = raceproof_create_file(path.buf, rename_tmp_log_callback, &cb);
6a7f3631
MH
1571 if (ret) {
1572 if (errno == EISDIR)
e9dcc305 1573 error("directory not empty: %s", path.buf);
6a7f3631 1574 else
990c98d2 1575 error("unable to move logfile %s to %s: %s",
e9dcc305
NTND
1576 tmp.buf, path.buf,
1577 strerror(cb.true_errno));
7bd9bcf3 1578 }
6a7f3631 1579
e9dcc305
NTND
1580 strbuf_release(&path);
1581 strbuf_release(&tmp);
7bd9bcf3
MH
1582 return ret;
1583}
1584
76e760b9
KN
1585static enum ref_transaction_error write_ref_to_lockfile(struct files_ref_store *refs,
1586 struct ref_lock *lock,
1587 const struct object_id *oid,
1588 int skip_oid_verification,
1589 struct strbuf *err);
f18a7892
MH
1590static int commit_ref_update(struct files_ref_store *refs,
1591 struct ref_lock *lock,
4417df8c 1592 const struct object_id *oid, const char *logmsg,
9a20b889 1593 int flags,
5d9b2de4 1594 struct strbuf *err);
7bd9bcf3 1595
c339ff69 1596/*
52106430
ÆAB
1597 * Emit a better error message than lockfile.c's
1598 * unable_to_lock_message() would in case there is a D/F conflict with
1599 * another existing reference. If there would be a conflict, emit an error
c339ff69
ÆAB
1600 * message and return false; otherwise, return true.
1601 *
1602 * Note that this function is not safe against all races with other
52106430
ÆAB
1603 * processes, and that's not its job. We'll emit a more verbose error on D/f
1604 * conflicts if we get past it into lock_ref_oid_basic().
c339ff69
ÆAB
1605 */
1606static int refs_rename_ref_available(struct ref_store *refs,
1607 const char *old_refname,
1608 const char *new_refname)
1609{
1610 struct string_list skip = STRING_LIST_INIT_NODUP;
1611 struct strbuf err = STRBUF_INIT;
1612 int ok;
1613
1614 string_list_insert(&skip, old_refname);
1615 ok = !refs_verify_refname_available(refs, new_refname,
e4929cdf 1616 NULL, &skip, 0, &err);
c339ff69
ÆAB
1617 if (!ok)
1618 error("%s", err.buf);
1619
1620 string_list_clear(&skip, 0);
1621 strbuf_release(&err);
1622 return ok;
1623}
1624
52d59cc6 1625static int files_copy_or_rename_ref(struct ref_store *ref_store,
9b6b40d9 1626 const char *oldrefname, const char *newrefname,
52d59cc6 1627 const char *logmsg, int copy)
7bd9bcf3 1628{
9b6b40d9 1629 struct files_ref_store *refs =
9e7ec634 1630 files_downcast(ref_store, REF_STORE_WRITE, "rename_ref");
e0ae2447 1631 struct object_id orig_oid;
7bd9bcf3
MH
1632 int flag = 0, logmoved = 0;
1633 struct ref_lock *lock;
1634 struct stat loginfo;
e9dcc305
NTND
1635 struct strbuf sb_oldref = STRBUF_INIT;
1636 struct strbuf sb_newref = STRBUF_INIT;
1637 struct strbuf tmp_renamed_log = STRBUF_INIT;
1638 int log, ret;
7bd9bcf3
MH
1639 struct strbuf err = STRBUF_INIT;
1640
802de3da
NTND
1641 files_reflog_path(refs, &sb_oldref, oldrefname);
1642 files_reflog_path(refs, &sb_newref, newrefname);
1643 files_reflog_path(refs, &tmp_renamed_log, TMP_RENAMED_LOG);
e9dcc305
NTND
1644
1645 log = !lstat(sb_oldref.buf, &loginfo);
0a3f07d6
NTND
1646 if (log && S_ISLNK(loginfo.st_mode)) {
1647 ret = error("reflog for %s is a symlink", oldrefname);
1648 goto out;
1649 }
7bd9bcf3 1650
2f40e954
NTND
1651 if (!refs_resolve_ref_unsafe(&refs->base, oldrefname,
1652 RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
ce14de03 1653 &orig_oid, &flag)) {
0a3f07d6
NTND
1654 ret = error("refname %s not found", oldrefname);
1655 goto out;
1656 }
e711b1af 1657
0a3f07d6 1658 if (flag & REF_ISSYMREF) {
52d59cc6
SD
1659 if (copy)
1660 ret = error("refname %s is a symbolic ref, copying it is not supported",
1661 oldrefname);
1662 else
1663 ret = error("refname %s is a symbolic ref, renaming it is not supported",
1664 oldrefname);
0a3f07d6
NTND
1665 goto out;
1666 }
7d2df051 1667 if (!refs_rename_ref_available(&refs->base, oldrefname, newrefname)) {
0a3f07d6
NTND
1668 ret = 1;
1669 goto out;
1670 }
7bd9bcf3 1671
52d59cc6 1672 if (!copy && log && rename(sb_oldref.buf, tmp_renamed_log.buf)) {
a5c1efd6 1673 ret = error("unable to move logfile logs/%s to logs/"TMP_RENAMED_LOG": %s",
0a3f07d6
NTND
1674 oldrefname, strerror(errno));
1675 goto out;
1676 }
7bd9bcf3 1677
52d59cc6
SD
1678 if (copy && log && copy_file(tmp_renamed_log.buf, sb_oldref.buf, 0644)) {
1679 ret = error("unable to copy logfile logs/%s to logs/"TMP_RENAMED_LOG": %s",
1680 oldrefname, strerror(errno));
1681 goto out;
1682 }
1683
1684 if (!copy && refs_delete_ref(&refs->base, logmsg, oldrefname,
91774afc 1685 &orig_oid, REF_NO_DEREF)) {
7bd9bcf3
MH
1686 error("unable to delete old %s", oldrefname);
1687 goto rollback;
1688 }
1689
12fd3496 1690 /*
4417df8c 1691 * Since we are doing a shallow lookup, oid is not the
1692 * correct value to pass to delete_ref as old_oid. But that
1693 * doesn't matter, because an old_oid check wouldn't add to
12fd3496
DT
1694 * the safety anyway; we want to delete the reference whatever
1695 * its current value.
1696 */
f1da24ca 1697 if (!copy && refs_resolve_ref_unsafe(&refs->base, newrefname,
76887df0 1698 RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
ce14de03 1699 NULL, NULL) &&
2f40e954 1700 refs_delete_ref(&refs->base, NULL, newrefname,
91774afc 1701 NULL, REF_NO_DEREF)) {
58364324 1702 if (errno == EISDIR) {
7bd9bcf3
MH
1703 struct strbuf path = STRBUF_INIT;
1704 int result;
1705
19e02f4f 1706 files_ref_path(refs, &path, newrefname);
7bd9bcf3
MH
1707 result = remove_empty_directories(&path);
1708 strbuf_release(&path);
1709
1710 if (result) {
1711 error("Directory not empty: %s", newrefname);
1712 goto rollback;
1713 }
1714 } else {
1715 error("unable to delete existing %s", newrefname);
1716 goto rollback;
1717 }
1718 }
1719
802de3da 1720 if (log && rename_tmp_log(refs, newrefname))
7bd9bcf3
MH
1721 goto rollback;
1722
1723 logmoved = log;
1724
52106430 1725 lock = lock_ref_oid_basic(refs, newrefname, &err);
7bd9bcf3 1726 if (!lock) {
52d59cc6
SD
1727 if (copy)
1728 error("unable to copy '%s' to '%s': %s", oldrefname, newrefname, err.buf);
1729 else
1730 error("unable to rename '%s' to '%s': %s", oldrefname, newrefname, err.buf);
7bd9bcf3
MH
1731 strbuf_release(&err);
1732 goto rollback;
1733 }
4417df8c 1734 oidcpy(&lock->old_oid, &orig_oid);
7bd9bcf3 1735
c9e9723e 1736 if (write_ref_to_lockfile(refs, lock, &orig_oid, 0, &err) ||
9a20b889 1737 commit_ref_update(refs, lock, &orig_oid, logmsg, 0, &err)) {
7bd9bcf3
MH
1738 error("unable to write current sha1 into %s: %s", newrefname, err.buf);
1739 strbuf_release(&err);
1740 goto rollback;
1741 }
1742
0a3f07d6
NTND
1743 ret = 0;
1744 goto out;
7bd9bcf3
MH
1745
1746 rollback:
52106430 1747 lock = lock_ref_oid_basic(refs, oldrefname, &err);
7bd9bcf3
MH
1748 if (!lock) {
1749 error("unable to lock %s for rollback: %s", oldrefname, err.buf);
1750 strbuf_release(&err);
1751 goto rollbacklog;
1752 }
1753
c9e9723e 1754 if (write_ref_to_lockfile(refs, lock, &orig_oid, 0, &err) ||
9a20b889 1755 commit_ref_update(refs, lock, &orig_oid, NULL, REF_SKIP_CREATE_REFLOG, &err)) {
7bd9bcf3
MH
1756 error("unable to write current sha1 into %s: %s", oldrefname, err.buf);
1757 strbuf_release(&err);
1758 }
7bd9bcf3
MH
1759
1760 rollbacklog:
e9dcc305 1761 if (logmoved && rename(sb_newref.buf, sb_oldref.buf))
7bd9bcf3
MH
1762 error("unable to restore logfile %s from %s: %s",
1763 oldrefname, newrefname, strerror(errno));
1764 if (!logmoved && log &&
e9dcc305 1765 rename(tmp_renamed_log.buf, sb_oldref.buf))
a5c1efd6 1766 error("unable to restore logfile %s from logs/"TMP_RENAMED_LOG": %s",
7bd9bcf3 1767 oldrefname, strerror(errno));
0a3f07d6
NTND
1768 ret = 1;
1769 out:
e9dcc305
NTND
1770 strbuf_release(&sb_newref);
1771 strbuf_release(&sb_oldref);
1772 strbuf_release(&tmp_renamed_log);
1773
0a3f07d6 1774 return ret;
7bd9bcf3
MH
1775}
1776
52d59cc6
SD
1777static int files_rename_ref(struct ref_store *ref_store,
1778 const char *oldrefname, const char *newrefname,
1779 const char *logmsg)
1780{
1781 return files_copy_or_rename_ref(ref_store, oldrefname,
1782 newrefname, logmsg, 0);
1783}
1784
1785static int files_copy_ref(struct ref_store *ref_store,
1786 const char *oldrefname, const char *newrefname,
1787 const char *logmsg)
1788{
1789 return files_copy_or_rename_ref(ref_store, oldrefname,
1790 newrefname, logmsg, 1);
1791}
1792
83a3069a 1793static int close_ref_gently(struct ref_lock *lock)
7bd9bcf3 1794{
ee4d8e45 1795 if (close_lock_file_gently(&lock->lk))
7bd9bcf3
MH
1796 return -1;
1797 return 0;
1798}
1799
1800static int commit_ref(struct ref_lock *lock)
1801{
ee4d8e45 1802 char *path = get_locked_file_path(&lock->lk);
5387c0d8
MH
1803 struct stat st;
1804
1805 if (!lstat(path, &st) && S_ISDIR(st.st_mode)) {
1806 /*
1807 * There is a directory at the path we want to rename
1808 * the lockfile to. Hopefully it is empty; try to
1809 * delete it.
1810 */
1811 size_t len = strlen(path);
1812 struct strbuf sb_path = STRBUF_INIT;
1813
1814 strbuf_attach(&sb_path, path, len, len);
1815
1816 /*
1817 * If this fails, commit_lock_file() will also fail
1818 * and will report the problem.
1819 */
1820 remove_empty_directories(&sb_path);
1821 strbuf_release(&sb_path);
1822 } else {
1823 free(path);
1824 }
1825
ee4d8e45 1826 if (commit_lock_file(&lock->lk))
7bd9bcf3
MH
1827 return -1;
1828 return 0;
1829}
1830
1fb0c809
MH
1831static int open_or_create_logfile(const char *path, void *cb)
1832{
1833 int *fd = cb;
1834
1835 *fd = open(path, O_APPEND | O_WRONLY | O_CREAT, 0666);
1836 return (*fd < 0) ? -1 : 0;
1837}
1838
7bd9bcf3 1839/*
4533e534
MH
1840 * Create a reflog for a ref. If force_create = 0, only create the
1841 * reflog for certain refs (those for which should_autocreate_reflog
1842 * returns non-zero). Otherwise, create it regardless of the reference
1843 * name. If the logfile already existed or was created, return 0 and
1844 * set *logfd to the file descriptor opened for appending to the file.
1845 * If no logfile exists and we decided not to create one, return 0 and
1846 * set *logfd to -1. On failure, fill in *err, set *logfd to -1, and
1847 * return -1.
7bd9bcf3 1848 */
802de3da
NTND
1849static int log_ref_setup(struct files_ref_store *refs,
1850 const char *refname, int force_create,
4533e534 1851 int *logfd, struct strbuf *err)
7bd9bcf3 1852{
eafb1264 1853 enum log_refs_config log_refs_cfg = refs->log_all_ref_updates;
802de3da
NTND
1854 struct strbuf logfile_sb = STRBUF_INIT;
1855 char *logfile;
1856
9a20b889
PS
1857 if (log_refs_cfg == LOG_REFS_UNSET)
1858 log_refs_cfg = is_bare_repository() ? LOG_REFS_NONE : LOG_REFS_NORMAL;
1859
802de3da
NTND
1860 files_reflog_path(refs, &logfile_sb, refname);
1861 logfile = strbuf_detach(&logfile_sb, NULL);
7bd9bcf3 1862
9a20b889 1863 if (force_create || should_autocreate_reflog(log_refs_cfg, refname)) {
4533e534 1864 if (raceproof_create_file(logfile, open_or_create_logfile, logfd)) {
1fb0c809
MH
1865 if (errno == ENOENT)
1866 strbuf_addf(err, "unable to create directory for '%s': "
4533e534 1867 "%s", logfile, strerror(errno));
1fb0c809
MH
1868 else if (errno == EISDIR)
1869 strbuf_addf(err, "there are still logs under '%s'",
4533e534 1870 logfile);
1fb0c809 1871 else
854bda6b 1872 strbuf_addf(err, "unable to append to '%s': %s",
4533e534 1873 logfile, strerror(errno));
7bd9bcf3 1874
4533e534 1875 goto error;
7bd9bcf3 1876 }
854bda6b 1877 } else {
35cf94ea 1878 *logfd = open(logfile, O_APPEND | O_WRONLY);
e404f459 1879 if (*logfd < 0) {
854bda6b
MH
1880 if (errno == ENOENT || errno == EISDIR) {
1881 /*
1882 * The logfile doesn't already exist,
1883 * but that is not an error; it only
1884 * means that we won't write log
1885 * entries to it.
1886 */
1887 ;
1888 } else {
1889 strbuf_addf(err, "unable to append to '%s': %s",
4533e534
MH
1890 logfile, strerror(errno));
1891 goto error;
854bda6b 1892 }
7bd9bcf3
MH
1893 }
1894 }
1895
e404f459 1896 if (*logfd >= 0)
028f6186 1897 adjust_shared_perm(the_repository, logfile);
854bda6b 1898
4533e534 1899 free(logfile);
7bd9bcf3 1900 return 0;
7bd9bcf3 1901
4533e534
MH
1902error:
1903 free(logfile);
1904 return -1;
7bd9bcf3 1905}
7bd9bcf3 1906
7b089120 1907static int files_create_reflog(struct ref_store *ref_store, const char *refname,
e3688bd6 1908 struct strbuf *err)
7bd9bcf3 1909{
802de3da 1910 struct files_ref_store *refs =
9e7ec634 1911 files_downcast(ref_store, REF_STORE_WRITE, "create_reflog");
e404f459 1912 int fd;
7bd9bcf3 1913
7b089120 1914 if (log_ref_setup(refs, refname, 1, &fd, err))
4533e534
MH
1915 return -1;
1916
e404f459
MH
1917 if (fd >= 0)
1918 close(fd);
4533e534
MH
1919
1920 return 0;
7bd9bcf3
MH
1921}
1922
4417df8c 1923static int log_ref_write_fd(int fd, const struct object_id *old_oid,
1924 const struct object_id *new_oid,
7bd9bcf3
MH
1925 const char *committer, const char *msg)
1926{
80a6c207
BP
1927 struct strbuf sb = STRBUF_INIT;
1928 int ret = 0;
1929
1a83e26d
KN
1930 if (!committer)
1931 committer = git_committer_info(0);
1932
80a6c207 1933 strbuf_addf(&sb, "%s %s %s", oid_to_hex(old_oid), oid_to_hex(new_oid), committer);
25429fed
HWN
1934 if (msg && *msg) {
1935 strbuf_addch(&sb, '\t');
523fa69c 1936 strbuf_addstr(&sb, msg);
25429fed 1937 }
80a6c207
BP
1938 strbuf_addch(&sb, '\n');
1939 if (write_in_full(fd, sb.buf, sb.len) < 0)
1940 ret = -1;
1941 strbuf_release(&sb);
1942 return ret;
7bd9bcf3
MH
1943}
1944
802de3da 1945static int files_log_ref_write(struct files_ref_store *refs,
1a83e26d
KN
1946 const char *refname,
1947 const struct object_id *old_oid,
1948 const struct object_id *new_oid,
1949 const char *committer_info, const char *msg,
11f8457f 1950 int flags, struct strbuf *err)
7bd9bcf3 1951{
e404f459 1952 int logfd, result;
7bd9bcf3 1953
fbd1a693
PS
1954 if (flags & REF_SKIP_CREATE_REFLOG)
1955 return 0;
1956
802de3da
NTND
1957 result = log_ref_setup(refs, refname,
1958 flags & REF_FORCE_CREATE_REFLOG,
4533e534 1959 &logfd, err);
7bd9bcf3
MH
1960
1961 if (result)
1962 return result;
1963
7bd9bcf3
MH
1964 if (logfd < 0)
1965 return 0;
1a83e26d 1966 result = log_ref_write_fd(logfd, old_oid, new_oid, committer_info, msg);
7bd9bcf3 1967 if (result) {
e9dcc305 1968 struct strbuf sb = STRBUF_INIT;
87b21e05
MH
1969 int save_errno = errno;
1970
802de3da 1971 files_reflog_path(refs, &sb, refname);
87b21e05 1972 strbuf_addf(err, "unable to append to '%s': %s",
e9dcc305
NTND
1973 sb.buf, strerror(save_errno));
1974 strbuf_release(&sb);
7bd9bcf3
MH
1975 close(logfd);
1976 return -1;
1977 }
1978 if (close(logfd)) {
e9dcc305 1979 struct strbuf sb = STRBUF_INIT;
87b21e05
MH
1980 int save_errno = errno;
1981
802de3da 1982 files_reflog_path(refs, &sb, refname);
87b21e05 1983 strbuf_addf(err, "unable to append to '%s': %s",
e9dcc305
NTND
1984 sb.buf, strerror(save_errno));
1985 strbuf_release(&sb);
7bd9bcf3
MH
1986 return -1;
1987 }
1988 return 0;
1989}
1990
7bd9bcf3 1991/*
78fb4579
MH
1992 * Write oid into the open lockfile, then close the lockfile. On
1993 * errors, rollback the lockfile, fill in *err and return -1.
7bd9bcf3 1994 */
76e760b9
KN
1995static enum ref_transaction_error write_ref_to_lockfile(struct files_ref_store *refs,
1996 struct ref_lock *lock,
1997 const struct object_id *oid,
1998 int skip_oid_verification,
1999 struct strbuf *err)
7bd9bcf3
MH
2000{
2001 static char term = '\n';
2002 struct object *o;
2003 int fd;
2004
e9706a18 2005 if (!skip_oid_verification) {
c9e9723e 2006 o = parse_object(refs->base.repo, oid);
e9706a18
HWN
2007 if (!o) {
2008 strbuf_addf(
2009 err,
2010 "trying to write ref '%s' with nonexistent object %s",
2011 lock->ref_name, oid_to_hex(oid));
2012 unlock_ref(lock);
76e760b9 2013 return REF_TRANSACTION_ERROR_INVALID_NEW_VALUE;
e9706a18
HWN
2014 }
2015 if (o->type != OBJ_COMMIT && is_branch(lock->ref_name)) {
2016 strbuf_addf(
2017 err,
2018 "trying to write non-commit object %s to branch '%s'",
2019 oid_to_hex(oid), lock->ref_name);
2020 unlock_ref(lock);
76e760b9 2021 return REF_TRANSACTION_ERROR_INVALID_NEW_VALUE;
e9706a18 2022 }
7bd9bcf3 2023 }
ee4d8e45 2024 fd = get_lock_file_fd(&lock->lk);
c1026b9d 2025 if (write_in_full(fd, oid_to_hex(oid), refs->base.repo->hash_algo->hexsz) < 0 ||
06f46f23 2026 write_in_full(fd, &term, 1) < 0 ||
bc22d845 2027 fsync_component(FSYNC_COMPONENT_REFERENCE, get_lock_file_fd(&lock->lk)) < 0 ||
83a3069a 2028 close_ref_gently(lock) < 0) {
7bd9bcf3 2029 strbuf_addf(err,
ee4d8e45 2030 "couldn't write '%s'", get_lock_file_path(&lock->lk));
7bd9bcf3 2031 unlock_ref(lock);
76e760b9 2032 return REF_TRANSACTION_ERROR_GENERIC;
7bd9bcf3
MH
2033 }
2034 return 0;
2035}
2036
2037/*
2038 * Commit a change to a loose reference that has already been written
2039 * to the loose reference lockfile. Also update the reflogs if
2040 * necessary, using the specified lockmsg (which can be NULL).
2041 */
f18a7892
MH
2042static int commit_ref_update(struct files_ref_store *refs,
2043 struct ref_lock *lock,
4417df8c 2044 const struct object_id *oid, const char *logmsg,
9a20b889 2045 int flags,
5d9b2de4 2046 struct strbuf *err)
7bd9bcf3 2047{
32c597e7 2048 files_assert_main_repository(refs, "commit_ref_update");
00eebe35
MH
2049
2050 clear_loose_ref_cache(refs);
1a83e26d 2051 if (files_log_ref_write(refs, lock->ref_name, &lock->old_oid, oid, NULL,
9a20b889 2052 logmsg, flags, err)) {
7bd9bcf3 2053 char *old_msg = strbuf_detach(err, NULL);
0568c8e9 2054 strbuf_addf(err, "cannot update the ref '%s': %s",
7bd9bcf3
MH
2055 lock->ref_name, old_msg);
2056 free(old_msg);
2057 unlock_ref(lock);
2058 return -1;
2059 }
7a418f3a
MH
2060
2061 if (strcmp(lock->ref_name, "HEAD") != 0) {
7bd9bcf3
MH
2062 /*
2063 * Special hack: If a branch is updated directly and HEAD
2064 * points to it (may happen on the remote side of a push
2065 * for example) then logically the HEAD reflog should be
2066 * updated too.
2067 * A generic solution implies reverse symref information,
2068 * but finding all symrefs pointing to the given branch
2069 * would be rather costly for this rare event (the direct
2070 * update of a branch) to be worth it. So let's cheat and
2071 * check with HEAD only which should cover 99% of all usage
2072 * scenarios (even 100% of the default ones).
2073 */
7bd9bcf3
MH
2074 int head_flag;
2075 const char *head_ref;
7a418f3a 2076
2f40e954
NTND
2077 head_ref = refs_resolve_ref_unsafe(&refs->base, "HEAD",
2078 RESOLVE_REF_READING,
ce14de03 2079 NULL, &head_flag);
7bd9bcf3
MH
2080 if (head_ref && (head_flag & REF_ISSYMREF) &&
2081 !strcmp(head_ref, lock->ref_name)) {
2082 struct strbuf log_err = STRBUF_INIT;
1a83e26d
KN
2083 if (files_log_ref_write(refs, "HEAD", &lock->old_oid,
2084 oid, NULL, logmsg, flags,
2085 &log_err)) {
7bd9bcf3
MH
2086 error("%s", log_err.buf);
2087 strbuf_release(&log_err);
2088 }
2089 }
2090 }
7a418f3a 2091
7bd9bcf3 2092 if (commit_ref(lock)) {
0568c8e9 2093 strbuf_addf(err, "couldn't set '%s'", lock->ref_name);
7bd9bcf3
MH
2094 unlock_ref(lock);
2095 return -1;
2096 }
2097
2098 unlock_ref(lock);
2099 return 0;
2100}
2101
f570bd91 2102#if defined(NO_SYMLINK_HEAD) || defined(WITH_BREAKING_CHANGES)
ab8bcd2d
JH
2103#define create_ref_symlink(a, b) (-1)
2104#else
370e5ad6 2105static int create_ref_symlink(struct ref_lock *lock, const char *target)
7bd9bcf3 2106{
f570bd91
PS
2107 static int warn_once = 1;
2108 char *ref_path;
370e5ad6 2109 int ret = -1;
ab8bcd2d 2110
f570bd91 2111 ref_path = get_locked_file_path(&lock->lk);
370e5ad6
JK
2112 unlink(ref_path);
2113 ret = symlink(target, ref_path);
2114 free(ref_path);
2115
2116 if (ret)
7bd9bcf3 2117 fprintf(stderr, "no symlink - falling back to symbolic ref\n");
f570bd91
PS
2118
2119 if (warn_once)
2120 warning(_("'core.preferSymlinkRefs=true' is nominated for removal.\n"
2121 "hint: The use of symbolic links for symbolic refs is deprecated\n"
2122 "hint: and will be removed in Git 3.0. The configuration that\n"
2123 "hint: tells Git to use them is thus going away. You can unset\n"
2124 "hint: it with:\n"
2125 "hint:\n"
2126 "hint:\tgit config unset core.preferSymlinkRefs\n"
2127 "hint:\n"
2128 "hint: Git will then use the textual symref format instead."));
2129 warn_once = 0;
2130
370e5ad6
JK
2131 return ret;
2132}
ab8bcd2d 2133#endif
7bd9bcf3 2134
65e7a447
JK
2135static int create_symref_lock(struct ref_lock *lock, const char *target,
2136 struct strbuf *err)
370e5ad6 2137{
57d0b1e2
KN
2138 if (!fdopen_lock_file(&lock->lk, "w")) {
2139 strbuf_addf(err, "unable to fdopen %s: %s",
2140 get_lock_file_path(&lock->lk), strerror(errno));
2141 return -1;
2142 }
2143
2144 if (fprintf(get_lock_file_fp(&lock->lk), "ref: %s\n", target) < 0) {
2145 strbuf_addf(err, "unable to write to %s: %s",
2146 get_lock_file_path(&lock->lk), strerror(errno));
2147 return -1;
2148 }
2149
2150 return 0;
2151}
2152
e3688bd6
DT
2153static int files_reflog_exists(struct ref_store *ref_store,
2154 const char *refname)
7bd9bcf3 2155{
802de3da 2156 struct files_ref_store *refs =
9e7ec634 2157 files_downcast(ref_store, REF_STORE_READ, "reflog_exists");
e9dcc305 2158 struct strbuf sb = STRBUF_INIT;
7bd9bcf3 2159 struct stat st;
e9dcc305 2160 int ret;
7bd9bcf3 2161
802de3da 2162 files_reflog_path(refs, &sb, refname);
e9dcc305
NTND
2163 ret = !lstat(sb.buf, &st) && S_ISREG(st.st_mode);
2164 strbuf_release(&sb);
2165 return ret;
7bd9bcf3
MH
2166}
2167
e3688bd6
DT
2168static int files_delete_reflog(struct ref_store *ref_store,
2169 const char *refname)
7bd9bcf3 2170{
802de3da 2171 struct files_ref_store *refs =
9e7ec634 2172 files_downcast(ref_store, REF_STORE_WRITE, "delete_reflog");
e9dcc305
NTND
2173 struct strbuf sb = STRBUF_INIT;
2174 int ret;
2175
802de3da 2176 files_reflog_path(refs, &sb, refname);
e9dcc305
NTND
2177 ret = remove_path(sb.buf);
2178 strbuf_release(&sb);
2179 return ret;
7bd9bcf3
MH
2180}
2181
b9fd73a2
PS
2182static int show_one_reflog_ent(struct files_ref_store *refs,
2183 const char *refname,
2184 struct strbuf *sb,
080b068f 2185 each_reflog_ent_fn fn, void *cb_data)
7bd9bcf3 2186{
9461d272 2187 struct object_id ooid, noid;
7bd9bcf3 2188 char *email_end, *message;
dddbad72 2189 timestamp_t timestamp;
7bd9bcf3 2190 int tz;
43bc3b6c 2191 const char *p = sb->buf;
7bd9bcf3
MH
2192
2193 /* old SP new SP name <email> SP time TAB msg LF */
43bc3b6c 2194 if (!sb->len || sb->buf[sb->len - 1] != '\n' ||
080b068f
PS
2195 parse_oid_hex_algop(p, &ooid, &p, refs->base.repo->hash_algo) || *p++ != ' ' ||
2196 parse_oid_hex_algop(p, &noid, &p, refs->base.repo->hash_algo) || *p++ != ' ' ||
43bc3b6c 2197 !(email_end = strchr(p, '>')) ||
7bd9bcf3 2198 email_end[1] != ' ' ||
1aeb7e75 2199 !(timestamp = parse_timestamp(email_end + 2, &message, 10)) ||
7bd9bcf3
MH
2200 !message || message[0] != ' ' ||
2201 (message[1] != '+' && message[1] != '-') ||
2202 !isdigit(message[2]) || !isdigit(message[3]) ||
2203 !isdigit(message[4]) || !isdigit(message[5]))
2204 return 0; /* corrupt? */
2205 email_end[1] = '\0';
2206 tz = strtol(message + 1, NULL, 10);
2207 if (message[6] != '\t')
2208 message += 6;
2209 else
2210 message += 7;
b9fd73a2 2211 return fn(refname, &ooid, &noid, p, timestamp, tz, message, cb_data);
7bd9bcf3
MH
2212}
2213
2214static char *find_beginning_of_line(char *bob, char *scan)
2215{
2216 while (bob < scan && *(--scan) != '\n')
2217 ; /* keep scanning backwards */
2218 /*
2219 * Return either beginning of the buffer, or LF at the end of
2220 * the previous line.
2221 */
2222 return scan;
2223}
2224
e3688bd6
DT
2225static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store,
2226 const char *refname,
2227 each_reflog_ent_fn fn,
2228 void *cb_data)
7bd9bcf3 2229{
802de3da 2230 struct files_ref_store *refs =
9e7ec634
NTND
2231 files_downcast(ref_store, REF_STORE_READ,
2232 "for_each_reflog_ent_reverse");
7bd9bcf3
MH
2233 struct strbuf sb = STRBUF_INIT;
2234 FILE *logfp;
2235 long pos;
2236 int ret = 0, at_tail = 1;
2237
802de3da 2238 files_reflog_path(refs, &sb, refname);
e9dcc305
NTND
2239 logfp = fopen(sb.buf, "r");
2240 strbuf_release(&sb);
7bd9bcf3
MH
2241 if (!logfp)
2242 return -1;
2243
2244 /* Jump to the end */
2245 if (fseek(logfp, 0, SEEK_END) < 0)
be686f03
RS
2246 ret = error("cannot seek back reflog for %s: %s",
2247 refname, strerror(errno));
7bd9bcf3
MH
2248 pos = ftell(logfp);
2249 while (!ret && 0 < pos) {
2250 int cnt;
2251 size_t nread;
2252 char buf[BUFSIZ];
2253 char *endp, *scanp;
2254
2255 /* Fill next block from the end */
2256 cnt = (sizeof(buf) < pos) ? sizeof(buf) : pos;
be686f03
RS
2257 if (fseek(logfp, pos - cnt, SEEK_SET)) {
2258 ret = error("cannot seek back reflog for %s: %s",
2259 refname, strerror(errno));
2260 break;
2261 }
7bd9bcf3 2262 nread = fread(buf, cnt, 1, logfp);
be686f03
RS
2263 if (nread != 1) {
2264 ret = error("cannot read %d bytes from reflog for %s: %s",
2265 cnt, refname, strerror(errno));
2266 break;
2267 }
7bd9bcf3
MH
2268 pos -= cnt;
2269
2270 scanp = endp = buf + cnt;
2271 if (at_tail && scanp[-1] == '\n')
2272 /* Looking at the final LF at the end of the file */
2273 scanp--;
2274 at_tail = 0;
2275
2276 while (buf < scanp) {
2277 /*
2278 * terminating LF of the previous line, or the beginning
2279 * of the buffer.
2280 */
2281 char *bp;
2282
2283 bp = find_beginning_of_line(buf, scanp);
2284
2285 if (*bp == '\n') {
2286 /*
2287 * The newline is the end of the previous line,
2288 * so we know we have complete line starting
2289 * at (bp + 1). Prefix it onto any prior data
2290 * we collected for the line and process it.
2291 */
2292 strbuf_splice(&sb, 0, 0, bp + 1, endp - (bp + 1));
2293 scanp = bp;
2294 endp = bp + 1;
b9fd73a2 2295 ret = show_one_reflog_ent(refs, refname, &sb, fn, cb_data);
7bd9bcf3
MH
2296 strbuf_reset(&sb);
2297 if (ret)
2298 break;
2299 } else if (!pos) {
2300 /*
2301 * We are at the start of the buffer, and the
2302 * start of the file; there is no previous
2303 * line, and we have everything for this one.
2304 * Process it, and we can end the loop.
2305 */
2306 strbuf_splice(&sb, 0, 0, buf, endp - buf);
b9fd73a2 2307 ret = show_one_reflog_ent(refs, refname, &sb, fn, cb_data);
7bd9bcf3
MH
2308 strbuf_reset(&sb);
2309 break;
2310 }
2311
2312 if (bp == buf) {
2313 /*
2314 * We are at the start of the buffer, and there
2315 * is more file to read backwards. Which means
2316 * we are in the middle of a line. Note that we
2317 * may get here even if *bp was a newline; that
2318 * just means we are at the exact end of the
2319 * previous line, rather than some spot in the
2320 * middle.
2321 *
2322 * Save away what we have to be combined with
2323 * the data from the next read.
2324 */
2325 strbuf_splice(&sb, 0, 0, buf, endp - buf);
2326 break;
2327 }
2328 }
2329
2330 }
2331 if (!ret && sb.len)
033abf97 2332 BUG("reverse reflog parser had leftover data");
7bd9bcf3
MH
2333
2334 fclose(logfp);
2335 strbuf_release(&sb);
2336 return ret;
2337}
2338
e3688bd6
DT
2339static int files_for_each_reflog_ent(struct ref_store *ref_store,
2340 const char *refname,
2341 each_reflog_ent_fn fn, void *cb_data)
7bd9bcf3 2342{
802de3da 2343 struct files_ref_store *refs =
9e7ec634
NTND
2344 files_downcast(ref_store, REF_STORE_READ,
2345 "for_each_reflog_ent");
7bd9bcf3
MH
2346 FILE *logfp;
2347 struct strbuf sb = STRBUF_INIT;
2348 int ret = 0;
2349
802de3da 2350 files_reflog_path(refs, &sb, refname);
e9dcc305
NTND
2351 logfp = fopen(sb.buf, "r");
2352 strbuf_release(&sb);
7bd9bcf3
MH
2353 if (!logfp)
2354 return -1;
2355
2356 while (!ret && !strbuf_getwholeline(&sb, logfp, '\n'))
b9fd73a2 2357 ret = show_one_reflog_ent(refs, refname, &sb, fn, cb_data);
7bd9bcf3
MH
2358 fclose(logfp);
2359 strbuf_release(&sb);
2360 return ret;
2361}
7bd9bcf3 2362
2880d16f
MH
2363struct files_reflog_iterator {
2364 struct ref_iterator base;
2f40e954 2365 struct ref_store *ref_store;
2880d16f 2366 struct dir_iterator *dir_iterator;
2880d16f 2367};
7bd9bcf3 2368
2880d16f
MH
2369static int files_reflog_iterator_advance(struct ref_iterator *ref_iterator)
2370{
2371 struct files_reflog_iterator *iter =
2372 (struct files_reflog_iterator *)ref_iterator;
2373 struct dir_iterator *diter = iter->dir_iterator;
2374 int ok;
2375
2376 while ((ok = dir_iterator_advance(diter)) == ITER_OK) {
2880d16f 2377 if (!S_ISREG(diter->st.st_mode))
7bd9bcf3 2378 continue;
59c50a96
PS
2379 if (check_refname_format(diter->basename,
2380 REFNAME_ALLOW_ONELEVEL))
2880d16f 2381 continue;
2880d16f 2382
89baa52d 2383 iter->base.ref.name = diter->relative_path;
2880d16f 2384 return ITER_OK;
7bd9bcf3 2385 }
2880d16f 2386
2880d16f
MH
2387 return ok;
2388}
2389
a95da5c8 2390static int files_reflog_iterator_seek(struct ref_iterator *ref_iterator UNUSED,
2b4648b9
KN
2391 const char *refname UNUSED,
2392 unsigned int flags UNUSED)
a95da5c8
PS
2393{
2394 BUG("ref_iterator_seek() called for reflog_iterator");
2395}
2396
cec2b6f5 2397static void files_reflog_iterator_release(struct ref_iterator *ref_iterator)
2880d16f
MH
2398{
2399 struct files_reflog_iterator *iter =
2400 (struct files_reflog_iterator *)ref_iterator;
cec2b6f5 2401 dir_iterator_free(iter->dir_iterator);
2880d16f
MH
2402}
2403
2404static struct ref_iterator_vtable files_reflog_iterator_vtable = {
e2f8acb6 2405 .advance = files_reflog_iterator_advance,
a95da5c8 2406 .seek = files_reflog_iterator_seek,
cec2b6f5 2407 .release = files_reflog_iterator_release,
2880d16f
MH
2408};
2409
944b4e30
NTND
2410static struct ref_iterator *reflog_iterator_begin(struct ref_store *ref_store,
2411 const char *gitdir)
2880d16f 2412{
3012397e
MT
2413 struct dir_iterator *diter;
2414 struct files_reflog_iterator *iter;
2415 struct ref_iterator *ref_iterator;
e9dcc305 2416 struct strbuf sb = STRBUF_INIT;
2880d16f 2417
944b4e30 2418 strbuf_addf(&sb, "%s/logs", gitdir);
3012397e 2419
e69e8ffe 2420 diter = dir_iterator_begin(sb.buf, DIR_ITERATOR_SORTED);
9b7b0295
RS
2421 if (!diter) {
2422 strbuf_release(&sb);
3012397e 2423 return empty_ref_iterator_begin();
9b7b0295 2424 }
3012397e 2425
ca56dadb 2426 CALLOC_ARRAY(iter, 1);
3012397e
MT
2427 ref_iterator = &iter->base;
2428
5e01d838 2429 base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable);
3012397e 2430 iter->dir_iterator = diter;
2f40e954 2431 iter->ref_store = ref_store;
e9dcc305 2432 strbuf_release(&sb);
944b4e30 2433
2880d16f 2434 return ref_iterator;
7bd9bcf3
MH
2435}
2436
944b4e30
NTND
2437static struct ref_iterator *files_reflog_iterator_begin(struct ref_store *ref_store)
2438{
2439 struct files_ref_store *refs =
2440 files_downcast(ref_store, REF_STORE_READ,
2441 "reflog_iterator_begin");
2442
5085aef4 2443 if (!strcmp(refs->base.gitdir, refs->gitcommondir)) {
944b4e30
NTND
2444 return reflog_iterator_begin(ref_store, refs->gitcommondir);
2445 } else {
2446 return merge_ref_iterator_begin(
5e01d838 2447 reflog_iterator_begin(ref_store, refs->base.gitdir),
944b4e30 2448 reflog_iterator_begin(ref_store, refs->gitcommondir),
6f227800 2449 ref_iterator_select, refs);
944b4e30
NTND
2450 }
2451}
2452
165056b2 2453/*
92b1551b
MH
2454 * If update is a direct update of head_ref (the reference pointed to
2455 * by HEAD), then add an extra REF_LOG_ONLY update for HEAD.
2456 */
76e760b9
KN
2457static enum ref_transaction_error split_head_update(struct ref_update *update,
2458 struct ref_transaction *transaction,
2459 const char *head_ref,
2460 struct strbuf *err)
92b1551b 2461{
92b1551b
MH
2462 struct ref_update *new_update;
2463
2464 if ((update->flags & REF_LOG_ONLY) ||
fbd1a693 2465 (update->flags & REF_SKIP_CREATE_REFLOG) ||
acedcde7 2466 (update->flags & REF_IS_PRUNING) ||
92b1551b
MH
2467 (update->flags & REF_UPDATE_VIA_HEAD))
2468 return 0;
2469
2470 if (strcmp(update->refname, head_ref))
2471 return 0;
2472
2473 /*
2474 * First make sure that HEAD is not already in the
276d0e35 2475 * transaction. This check is O(lg N) in the transaction
92b1551b
MH
2476 * size, but it happens at most once per transaction.
2477 */
c3baddf0 2478 if (string_list_has_string(&transaction->refnames, "HEAD")) {
92b1551b
MH
2479 /* An entry already existed */
2480 strbuf_addf(err,
2481 "multiple updates for 'HEAD' (including one "
2482 "via its referent '%s') are not allowed",
2483 update->refname);
76e760b9 2484 return REF_TRANSACTION_ERROR_NAME_CONFLICT;
92b1551b
MH
2485 }
2486
2487 new_update = ref_transaction_add_update(
2488 transaction, "HEAD",
211fa8b2 2489 update->flags | REF_LOG_ONLY | REF_NO_DEREF | REF_LOG_VIA_SPLIT,
89f3bbdd 2490 &update->new_oid, &update->old_oid,
4483be36 2491 NULL, NULL, update->committer_info, update->msg);
211fa8b2 2492 new_update->parent_update = update;
92b1551b 2493
276d0e35
2494 /*
2495 * Add "HEAD". This insertion is O(N) in the transaction
2496 * size, but it happens at most once per transaction.
2497 * Add new_update->refname instead of a literal "HEAD".
2498 */
2499 if (strcmp(new_update->refname, "HEAD"))
2500 BUG("%s unexpectedly not 'HEAD'", new_update->refname);
92b1551b
MH
2501
2502 return 0;
2503}
2504
2505/*
2506 * update is for a symref that points at referent and doesn't have
91774afc
MH
2507 * REF_NO_DEREF set. Split it into two updates:
2508 * - The original update, but with REF_LOG_ONLY and REF_NO_DEREF set
92b1551b
MH
2509 * - A new, separate update for the referent reference
2510 * Note that the new update will itself be subject to splitting when
2511 * the iteration gets to it.
2512 */
76e760b9
KN
2513static enum ref_transaction_error split_symref_update(struct ref_update *update,
2514 const char *referent,
2515 struct ref_transaction *transaction,
2516 struct strbuf *err)
92b1551b 2517{
92b1551b
MH
2518 struct ref_update *new_update;
2519 unsigned int new_flags;
2520
2521 /*
2522 * First make sure that referent is not already in the
c299468b 2523 * transaction. This check is O(lg N) in the transaction
92b1551b
MH
2524 * size, but it happens at most once per symref in a
2525 * transaction.
2526 */
c3baddf0 2527 if (string_list_has_string(&transaction->refnames, referent)) {
c299468b 2528 /* An entry already exists */
92b1551b
MH
2529 strbuf_addf(err,
2530 "multiple updates for '%s' (including one "
2531 "via symref '%s') are not allowed",
2532 referent, update->refname);
76e760b9 2533 return REF_TRANSACTION_ERROR_NAME_CONFLICT;
92b1551b
MH
2534 }
2535
2536 new_flags = update->flags;
2537 if (!strcmp(update->refname, "HEAD")) {
2538 /*
2539 * Record that the new update came via HEAD, so that
2540 * when we process it, split_head_update() doesn't try
2541 * to add another reflog update for HEAD. Note that
2542 * this bit will be propagated if the new_update
2543 * itself needs to be split.
2544 */
2545 new_flags |= REF_UPDATE_VIA_HEAD;
2546 }
2547
2548 new_update = ref_transaction_add_update(
2549 transaction, referent, new_flags,
644daf77
KN
2550 update->new_target ? NULL : &update->new_oid,
2551 update->old_target ? NULL : &update->old_oid,
4483be36
KN
2552 update->new_target, update->old_target, NULL,
2553 update->msg);
92b1551b 2554
6e30b2f6
MH
2555 new_update->parent_update = update;
2556
2557 /*
2558 * Change the symbolic ref update to log only. Also, it
78fb4579 2559 * doesn't need to check its old OID value, as that will be
6e30b2f6
MH
2560 * done when new_update is processed.
2561 */
91774afc 2562 update->flags |= REF_LOG_ONLY | REF_NO_DEREF;
92b1551b 2563
92b1551b
MH
2564 return 0;
2565}
2566
e3f51039
MH
2567/*
2568 * Check whether the REF_HAVE_OLD and old_oid values stored in update
2569 * are consistent with oid, which is the reference's current value. If
2570 * everything is OK, return 0; otherwise, write an error message to
2571 * err and return -1.
2572 */
76e760b9
KN
2573static enum ref_transaction_error check_old_oid(struct ref_update *update,
2574 struct object_id *oid,
450fc2ba 2575 struct strbuf *referent,
76e760b9 2576 struct strbuf *err)
e3f51039 2577{
046c6732 2578 if (update->flags & REF_LOG_ONLY ||
450fc2ba 2579 !(update->flags & REF_HAVE_OLD))
e3f51039
MH
2580 return 0;
2581
450fc2ba
JK
2582 if (oideq(oid, &update->old_oid)) {
2583 /*
2584 * Normally matching the expected old oid is enough. Either we
2585 * found the ref at the expected state, or we are creating and
2586 * expect the null oid (and likewise found nothing).
2587 *
2588 * But there is one exception for the null oid: if we found a
2589 * symref pointing to nothing we'll also get the null oid. In
2590 * regular recursive mode, that's good (we'll write to what the
2591 * symref points to, which doesn't exist). But in no-deref
2592 * mode, it means we'll clobber the symref, even though the
2593 * caller asked for this to be a creation event. So flag
2594 * that case to preserve the dangling symref.
2595 */
2596 if ((update->flags & REF_NO_DEREF) && referent->len &&
2597 is_null_oid(oid)) {
2598 strbuf_addf(err, "cannot lock ref '%s': "
2599 "dangling symref already exists",
2600 ref_update_original_update_refname(update));
2601 return REF_TRANSACTION_ERROR_CREATE_EXISTS;
2602 }
2603 return 0;
2604 }
2605
ed2f6f88 2606 if (is_null_oid(&update->old_oid)) {
e3f51039
MH
2607 strbuf_addf(err, "cannot lock ref '%s': "
2608 "reference already exists",
e9965ba4 2609 ref_update_original_update_refname(update));
76e760b9
KN
2610 return REF_TRANSACTION_ERROR_CREATE_EXISTS;
2611 } else if (is_null_oid(oid)) {
e3f51039
MH
2612 strbuf_addf(err, "cannot lock ref '%s': "
2613 "reference is missing but expected %s",
e9965ba4 2614 ref_update_original_update_refname(update),
98491298 2615 oid_to_hex(&update->old_oid));
76e760b9
KN
2616 return REF_TRANSACTION_ERROR_NONEXISTENT_REF;
2617 }
e3f51039 2618
76e760b9
KN
2619 strbuf_addf(err, "cannot lock ref '%s': is at %s but expected %s",
2620 ref_update_original_update_refname(update), oid_to_hex(oid),
2621 oid_to_hex(&update->old_oid));
2622
2623 return REF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE;
e3f51039
MH
2624}
2625
611986f3
KN
2626struct files_transaction_backend_data {
2627 struct ref_transaction *packed_transaction;
2628 int packed_refs_locked;
2629 struct strmap ref_locks;
2630};
2631
92b1551b
MH
2632/*
2633 * Prepare for carrying out update:
2634 * - Lock the reference referred to by update.
2635 * - Read the reference under lock.
78fb4579 2636 * - Check that its old OID value (if specified) is correct, and in
92b1551b
MH
2637 * any case record it in update->lock->old_oid for later use when
2638 * writing the reflog.
91774afc 2639 * - If it is a symref update without REF_NO_DEREF, split it up into a
92b1551b
MH
2640 * REF_LOG_ONLY update of the symref and add a separate update for
2641 * the referent to transaction.
2642 * - If it is an update of head_ref, add a corresponding REF_LOG_ONLY
2643 * update of HEAD.
165056b2 2644 */
76e760b9
KN
2645static enum ref_transaction_error lock_ref_for_update(struct files_ref_store *refs,
2646 struct ref_update *update,
31726bb9 2647 size_t update_idx,
76e760b9
KN
2648 struct ref_transaction *transaction,
2649 const char *head_ref,
2650 struct string_list *refnames_to_check,
2651 struct strbuf *err)
165056b2 2652{
92b1551b 2653 struct strbuf referent = STRBUF_INIT;
aba381c0 2654 int mustexist = ref_update_expects_existing_old_ref(update);
611986f3 2655 struct files_transaction_backend_data *backend_data;
76e760b9 2656 enum ref_transaction_error ret = 0;
92b1551b 2657 struct ref_lock *lock;
165056b2 2658
32c597e7 2659 files_assert_main_repository(refs, "lock_ref_for_update");
b3bbbc5c 2660
611986f3
KN
2661 backend_data = transaction->backend_data;
2662
644daf77 2663 if ((update->flags & REF_HAVE_NEW) && ref_update_has_null_new_value(update))
165056b2 2664 update->flags |= REF_DELETING;
92b1551b
MH
2665
2666 if (head_ref) {
c3baddf0 2667 ret = split_head_update(update, transaction, head_ref, err);
92b1551b 2668 if (ret)
851e1fbd 2669 goto out;
92b1551b
MH
2670 }
2671
611986f3
KN
2672 lock = strmap_get(&backend_data->ref_locks, update->refname);
2673 if (lock) {
2674 lock->count++;
2675 } else {
3c070632
KN
2676 ret = lock_raw_ref(refs, transaction, update_idx, mustexist,
2677 refnames_to_check, &lock, &referent, err);
611986f3
KN
2678 if (ret) {
2679 char *reason;
2680
2681 reason = strbuf_detach(err, NULL);
2682 strbuf_addf(err, "cannot lock ref '%s': %s",
2683 ref_update_original_update_refname(update), reason);
2684 free(reason);
2685 goto out;
2686 }
165056b2 2687
611986f3 2688 strmap_put(&backend_data->ref_locks, update->refname, lock);
165056b2 2689 }
92b1551b 2690
7d618264 2691 update->backend_data = lock;
92b1551b 2692
211fa8b2
PS
2693 if (update->flags & REF_LOG_VIA_SPLIT) {
2694 struct ref_lock *parent_lock;
2695
2696 if (!update->parent_update)
2697 BUG("split update without a parent");
2698
2699 parent_lock = update->parent_update->backend_data;
2700
2701 /*
2702 * Check that "HEAD" didn't racily change since we have looked
2703 * it up. If it did we must refuse to write the reflog entry.
2704 *
2705 * Note that this does not catch all races: if "HEAD" was
2706 * racily changed to point to one of the refs part of the
2707 * transaction then we would miss writing the split reflog
2708 * entry for "HEAD".
2709 */
2710 if (!(update->type & REF_ISSYMREF) ||
2711 strcmp(update->parent_update->refname, referent.buf)) {
2712 strbuf_addstr(err, "HEAD has been racily updated");
2713 ret = REF_TRANSACTION_ERROR_GENERIC;
2714 goto out;
2715 }
2716
2717 if (update->flags & REF_HAVE_OLD) {
2718 oidcpy(&lock->old_oid, &update->old_oid);
2719 } else {
2720 oidcpy(&lock->old_oid, &parent_lock->old_oid);
2721 }
2722 } else if (update->type & REF_ISSYMREF) {
91774afc 2723 if (update->flags & REF_NO_DEREF) {
6e30b2f6
MH
2724 /*
2725 * We won't be reading the referent as part of
2726 * the transaction, so we have to read it here
78fb4579 2727 * to record and possibly check old_oid:
6e30b2f6 2728 */
f1da24ca 2729 if (!refs_resolve_ref_unsafe(&refs->base,
76887df0 2730 referent.buf, 0,
ce14de03 2731 &lock->old_oid, NULL)) {
6e30b2f6
MH
2732 if (update->flags & REF_HAVE_OLD) {
2733 strbuf_addf(err, "cannot lock ref '%s': "
e3f51039 2734 "error reading reference",
e9965ba4 2735 ref_update_original_update_refname(update));
76e760b9 2736 ret = REF_TRANSACTION_ERROR_GENERIC;
851e1fbd 2737 goto out;
6e30b2f6 2738 }
644daf77
KN
2739 }
2740
76e760b9
KN
2741 if (update->old_target)
2742 ret = ref_update_check_old_target(referent.buf, update, err);
2743 else
450fc2ba
JK
2744 ret = check_old_oid(update, &lock->old_oid,
2745 &referent, err);
76e760b9
KN
2746 if (ret)
2747 goto out;
6e30b2f6
MH
2748 } else {
2749 /*
2750 * Create a new update for the reference this
2751 * symref is pointing at. Also, we will record
78fb4579 2752 * and verify old_oid for this update as part
6e30b2f6
MH
2753 * of processing the split-off update, so we
2754 * don't have to do it here.
2755 */
c3baddf0
KN
2756 ret = split_symref_update(update, referent.buf,
2757 transaction, err);
92b1551b 2758 if (ret)
851e1fbd 2759 goto out;
92b1551b 2760 }
6e30b2f6
MH
2761 } else {
2762 struct ref_update *parent_update;
8169d0d0 2763
644daf77
KN
2764 /*
2765 * Even if the ref is a regular ref, if `old_target` is set, we
aa6e99f1 2766 * fail with an error.
644daf77
KN
2767 */
2768 if (update->old_target) {
aa6e99f1
KN
2769 strbuf_addf(err, _("cannot lock ref '%s': "
2770 "expected symref with target '%s': "
2771 "but is a regular ref"),
2772 ref_update_original_update_refname(update),
2773 update->old_target);
76e760b9 2774 ret = REF_TRANSACTION_ERROR_EXPECTED_SYMREF;
aa6e99f1 2775 goto out;
ed2f6f88 2776 } else {
450fc2ba
JK
2777 ret = check_old_oid(update, &lock->old_oid,
2778 &referent, err);
ed2f6f88
BF
2779 if (ret) {
2780 goto out;
2781 }
851e1fbd 2782 }
e3f51039 2783
6e30b2f6
MH
2784 /*
2785 * If this update is happening indirectly because of a
78fb4579 2786 * symref update, record the old OID in the parent
6e30b2f6
MH
2787 * update:
2788 */
2789 for (parent_update = update->parent_update;
2790 parent_update;
2791 parent_update = parent_update->parent_update) {
7d618264
DT
2792 struct ref_lock *parent_lock = parent_update->backend_data;
2793 oidcpy(&parent_lock->old_oid, &lock->old_oid);
6e30b2f6 2794 }
92b1551b
MH
2795 }
2796
644daf77 2797 if (update->new_target && !(update->flags & REF_LOG_ONLY)) {
65e7a447 2798 if (create_symref_lock(lock, update->new_target, err)) {
76e760b9 2799 ret = REF_TRANSACTION_ERROR_GENERIC;
644daf77
KN
2800 goto out;
2801 }
2802
2803 if (close_ref_gently(lock)) {
2804 strbuf_addf(err, "couldn't close '%s.lock'",
2805 update->refname);
76e760b9 2806 ret = REF_TRANSACTION_ERROR_GENERIC;
644daf77
KN
2807 goto out;
2808 }
2809
2810 /*
2811 * Once we have created the symref lock, the commit
2812 * phase of the transaction only needs to commit the lock.
2813 */
2814 update->flags |= REF_NEEDS_COMMIT;
2815 } else if ((update->flags & REF_HAVE_NEW) &&
2816 !(update->flags & REF_DELETING) &&
2817 !(update->flags & REF_LOG_ONLY)) {
92b1551b 2818 if (!(update->type & REF_ISSYMREF) &&
4a7e27e9 2819 oideq(&lock->old_oid, &update->new_oid)) {
165056b2
MH
2820 /*
2821 * The reference already has the desired
2822 * value, so we don't need to write it.
2823 */
165056b2 2824 } else {
76e760b9
KN
2825 ret = write_ref_to_lockfile(
2826 refs, lock, &update->new_oid,
2827 update->flags & REF_SKIP_OID_VERIFICATION,
2828 err);
2829 if (ret) {
2830 char *write_err = strbuf_detach(err, NULL);
2831
2832 /*
2833 * The lock was freed upon failure of
2834 * write_ref_to_lockfile():
2835 */
2836 update->backend_data = NULL;
2837 strbuf_addf(err,
2838 "cannot update ref '%s': %s",
2839 update->refname, write_err);
2840 free(write_err);
2841 goto out;
2842 } else {
2843 update->flags |= REF_NEEDS_COMMIT;
2844 }
165056b2
MH
2845 }
2846 }
2847 if (!(update->flags & REF_NEEDS_COMMIT)) {
2848 /*
2849 * We didn't call write_ref_to_lockfile(), so
2850 * the lockfile is still open. Close it to
2851 * free up the file descriptor:
2852 */
83a3069a 2853 if (close_ref_gently(lock)) {
165056b2
MH
2854 strbuf_addf(err, "couldn't close '%s.lock'",
2855 update->refname);
76e760b9 2856 ret = REF_TRANSACTION_ERROR_GENERIC;
851e1fbd 2857 goto out;
165056b2
MH
2858 }
2859 }
851e1fbd
2860
2861out:
2862 strbuf_release(&referent);
2863 return ret;
165056b2
MH
2864}
2865
c0ca9357
MH
2866/*
2867 * Unlock any references in `transaction` that are still locked, and
2868 * mark the transaction closed.
2869 */
dc39e099
MH
2870static void files_transaction_cleanup(struct files_ref_store *refs,
2871 struct ref_transaction *transaction)
c0ca9357
MH
2872{
2873 size_t i;
dc39e099
MH
2874 struct files_transaction_backend_data *backend_data =
2875 transaction->backend_data;
2876 struct strbuf err = STRBUF_INIT;
c0ca9357
MH
2877
2878 for (i = 0; i < transaction->nr; i++) {
2879 struct ref_update *update = transaction->updates[i];
2880 struct ref_lock *lock = update->backend_data;
2881
2882 if (lock) {
2883 unlock_ref(lock);
a3a7f205
PS
2884 try_remove_empty_parents(refs, update->refname,
2885 REMOVE_EMPTY_PARENTS_REF);
c0ca9357
MH
2886 update->backend_data = NULL;
2887 }
2888 }
2889
edc30691
PS
2890 if (backend_data) {
2891 if (backend_data->packed_transaction &&
2892 ref_transaction_abort(backend_data->packed_transaction, &err)) {
2893 error("error aborting transaction: %s", err.buf);
2894 strbuf_release(&err);
2895 }
dc39e099 2896
edc30691
PS
2897 if (backend_data->packed_refs_locked)
2898 packed_refs_unlock(refs->packed_ref_store);
dc39e099 2899
611986f3
KN
2900 strmap_clear(&backend_data->ref_locks, 0);
2901
edc30691
PS
2902 free(backend_data);
2903 }
dc39e099 2904
c0ca9357
MH
2905 transaction->state = REF_TRANSACTION_CLOSED;
2906}
2907
30173b88
MH
2908static int files_transaction_prepare(struct ref_store *ref_store,
2909 struct ref_transaction *transaction,
2910 struct strbuf *err)
7bd9bcf3 2911{
00eebe35 2912 struct files_ref_store *refs =
9e7ec634 2913 files_downcast(ref_store, REF_STORE_WRITE,
30173b88 2914 "ref_transaction_prepare");
43a2dfde
MH
2915 size_t i;
2916 int ret = 0;
770f389b 2917 struct string_list refnames_to_check = STRING_LIST_INIT_DUP;
92b1551b
MH
2918 char *head_ref = NULL;
2919 int head_type;
dc39e099
MH
2920 struct files_transaction_backend_data *backend_data;
2921 struct ref_transaction *packed_transaction = NULL;
7bd9bcf3
MH
2922
2923 assert(err);
2924
1c299d03
PS
2925 if (transaction->flags & REF_TRANSACTION_FLAG_INITIAL)
2926 goto cleanup;
c0ca9357
MH
2927 if (!transaction->nr)
2928 goto cleanup;
7bd9bcf3 2929
ca56dadb 2930 CALLOC_ARRAY(backend_data, 1);
611986f3 2931 strmap_init(&backend_data->ref_locks);
dc39e099
MH
2932 transaction->backend_data = backend_data;
2933
92b1551b 2934 /*
c3baddf0 2935 * Fail if any of the updates use REF_IS_PRUNING without REF_NO_DEREF.
92b1551b
MH
2936 */
2937 for (i = 0; i < transaction->nr; i++) {
2938 struct ref_update *update = transaction->updates[i];
92b1551b 2939
acedcde7 2940 if ((update->flags & REF_IS_PRUNING) &&
91774afc 2941 !(update->flags & REF_NO_DEREF))
acedcde7 2942 BUG("REF_IS_PRUNING set without REF_NO_DEREF");
7bd9bcf3
MH
2943 }
2944
92b1551b
MH
2945 /*
2946 * Special hack: If a branch is updated directly and HEAD
2947 * points to it (may happen on the remote side of a push
2948 * for example) then logically the HEAD reflog should be
2949 * updated too.
2950 *
2951 * A generic solution would require reverse symref lookups,
2952 * but finding all symrefs pointing to a given branch would be
2953 * rather costly for this rare event (the direct update of a
2954 * branch) to be worth it. So let's cheat and check with HEAD
2955 * only, which should cover 99% of all usage scenarios (even
2956 * 100% of the default ones).
2957 *
2958 * So if HEAD is a symbolic reference, then record the name of
2959 * the reference that it points to. If we see an update of
2960 * head_ref within the transaction, then split_head_update()
2961 * arranges for the reflog of HEAD to be updated, too.
2962 */
2f40e954
NTND
2963 head_ref = refs_resolve_refdup(ref_store, "HEAD",
2964 RESOLVE_REF_NO_RECURSE,
872ccb2c 2965 NULL, &head_type);
92b1551b
MH
2966
2967 if (head_ref && !(head_type & REF_ISSYMREF)) {
6a83d902 2968 FREE_AND_NULL(head_ref);
92b1551b
MH
2969 }
2970
7bd9bcf3
MH
2971 /*
2972 * Acquire all locks, verify old values if provided, check
2973 * that new values are valid, and write new values to the
2974 * lockfiles, ready to be activated. Only keep one lockfile
2975 * open at a time to avoid running out of file descriptors.
30173b88
MH
2976 * Note that lock_ref_for_update() might append more updates
2977 * to the transaction.
7bd9bcf3 2978 */
efe47281
MH
2979 for (i = 0; i < transaction->nr; i++) {
2980 struct ref_update *update = transaction->updates[i];
7bd9bcf3 2981
31726bb9 2982 ret = lock_ref_for_update(refs, update, i, transaction,
6c90726b 2983 head_ref, &refnames_to_check,
c3baddf0 2984 err);
23fc8e4f
KN
2985 if (ret) {
2986 if (ref_transaction_maybe_set_rejected(transaction, i, ret)) {
2987 strbuf_reset(err);
2988 ret = 0;
2989
2990 continue;
2991 }
da5267f1 2992 goto cleanup;
23fc8e4f 2993 }
dc39e099
MH
2994
2995 if (update->flags & REF_DELETING &&
2996 !(update->flags & REF_LOG_ONLY) &&
acedcde7 2997 !(update->flags & REF_IS_PRUNING)) {
dc39e099
MH
2998 /*
2999 * This reference has to be deleted from
3000 * packed-refs if it exists there.
3001 */
3002 if (!packed_transaction) {
3003 packed_transaction = ref_store_transaction_begin(
a0efef14
PS
3004 refs->packed_ref_store,
3005 transaction->flags, err);
dc39e099 3006 if (!packed_transaction) {
76e760b9 3007 ret = REF_TRANSACTION_ERROR_GENERIC;
dc39e099
MH
3008 goto cleanup;
3009 }
3010
3011 backend_data->packed_transaction =
3012 packed_transaction;
3013 }
3014
3015 ref_transaction_add_update(
3016 packed_transaction, update->refname,
91774afc 3017 REF_HAVE_NEW | REF_NO_DEREF,
b0ca4110 3018 &update->new_oid, NULL,
4483be36 3019 NULL, NULL, NULL, NULL);
dc39e099
MH
3020 }
3021 }
3022
6c90726b
PS
3023 /*
3024 * Verify that none of the loose reference that we're about to write
3025 * conflict with any existing packed references. Ideally, we'd do this
3026 * check after the packed-refs are locked so that the file cannot
3027 * change underneath our feet. But introducing such a lock now would
3028 * probably do more harm than good as users rely on there not being a
3029 * global lock with the "files" backend.
3030 *
3031 * Another alternative would be to do the check after the (optional)
3032 * lock, but that would extend the time we spend in the globally-locked
3033 * state.
3034 *
3035 * So instead, we accept the race for now.
3036 */
3037 if (refs_verify_refnames_available(refs->packed_ref_store, &refnames_to_check,
31726bb9
KN
3038 &transaction->refnames, NULL, transaction,
3039 0, err)) {
76e760b9 3040 ret = REF_TRANSACTION_ERROR_NAME_CONFLICT;
6c90726b
PS
3041 goto cleanup;
3042 }
3043
dc39e099
MH
3044 if (packed_transaction) {
3045 if (packed_refs_lock(refs->packed_ref_store, 0, err)) {
76e760b9 3046 ret = REF_TRANSACTION_ERROR_GENERIC;
dc39e099
MH
3047 goto cleanup;
3048 }
3049 backend_data->packed_refs_locked = 1;
7c6bd25c
MH
3050
3051 if (is_packed_transaction_needed(refs->packed_ref_store,
3052 packed_transaction)) {
3053 ret = ref_transaction_prepare(packed_transaction, err);
249e8dc7
JK
3054 /*
3055 * A failure during the prepare step will abort
3056 * itself, but not free. Do that now, and disconnect
3057 * from the files_transaction so it does not try to
3058 * abort us when we hit the cleanup code below.
3059 */
3060 if (ret) {
3061 ref_transaction_free(packed_transaction);
3062 backend_data->packed_transaction = NULL;
3063 }
7c6bd25c
MH
3064 } else {
3065 /*
3066 * We can skip rewriting the `packed-refs`
3067 * file. But we do need to leave it locked, so
3068 * that somebody else doesn't pack a reference
3069 * that we are trying to delete.
d3322eb2
JK
3070 *
3071 * We need to disconnect our transaction from
3072 * backend_data, since the abort (whether successful or
3073 * not) will free it.
7c6bd25c 3074 */
d3322eb2 3075 backend_data->packed_transaction = NULL;
7c6bd25c 3076 if (ref_transaction_abort(packed_transaction, err)) {
76e760b9 3077 ret = REF_TRANSACTION_ERROR_GENERIC;
7c6bd25c
MH
3078 goto cleanup;
3079 }
7c6bd25c 3080 }
30173b88
MH
3081 }
3082
3083cleanup:
3084 free(head_ref);
31726bb9 3085 string_list_clear(&refnames_to_check, 1);
30173b88
MH
3086
3087 if (ret)
dc39e099 3088 files_transaction_cleanup(refs, transaction);
30173b88
MH
3089 else
3090 transaction->state = REF_TRANSACTION_PREPARED;
3091
3092 return ret;
3093}
3094
644daf77
KN
3095static int parse_and_write_reflog(struct files_ref_store *refs,
3096 struct ref_update *update,
3097 struct ref_lock *lock,
3098 struct strbuf *err)
3099{
465eff81
PS
3100 struct object_id *old_oid = &lock->old_oid;
3101
3102 if (update->flags & REF_LOG_USE_PROVIDED_OIDS) {
3103 if (!(update->flags & REF_HAVE_OLD) ||
3104 !(update->flags & REF_HAVE_NEW) ||
3105 !(update->flags & REF_LOG_ONLY)) {
d9988b06 3106 strbuf_addf(err, _("trying to write reflog for '%s' "
465eff81
PS
3107 "with incomplete values"), update->refname);
3108 return REF_TRANSACTION_ERROR_GENERIC;
3109 }
3110
3111 old_oid = &update->old_oid;
3112 }
3113
644daf77
KN
3114 if (update->new_target) {
3115 /*
3116 * We want to get the resolved OID for the target, to ensure
3117 * that the correct value is added to the reflog.
3118 */
3119 if (!refs_resolve_ref_unsafe(&refs->base, update->new_target,
3120 RESOLVE_REF_READING,
3121 &update->new_oid, NULL)) {
3122 /*
3123 * TODO: currently we skip creating reflogs for dangling
3124 * symref updates. It would be nice to capture this as
3125 * zero oid updates however.
3126 */
3127 return 0;
3128 }
3129 }
3130
465eff81 3131 if (files_log_ref_write(refs, lock->ref_name, old_oid,
1a83e26d
KN
3132 &update->new_oid, update->committer_info,
3133 update->msg, update->flags, err)) {
644daf77
KN
3134 char *old_msg = strbuf_detach(err, NULL);
3135
3136 strbuf_addf(err, "cannot update the ref '%s': %s",
3137 lock->ref_name, old_msg);
3138 free(old_msg);
3139 unlock_ref(lock);
3140 update->backend_data = NULL;
3141 return -1;
3142 }
3143
3144 return 0;
3145}
3146
bdbebe57 3147static int ref_present(const struct reference *ref, void *cb_data)
83b8ed8b
PS
3148{
3149 struct string_list *affected_refnames = cb_data;
3150
bdbebe57 3151 return string_list_has_string(affected_refnames, ref->name);
83b8ed8b
PS
3152}
3153
1c299d03 3154static int files_transaction_finish_initial(struct files_ref_store *refs,
83b8ed8b
PS
3155 struct ref_transaction *transaction,
3156 struct strbuf *err)
3157{
83b8ed8b
PS
3158 size_t i;
3159 int ret = 0;
3160 struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
268ea851 3161 struct string_list refnames_to_check = STRING_LIST_INIT_NODUP;
83b8ed8b 3162 struct ref_transaction *packed_transaction = NULL;
c0b9cf3b 3163 struct ref_transaction *loose_transaction = NULL;
83b8ed8b
PS
3164
3165 assert(err);
3166
1c299d03
PS
3167 if (transaction->state != REF_TRANSACTION_PREPARED)
3168 BUG("commit called for transaction that is not prepared");
83b8ed8b 3169
83b8ed8b
PS
3170 /*
3171 * It's really undefined to call this function in an active
3172 * repository or when there are existing references: we are
3173 * only locking and changing packed-refs, so (1) any
3174 * simultaneous processes might try to change a reference at
3175 * the same time we do, and (2) any existing loose versions of
3176 * the references that we are setting would have precedence
3177 * over our values. But some remote helpers create the remote
3178 * "HEAD" and "master" branches before calling this function,
3179 * so here we really only check that none of the references
3180 * that we are creating already exists.
3181 */
3182 if (refs_for_each_rawref(&refs->base, ref_present,
c3baddf0 3183 &transaction->refnames))
83b8ed8b
PS
3184 BUG("initial ref transaction called with existing refs");
3185
3186 packed_transaction = ref_store_transaction_begin(refs->packed_ref_store,
3187 transaction->flags, err);
3188 if (!packed_transaction) {
76e760b9 3189 ret = REF_TRANSACTION_ERROR_GENERIC;
83b8ed8b
PS
3190 goto cleanup;
3191 }
3192
3193 for (i = 0; i < transaction->nr; i++) {
3194 struct ref_update *update = transaction->updates[i];
3195
046c6732
PS
3196 if (!(update->flags & REF_LOG_ONLY) &&
3197 (update->flags & REF_HAVE_OLD) &&
83b8ed8b
PS
3198 !is_null_oid(&update->old_oid))
3199 BUG("initial ref transaction with old_sha1 set");
c0b9cf3b 3200
268ea851 3201 string_list_append(&refnames_to_check, update->refname);
83b8ed8b
PS
3202
3203 /*
84675fa2
KN
3204 * packed-refs don't support symbolic refs, root refs and reflogs,
3205 * so we have to queue these references via the loose transaction.
83b8ed8b 3206 */
84675fa2
KN
3207 if (update->new_target ||
3208 is_root_ref(update->refname) ||
3209 (update->flags & REF_LOG_ONLY)) {
c0b9cf3b
PS
3210 if (!loose_transaction) {
3211 loose_transaction = ref_store_transaction_begin(&refs->base, 0, err);
3212 if (!loose_transaction) {
76e760b9 3213 ret = REF_TRANSACTION_ERROR_GENERIC;
c0b9cf3b
PS
3214 goto cleanup;
3215 }
3216 }
3217
84675fa2
KN
3218 if (update->flags & REF_LOG_ONLY)
3219 ref_transaction_add_update(loose_transaction, update->refname,
3220 update->flags, &update->new_oid,
3221 &update->old_oid, NULL, NULL,
3222 update->committer_info, update->msg);
3223 else
3224 ref_transaction_add_update(loose_transaction, update->refname,
3225 update->flags & ~REF_HAVE_OLD,
3226 update->new_target ? NULL : &update->new_oid, NULL,
3227 update->new_target, NULL, update->committer_info,
3228 NULL);
c0b9cf3b
PS
3229 } else {
3230 ref_transaction_add_update(packed_transaction, update->refname,
3231 update->flags & ~REF_HAVE_OLD,
3232 &update->new_oid, &update->old_oid,
4483be36 3233 NULL, NULL, update->committer_info, NULL);
c0b9cf3b 3234 }
83b8ed8b
PS
3235 }
3236
268ea851 3237 if (packed_refs_lock(refs->packed_ref_store, 0, err)) {
76e760b9 3238 ret = REF_TRANSACTION_ERROR_GENERIC;
268ea851
PS
3239 goto cleanup;
3240 }
3241
3242 if (refs_verify_refnames_available(&refs->base, &refnames_to_check,
31726bb9
KN
3243 &affected_refnames, NULL, transaction,
3244 1, err)) {
268ea851 3245 packed_refs_unlock(refs->packed_ref_store);
76e760b9 3246 ret = REF_TRANSACTION_ERROR_NAME_CONFLICT;
268ea851
PS
3247 goto cleanup;
3248 }
3249
3250 if (ref_transaction_commit(packed_transaction, err)) {
76e760b9 3251 ret = REF_TRANSACTION_ERROR_GENERIC;
83b8ed8b
PS
3252 goto cleanup;
3253 }
c0b9cf3b 3254 packed_refs_unlock(refs->packed_ref_store);
83b8ed8b 3255
c0b9cf3b
PS
3256 if (loose_transaction) {
3257 if (ref_transaction_prepare(loose_transaction, err) ||
3258 ref_transaction_commit(loose_transaction, err)) {
76e760b9 3259 ret = REF_TRANSACTION_ERROR_GENERIC;
c0b9cf3b
PS
3260 goto cleanup;
3261 }
83b8ed8b
PS
3262 }
3263
83b8ed8b 3264cleanup:
c0b9cf3b
PS
3265 if (loose_transaction)
3266 ref_transaction_free(loose_transaction);
83b8ed8b
PS
3267 if (packed_transaction)
3268 ref_transaction_free(packed_transaction);
3269 transaction->state = REF_TRANSACTION_CLOSED;
3270 string_list_clear(&affected_refnames, 0);
268ea851 3271 string_list_clear(&refnames_to_check, 0);
83b8ed8b
PS
3272 return ret;
3273}
3274
30173b88
MH
3275static int files_transaction_finish(struct ref_store *ref_store,
3276 struct ref_transaction *transaction,
3277 struct strbuf *err)
3278{
3279 struct files_ref_store *refs =
3280 files_downcast(ref_store, 0, "ref_transaction_finish");
3281 size_t i;
3282 int ret = 0;
30173b88 3283 struct strbuf sb = STRBUF_INIT;
dc39e099
MH
3284 struct files_transaction_backend_data *backend_data;
3285 struct ref_transaction *packed_transaction;
3286
30173b88
MH
3287
3288 assert(err);
3289
1c299d03
PS
3290 if (transaction->flags & REF_TRANSACTION_FLAG_INITIAL)
3291 return files_transaction_finish_initial(refs, transaction, err);
30173b88
MH
3292 if (!transaction->nr) {
3293 transaction->state = REF_TRANSACTION_CLOSED;
3294 return 0;
7bd9bcf3 3295 }
7bd9bcf3 3296
dc39e099
MH
3297 backend_data = transaction->backend_data;
3298 packed_transaction = backend_data->packed_transaction;
3299
7bd9bcf3 3300 /* Perform updates first so live commits remain referenced */
efe47281
MH
3301 for (i = 0; i < transaction->nr; i++) {
3302 struct ref_update *update = transaction->updates[i];
7d618264 3303 struct ref_lock *lock = update->backend_data;
7bd9bcf3 3304
23fc8e4f
KN
3305 if (update->rejection_err)
3306 continue;
3307
d99aa884
DT
3308 if (update->flags & REF_NEEDS_COMMIT ||
3309 update->flags & REF_LOG_ONLY) {
644daf77 3310 if (parse_and_write_reflog(refs, update, lock, err)) {
76e760b9 3311 ret = REF_TRANSACTION_ERROR_GENERIC;
7bd9bcf3 3312 goto cleanup;
7bd9bcf3
MH
3313 }
3314 }
644daf77
KN
3315
3316 /*
3317 * We try creating a symlink, if that succeeds we continue to the
3318 * next update. If not, we try and create a regular symref.
3319 */
8e2e8a33 3320 if (update->new_target && refs->prefer_symlink_refs)
38609851
JS
3321 /*
3322 * By using the `NOT_CONSTANT()` trick, we can avoid
3323 * errors by `clang`'s `-Wunreachable` logic that would
3324 * report that the `continue` statement is not reachable
3325 * when `NO_SYMLINK_HEAD` is `#define`d.
3326 */
3327 if (NOT_CONSTANT(!create_ref_symlink(lock, update->new_target)))
644daf77
KN
3328 continue;
3329
7bd9bcf3 3330 if (update->flags & REF_NEEDS_COMMIT) {
00eebe35 3331 clear_loose_ref_cache(refs);
92b1551b
MH
3332 if (commit_ref(lock)) {
3333 strbuf_addf(err, "couldn't set '%s'", lock->ref_name);
3334 unlock_ref(lock);
7d618264 3335 update->backend_data = NULL;
76e760b9 3336 ret = REF_TRANSACTION_ERROR_GENERIC;
7bd9bcf3 3337 goto cleanup;
7bd9bcf3
MH
3338 }
3339 }
3340 }
dc39e099 3341
5e00a6c8
MH
3342 /*
3343 * Now that updates are safely completed, we can perform
3344 * deletes. First delete the reflogs of any references that
3345 * will be deleted, since (in the unexpected event of an
3346 * error) leaving a reference without a reflog is less bad
3347 * than leaving a reflog without a reference (the latter is a
3348 * mildly invalid repository state):
3349 */
3350 for (i = 0; i < transaction->nr; i++) {
3351 struct ref_update *update = transaction->updates[i];
15c45c74
KN
3352
3353 if (update->rejection_err)
3354 continue;
3355
5e00a6c8
MH
3356 if (update->flags & REF_DELETING &&
3357 !(update->flags & REF_LOG_ONLY) &&
acedcde7 3358 !(update->flags & REF_IS_PRUNING)) {
5e00a6c8
MH
3359 strbuf_reset(&sb);
3360 files_reflog_path(refs, &sb, update->refname);
3361 if (!unlink_or_warn(sb.buf))
3362 try_remove_empty_parents(refs, update->refname,
3363 REMOVE_EMPTY_PARENTS_REFLOG);
3364 }
3365 }
3366
dc39e099
MH
3367 /*
3368 * Perform deletes now that updates are safely completed.
3369 *
3370 * First delete any packed versions of the references, while
3371 * retaining the packed-refs lock:
3372 */
3373 if (packed_transaction) {
3374 ret = ref_transaction_commit(packed_transaction, err);
3375 ref_transaction_free(packed_transaction);
3376 packed_transaction = NULL;
3377 backend_data->packed_transaction = NULL;
3378 if (ret)
3379 goto cleanup;
3380 }
3381
3382 /* Now delete the loose versions of the references: */
efe47281
MH
3383 for (i = 0; i < transaction->nr; i++) {
3384 struct ref_update *update = transaction->updates[i];
7d618264 3385 struct ref_lock *lock = update->backend_data;
7bd9bcf3 3386
15c45c74
KN
3387 if (update->rejection_err)
3388 continue;
3389
d99aa884
DT
3390 if (update->flags & REF_DELETING &&
3391 !(update->flags & REF_LOG_ONLY)) {
5f03e512 3392 update->flags |= REF_DELETED_RMDIR;
ce0af24d
MH
3393 if (!(update->type & REF_ISPACKED) ||
3394 update->type & REF_ISSYMREF) {
3395 /* It is a loose reference. */
e9dcc305 3396 strbuf_reset(&sb);
19e02f4f 3397 files_ref_path(refs, &sb, lock->ref_name);
e9dcc305 3398 if (unlink_or_msg(sb.buf, err)) {
76e760b9 3399 ret = REF_TRANSACTION_ERROR_GENERIC;
ce0af24d
MH
3400 goto cleanup;
3401 }
7bd9bcf3 3402 }
7bd9bcf3
MH
3403 }
3404 }
3405
00eebe35 3406 clear_loose_ref_cache(refs);
7bd9bcf3
MH
3407
3408cleanup:
dc39e099 3409 files_transaction_cleanup(refs, transaction);
7bd9bcf3 3410
44639777
MH
3411 for (i = 0; i < transaction->nr; i++) {
3412 struct ref_update *update = transaction->updates[i];
44639777 3413
5f03e512 3414 if (update->flags & REF_DELETED_RMDIR) {
44639777 3415 /*
5f03e512 3416 * The reference was deleted. Delete any
44639777
MH
3417 * empty parent directories. (Note that this
3418 * can only work because we have already
3419 * removed the lockfile.)
3420 */
802de3da 3421 try_remove_empty_parents(refs, update->refname,
44639777
MH
3422 REMOVE_EMPTY_PARENTS_REF);
3423 }
3424 }
3425
30173b88 3426 strbuf_release(&sb);
7bd9bcf3
MH
3427 return ret;
3428}
3429
30173b88
MH
3430static int files_transaction_abort(struct ref_store *ref_store,
3431 struct ref_transaction *transaction,
5cf88fd8 3432 struct strbuf *err UNUSED)
30173b88 3433{
dc39e099
MH
3434 struct files_ref_store *refs =
3435 files_downcast(ref_store, 0, "ref_transaction_abort");
3436
3437 files_transaction_cleanup(refs, transaction);
30173b88
MH
3438 return 0;
3439}
3440
7bd9bcf3 3441struct expire_reflog_cb {
7bd9bcf3
MH
3442 reflog_expiry_should_prune_fn *should_prune_fn;
3443 void *policy_cb;
3444 FILE *newlog;
9461d272 3445 struct object_id last_kept_oid;
fcd2c3d9
ÆAB
3446 unsigned int rewrite:1,
3447 dry_run:1;
7bd9bcf3
MH
3448};
3449
b9fd73a2
PS
3450static int expire_reflog_ent(const char *refname UNUSED,
3451 struct object_id *ooid, struct object_id *noid,
dddbad72 3452 const char *email, timestamp_t timestamp, int tz,
7bd9bcf3
MH
3453 const char *message, void *cb_data)
3454{
3455 struct expire_reflog_cb *cb = cb_data;
fcd2c3d9 3456 reflog_expiry_should_prune_fn *fn = cb->should_prune_fn;
7bd9bcf3 3457
fcd2c3d9 3458 if (cb->rewrite)
9461d272 3459 ooid = &cb->last_kept_oid;
7bd9bcf3 3460
fcd2c3d9
ÆAB
3461 if (fn(ooid, noid, email, timestamp, tz, message, cb->policy_cb))
3462 return 0;
3463
3464 if (cb->dry_run)
3465 return 0; /* --dry-run */
3466
3467 fprintf(cb->newlog, "%s %s %s %"PRItime" %+05d\t%s", oid_to_hex(ooid),
3468 oid_to_hex(noid), email, timestamp, tz, message);
3469 oidcpy(&cb->last_kept_oid, noid);
3470
7bd9bcf3
MH
3471 return 0;
3472}
3473
e3688bd6 3474static int files_reflog_expire(struct ref_store *ref_store,
cc40b5ce 3475 const char *refname,
fcd2c3d9 3476 unsigned int expire_flags,
e3688bd6
DT
3477 reflog_expiry_prepare_fn prepare_fn,
3478 reflog_expiry_should_prune_fn should_prune_fn,
3479 reflog_expiry_cleanup_fn cleanup_fn,
3480 void *policy_cb_data)
7bd9bcf3 3481{
7eb27cdf 3482 struct files_ref_store *refs =
9e7ec634 3483 files_downcast(ref_store, REF_STORE_WRITE, "reflog_expire");
b2275868 3484 struct lock_file reflog_lock = LOCK_INIT;
7bd9bcf3
MH
3485 struct expire_reflog_cb cb;
3486 struct ref_lock *lock;
802de3da 3487 struct strbuf log_file_sb = STRBUF_INIT;
7bd9bcf3
MH
3488 char *log_file;
3489 int status = 0;
7bd9bcf3 3490 struct strbuf err = STRBUF_INIT;
ae35e16c 3491 const struct object_id *oid;
7bd9bcf3
MH
3492
3493 memset(&cb, 0, sizeof(cb));
fcd2c3d9
ÆAB
3494 cb.rewrite = !!(expire_flags & EXPIRE_REFLOGS_REWRITE);
3495 cb.dry_run = !!(expire_flags & EXPIRE_REFLOGS_DRY_RUN);
7bd9bcf3
MH
3496 cb.policy_cb = policy_cb_data;
3497 cb.should_prune_fn = should_prune_fn;
3498
3499 /*
3500 * The reflog file is locked by holding the lock on the
3501 * reference itself, plus we might need to update the
3502 * reference if --updateref was specified:
3503 */
52106430 3504 lock = lock_ref_oid_basic(refs, refname, &err);
7bd9bcf3
MH
3505 if (!lock) {
3506 error("cannot lock ref '%s': %s", refname, err.buf);
3507 strbuf_release(&err);
3508 return -1;
3509 }
ae35e16c 3510 oid = &lock->old_oid;
7aa7829f
ÆAB
3511
3512 /*
3513 * When refs are deleted, their reflog is deleted before the
3514 * ref itself is deleted. This is because there is no separate
3515 * lock for reflog; instead we take a lock on the ref with
3516 * lock_ref_oid_basic().
3517 *
3518 * If a race happens and the reflog doesn't exist after we've
3519 * acquired the lock that's OK. We've got nothing more to do;
3520 * We were asked to delete the reflog, but someone else
3521 * deleted it! The caller doesn't care that we deleted it,
3522 * just that it is deleted. So we can return successfully.
3523 */
2f40e954 3524 if (!refs_reflog_exists(ref_store, refname)) {
7bd9bcf3
MH
3525 unlock_ref(lock);
3526 return 0;
3527 }
3528
802de3da
NTND
3529 files_reflog_path(refs, &log_file_sb, refname);
3530 log_file = strbuf_detach(&log_file_sb, NULL);
fcd2c3d9 3531 if (!cb.dry_run) {
7bd9bcf3
MH
3532 /*
3533 * Even though holding $GIT_DIR/logs/$reflog.lock has
3534 * no locking implications, we use the lock_file
3535 * machinery here anyway because it does a lot of the
3536 * work we need, including cleaning up if the program
3537 * exits unexpectedly.
3538 */
3539 if (hold_lock_file_for_update(&reflog_lock, log_file, 0) < 0) {
3540 struct strbuf err = STRBUF_INIT;
3541 unable_to_lock_message(log_file, errno, &err);
3542 error("%s", err.buf);
3543 strbuf_release(&err);
3544 goto failure;
3545 }
3546 cb.newlog = fdopen_lock_file(&reflog_lock, "w");
3547 if (!cb.newlog) {
3548 error("cannot fdopen %s (%s)",
3549 get_lock_file_path(&reflog_lock), strerror(errno));
3550 goto failure;
3551 }
3552 }
3553
0155f710 3554 (*prepare_fn)(refname, oid, cb.policy_cb);
2f40e954 3555 refs_for_each_reflog_ent(ref_store, refname, expire_reflog_ent, &cb);
7bd9bcf3
MH
3556 (*cleanup_fn)(cb.policy_cb);
3557
fcd2c3d9 3558 if (!cb.dry_run) {
7bd9bcf3
MH
3559 /*
3560 * It doesn't make sense to adjust a reference pointed
3561 * to by a symbolic ref based on expiring entries in
3562 * the symbolic reference's reflog. Nor can we update
3563 * a reference if there are no remaining reflog
3564 * entries.
3565 */
52106430
ÆAB
3566 int update = 0;
3567
fcd2c3d9 3568 if ((expire_flags & EXPIRE_REFLOGS_UPDATE_REF) &&
52106430 3569 !is_null_oid(&cb.last_kept_oid)) {
52106430
ÆAB
3570 int type;
3571 const char *ref;
3572
f1da24ca 3573 ref = refs_resolve_ref_unsafe(&refs->base, refname,
52106430 3574 RESOLVE_REF_NO_RECURSE,
ce14de03 3575 NULL, &type);
52106430
ÆAB
3576 update = !!(ref && !(type & REF_ISSYMREF));
3577 }
7bd9bcf3 3578
83a3069a 3579 if (close_lock_file_gently(&reflog_lock)) {
7bd9bcf3
MH
3580 status |= error("couldn't write %s: %s", log_file,
3581 strerror(errno));
83a3069a 3582 rollback_lock_file(&reflog_lock);
7bd9bcf3 3583 } else if (update &&
ee4d8e45 3584 (write_in_full(get_lock_file_fd(&lock->lk),
c1026b9d 3585 oid_to_hex(&cb.last_kept_oid), refs->base.repo->hash_algo->hexsz) < 0 ||
88780c37 3586 write_str_in_full(get_lock_file_fd(&lock->lk), "\n") < 0 ||
83a3069a 3587 close_ref_gently(lock) < 0)) {
7bd9bcf3 3588 status |= error("couldn't write %s",
ee4d8e45 3589 get_lock_file_path(&lock->lk));
7bd9bcf3
MH
3590 rollback_lock_file(&reflog_lock);
3591 } else if (commit_lock_file(&reflog_lock)) {
e0048d3e 3592 status |= error("unable to write reflog '%s' (%s)",
7bd9bcf3
MH
3593 log_file, strerror(errno));
3594 } else if (update && commit_ref(lock)) {
3595 status |= error("couldn't set %s", lock->ref_name);
3596 }
3597 }
3598 free(log_file);
3599 unlock_ref(lock);
3600 return status;
3601
3602 failure:
3603 rollback_lock_file(&reflog_lock);
3604 free(log_file);
3605 unlock_ref(lock);
3606 return -1;
3607}
3dce444f 3608
ed93ea16
PS
3609static int files_ref_store_create_on_disk(struct ref_store *ref_store,
3610 int flags,
3611 struct strbuf *err UNUSED)
6fb5acfd 3612{
19e02f4f 3613 struct files_ref_store *refs =
ed93ea16 3614 files_downcast(ref_store, REF_STORE_WRITE, "create");
e9dcc305
NTND
3615 struct strbuf sb = STRBUF_INIT;
3616
6fb5acfd 3617 /*
c358d165
PS
3618 * We need to create a "refs" dir in any case so that older versions of
3619 * Git can tell that this is a repository. This serves two main purposes:
3620 *
3621 * - Clients will know to stop walking the parent-directory chain when
3622 * detecting the Git repository. Otherwise they may end up detecting
3623 * a Git repository in a parent directory instead.
3624 *
3625 * - Instead of failing to detect a repository with unknown reference
3626 * format altogether, old clients will print an error saying that
3627 * they do not understand the reference format extension.
6fb5acfd 3628 */
c358d165 3629 strbuf_addf(&sb, "%s/refs", ref_store->gitdir);
028f6186
PS
3630 safe_create_dir(the_repository, sb.buf, 1);
3631 adjust_shared_perm(the_repository, sb.buf);
e9dcc305 3632
6fb5acfd 3633 /*
2eb1d0c4
PS
3634 * There is no need to create directories for common refs when creating
3635 * a worktree ref store.
6fb5acfd 3636 */
ed93ea16 3637 if (!(flags & REF_STORE_CREATE_ON_DISK_IS_WORKTREE)) {
2eb1d0c4
PS
3638 /*
3639 * Create .git/refs/{heads,tags}
3640 */
3641 strbuf_reset(&sb);
3642 files_ref_path(refs, &sb, "refs/heads");
028f6186 3643 safe_create_dir(the_repository, sb.buf, 1);
e9dcc305 3644
2eb1d0c4
PS
3645 strbuf_reset(&sb);
3646 files_ref_path(refs, &sb, "refs/tags");
028f6186 3647 safe_create_dir(the_repository, sb.buf, 1);
2eb1d0c4 3648 }
e9dcc305
NTND
3649
3650 strbuf_release(&sb);
6fb5acfd
DT
3651 return 0;
3652}
3653
64a6dd8f
PS
3654struct remove_one_root_ref_data {
3655 const char *gitdir;
3656 struct strbuf *err;
3657};
3658
3659static int remove_one_root_ref(const char *refname,
3660 void *cb_data)
3661{
3662 struct remove_one_root_ref_data *data = cb_data;
3663 struct strbuf buf = STRBUF_INIT;
3664 int ret = 0;
3665
3666 strbuf_addf(&buf, "%s/%s", data->gitdir, refname);
3667
3668 ret = unlink(buf.buf);
3669 if (ret < 0)
3670 strbuf_addf(data->err, "could not delete %s: %s\n",
3671 refname, strerror(errno));
3672
3673 strbuf_release(&buf);
3674 return ret;
3675}
3676
3677static int files_ref_store_remove_on_disk(struct ref_store *ref_store,
3678 struct strbuf *err)
3679{
3680 struct files_ref_store *refs =
3681 files_downcast(ref_store, REF_STORE_WRITE, "remove");
3682 struct remove_one_root_ref_data data = {
3683 .gitdir = refs->base.gitdir,
3684 .err = err,
3685 };
3686 struct strbuf sb = STRBUF_INIT;
3687 int ret = 0;
3688
3689 strbuf_addf(&sb, "%s/refs", refs->base.gitdir);
3690 if (remove_dir_recursively(&sb, 0) < 0) {
3691 strbuf_addf(err, "could not delete refs: %s",
3692 strerror(errno));
3693 ret = -1;
3694 }
3695 strbuf_reset(&sb);
3696
3697 strbuf_addf(&sb, "%s/logs", refs->base.gitdir);
3698 if (remove_dir_recursively(&sb, 0) < 0) {
3699 strbuf_addf(err, "could not delete logs: %s",
3700 strerror(errno));
3701 ret = -1;
3702 }
3703 strbuf_reset(&sb);
3704
3705 if (for_each_root_ref(refs, remove_one_root_ref, &data) < 0)
3706 ret = -1;
3707
3708 if (ref_store_remove_on_disk(refs->packed_ref_store, err) < 0)
3709 ret = -1;
3710
3711 strbuf_release(&sb);
3712 return ret;
3713}
3714
a7600b84 3715/*
3716 * For refs and reflogs, they share a unified interface when scanning
3717 * the whole directory. This function is used as the callback for each
3718 * regular file or symlink in the directory.
3719 */
3720typedef int (*files_fsck_refs_fn)(struct ref_store *ref_store,
3721 struct fsck_options *o,
56ca6039 3722 const char *refname,
a7600b84 3723 struct dir_iterator *iter);
3724
a6354e60 3725static int files_fsck_symref_target(struct fsck_options *o,
3726 struct fsck_ref_report *report,
c9f03f38 3727 struct strbuf *referent,
3728 unsigned int symbolic_link)
a6354e60 3729{
d996b447 3730 int is_referent_root;
a6354e60 3731 char orig_last_byte;
3732 size_t orig_len;
3733 int ret = 0;
3734
3735 orig_len = referent->len;
3736 orig_last_byte = referent->buf[orig_len - 1];
c9f03f38 3737 if (!symbolic_link)
3738 strbuf_rtrim(referent);
a6354e60 3739
d996b447 3740 is_referent_root = is_root_ref(referent->buf);
3741 if (!is_referent_root &&
3742 !starts_with(referent->buf, "refs/") &&
3743 !starts_with(referent->buf, "worktrees/")) {
3744 ret = fsck_report_ref(o, report,
3745 FSCK_MSG_SYMREF_TARGET_IS_NOT_A_REF,
3746 "points to non-ref target '%s'", referent->buf);
3747
3748 }
3749
3750 if (!is_referent_root && check_refname_format(referent->buf, 0)) {
a6354e60 3751 ret = fsck_report_ref(o, report,
3752 FSCK_MSG_BAD_REFERENT_NAME,
3753 "points to invalid refname '%s'", referent->buf);
3754 goto out;
3755 }
3756
c9f03f38 3757 if (symbolic_link)
3758 goto out;
3759
a6354e60 3760 if (referent->len == orig_len ||
3761 (referent->len < orig_len && orig_last_byte != '\n')) {
3762 ret = fsck_report_ref(o, report,
3763 FSCK_MSG_REF_MISSING_NEWLINE,
3764 "misses LF at the end");
3765 }
3766
3767 if (referent->len != orig_len && referent->len != orig_len - 1) {
3768 ret = fsck_report_ref(o, report,
3769 FSCK_MSG_TRAILING_REF_CONTENT,
3770 "has trailing whitespaces or newlines");
3771 }
3772
3773out:
3774 return ret;
3775}
3776
824aa541 3777static int files_fsck_refs_content(struct ref_store *ref_store,
3778 struct fsck_options *o,
3779 const char *target_name,
3780 struct dir_iterator *iter)
3781{
3782 struct strbuf ref_content = STRBUF_INIT;
c9f03f38 3783 struct strbuf abs_gitdir = STRBUF_INIT;
824aa541 3784 struct strbuf referent = STRBUF_INIT;
3785 struct fsck_ref_report report = { 0 };
1c0e2a00 3786 const char *trailing = NULL;
824aa541 3787 unsigned int type = 0;
3788 int failure_errno = 0;
3789 struct object_id oid;
3790 int ret = 0;
3791
3792 report.path = target_name;
3793
c9f03f38 3794 if (S_ISLNK(iter->st.st_mode)) {
3795 const char *relative_referent_path = NULL;
3796
3797 ret = fsck_report_ref(o, &report,
3798 FSCK_MSG_SYMLINK_REF,
3799 "use deprecated symbolic link for symref");
3800
3801 strbuf_add_absolute_path(&abs_gitdir, ref_store->repo->gitdir);
3802 strbuf_normalize_path(&abs_gitdir);
3803 if (!is_dir_sep(abs_gitdir.buf[abs_gitdir.len - 1]))
3804 strbuf_addch(&abs_gitdir, '/');
3805
3806 strbuf_add_real_path(&ref_content, iter->path.buf);
3807 skip_prefix(ref_content.buf, abs_gitdir.buf,
3808 &relative_referent_path);
3809
3810 if (relative_referent_path)
3811 strbuf_addstr(&referent, relative_referent_path);
3812 else
3813 strbuf_addbuf(&referent, &ref_content);
3814
3815 ret |= files_fsck_symref_target(o, &report, &referent, 1);
824aa541 3816 goto cleanup;
c9f03f38 3817 }
824aa541 3818
3819 if (strbuf_read_file(&ref_content, iter->path.buf, 0) < 0) {
3820 /*
3821 * Ref file could be removed by another concurrent process. We should
3822 * ignore this error and continue to the next ref.
3823 */
3824 if (errno == ENOENT)
3825 goto cleanup;
3826
3827 ret = error_errno(_("cannot read ref file '%s'"), iter->path.buf);
3828 goto cleanup;
3829 }
3830
3831 if (parse_loose_ref_contents(ref_store->repo->hash_algo,
3832 ref_content.buf, &oid, &referent,
1c0e2a00 3833 &type, &trailing, &failure_errno)) {
824aa541 3834 strbuf_rtrim(&ref_content);
3835 ret = fsck_report_ref(o, &report,
3836 FSCK_MSG_BAD_REF_CONTENT,
3837 "%s", ref_content.buf);
3838 goto cleanup;
3839 }
3840
1c0e2a00 3841 if (!(type & REF_ISSYMREF)) {
3842 if (!*trailing) {
3843 ret = fsck_report_ref(o, &report,
3844 FSCK_MSG_REF_MISSING_NEWLINE,
3845 "misses LF at the end");
3846 goto cleanup;
3847 }
3848 if (*trailing != '\n' || *(trailing + 1)) {
3849 ret = fsck_report_ref(o, &report,
3850 FSCK_MSG_TRAILING_REF_CONTENT,
3851 "has trailing garbage: '%s'", trailing);
3852 goto cleanup;
3853 }
a6354e60 3854 } else {
c9f03f38 3855 ret = files_fsck_symref_target(o, &report, &referent, 0);
a6354e60 3856 goto cleanup;
1c0e2a00 3857 }
3858
824aa541 3859cleanup:
3860 strbuf_release(&ref_content);
3861 strbuf_release(&referent);
c9f03f38 3862 strbuf_release(&abs_gitdir);
824aa541 3863 return ret;
3864}
3865
1c31be45 3866static int files_fsck_refs_name(struct ref_store *ref_store UNUSED,
3867 struct fsck_options *o,
56ca6039 3868 const char *refname,
1c31be45 3869 struct dir_iterator *iter)
3870{
3871 struct strbuf sb = STRBUF_INIT;
3872 int ret = 0;
3873
3874 /*
3875 * Ignore the files ending with ".lock" as they may be lock files
3876 * However, do not allow bare ".lock" files.
3877 */
3878 if (iter->basename[0] != '.' && ends_with(iter->basename, ".lock"))
3879 goto cleanup;
3880
32dc1c7e 3881 /*
3882 * This works right now because we never check the root refs.
3883 */
56ca6039 3884 if (check_refname_format(refname, 0)) {
38cd6eea 3885 struct fsck_ref_report report = { 0 };
1c31be45 3886
56ca6039 3887 report.path = refname;
1c31be45 3888 ret = fsck_report_ref(o, &report,
3889 FSCK_MSG_BAD_REF_NAME,
3890 "invalid refname format");
3891 }
3892
3893cleanup:
3894 strbuf_release(&sb);
3895 return ret;
3896}
3897
a7600b84 3898static int files_fsck_refs_dir(struct ref_store *ref_store,
3899 struct fsck_options *o,
3900 const char *refs_check_dir,
7c78d819 3901 struct worktree *wt,
a7600b84 3902 files_fsck_refs_fn *fsck_refs_fn)
3903{
56ca6039 3904 struct strbuf refname = STRBUF_INIT;
a7600b84 3905 struct strbuf sb = STRBUF_INIT;
3906 struct dir_iterator *iter;
3907 int iter_status;
3908 int ret = 0;
3909
3910 strbuf_addf(&sb, "%s/%s", ref_store->gitdir, refs_check_dir);
3911
3912 iter = dir_iterator_begin(sb.buf, 0);
3913 if (!iter) {
d5b3c38b 3914 if (errno == ENOENT && !is_main_worktree(wt))
3915 goto out;
3916
a7600b84 3917 ret = error_errno(_("cannot open directory %s"), sb.buf);
3918 goto out;
3919 }
3920
3921 while ((iter_status = dir_iterator_advance(iter)) == ITER_OK) {
3922 if (S_ISDIR(iter->st.st_mode)) {
3923 continue;
3924 } else if (S_ISREG(iter->st.st_mode) ||
3925 S_ISLNK(iter->st.st_mode)) {
56ca6039 3926 strbuf_reset(&refname);
7c78d819 3927
3928 if (!is_main_worktree(wt))
3929 strbuf_addf(&refname, "worktrees/%s/", wt->id);
56ca6039 3930 strbuf_addf(&refname, "%s/%s", refs_check_dir,
3931 iter->relative_path);
3932
a7600b84 3933 if (o->verbose)
56ca6039 3934 fprintf_ln(stderr, "Checking %s", refname.buf);
3935
a7600b84 3936 for (size_t i = 0; fsck_refs_fn[i]; i++) {
56ca6039 3937 if (fsck_refs_fn[i](ref_store, o, refname.buf, iter))
a7600b84 3938 ret = -1;
3939 }
3940 } else {
3941 struct fsck_ref_report report = { .path = iter->basename };
3942 if (fsck_report_ref(o, &report,
3943 FSCK_MSG_BAD_REF_FILETYPE,
3944 "unexpected file type"))
3945 ret = -1;
3946 }
3947 }
3948
3949 if (iter_status != ITER_DONE)
3950 ret = error(_("failed to iterate over '%s'"), sb.buf);
3951
3952out:
cec2b6f5 3953 dir_iterator_free(iter);
a7600b84 3954 strbuf_release(&sb);
56ca6039 3955 strbuf_release(&refname);
a7600b84 3956 return ret;
3957}
3958
3959static int files_fsck_refs(struct ref_store *ref_store,
7c78d819 3960 struct fsck_options *o,
3961 struct worktree *wt)
a7600b84 3962{
3963 files_fsck_refs_fn fsck_refs_fn[]= {
1c31be45 3964 files_fsck_refs_name,
824aa541 3965 files_fsck_refs_content,
a7600b84 3966 NULL,
3967 };
3968
7c78d819 3969 return files_fsck_refs_dir(ref_store, o, "refs", wt, fsck_refs_fn);
a7600b84 3970}
3971
ab6f79d8 3972static int files_fsck(struct ref_store *ref_store,
7c78d819 3973 struct fsck_options *o,
3974 struct worktree *wt)
ab6f79d8 3975{
3976 struct files_ref_store *refs =
3977 files_downcast(ref_store, REF_STORE_READ, "fsck");
3978
7c78d819 3979 return files_fsck_refs(ref_store, o, wt) |
3980 refs->packed_ref_store->be->fsck(refs->packed_ref_store, o, wt);
ab6f79d8 3981}
3982
3dce444f 3983struct ref_storage_be refs_be_files = {
32bff617 3984 .name = "files",
1febabff 3985 .init = files_ref_store_init,
71c871b4 3986 .release = files_ref_store_release,
ed93ea16 3987 .create_on_disk = files_ref_store_create_on_disk,
64a6dd8f 3988 .remove_on_disk = files_ref_store_remove_on_disk,
71c871b4 3989
32bff617
ÆAB
3990 .transaction_prepare = files_transaction_prepare,
3991 .transaction_finish = files_transaction_finish,
3992 .transaction_abort = files_transaction_abort,
32bff617 3993
1fd60671 3994 .optimize = files_optimize,
f6c5ca38 3995 .optimize_required = files_optimize_required,
32bff617
ÆAB
3996 .rename_ref = files_rename_ref,
3997 .copy_ref = files_copy_ref,
3998
3999 .iterator_begin = files_ref_iterator_begin,
4000 .read_raw_ref = files_read_raw_ref,
4001 .read_symbolic_ref = files_read_symbolic_ref,
4002
4003 .reflog_iterator_begin = files_reflog_iterator_begin,
4004 .for_each_reflog_ent = files_for_each_reflog_ent,
4005 .for_each_reflog_ent_reverse = files_for_each_reflog_ent_reverse,
4006 .reflog_exists = files_reflog_exists,
4007 .create_reflog = files_create_reflog,
4008 .delete_reflog = files_delete_reflog,
ab6f79d8 4009 .reflog_expire = files_reflog_expire,
4010
4011 .fsck = files_fsck,
3dce444f 4012};