Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 563c19a

Browse files
committed
packbuilder: also write index in git_packbuilder_write
git_packbuilder_write() used to write a packfile to the passed file path. Instead, ask for a destination directory and create both the packfile and an index, as most users probably do expect.
1 parent 63908ce commit 563c19a

File tree

2 files changed

+45
-25
lines changed

2 files changed

+45
-25
lines changed

include/git2/pack.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,20 @@ GIT_EXTERN(int) git_packbuilder_insert_tree(git_packbuilder *pb, const git_oid *
107107
GIT_EXTERN(int) git_packbuilder_insert_commit(git_packbuilder *pb, const git_oid *id);
108108

109109
/**
110-
* Write the new pack and the corresponding index to path
110+
* Write the new pack and corresponding index file to path.
111111
*
112112
* @param pb The packbuilder
113-
* @param path Directory to store the new pack and index
113+
* @param path to the directory where the packfile and index should be stored
114+
* @param progress_cb function to call with progress information from the indexer (optional)
115+
* @param progress_payload payload for the progress callback (optional)
114116
*
115117
* @return 0 or an error code
116118
*/
117-
GIT_EXTERN(int) git_packbuilder_write(git_packbuilder *pb, const char *file);
119+
GIT_EXTERN(int) git_packbuilder_write(
120+
git_packbuilder *pb,
121+
const char *path,
122+
git_transfer_progress_callback progress_cb,
123+
void *progress_cb_payload);
118124

119125
typedef int (*git_packbuilder_foreach_cb)(void *buf, size_t size, void *payload);
120126
/**

src/pack-objects.c

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ struct tree_walk_context {
3333
git_buf buf;
3434
};
3535

36+
struct pack_write_context {
37+
git_indexer_stream *indexer;
38+
git_transfer_progress *stats;
39+
};
40+
3641
#ifdef GIT_THREADS
3742

3843
#define GIT_PACKBUILDER__MUTEX_OP(pb, mtx, op) do { \
@@ -620,26 +625,6 @@ static int write_pack_buf(void *buf, size_t size, void *data)
620625
return git_buf_put(b, buf, size);
621626
}
622627

623-
static int write_pack_to_file(void *buf, size_t size, void *data)
624-
{
625-
git_filebuf *file = (git_filebuf *)data;
626-
return git_filebuf_write(file, buf, size);
627-
}
628-
629-
static int write_pack_file(git_packbuilder *pb, const char *path)
630-
{
631-
git_filebuf file = GIT_FILEBUF_INIT;
632-
633-
if (git_filebuf_open(&file, path, 0) < 0 ||
634-
write_pack(pb, &write_pack_to_file, &file) < 0 ||
635-
git_filebuf_commit(&file, GIT_PACK_FILE_MODE) < 0) {
636-
git_filebuf_cleanup(&file);
637-
return -1;
638-
}
639-
640-
return 0;
641-
}
642-
643628
static int type_size_sort(const void *_a, const void *_b)
644629
{
645630
const git_pobject *a = (git_pobject *)_a;
@@ -1259,10 +1244,39 @@ int git_packbuilder_write_buf(git_buf *buf, git_packbuilder *pb)
12591244
return write_pack(pb, &write_pack_buf, buf);
12601245
}
12611246

1262-
int git_packbuilder_write(git_packbuilder *pb, const char *path)
1247+
static int write_cb(void *buf, size_t len, void *payload)
12631248
{
1249+
struct pack_write_context *ctx = payload;
1250+
return git_indexer_stream_add(ctx->indexer, buf, len, ctx->stats);
1251+
}
1252+
1253+
int git_packbuilder_write(
1254+
git_packbuilder *pb,
1255+
const char *path,
1256+
git_transfer_progress_callback progress_cb,
1257+
void *progress_cb_payload)
1258+
{
1259+
git_indexer_stream *indexer;
1260+
git_transfer_progress stats;
1261+
struct pack_write_context ctx;
1262+
12641263
PREPARE_PACK;
1265-
return write_pack_file(pb, path);
1264+
1265+
if (git_indexer_stream_new(
1266+
&indexer, path, progress_cb, progress_cb_payload) < 0)
1267+
return -1;
1268+
1269+
ctx.indexer = indexer;
1270+
ctx.stats = &stats;
1271+
1272+
if (git_packbuilder_foreach(pb, write_cb, &ctx) < 0 ||
1273+
git_indexer_stream_finalize(indexer, &stats) < 0) {
1274+
git_indexer_stream_free(indexer);
1275+
return -1;
1276+
}
1277+
1278+
git_indexer_stream_free(indexer);
1279+
return 0;
12661280
}
12671281

12681282
#undef PREPARE_PACK

0 commit comments

Comments
 (0)