git_packbuilder_write: Allow setting path to NULL to use the default path If given a NULL path, write to the object path of the repository. Add tests for the new behavior.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
diff --git a/include/git2/pack.h b/include/git2/pack.h
index 922a3cd..3b9beb6 100644
--- a/include/git2/pack.h
+++ b/include/git2/pack.h
@@ -155,7 +155,7 @@ GIT_EXTERN(int) git_packbuilder_write_buf(git_buf *buf, git_packbuilder *pb);
* Write the new pack and corresponding index file to path.
*
* @param pb The packbuilder
- * @param path to the directory where the packfile and index should be stored
+ * @param path Path to the directory where the packfile and index should be stored, or NULL for default location
* @param mode permissions to use creating a packfile or 0 for defaults
* @param progress_cb function to call with progress information from the indexer (optional)
* @param progress_cb_payload payload for the progress callback (optional)
diff --git a/src/pack-objects.c b/src/pack-objects.c
index 55a99e4..2c88858 100644
--- a/src/pack-objects.c
+++ b/src/pack-objects.c
@@ -1385,6 +1385,7 @@ int git_packbuilder_write(
void *progress_cb_payload)
{
int error = -1;
+ git_buf object_path = GIT_BUF_INIT;
git_indexer_options opts = GIT_INDEXER_OPTIONS_INIT;
git_indexer *indexer = NULL;
git_indexer_progress stats;
@@ -1393,6 +1394,14 @@ int git_packbuilder_write(
PREPARE_PACK;
+ if (path == NULL) {
+ if ((error = git_repository_item_path(&object_path, pb->repo, GIT_REPOSITORY_ITEM_OBJECTS)) < 0)
+ goto cleanup;
+ if ((error = git_buf_joinpath(&object_path, git_buf_cstr(&object_path), "pack")) < 0)
+ goto cleanup;
+ path = git_buf_cstr(&object_path);
+ }
+
opts.progress_cb = progress_cb;
opts.progress_cb_payload = progress_cb_payload;
@@ -1415,6 +1424,7 @@ int git_packbuilder_write(
cleanup:
git_indexer_free(indexer);
+ git_buf_dispose(&object_path);
return error;
}
diff --git a/tests/pack/packbuilder.c b/tests/pack/packbuilder.c
index 32f0612..5f5441a 100644
--- a/tests/pack/packbuilder.c
+++ b/tests/pack/packbuilder.c
@@ -151,6 +151,15 @@ void test_pack_packbuilder__get_hash(void)
cl_assert_equal_s(hex, "7f5fa362c664d68ba7221259be1cbd187434b2f0");
}
+void test_pack_packbuilder__write_default_path(void)
+{
+ seed_packbuilder();
+
+ cl_git_pass(git_packbuilder_write(_packbuilder, NULL, 0, NULL, NULL));
+ cl_assert(git_path_exists("objects/pack/pack-7f5fa362c664d68ba7221259be1cbd187434b2f0.idx"));
+ cl_assert(git_path_exists("objects/pack/pack-7f5fa362c664d68ba7221259be1cbd187434b2f0.pack"));
+}
+
static void test_write_pack_permission(mode_t given, mode_t expected)
{
struct stat statbuf;