diff options
| author | Junio C Hamano <gitster@pobox.com> | 2024-09-12 11:47:22 -0700 |
|---|---|---|
| committer | Junio C Hamano <gitster@pobox.com> | 2024-09-12 11:47:22 -0700 |
| commit | 63b5fcdde90361195f7f6ade199c90d91e2ecf53 (patch) | |
| tree | bb98b7f26f1ddce25032e33e57c65ca8a0fc28c8 | |
| parent | 3265304f944ed95dfa94d2e834bdd615e4cfb0f0 (diff) | |
| parent | b2dbf97f47870dd71eab319a55b362102d65c209 (diff) | |
Merge branch 'ps/index-pack-outside-repo-fix'
"git verify-pack" and "git index-pack" started dying outside a
repository, which has been corrected.
* ps/index-pack-outside-repo-fix:
builtin/index-pack: fix segfaults when running outside of a repo
| -rw-r--r-- | builtin/index-pack.c | 9 | ||||
| -rwxr-xr-x | t/t5300-pack-object.sh | 39 |
2 files changed, 48 insertions, 0 deletions
diff --git a/builtin/index-pack.c b/builtin/index-pack.c index fd968d673d..763b01372a 100644 --- a/builtin/index-pack.c +++ b/builtin/index-pack.c @@ -1868,6 +1868,15 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix) if (!index_name && pack_name) index_name = derive_filename(pack_name, "pack", "idx", &index_name_buf); + /* + * Packfiles and indices do not carry enough information to be able to + * identify their object hash. So when we are neither in a repository + * nor has the user told us which object hash to use we have no other + * choice but to guess the object hash. + */ + if (!the_repository->hash_algo) + repo_set_hash_algo(the_repository, GIT_HASH_SHA1); + opts.flags &= ~(WRITE_REV | WRITE_REV_VERIFY); if (rev_index) { opts.flags |= verify ? WRITE_REV_VERIFY : WRITE_REV; diff --git a/t/t5300-pack-object.sh b/t/t5300-pack-object.sh index 4ad023c846..3b9dae331a 100755 --- a/t/t5300-pack-object.sh +++ b/t/t5300-pack-object.sh @@ -635,4 +635,43 @@ test_expect_success 'negative window clamps to 0' ' check_deltas stderr = 0 ' +for hash in sha1 sha256 +do + test_expect_success "verify-pack with $hash packfile" ' + test_when_finished "rm -rf repo" && + git init --object-format=$hash repo && + test_commit -C repo initial && + git -C repo repack -ad && + git -C repo verify-pack "$(pwd)"/repo/.git/objects/pack/*.idx && + if test $hash = sha1 + then + nongit git verify-pack "$(pwd)"/repo/.git/objects/pack/*.idx + else + # We have no way to identify the hash used by packfiles + # or indices, so we always fall back to SHA1. + nongit test_must_fail git verify-pack "$(pwd)"/repo/.git/objects/pack/*.idx && + # But with an explicit object format we should succeed. + nongit git verify-pack --object-format=$hash "$(pwd)"/repo/.git/objects/pack/*.idx + fi + ' + + test_expect_success "index-pack outside of a $hash repository" ' + test_when_finished "rm -rf repo" && + git init --object-format=$hash repo && + test_commit -C repo initial && + git -C repo repack -ad && + git -C repo index-pack --verify "$(pwd)"/repo/.git/objects/pack/*.pack && + if test $hash = sha1 + then + nongit git index-pack --verify "$(pwd)"/repo/.git/objects/pack/*.pack + else + # We have no way to identify the hash used by packfiles + # or indices, so we always fall back to SHA1. + nongit test_must_fail git index-pack --verify "$(pwd)"/repo/.git/objects/pack/*.pack 2>err && + # But with an explicit object format we should succeed. + nongit git index-pack --object-format=$hash --verify "$(pwd)"/repo/.git/objects/pack/*.pack + fi + ' +done + test_done |
