Merge branch 'mg/bisect-doc' into maint
[git/git-svn.git] / builtin / fetch-pack.c
blob940ae35dc2ce4923a248896eb764f145312d71b4
1 #include "builtin.h"
2 #include "pkt-line.h"
3 #include "fetch-pack.h"
5 static const char fetch_pack_usage[] =
6 "git fetch-pack [--all] [--stdin] [--quiet|-q] [--keep|-k] [--thin] "
7 "[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] "
8 "[--no-progress] [-v] [<host>:]<directory> [<refs>...]";
10 int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
12 int i, ret;
13 struct ref *ref = NULL;
14 const char *dest = NULL;
15 struct string_list sought = STRING_LIST_INIT_DUP;
16 int fd[2];
17 char *pack_lockfile = NULL;
18 char **pack_lockfile_ptr = NULL;
19 struct child_process *conn;
20 struct fetch_pack_args args;
22 packet_trace_identity("fetch-pack");
24 memset(&args, 0, sizeof(args));
25 args.uploadpack = "git-upload-pack";
27 for (i = 1; i < argc && *argv[i] == '-'; i++) {
28 const char *arg = argv[i];
30 if (!prefixcmp(arg, "--upload-pack=")) {
31 args.uploadpack = arg + 14;
32 continue;
34 if (!prefixcmp(arg, "--exec=")) {
35 args.uploadpack = arg + 7;
36 continue;
38 if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
39 args.quiet = 1;
40 continue;
42 if (!strcmp("--keep", arg) || !strcmp("-k", arg)) {
43 args.lock_pack = args.keep_pack;
44 args.keep_pack = 1;
45 continue;
47 if (!strcmp("--thin", arg)) {
48 args.use_thin_pack = 1;
49 continue;
51 if (!strcmp("--include-tag", arg)) {
52 args.include_tag = 1;
53 continue;
55 if (!strcmp("--all", arg)) {
56 args.fetch_all = 1;
57 continue;
59 if (!strcmp("--stdin", arg)) {
60 args.stdin_refs = 1;
61 continue;
63 if (!strcmp("-v", arg)) {
64 args.verbose = 1;
65 continue;
67 if (!prefixcmp(arg, "--depth=")) {
68 args.depth = strtol(arg + 8, NULL, 0);
69 continue;
71 if (!strcmp("--no-progress", arg)) {
72 args.no_progress = 1;
73 continue;
75 if (!strcmp("--stateless-rpc", arg)) {
76 args.stateless_rpc = 1;
77 continue;
79 if (!strcmp("--lock-pack", arg)) {
80 args.lock_pack = 1;
81 pack_lockfile_ptr = &pack_lockfile;
82 continue;
84 usage(fetch_pack_usage);
87 if (i < argc)
88 dest = argv[i++];
89 else
90 usage(fetch_pack_usage);
93 * Copy refs from cmdline to growable list, then append any
94 * refs from the standard input:
96 for (; i < argc; i++)
97 string_list_append(&sought, xstrdup(argv[i]));
98 if (args.stdin_refs) {
99 if (args.stateless_rpc) {
100 /* in stateless RPC mode we use pkt-line to read
101 * from stdin, until we get a flush packet
103 static char line[1000];
104 for (;;) {
105 int n = packet_read_line(0, line, sizeof(line));
106 if (!n)
107 break;
108 if (line[n-1] == '\n')
109 n--;
110 string_list_append(&sought, xmemdupz(line, n));
113 else {
114 /* read from stdin one ref per line, until EOF */
115 struct strbuf line = STRBUF_INIT;
116 while (strbuf_getline(&line, stdin, '\n') != EOF)
117 string_list_append(&sought, strbuf_detach(&line, NULL));
118 strbuf_release(&line);
122 if (args.stateless_rpc) {
123 conn = NULL;
124 fd[0] = 0;
125 fd[1] = 1;
126 } else {
127 conn = git_connect(fd, dest, args.uploadpack,
128 args.verbose ? CONNECT_VERBOSE : 0);
131 get_remote_heads(fd[0], &ref, 0, NULL);
133 ref = fetch_pack(&args, fd, conn, ref, dest,
134 &sought, pack_lockfile_ptr);
135 if (pack_lockfile) {
136 printf("lock %s\n", pack_lockfile);
137 fflush(stdout);
139 close(fd[0]);
140 close(fd[1]);
141 if (finish_connect(conn))
142 return 1;
144 ret = !ref || sought.nr;
147 * If the heads to pull were given, we should have consumed
148 * all of them by matching the remote. Otherwise, 'git fetch
149 * remote no-such-ref' would silently succeed without issuing
150 * an error.
152 for (i = 0; i < sought.nr; i++)
153 error("no such remote ref %s", sought.items[i].string);
154 while (ref) {
155 printf("%s %s\n",
156 sha1_to_hex(ref->old_sha1), ref->name);
157 ref = ref->next;
160 return ret;