Make fetch-pack a builtin with an internal API
Signed-off-by: Daniel Barkalow <barkalow@iabervon.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Daniel Barkalow committed
Sep 10, 2007 at 23:03 UTC
2d4177c01c238071777db5b1fbd8a14efb62ce02
5 files changed
+86
-22
Makefile
+1
@@ -331,6 +331,7 @@ BUILTIN_OBJS = \
331
builtin-diff-files.o \
332
builtin-diff-index.o \
333
builtin-diff-tree.o \
334
+ builtin-fetch-pack.o \
335
builtin-fetch--tool.o \
336
builtin-fmt-merge-msg.o \
337
builtin-for-each-ref.o \
builtin-fetch-pack.c
renamed
+62
-22
@@ -6,6 +6,7 @@
6
#include "exec_cmd.h"
7
#include "pack.h"
8
#include "sideband.h"
9
+#include "fetch-pack.h"
10
11
static int keep_pack;
12
static int transfer_unpack_limit = -1;
@@ -573,7 +574,7 @@ static int get_pack(int xd[2])
574
die("%s died of unnatural causes %d", argv[0], status);
575
}
576
576
-static int fetch_pack(int fd[2], int nr_match, char **match)
577
+static struct ref *do_fetch_pack(int fd[2], int nr_match, char **match)
578
{
579
struct ref *ref;
580
unsigned char sha1[20];
@@ -615,12 +616,7 @@ static int fetch_pack(int fd[2], int nr_match, char **match)
616
die("git-fetch-pack: fetch failed.");
617
618
all_done:
618
- while (ref) {
619
- printf("%s %s\n",
620
- sha1_to_hex(ref->old_sha1), ref->name);
621
- ref = ref->next;
622
- }
623
- return 0;
619
+ return ref;
620
}
621
622
static int remove_duplicates(int nr_heads, char **heads)
@@ -663,15 +659,28 @@ static int fetch_pack_config(const char *var, const char *value)
659
660
static struct lock_file lock;
661
666
-int main(int argc, char **argv)
662
+void setup_fetch_pack(struct fetch_pack_args *args)
663
+{
664
+ uploadpack = args->uploadpack;
665
+ quiet = args->quiet;
666
+ keep_pack = args->keep_pack;
667
+ if (args->unpacklimit >= 0)
668
+ unpack_limit = args->unpacklimit;
669
+ if (args->keep_pack)
670
+ unpack_limit = 0;
671
+ use_thin_pack = args->use_thin_pack;
672
+ fetch_all = args->fetch_all;
673
+ verbose = args->verbose;
674
+ depth = args->depth;
675
+ no_progress = args->no_progress;
676
+}
677
+
678
+int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
679
{
680
int i, ret, nr_heads;
681
+ struct ref *ref;
682
char *dest = NULL, **heads;
670
- int fd[2];
671
- pid_t pid;
672
- struct stat st;
683
674
- setup_git_directory();
684
git_config(fetch_pack_config);
685
686
if (0 <= transfer_unpack_limit)
@@ -682,7 +691,7 @@ int main(int argc, char **argv)
691
nr_heads = 0;
692
heads = NULL;
693
for (i = 1; i < argc; i++) {
685
- char *arg = argv[i];
694
+ const char *arg = argv[i];
695
696
if (*arg == '-') {
697
if (!prefixcmp(arg, "--upload-pack=")) {
@@ -716,8 +725,6 @@ int main(int argc, char **argv)
725
}
726
if (!prefixcmp(arg, "--depth=")) {
727
depth = strtol(arg + 8, NULL, 0);
719
- if (stat(git_path("shallow"), &st))
720
- st.st_mtime = 0;
728
continue;
729
}
730
if (!strcmp("--no-progress", arg)) {
@@ -726,22 +733,52 @@ int main(int argc, char **argv)
733
}
734
usage(fetch_pack_usage);
735
}
729
- dest = arg;
730
- heads = argv + i + 1;
736
+ dest = (char *)arg;
737
+ heads = (char **)(argv + i + 1);
738
nr_heads = argc - i - 1;
739
break;
740
}
741
if (!dest)
742
usage(fetch_pack_usage);
736
- pid = git_connect(fd, dest, uploadpack, verbose ? CONNECT_VERBOSE : 0);
743
+
744
+ ref = fetch_pack(dest, nr_heads, heads);
745
+
746
+ ret = !ref;
747
+
748
+ while (ref) {
749
+ printf("%s %s\n",
750
+ sha1_to_hex(ref->old_sha1), ref->name);
751
+ ref = ref->next;
752
+ }
753
+
754
+ return ret;
755
+}
756
+
757
+struct ref *fetch_pack(const char *dest, int nr_heads, char **heads)
758
+{
759
+ int i, ret;
760
+ int fd[2];
761
+ pid_t pid;
762
+ struct ref *ref;
763
+ struct stat st;
764
+
765
+ if (depth > 0) {
766
+ if (stat(git_path("shallow"), &st))
767
+ st.st_mtime = 0;
768
+ }
769
+
770
+ printf("connect to %s\n", dest);
771
+
772
+ pid = git_connect(fd, (char *)dest, uploadpack,
773
+ verbose ? CONNECT_VERBOSE : 0);
774
if (pid < 0)
738
- return 1;
775
+ return NULL;
776
if (heads && nr_heads)
777
nr_heads = remove_duplicates(nr_heads, heads);
741
- ret = fetch_pack(fd, nr_heads, heads);
778
+ ref = do_fetch_pack(fd, nr_heads, heads);
779
close(fd[0]);
780
close(fd[1]);
744
- ret |= finish_connect(pid);
781
+ ret = finish_connect(pid);
782
783
if (!ret && nr_heads) {
784
/* If the heads to pull were given, we should have
@@ -785,5 +822,8 @@ int main(int argc, char **argv)
822
}
823
}
824
788
- return !!ret;
825
+ if (ret)
826
+ ref = NULL;
827
+
828
+ return ref;
829
}
builtin.h
+1
@@ -31,6 +31,7 @@ extern int cmd_diff_files(int argc, const char **argv, const char *prefix);
31
extern int cmd_diff_index(int argc, const char **argv, const char *prefix);
32
extern int cmd_diff(int argc, const char **argv, const char *prefix);
33
extern int cmd_diff_tree(int argc, const char **argv, const char *prefix);
34
+extern int cmd_fetch_pack(int argc, const char **argv, const char *prefix);
35
extern int cmd_fetch__tool(int argc, const char **argv, const char *prefix);
36
extern int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix);
37
extern int cmd_for_each_ref(int argc, const char **argv, const char *prefix);
fetch-pack.h
new
+21
@@ -0,0 +1,21 @@
1
+#ifndef FETCH_PACK_H
2
+#define FETCH_PACK_H
3
+
4
+struct fetch_pack_args
5
+{
6
+ const char *uploadpack;
7
+ int quiet;
8
+ int keep_pack;
9
+ int unpacklimit;
10
+ int use_thin_pack;
11
+ int fetch_all;
12
+ int verbose;
13
+ int depth;
14
+ int no_progress;
15
+};
16
+
17
+void setup_fetch_pack(struct fetch_pack_args *args);
18
+
19
+struct ref *fetch_pack(const char *dest, int nr_heads, char **heads);
20
+
21
+#endif
git.c
+1
@@ -334,6 +334,7 @@ static void handle_internal_command(int argc, const char **argv)
334
{ "diff-files", cmd_diff_files },
335
{ "diff-index", cmd_diff_index, RUN_SETUP },
336
{ "diff-tree", cmd_diff_tree, RUN_SETUP },
337
+ { "fetch-pack", cmd_fetch_pack, RUN_SETUP },
338
{ "fetch--tool", cmd_fetch__tool, RUN_SETUP },
339
{ "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP },
340
{ "for-each-ref", cmd_for_each_ref, RUN_SETUP },