indexer: name pack files after trailer hash

Upstream git.git has changed the way how packfiles are named.
Previously, they were using a hash of the contained object's OIDs, which
has then been changed to use the hash of the complete packfile instead.
See 1190a1acf (pack-objects: name pack files after trailer hash,
2013-12-05) in the git.git repository for more information on this
change.

This commit changes our logic to match the behavior of core git.
diff --git a/src/indexer.c b/src/indexer.c
index 606de2e..0ad7869 100644
--- a/src/indexer.c
+++ b/src/indexer.c
@@ -931,7 +931,6 @@
 	git_buf filename = GIT_BUF_INIT;
 	struct entry *entry;
 	git_oid trailer_hash, file_hash;
-	git_hash_ctx ctx;
 	git_filebuf index_file = {0};
 	void *packfile_trailer;
 
@@ -940,9 +939,6 @@
 		return -1;
 	}
 
-	if (git_hash_ctx_init(&ctx) < 0)
-		return -1;
-
 	/* Test for this before resolve_deltas(), as it plays with idx->off */
 	if (idx->off + 20 < idx->pack->mwf.size) {
 		giterr_set(GITERR_INDEXER, "unexpected data at the end of the pack");
@@ -986,6 +982,10 @@
 
 	git_vector_sort(&idx->objects);
 
+	/* Use the trailer hash as the pack file name to ensure
+	 * files with different contents have different names */
+	git_oid_cpy(&idx->hash, &trailer_hash);
+
 	git_buf_sets(&filename, idx->pack->pack_name);
 	git_buf_shorten(&filename, strlen("pack"));
 	git_buf_puts(&filename, "idx");
@@ -1010,9 +1010,7 @@
 	/* Write out the object names (SHA-1 hashes) */
 	git_vector_foreach(&idx->objects, i, entry) {
 		git_filebuf_write(&index_file, &entry->oid, sizeof(git_oid));
-		git_hash_update(&ctx, &entry->oid, GIT_OID_RAWSZ);
 	}
-	git_hash_final(&idx->hash, &ctx);
 
 	/* Write out the CRC32 values */
 	git_vector_foreach(&idx->objects, i, entry) {
@@ -1086,14 +1084,12 @@
 	idx->pack_committed = 1;
 
 	git_buf_free(&filename);
-	git_hash_ctx_cleanup(&ctx);
 	return 0;
 
 on_error:
 	git_mwindow_free_all(&idx->pack->mwf);
 	git_filebuf_cleanup(&index_file);
 	git_buf_free(&filename);
-	git_hash_ctx_cleanup(&ctx);
 	return -1;
 }
 
diff --git a/tests/pack/indexer.c b/tests/pack/indexer.c
index 1e514b2..c73d397 100644
--- a/tests/pack/indexer.c
+++ b/tests/pack/indexer.c
@@ -85,7 +85,7 @@
 	cl_assert_equal_i(stats.indexed_objects, 2);
 	cl_assert_equal_i(stats.local_objects, 1);
 
-	git_oid_fromstr(&should_id, "11f0f69b334728fdd8bc86b80499f22f29d85b15");
+	git_oid_fromstr(&should_id, "fefdb2d740a3a6b6c03a0c7d6ce431c6d5810e13");
 	cl_assert_equal_oid(&should_id, git_indexer_hash(idx));
 
 	git_indexer_free(idx);
@@ -102,7 +102,7 @@
 		int fd;
 		ssize_t read;
 		struct stat st;
-		const char *name = "pack-11f0f69b334728fdd8bc86b80499f22f29d85b15.pack";
+		const char *name = "pack-fefdb2d740a3a6b6c03a0c7d6ce431c6d5810e13.pack";
 
 		fd = p_open(name, O_RDONLY);
 		cl_assert(fd != -1);
diff --git a/tests/pack/packbuilder.c b/tests/pack/packbuilder.c
index 29f3e2d..9ad52f3 100644
--- a/tests/pack/packbuilder.c
+++ b/tests/pack/packbuilder.c
@@ -113,7 +113,7 @@
 	 * $ cd tests/resources/testrepo.git
 	 * $ git rev-list --objects HEAD | \
 	 * 	git pack-objects -q --no-reuse-delta --threads=1 pack
-	 * $ sha1sum git-80e61eb315239ef3c53033e37fee43b744d57122.pack
+	 * $ sha1sum pack-7f5fa362c664d68ba7221259be1cbd187434b2f0.pack
 	 * 5d410bdf97cf896f9007681b92868471d636954b
 	 *
 	 */
@@ -142,7 +142,7 @@
 	git_packbuilder_write(_packbuilder, ".", 0, NULL, NULL);
 	git_oid_fmt(hex, git_packbuilder_hash(_packbuilder));
 
-	cl_assert_equal_s(hex, "80e61eb315239ef3c53033e37fee43b744d57122");
+	cl_assert_equal_s(hex, "7f5fa362c664d68ba7221259be1cbd187434b2f0");
 }
 
 static void test_write_pack_permission(mode_t given, mode_t expected)
@@ -166,10 +166,10 @@
 	mask = p_umask(0);
 	p_umask(mask);
 
-	cl_git_pass(p_stat("pack-80e61eb315239ef3c53033e37fee43b744d57122.idx", &statbuf));
+	cl_git_pass(p_stat("pack-7f5fa362c664d68ba7221259be1cbd187434b2f0.idx", &statbuf));
 	cl_assert_equal_i(statbuf.st_mode & os_mask, (expected & ~mask) & os_mask);
 
-	cl_git_pass(p_stat("pack-80e61eb315239ef3c53033e37fee43b744d57122.pack", &statbuf));
+	cl_git_pass(p_stat("pack-7f5fa362c664d68ba7221259be1cbd187434b2f0.pack", &statbuf));
 	cl_assert_equal_i(statbuf.st_mode & os_mask, (expected & ~mask) & os_mask);
 }