Skip to content

Commit 14d63bb

Browse files
mhaggerGit for Windows Build Agent
authored andcommitted
packed_ref_store: handle a packed-refs file that is a symlink
One of the tricks that `contrib/workdir/git-new-workdir` plays is to making `packed-refs` in the new workdir a symlink to the `packed-refs` file in the original repository. Before 42dfa7e ("commit_packed_refs(): use a staging file separate from the lockfile", 2017-06-23), a lockfile was used as the staging file, and because the `LOCK_NO_DEREF` was not used, the pointed-to file was locked and modified. But after that commit, the staging file was created using a tempfile, with the end result that rewriting the `packed-refs` file in the workdir overwrote the symlink rather than the original `packed-refs` file. Change `commit_packed_refs()` to use `get_locked_file_path()` to find the path of the file that it should overwrite. Since that path was properly resolved when the lockfile was created, this restores the pre-42dfa7ecef behavior. Also add a test case to document this use case and prevent a regression like this from recurring. Signed-off-by: Michael Haggerty <[email protected]> Reviewed-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d8ff9f6 commit 14d63bb

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

refs/packed-backend.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -611,19 +611,27 @@ int commit_packed_refs(struct ref_store *ref_store, struct strbuf *err)
611611
struct packed_ref_cache *packed_ref_cache =
612612
get_packed_ref_cache(refs);
613613
int ok;
614+
int ret = -1;
614615
struct strbuf sb = STRBUF_INIT;
615616
FILE *out;
616617
struct ref_iterator *iter;
618+
char *packed_refs_path;
617619

618620
if (!is_lock_file_locked(&refs->lock))
619621
die("BUG: commit_packed_refs() called when unlocked");
620622

621-
strbuf_addf(&sb, "%s.new", refs->path);
623+
/*
624+
* If packed-refs is a symlink, we want to overwrite the
625+
* symlinked-to file, not the symlink itself. Also, put the
626+
* staging file next to it:
627+
*/
628+
packed_refs_path = get_locked_file_path(&refs->lock);
629+
strbuf_addf(&sb, "%s.new", packed_refs_path);
622630
if (create_tempfile(&refs->tempfile, sb.buf) < 0) {
623631
strbuf_addf(err, "unable to create file %s: %s",
624632
sb.buf, strerror(errno));
625633
strbuf_release(&sb);
626-
return -1;
634+
goto out;
627635
}
628636
strbuf_release(&sb);
629637

@@ -661,17 +669,21 @@ int commit_packed_refs(struct ref_store *ref_store, struct strbuf *err)
661669
goto error;
662670
}
663671

664-
if (rename_tempfile(&refs->tempfile, refs->path)) {
672+
if (rename_tempfile(&refs->tempfile, packed_refs_path)) {
665673
strbuf_addf(err, "error replacing %s: %s",
666674
refs->path, strerror(errno));
667-
return -1;
675+
goto out;
668676
}
669677

670-
return 0;
678+
ret = 0;
679+
goto out;
671680

672681
error:
673682
delete_tempfile(&refs->tempfile);
674-
return -1;
683+
684+
out:
685+
free(packed_refs_path);
686+
return ret;
675687
}
676688

677689
/*

t/t3210-pack-refs.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,4 +238,19 @@ test_expect_success 'retry acquiring packed-refs.lock' '
238238
git -c core.packedrefstimeout=3000 pack-refs --all --prune
239239
'
240240

241+
test_expect_success SYMLINKS 'pack symlinked packed-refs' '
242+
# First make sure that symlinking works when reading:
243+
git update-ref refs/heads/loosy refs/heads/master &&
244+
git for-each-ref >all-refs-before &&
245+
mv .git/packed-refs .git/my-deviant-packed-refs &&
246+
ln -s my-deviant-packed-refs .git/packed-refs &&
247+
git for-each-ref >all-refs-linked &&
248+
test_cmp all-refs-before all-refs-linked &&
249+
git pack-refs --all --prune &&
250+
git for-each-ref >all-refs-packed &&
251+
test_cmp all-refs-before all-refs-packed &&
252+
test -h .git/packed-refs &&
253+
test "$(readlink .git/packed-refs)" = "my-deviant-packed-refs"
254+
'
255+
241256
test_done

0 commit comments

Comments
 (0)