Commit cc2447da32a20f5d089125839555ed0fe2a05592

Edward Thomson 2013-11-06T18:41:08

Add git_packbuilder_hash to query pack filename

diff --git a/include/git2/pack.h b/include/git2/pack.h
index 7488176..4632699 100644
--- a/include/git2/pack.h
+++ b/include/git2/pack.h
@@ -130,6 +130,16 @@ GIT_EXTERN(int) git_packbuilder_write(
 	git_transfer_progress_callback progress_cb,
 	void *progress_cb_payload);
 
+/**
+* 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
+*/
+GIT_EXTERN(const git_oid *) git_packbuilder_hash(git_packbuilder *pb);
+
 typedef int (*git_packbuilder_foreach_cb)(void *buf, size_t size, void *payload);
 /**
  * Create the new pack and pass each object to the callback
diff --git a/src/pack-objects.c b/src/pack-objects.c
index 9ea7c65..91811b9 100644
--- a/src/pack-objects.c
+++ b/src/pack-objects.c
@@ -560,6 +560,7 @@ static int write_pack(git_packbuilder *pb,
 	git_buf buf = GIT_BUF_INIT;
 	enum write_one_status status;
 	struct git_pack_header ph;
+	git_oid entry_oid;
 	unsigned int i = 0;
 	int error = 0;
 
@@ -596,10 +597,10 @@ static int write_pack(git_packbuilder *pb,
 	} while (pb->nr_remaining && i < pb->nr_objects);
 
 
-	if ((error = git_hash_final(&pb->pack_oid, &pb->ctx)) < 0)
+	if ((error = git_hash_final(&entry_oid, &pb->ctx)) < 0)
 		goto done;
 
-	error = cb(pb->pack_oid.id, GIT_OID_RAWSZ, data);
+	error = cb(entry_oid.id, GIT_OID_RAWSZ, data);
 
 done:
 	git__free(write_order);
@@ -1269,12 +1270,19 @@ int git_packbuilder_write(
 		return -1;
 	}
 
+	git_oid_cpy(&pb->pack_oid, git_indexer_hash(indexer));
+
 	git_indexer_free(indexer);
 	return 0;
 }
 
 #undef PREPARE_PACK
 
+const git_oid *git_packbuilder_hash(git_packbuilder *pb)
+{
+	return &pb->pack_oid;
+}
+
 static int cb_tree_walk(const char *root, const git_tree_entry *entry, void *payload)
 {
 	struct tree_walk_context *ctx = payload;
diff --git a/tests-clar/pack/packbuilder.c b/tests-clar/pack/packbuilder.c
index ac6f731..dd028a1 100644
--- a/tests-clar/pack/packbuilder.c
+++ b/tests-clar/pack/packbuilder.c
@@ -128,6 +128,18 @@ void test_pack_packbuilder__create_pack(void)
 	cl_assert_equal_s(hex, "5d410bdf97cf896f9007681b92868471d636954b");
 }
 
+void test_pack_packbuilder__get_hash(void)
+{
+	char hex[41]; hex[40] = '\0';
+
+	seed_packbuilder();
+
+	git_packbuilder_write(_packbuilder, ".", NULL, NULL);
+	git_oid_fmt(hex, git_packbuilder_hash(_packbuilder));
+
+	cl_assert_equal_s(hex, "80e61eb315239ef3c53033e37fee43b744d57122");
+}
+
 static git_transfer_progress stats;
 static int foreach_cb(void *buf, size_t len, void *payload)
 {