Skip to content

Commit 5278a00

Browse files
committed
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 parent 0bc091d commit 5278a00

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

include/git2/pack.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ GIT_EXTERN(int) git_packbuilder_write_buf(git_buf *buf, git_packbuilder *pb);
155155
* Write the new pack and corresponding index file to path.
156156
*
157157
* @param pb The packbuilder
158-
* @param path to the directory where the packfile and index should be stored
158+
* @param path Path to the directory where the packfile and index should be stored, or NULL for default location
159159
* @param mode permissions to use creating a packfile or 0 for defaults
160160
* @param progress_cb function to call with progress information from the indexer (optional)
161161
* @param progress_cb_payload payload for the progress callback (optional)

src/pack-objects.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,6 +1385,7 @@ int git_packbuilder_write(
13851385
void *progress_cb_payload)
13861386
{
13871387
int error = -1;
1388+
git_buf object_path = GIT_BUF_INIT;
13881389
git_indexer_options opts = GIT_INDEXER_OPTIONS_INIT;
13891390
git_indexer *indexer = NULL;
13901391
git_indexer_progress stats;
@@ -1393,6 +1394,14 @@ int git_packbuilder_write(
13931394

13941395
PREPARE_PACK;
13951396

1397+
if (path == NULL) {
1398+
if ((error = git_repository_item_path(&object_path, pb->repo, GIT_REPOSITORY_ITEM_OBJECTS)) < 0)
1399+
goto cleanup;
1400+
if ((error = git_buf_joinpath(&object_path, git_buf_cstr(&object_path), "pack")) < 0)
1401+
goto cleanup;
1402+
path = git_buf_cstr(&object_path);
1403+
}
1404+
13961405
opts.progress_cb = progress_cb;
13971406
opts.progress_cb_payload = progress_cb_payload;
13981407

@@ -1415,6 +1424,7 @@ int git_packbuilder_write(
14151424

14161425
cleanup:
14171426
git_indexer_free(indexer);
1427+
git_buf_dispose(&object_path);
14181428
return error;
14191429
}
14201430

tests/pack/packbuilder.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,15 @@ void test_pack_packbuilder__get_hash(void)
151151
cl_assert_equal_s(hex, "7f5fa362c664d68ba7221259be1cbd187434b2f0");
152152
}
153153

154+
void test_pack_packbuilder__write_default_path(void)
155+
{
156+
seed_packbuilder();
157+
158+
cl_git_pass(git_packbuilder_write(_packbuilder, NULL, 0, NULL, NULL));
159+
cl_assert(git_path_exists("objects/pack/pack-7f5fa362c664d68ba7221259be1cbd187434b2f0.idx"));
160+
cl_assert(git_path_exists("objects/pack/pack-7f5fa362c664d68ba7221259be1cbd187434b2f0.pack"));
161+
}
162+
154163
static void test_write_pack_permission(mode_t given, mode_t expected)
155164
{
156165
struct stat statbuf;

0 commit comments

Comments
 (0)