Commit 70d9bfa47c14fd230d081cebb9933622136479ea

Edward Thomson 2022-01-22T17:34:36

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.

diff --git a/fuzzers/packfile_fuzzer.c b/fuzzers/packfile_fuzzer.c
index 8667cb9..6002fa1 100644
--- a/fuzzers/packfile_fuzzer.c
+++ b/fuzzers/packfile_fuzzer.c
@@ -101,13 +101,13 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
 	if (git_indexer_commit(indexer, &stats) < 0)
 		goto cleanup;
 
-	if (git_str_printf(&path, "pack-%s.idx", git_oid_tostr_s(git_indexer_hash(indexer))) < 0)
+	if (git_str_printf(&path, "pack-%s.idx", git_indexer_name(indexer)) < 0)
 		goto cleanup;
 	p_unlink(git_str_cstr(&path));
 
 	git_str_clear(&path);
 
-	if (git_str_printf(&path, "pack-%s.pack", git_oid_tostr_s(git_indexer_hash(indexer))) < 0)
+	if (git_str_printf(&path, "pack-%s.pack", git_indexer_name(indexer)) < 0)
 		goto cleanup;
 	p_unlink(git_str_cstr(&path));
 
diff --git a/include/git2/pack.h b/include/git2/pack.h
index 253a860..0f6bd2a 100644
--- a/include/git2/pack.h
+++ b/include/git2/pack.h
@@ -170,16 +170,30 @@ GIT_EXTERN(int) git_packbuilder_write(
 	git_indexer_progress_cb progress_cb,
 	void *progress_cb_payload);
 
+#ifndef GIT_DEPRECATE_HARD
 /**
-* Get the packfile's hash
-*
-* A packfile's name is derived from the sorted hashing of all object
-* names. This is only correct after the packfile has been written.
-*
-* @param pb The packbuilder object
+ * Get the packfile's hash
+ *
+ * A packfile's name is derived from the sorted hashing of all object
+ * names. This is only correct after the packfile has been written.
+ *
+ * @deprecated use git_packbuilder_name
+ * @param pb The packbuilder object
  * @return 0 or an error code
-*/
+ */
 GIT_EXTERN(const git_oid *) git_packbuilder_hash(git_packbuilder *pb);
+#endif
+
+/**
+ * Get the unique name for the resulting packfile.
+ *
+ * The packfile's name is derived from the packfile's content.
+ * This is only correct after the packfile has been written.
+ *
+ * @param pb the packbuilder instance
+ * @return a NUL terminated string for the packfile name
+ */
+GIT_EXTERN(const char *) git_packbuilder_name(git_packbuilder *pb);
 
 /**
  * Callback used to iterate over packed objects
diff --git a/src/pack-objects.c b/src/pack-objects.c
index e5fc625..35c1d55 100644
--- a/src/pack-objects.c
+++ b/src/pack-objects.c
@@ -1422,7 +1422,12 @@ int git_packbuilder_write(
 	if ((error = git_indexer_commit(indexer, &stats)) < 0)
 		goto cleanup;
 
+#ifndef GIT_DEPRECATE_HARD
 	git_oid_cpy(&pb->pack_oid, git_indexer_hash(indexer));
+#endif
+
+	pb->pack_name = git__strdup(git_indexer_name(indexer));
+	GIT_ERROR_CHECK_ALLOC(pb->pack_name);
 
 cleanup:
 	git_indexer_free(indexer);
@@ -1432,10 +1437,17 @@ cleanup:
 
 #undef PREPARE_PACK
 
+#ifndef GIT_DEPRECATE_HARD
 const git_oid *git_packbuilder_hash(git_packbuilder *pb)
 {
 	return &pb->pack_oid;
 }
+#endif
+
+const char *git_packbuilder_name(git_packbuilder *pb)
+{
+	return pb->pack_name;
+}
 
 
 static int cb_tree_walk(
@@ -1803,5 +1815,7 @@ void git_packbuilder_free(git_packbuilder *pb)
 	git_hash_ctx_cleanup(&pb->ctx);
 	git_zstream_free(&pb->zstream);
 
+	git__free(pb->pack_name);
+
 	git__free(pb);
 }
diff --git a/src/pack-objects.h b/src/pack-objects.h
index cefee40..53c9f5d 100644
--- a/src/pack-objects.h
+++ b/src/pack-objects.h
@@ -73,7 +73,10 @@ struct git_packbuilder {
 	git_oidmap *walk_objects;
 	git_pool object_pool;
 
+#ifndef GIT_DEPRECATE_HARD
 	git_oid pack_oid; /* hash of written pack */
+#endif
+	char *pack_name; /* name of written pack */
 
 	/* synchronization objects */
 	git_mutex cache_mutex;
diff --git a/tests/checkout/crlf.c b/tests/checkout/crlf.c
index 8fd16a0..21f8a85 100644
--- a/tests/checkout/crlf.c
+++ b/tests/checkout/crlf.c
@@ -239,10 +239,10 @@ void test_checkout_crlf__autocrlf_false_index_size_is_unfiltered_size(void)
 
 	cl_repo_set_bool(g_repo, "core.autocrlf", false);
 
-	git_repository_index(&index, g_repo);
+	cl_git_pass(git_repository_index(&index, g_repo));
 	tick_index(index);
 
-	git_checkout_head(g_repo, &opts);
+	cl_git_pass(git_checkout_head(g_repo, &opts));
 
 	cl_assert((entry = git_index_get_bypath(index, "all-lf", 0)) != NULL);
 	cl_assert(entry->file_size == strlen(ALL_LF_TEXT_RAW));
diff --git a/tests/pack/packbuilder.c b/tests/pack/packbuilder.c
index 73bc667..f235798 100644
--- a/tests/pack/packbuilder.c
+++ b/tests/pack/packbuilder.c
@@ -138,16 +138,12 @@ void test_pack_packbuilder__create_pack(void)
 	cl_assert_equal_s(hex, "5d410bdf97cf896f9007681b92868471d636954b");
 }
 
-void test_pack_packbuilder__get_hash(void)
+void test_pack_packbuilder__get_name(void)
 {
-	char hex[GIT_OID_HEXSZ+1]; hex[GIT_OID_HEXSZ] = '\0';
-
 	seed_packbuilder();
 
 	cl_git_pass(git_packbuilder_write(_packbuilder, ".", 0, NULL, NULL));
-	git_oid_fmt(hex, git_packbuilder_hash(_packbuilder));
-
-	cl_assert_equal_s(hex, "7f5fa362c664d68ba7221259be1cbd187434b2f0");
+	cl_assert_equal_s("7f5fa362c664d68ba7221259be1cbd187434b2f0", git_packbuilder_name(_packbuilder));
 }
 
 void test_pack_packbuilder__write_default_path(void)