Skip to content

Commit 6f960b5

Browse files
authored
Merge pull request libgit2#4088 from chescock/packfile-name-using-complete-hash
Ensure packfiles with different contents have different names
2 parents d2c4f76 + c0e5415 commit 6f960b5

File tree

3 files changed

+10
-14
lines changed

3 files changed

+10
-14
lines changed

src/indexer.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,6 @@ int git_indexer_commit(git_indexer *idx, git_transfer_progress *stats)
937937
git_buf filename = GIT_BUF_INIT;
938938
struct entry *entry;
939939
git_oid trailer_hash, file_hash;
940-
git_hash_ctx ctx;
941940
git_filebuf index_file = {0};
942941
void *packfile_trailer;
943942

@@ -946,9 +945,6 @@ int git_indexer_commit(git_indexer *idx, git_transfer_progress *stats)
946945
return -1;
947946
}
948947

949-
if (git_hash_ctx_init(&ctx) < 0)
950-
return -1;
951-
952948
/* Test for this before resolve_deltas(), as it plays with idx->off */
953949
if (idx->off + 20 < idx->pack->mwf.size) {
954950
giterr_set(GITERR_INDEXER, "unexpected data at the end of the pack");
@@ -992,6 +988,10 @@ int git_indexer_commit(git_indexer *idx, git_transfer_progress *stats)
992988

993989
git_vector_sort(&idx->objects);
994990

991+
/* Use the trailer hash as the pack file name to ensure
992+
* files with different contents have different names */
993+
git_oid_cpy(&idx->hash, &trailer_hash);
994+
995995
git_buf_sets(&filename, idx->pack->pack_name);
996996
git_buf_shorten(&filename, strlen("pack"));
997997
git_buf_puts(&filename, "idx");
@@ -1018,9 +1018,7 @@ int git_indexer_commit(git_indexer *idx, git_transfer_progress *stats)
10181018
/* Write out the object names (SHA-1 hashes) */
10191019
git_vector_foreach(&idx->objects, i, entry) {
10201020
git_filebuf_write(&index_file, &entry->oid, sizeof(git_oid));
1021-
git_hash_update(&ctx, &entry->oid, GIT_OID_RAWSZ);
10221021
}
1023-
git_hash_final(&idx->hash, &ctx);
10241022

10251023
/* Write out the CRC32 values */
10261024
git_vector_foreach(&idx->objects, i, entry) {
@@ -1106,14 +1104,12 @@ int git_indexer_commit(git_indexer *idx, git_transfer_progress *stats)
11061104
idx->pack_committed = 1;
11071105

11081106
git_buf_free(&filename);
1109-
git_hash_ctx_cleanup(&ctx);
11101107
return 0;
11111108

11121109
on_error:
11131110
git_mwindow_free_all(&idx->pack->mwf);
11141111
git_filebuf_cleanup(&index_file);
11151112
git_buf_free(&filename);
1116-
git_hash_ctx_cleanup(&ctx);
11171113
return -1;
11181114
}
11191115

tests/pack/indexer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ void test_pack_indexer__fix_thin(void)
8585
cl_assert_equal_i(stats.indexed_objects, 2);
8686
cl_assert_equal_i(stats.local_objects, 1);
8787

88-
git_oid_fromstr(&should_id, "11f0f69b334728fdd8bc86b80499f22f29d85b15");
88+
git_oid_fromstr(&should_id, "fefdb2d740a3a6b6c03a0c7d6ce431c6d5810e13");
8989
cl_assert_equal_oid(&should_id, git_indexer_hash(idx));
9090

9191
git_indexer_free(idx);
@@ -102,7 +102,7 @@ void test_pack_indexer__fix_thin(void)
102102
int fd;
103103
ssize_t read;
104104
struct stat st;
105-
const char *name = "pack-11f0f69b334728fdd8bc86b80499f22f29d85b15.pack";
105+
const char *name = "pack-fefdb2d740a3a6b6c03a0c7d6ce431c6d5810e13.pack";
106106

107107
fd = p_open(name, O_RDONLY);
108108
cl_assert(fd != -1);

tests/pack/packbuilder.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ void test_pack_packbuilder__create_pack(void)
116116
* $ cd tests/resources/testrepo.git
117117
* $ git rev-list --objects HEAD | \
118118
* git pack-objects -q --no-reuse-delta --threads=1 pack
119-
* $ sha1sum git-80e61eb315239ef3c53033e37fee43b744d57122.pack
119+
* $ sha1sum pack-7f5fa362c664d68ba7221259be1cbd187434b2f0.pack
120120
* 5d410bdf97cf896f9007681b92868471d636954b
121121
*
122122
*/
@@ -145,7 +145,7 @@ void test_pack_packbuilder__get_hash(void)
145145
git_packbuilder_write(_packbuilder, ".", 0, NULL, NULL);
146146
git_oid_fmt(hex, git_packbuilder_hash(_packbuilder));
147147

148-
cl_assert_equal_s(hex, "80e61eb315239ef3c53033e37fee43b744d57122");
148+
cl_assert_equal_s(hex, "7f5fa362c664d68ba7221259be1cbd187434b2f0");
149149
}
150150

151151
static void test_write_pack_permission(mode_t given, mode_t expected)
@@ -169,10 +169,10 @@ static void test_write_pack_permission(mode_t given, mode_t expected)
169169
mask = p_umask(0);
170170
p_umask(mask);
171171

172-
cl_git_pass(p_stat("pack-80e61eb315239ef3c53033e37fee43b744d57122.idx", &statbuf));
172+
cl_git_pass(p_stat("pack-7f5fa362c664d68ba7221259be1cbd187434b2f0.idx", &statbuf));
173173
cl_assert_equal_i(statbuf.st_mode & os_mask, (expected & ~mask) & os_mask);
174174

175-
cl_git_pass(p_stat("pack-80e61eb315239ef3c53033e37fee43b744d57122.pack", &statbuf));
175+
cl_git_pass(p_stat("pack-7f5fa362c664d68ba7221259be1cbd187434b2f0.pack", &statbuf));
176176
cl_assert_equal_i(statbuf.st_mode & os_mask, (expected & ~mask) & os_mask);
177177
}
178178

0 commit comments

Comments
 (0)