Skip to content

Commit 6de8aa7

Browse files
authored
Merge pull request libgit2#5532 from joshtriplett/pack-default-path
git_packbuilder_write: Allow setting path to NULL to use the default path
2 parents 22f9a0f + 5278a00 commit 6de8aa7

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
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: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,37 +1381,48 @@ int git_packbuilder_write(
13811381
git_indexer_progress_cb progress_cb,
13821382
void *progress_cb_payload)
13831383
{
1384+
int error = -1;
1385+
git_buf object_path = GIT_BUF_INIT;
13841386
git_indexer_options opts = GIT_INDEXER_OPTIONS_INIT;
1385-
git_indexer *indexer;
1387+
git_indexer *indexer = NULL;
13861388
git_indexer_progress stats;
13871389
struct pack_write_context ctx;
13881390
int t;
13891391

13901392
PREPARE_PACK;
13911393

1394+
if (path == NULL) {
1395+
if ((error = git_repository_item_path(&object_path, pb->repo, GIT_REPOSITORY_ITEM_OBJECTS)) < 0)
1396+
goto cleanup;
1397+
if ((error = git_buf_joinpath(&object_path, git_buf_cstr(&object_path), "pack")) < 0)
1398+
goto cleanup;
1399+
path = git_buf_cstr(&object_path);
1400+
}
1401+
13921402
opts.progress_cb = progress_cb;
13931403
opts.progress_cb_payload = progress_cb_payload;
13941404

1395-
if (git_indexer_new(
1396-
&indexer, path, mode, pb->odb, &opts) < 0)
1397-
return -1;
1405+
if ((error = git_indexer_new(&indexer, path, mode, pb->odb, &opts)) < 0)
1406+
goto cleanup;
13981407

13991408
if (!git_repository__configmap_lookup(&t, pb->repo, GIT_CONFIGMAP_FSYNCOBJECTFILES) && t)
14001409
git_indexer__set_fsync(indexer, 1);
14011410

14021411
ctx.indexer = indexer;
14031412
ctx.stats = &stats;
14041413

1405-
if (git_packbuilder_foreach(pb, write_cb, &ctx) < 0 ||
1406-
git_indexer_commit(indexer, &stats) < 0) {
1407-
git_indexer_free(indexer);
1408-
return -1;
1409-
}
1414+
if ((error = git_packbuilder_foreach(pb, write_cb, &ctx)) < 0)
1415+
goto cleanup;
1416+
1417+
if ((error = git_indexer_commit(indexer, &stats)) < 0)
1418+
goto cleanup;
14101419

14111420
git_oid_cpy(&pb->pack_oid, git_indexer_hash(indexer));
14121421

1422+
cleanup:
14131423
git_indexer_free(indexer);
1414-
return 0;
1424+
git_buf_dispose(&object_path);
1425+
return error;
14151426
}
14161427

14171428
#undef PREPARE_PACK

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)