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>
Daniel Barkalow committed
Oct 29, 2007 at 22:03 UTC
96249c04c07f00485a3ed1052ff10b27ab147fc5
5 files changed
+64
-37
Makefile
+1
@@ -358,6 +358,7 @@ BUILTIN_OBJS = \
358
builtin-push.o \
359
builtin-read-tree.o \
360
builtin-reflog.o \
361
+ builtin-send-pack.o \
362
builtin-config.o \
363
builtin-rerere.o \
364
builtin-reset.o \
builtin-send-pack.c
renamed
+44
-37
@@ -5,16 +5,15 @@
5
#include "pkt-line.h"
6
#include "run-command.h"
7
#include "remote.h"
8
+#include "send-pack.h"
9
10
static const char send_pack_usage[] =
11
"git-send-pack [--all] [--dry-run] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [<host>:]<directory> [<ref>...]\n"
12
" --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
+};
17
18
/*
19
* 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)
25
* the revision parameters to it via its stdin and
26
* let its stdout go back to the other end.
27
*/
29
- const char *args[] = {
28
+ const char *argv[] = {
29
"pack-objects",
30
"--all-progress",
31
"--revs",
@@ -36,10 +35,10 @@ static int pack_objects(int fd, struct ref *refs)
35
};
36
struct child_process po;
37
39
- if (use_thin_pack)
40
- args[4] = "--thin";
38
+ if (args.use_thin_pack)
39
+ argv[4] = "--thin";
40
memset(&po, 0, sizeof(po));
42
- po.argv = args;
41
+ po.argv = argv;
42
po.in = -1;
43
po.out = fd;
44
po.git_cmd = 1;
@@ -207,7 +206,7 @@ static void update_tracking_ref(struct remote *remote, struct ref *ref)
206
}
207
}
208
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)
210
{
211
struct ref *ref;
212
int new_refs;
@@ -230,7 +229,7 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, con
229
if (!remote_tail)
230
remote_tail = &remote_refs;
231
if (match_refs(local_refs, remote_refs, &remote_tail,
233
- nr_refspec, refspec, send_all))
232
+ nr_refspec, refspec, args.send_all))
233
return -1;
234
235
if (!remote_refs) {
@@ -259,7 +258,7 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, con
258
}
259
if (!will_delete_ref &&
260
!hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
262
- if (verbose)
261
+ if (args.verbose)
262
fprintf(stderr, "'%s': up-to-date\n", ref->name);
263
continue;
264
}
@@ -283,7 +282,7 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, con
282
* always allowed.
283
*/
284
286
- if (!force_update &&
285
+ if (!args.force_update &&
286
!will_delete_ref &&
287
!is_null_sha1(ref->old_sha1) &&
288
!ref->force) {
@@ -313,7 +312,7 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, con
312
strcpy(old_hex, sha1_to_hex(ref->old_sha1));
313
new_hex = sha1_to_hex(ref->new_sha1);
314
316
- if (!dry_run) {
315
+ if (!args.dry_run) {
316
if (ask_for_status_report) {
317
packet_write(out, "%s %s %s%c%s",
318
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
337
}
338
339
packet_flush(out);
341
- if (new_refs && !dry_run)
340
+ if (new_refs && !args.dry_run)
341
ret = pack_objects(out, remote_refs);
342
close(out);
343
@@ -347,7 +346,7 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, con
346
ret = -4;
347
}
348
350
- if (!dry_run && remote && ret == 0) {
349
+ if (!args.dry_run && remote && ret == 0) {
350
for (ref = remote_refs; ref; ref = ref->next)
351
update_tracking_ref(remote, ref);
352
}
@@ -378,30 +377,25 @@ static void verify_remote_names(int nr_heads, const char **heads)
377
}
378
}
379
381
-int main(int argc, char **argv)
380
+int cmd_send_pack(int argc, const char **argv, const char *prefix)
381
{
382
int i, nr_heads = 0;
384
- char *dest = NULL;
383
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;
385
struct remote *remote = NULL;
390
-
391
- setup_git_directory();
392
- git_config(git_default_config);
386
+ const char *dest = NULL;
387
388
argv++;
389
for (i = 1; i < argc; i++, argv++) {
396
- char *arg = *argv;
390
+ const char *arg = *argv;
391
392
if (*arg == '-') {
393
if (!prefixcmp(arg, "--receive-pack=")) {
400
- receivepack = arg + 15;
394
+ args.receivepack = arg + 15;
395
continue;
396
}
397
if (!prefixcmp(arg, "--exec=")) {
404
- receivepack = arg + 7;
398
+ args.receivepack = arg + 7;
399
continue;
400
}
401
if (!prefixcmp(arg, "--remote=")) {
@@ -409,23 +403,23 @@ int main(int argc, char **argv)
403
continue;
404
}
405
if (!strcmp(arg, "--all")) {
412
- send_all = 1;
406
+ args.send_all = 1;
407
continue;
408
}
409
if (!strcmp(arg, "--dry-run")) {
416
- dry_run = 1;
410
+ args.dry_run = 1;
411
continue;
412
}
413
if (!strcmp(arg, "--force")) {
420
- force_update = 1;
414
+ args.force_update = 1;
415
continue;
416
}
417
if (!strcmp(arg, "--verbose")) {
424
- verbose = 1;
418
+ args.verbose = 1;
419
continue;
420
}
421
if (!strcmp(arg, "--thin")) {
428
- use_thin_pack = 1;
422
+ args.use_thin_pack = 1;
423
continue;
424
}
425
usage(send_pack_usage);
@@ -440,9 +434,8 @@ int main(int argc, char **argv)
434
}
435
if (!dest)
436
usage(send_pack_usage);
443
- if (heads && send_all)
437
+ if (heads && args.send_all)
438
usage(send_pack_usage);
445
- verify_remote_names(nr_heads, heads);
439
440
if (remote_name) {
441
remote = remote_get(remote_name);
@@ -452,8 +445,22 @@ int main(int argc, char **argv)
445
}
446
}
447
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);
464
close(fd[0]);
465
close(fd[1]);
466
ret |= finish_connect(conn);
builtin.h
+1
@@ -70,6 +70,7 @@ extern int cmd_rev_parse(int argc, const char **argv, const char *prefix);
70
extern int cmd_revert(int argc, const char **argv, const char *prefix);
71
extern int cmd_rm(int argc, const char **argv, const char *prefix);
72
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);
74
extern int cmd_shortlog(int argc, const char **argv, const char *prefix);
75
extern int cmd_show(int argc, const char **argv, const char *prefix);
76
extern int cmd_show_branch(int argc, const char **argv, const char *prefix);
git.c
+1
@@ -348,6 +348,7 @@ static void handle_internal_command(int argc, const char **argv)
348
{ "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE },
349
{ "rm", cmd_rm, RUN_SETUP | NEED_WORK_TREE },
350
{ "runstatus", cmd_runstatus, RUN_SETUP | NEED_WORK_TREE },
351
+ { "send-pack", cmd_send_pack, RUN_SETUP },
352
{ "shortlog", cmd_shortlog, RUN_SETUP | USE_PAGER },
353
{ "show-branch", cmd_show_branch, RUN_SETUP },
354
{ "show", cmd_show, RUN_SETUP | USE_PAGER },
send-pack.h
new
+17
@@ -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