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