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