Skip to content

Commit 70d9bfa

Browse files
committed
packbuilder: use the packfile name instead of hash
Deprecate the `git_packfile_hash` function. Callers should use the new `git_packfile_name` function which provides a unique packfile name.
1 parent d2458af commit 70d9bfa

File tree

6 files changed

+44
-17
lines changed

6 files changed

+44
-17
lines changed

fuzzers/packfile_fuzzer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
101101
if (git_indexer_commit(indexer, &stats) < 0)
102102
goto cleanup;
103103

104-
if (git_str_printf(&path, "pack-%s.idx", git_oid_tostr_s(git_indexer_hash(indexer))) < 0)
104+
if (git_str_printf(&path, "pack-%s.idx", git_indexer_name(indexer)) < 0)
105105
goto cleanup;
106106
p_unlink(git_str_cstr(&path));
107107

108108
git_str_clear(&path);
109109

110-
if (git_str_printf(&path, "pack-%s.pack", git_oid_tostr_s(git_indexer_hash(indexer))) < 0)
110+
if (git_str_printf(&path, "pack-%s.pack", git_indexer_name(indexer)) < 0)
111111
goto cleanup;
112112
p_unlink(git_str_cstr(&path));
113113

include/git2/pack.h

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,16 +170,30 @@ GIT_EXTERN(int) git_packbuilder_write(
170170
git_indexer_progress_cb progress_cb,
171171
void *progress_cb_payload);
172172

173+
#ifndef GIT_DEPRECATE_HARD
173174
/**
174-
* Get the packfile's hash
175-
*
176-
* A packfile's name is derived from the sorted hashing of all object
177-
* names. This is only correct after the packfile has been written.
178-
*
179-
* @param pb The packbuilder object
175+
* Get the packfile's hash
176+
*
177+
* A packfile's name is derived from the sorted hashing of all object
178+
* names. This is only correct after the packfile has been written.
179+
*
180+
* @deprecated use git_packbuilder_name
181+
* @param pb The packbuilder object
180182
* @return 0 or an error code
181-
*/
183+
*/
182184
GIT_EXTERN(const git_oid *) git_packbuilder_hash(git_packbuilder *pb);
185+
#endif
186+
187+
/**
188+
* Get the unique name for the resulting packfile.
189+
*
190+
* The packfile's name is derived from the packfile's content.
191+
* This is only correct after the packfile has been written.
192+
*
193+
* @param pb the packbuilder instance
194+
* @return a NUL terminated string for the packfile name
195+
*/
196+
GIT_EXTERN(const char *) git_packbuilder_name(git_packbuilder *pb);
183197

184198
/**
185199
* Callback used to iterate over packed objects

src/pack-objects.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,7 +1422,12 @@ int git_packbuilder_write(
14221422
if ((error = git_indexer_commit(indexer, &stats)) < 0)
14231423
goto cleanup;
14241424

1425+
#ifndef GIT_DEPRECATE_HARD
14251426
git_oid_cpy(&pb->pack_oid, git_indexer_hash(indexer));
1427+
#endif
1428+
1429+
pb->pack_name = git__strdup(git_indexer_name(indexer));
1430+
GIT_ERROR_CHECK_ALLOC(pb->pack_name);
14261431

14271432
cleanup:
14281433
git_indexer_free(indexer);
@@ -1432,10 +1437,17 @@ int git_packbuilder_write(
14321437

14331438
#undef PREPARE_PACK
14341439

1440+
#ifndef GIT_DEPRECATE_HARD
14351441
const git_oid *git_packbuilder_hash(git_packbuilder *pb)
14361442
{
14371443
return &pb->pack_oid;
14381444
}
1445+
#endif
1446+
1447+
const char *git_packbuilder_name(git_packbuilder *pb)
1448+
{
1449+
return pb->pack_name;
1450+
}
14391451

14401452

14411453
static int cb_tree_walk(
@@ -1803,5 +1815,7 @@ void git_packbuilder_free(git_packbuilder *pb)
18031815
git_hash_ctx_cleanup(&pb->ctx);
18041816
git_zstream_free(&pb->zstream);
18051817

1818+
git__free(pb->pack_name);
1819+
18061820
git__free(pb);
18071821
}

src/pack-objects.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ struct git_packbuilder {
7373
git_oidmap *walk_objects;
7474
git_pool object_pool;
7575

76+
#ifndef GIT_DEPRECATE_HARD
7677
git_oid pack_oid; /* hash of written pack */
78+
#endif
79+
char *pack_name; /* name of written pack */
7780

7881
/* synchronization objects */
7982
git_mutex cache_mutex;

tests/checkout/crlf.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,10 @@ void test_checkout_crlf__autocrlf_false_index_size_is_unfiltered_size(void)
239239

240240
cl_repo_set_bool(g_repo, "core.autocrlf", false);
241241

242-
git_repository_index(&index, g_repo);
242+
cl_git_pass(git_repository_index(&index, g_repo));
243243
tick_index(index);
244244

245-
git_checkout_head(g_repo, &opts);
245+
cl_git_pass(git_checkout_head(g_repo, &opts));
246246

247247
cl_assert((entry = git_index_get_bypath(index, "all-lf", 0)) != NULL);
248248
cl_assert(entry->file_size == strlen(ALL_LF_TEXT_RAW));

tests/pack/packbuilder.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,12 @@ void test_pack_packbuilder__create_pack(void)
138138
cl_assert_equal_s(hex, "5d410bdf97cf896f9007681b92868471d636954b");
139139
}
140140

141-
void test_pack_packbuilder__get_hash(void)
141+
void test_pack_packbuilder__get_name(void)
142142
{
143-
char hex[GIT_OID_HEXSZ+1]; hex[GIT_OID_HEXSZ] = '\0';
144-
145143
seed_packbuilder();
146144

147145
cl_git_pass(git_packbuilder_write(_packbuilder, ".", 0, NULL, NULL));
148-
git_oid_fmt(hex, git_packbuilder_hash(_packbuilder));
149-
150-
cl_assert_equal_s(hex, "7f5fa362c664d68ba7221259be1cbd187434b2f0");
146+
cl_assert_equal_s("7f5fa362c664d68ba7221259be1cbd187434b2f0", git_packbuilder_name(_packbuilder));
151147
}
152148

153149
void test_pack_packbuilder__write_default_path(void)

0 commit comments

Comments
 (0)