Skip to content

Commit 96249c0

Browse files
iabervongitster
authored andcommitted
Build-in send-pack, with an API for other programs to call.
Also marks some more things as const, as needed. Signed-off-by: Daniel Barkalow <barkalow@iabervon.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 18f7c51 commit 96249c0

File tree

5 files changed

+64
-37
lines changed

5 files changed

+64
-37
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ BUILTIN_OBJS = \
358358
builtin-push.o \
359359
builtin-read-tree.o \
360360
builtin-reflog.o \
361+
builtin-send-pack.o \
361362
builtin-config.o \
362363
builtin-rerere.o \
363364
builtin-reset.o \

send-pack.c renamed to builtin-send-pack.c

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@
55
#include "pkt-line.h"
66
#include "run-command.h"
77
#include "remote.h"
8+
#include "send-pack.h"
89

910
static const char send_pack_usage[] =
1011
"git-send-pack [--all] [--dry-run] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [<host>:]<directory> [<ref>...]\n"
1112
" --all and explicit <ref> specification are mutually exclusive.";
12-
static const char *receivepack = "git-receive-pack";
13-
static int verbose;
14-
static int send_all;
15-
static int force_update;
16-
static int use_thin_pack;
17-
static int dry_run;
13+
14+
static struct send_pack_args args = {
15+
/* .receivepack = */ "git-receive-pack",
16+
};
1817

1918
/*
2019
* Make a pack stream and spit it out into file descriptor fd
@@ -26,7 +25,7 @@ static int pack_objects(int fd, struct ref *refs)
2625
* the revision parameters to it via its stdin and
2726
* let its stdout go back to the other end.
2827
*/
29-
const char *args[] = {
28+
const char *argv[] = {
3029
"pack-objects",
3130
"--all-progress",
3231
"--revs",
@@ -36,10 +35,10 @@ static int pack_objects(int fd, struct ref *refs)
3635
};
3736
struct child_process po;
3837

39-
if (use_thin_pack)
40-
args[4] = "--thin";
38+
if (args.use_thin_pack)
39+
argv[4] = "--thin";
4140
memset(&po, 0, sizeof(po));
42-
po.argv = args;
41+
po.argv = argv;
4342
po.in = -1;
4443
po.out = fd;
4544
po.git_cmd = 1;
@@ -207,7 +206,7 @@ static void update_tracking_ref(struct remote *remote, struct ref *ref)
207206
}
208207
}
209208

210-
static int send_pack(int in, int out, struct remote *remote, int nr_refspec, const char **refspec)
209+
static int do_send_pack(int in, int out, struct remote *remote, int nr_refspec, const char **refspec)
211210
{
212211
struct ref *ref;
213212
int new_refs;
@@ -230,7 +229,7 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, con
230229
if (!remote_tail)
231230
remote_tail = &remote_refs;
232231
if (match_refs(local_refs, remote_refs, &remote_tail,
233-
nr_refspec, refspec, send_all))
232+
nr_refspec, refspec, args.send_all))
234233
return -1;
235234

236235
if (!remote_refs) {
@@ -259,7 +258,7 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, con
259258
}
260259
if (!will_delete_ref &&
261260
!hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
262-
if (verbose)
261+
if (args.verbose)
263262
fprintf(stderr, "'%s': up-to-date\n", ref->name);
264263
continue;
265264
}
@@ -283,7 +282,7 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, con
283282
* always allowed.
284283
*/
285284

286-
if (!force_update &&
285+
if (!args.force_update &&
287286
!will_delete_ref &&
288287
!is_null_sha1(ref->old_sha1) &&
289288
!ref->force) {
@@ -313,7 +312,7 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, con
313312
strcpy(old_hex, sha1_to_hex(ref->old_sha1));
314313
new_hex = sha1_to_hex(ref->new_sha1);
315314

316-
if (!dry_run) {
315+
if (!args.dry_run) {
317316
if (ask_for_status_report) {
318317
packet_write(out, "%s %s %s%c%s",
319318
old_hex, new_hex, ref->name, 0,
@@ -338,7 +337,7 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, con
338337
}
339338

340339
packet_flush(out);
341-
if (new_refs && !dry_run)
340+
if (new_refs && !args.dry_run)
342341
ret = pack_objects(out, remote_refs);
343342
close(out);
344343

@@ -347,7 +346,7 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, con
347346
ret = -4;
348347
}
349348

350-
if (!dry_run && remote && ret == 0) {
349+
if (!args.dry_run && remote && ret == 0) {
351350
for (ref = remote_refs; ref; ref = ref->next)
352351
update_tracking_ref(remote, ref);
353352
}
@@ -378,54 +377,49 @@ static void verify_remote_names(int nr_heads, const char **heads)
378377
}
379378
}
380379

381-
int main(int argc, char **argv)
380+
int cmd_send_pack(int argc, const char **argv, const char *prefix)
382381
{
383382
int i, nr_heads = 0;
384-
char *dest = NULL;
385383
const char **heads = NULL;
386-
int fd[2], ret;
387-
struct child_process *conn;
388-
char *remote_name = NULL;
384+
const char *remote_name = NULL;
389385
struct remote *remote = NULL;
390-
391-
setup_git_directory();
392-
git_config(git_default_config);
386+
const char *dest = NULL;
393387

394388
argv++;
395389
for (i = 1; i < argc; i++, argv++) {
396-
char *arg = *argv;
390+
const char *arg = *argv;
397391

398392
if (*arg == '-') {
399393
if (!prefixcmp(arg, "--receive-pack=")) {
400-
receivepack = arg + 15;
394+
args.receivepack = arg + 15;
401395
continue;
402396
}
403397
if (!prefixcmp(arg, "--exec=")) {
404-
receivepack = arg + 7;
398+
args.receivepack = arg + 7;
405399
continue;
406400
}
407401
if (!prefixcmp(arg, "--remote=")) {
408402
remote_name = arg + 9;
409403
continue;
410404
}
411405
if (!strcmp(arg, "--all")) {
412-
send_all = 1;
406+
args.send_all = 1;
413407
continue;
414408
}
415409
if (!strcmp(arg, "--dry-run")) {
416-
dry_run = 1;
410+
args.dry_run = 1;
417411
continue;
418412
}
419413
if (!strcmp(arg, "--force")) {
420-
force_update = 1;
414+
args.force_update = 1;
421415
continue;
422416
}
423417
if (!strcmp(arg, "--verbose")) {
424-
verbose = 1;
418+
args.verbose = 1;
425419
continue;
426420
}
427421
if (!strcmp(arg, "--thin")) {
428-
use_thin_pack = 1;
422+
args.use_thin_pack = 1;
429423
continue;
430424
}
431425
usage(send_pack_usage);
@@ -440,9 +434,8 @@ int main(int argc, char **argv)
440434
}
441435
if (!dest)
442436
usage(send_pack_usage);
443-
if (heads && send_all)
437+
if (heads && args.send_all)
444438
usage(send_pack_usage);
445-
verify_remote_names(nr_heads, heads);
446439

447440
if (remote_name) {
448441
remote = remote_get(remote_name);
@@ -452,8 +445,22 @@ int main(int argc, char **argv)
452445
}
453446
}
454447

455-
conn = git_connect(fd, dest, receivepack, verbose ? CONNECT_VERBOSE : 0);
456-
ret = send_pack(fd[0], fd[1], remote, nr_heads, heads);
448+
return send_pack(&args, dest, remote, nr_heads, heads);
449+
}
450+
451+
int send_pack(struct send_pack_args *my_args,
452+
const char *dest, struct remote *remote,
453+
int nr_heads, const char **heads)
454+
{
455+
int fd[2], ret;
456+
struct child_process *conn;
457+
458+
memcpy(&args, my_args, sizeof(args));
459+
460+
verify_remote_names(nr_heads, heads);
461+
462+
conn = git_connect(fd, dest, args.receivepack, args.verbose ? CONNECT_VERBOSE : 0);
463+
ret = do_send_pack(fd[0], fd[1], remote, nr_heads, heads);
457464
close(fd[0]);
458465
close(fd[1]);
459466
ret |= finish_connect(conn);

builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ extern int cmd_rev_parse(int argc, const char **argv, const char *prefix);
7070
extern int cmd_revert(int argc, const char **argv, const char *prefix);
7171
extern int cmd_rm(int argc, const char **argv, const char *prefix);
7272
extern int cmd_runstatus(int argc, const char **argv, const char *prefix);
73+
extern int cmd_send_pack(int argc, const char **argv, const char *prefix);
7374
extern int cmd_shortlog(int argc, const char **argv, const char *prefix);
7475
extern int cmd_show(int argc, const char **argv, const char *prefix);
7576
extern int cmd_show_branch(int argc, const char **argv, const char *prefix);

git.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ static void handle_internal_command(int argc, const char **argv)
348348
{ "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE },
349349
{ "rm", cmd_rm, RUN_SETUP | NEED_WORK_TREE },
350350
{ "runstatus", cmd_runstatus, RUN_SETUP | NEED_WORK_TREE },
351+
{ "send-pack", cmd_send_pack, RUN_SETUP },
351352
{ "shortlog", cmd_shortlog, RUN_SETUP | USE_PAGER },
352353
{ "show-branch", cmd_show_branch, RUN_SETUP },
353354
{ "show", cmd_show, RUN_SETUP | USE_PAGER },

send-pack.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef SEND_PACK_H
2+
#define SEND_PACK_H
3+
4+
struct send_pack_args {
5+
const char *receivepack;
6+
unsigned verbose:1,
7+
send_all:1,
8+
force_update:1,
9+
use_thin_pack:1,
10+
dry_run:1;
11+
};
12+
13+
int send_pack(struct send_pack_args *args,
14+
const char *dest, struct remote *remote,
15+
int nr_heads, const char **heads);
16+
17+
#endif

0 commit comments

Comments
 (0)