More fixes for 2.9.1
[git.git] / builtin / fetch-pack.c
blobbfd0be44a91f66034c98361dc713cc0883693a69
1 #include "builtin.h"
2 #include "pkt-line.h"
3 #include "fetch-pack.h"
4 #include "remote.h"
5 #include "connect.h"
6 #include "sha1-array.h"
8 static const char fetch_pack_usage[] =
9 "git fetch-pack [--all] [--stdin] [--quiet | -q] [--keep | -k] [--thin] "
10 "[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] "
11 "[--no-progress] [--diag-url] [-v] [<host>:]<directory> [<refs>...]";
13 static void add_sought_entry(struct ref ***sought, int *nr, int *alloc,
14 const char *name)
16 struct ref *ref;
17 struct object_id oid;
19 if (!get_oid_hex(name, &oid)) {
20 if (name[GIT_SHA1_HEXSZ] == ' ') {
21 /* <sha1> <ref>, find refname */
22 name += GIT_SHA1_HEXSZ + 1;
23 } else if (name[GIT_SHA1_HEXSZ] == '\0') {
24 ; /* <sha1>, leave sha1 as name */
25 } else {
26 /* <ref>, clear cruft from oid */
27 oidclr(&oid);
29 } else {
30 /* <ref>, clear cruft from get_oid_hex */
31 oidclr(&oid);
34 ref = alloc_ref(name);
35 oidcpy(&ref->old_oid, &oid);
36 (*nr)++;
37 ALLOC_GROW(*sought, *nr, *alloc);
38 (*sought)[*nr - 1] = ref;
41 int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
43 int i, ret;
44 struct ref *ref = NULL;
45 const char *dest = NULL;
46 struct ref **sought = NULL;
47 int nr_sought = 0, alloc_sought = 0;
48 int fd[2];
49 char *pack_lockfile = NULL;
50 char **pack_lockfile_ptr = NULL;
51 struct child_process *conn;
52 struct fetch_pack_args args;
53 struct sha1_array shallow = SHA1_ARRAY_INIT;
55 packet_trace_identity("fetch-pack");
57 memset(&args, 0, sizeof(args));
58 args.uploadpack = "git-upload-pack";
60 for (i = 1; i < argc && *argv[i] == '-'; i++) {
61 const char *arg = argv[i];
63 if (starts_with(arg, "--upload-pack=")) {
64 args.uploadpack = arg + 14;
65 continue;
67 if (starts_with(arg, "--exec=")) {
68 args.uploadpack = arg + 7;
69 continue;
71 if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
72 args.quiet = 1;
73 continue;
75 if (!strcmp("--keep", arg) || !strcmp("-k", arg)) {
76 args.lock_pack = args.keep_pack;
77 args.keep_pack = 1;
78 continue;
80 if (!strcmp("--thin", arg)) {
81 args.use_thin_pack = 1;
82 continue;
84 if (!strcmp("--include-tag", arg)) {
85 args.include_tag = 1;
86 continue;
88 if (!strcmp("--all", arg)) {
89 args.fetch_all = 1;
90 continue;
92 if (!strcmp("--stdin", arg)) {
93 args.stdin_refs = 1;
94 continue;
96 if (!strcmp("--diag-url", arg)) {
97 args.diag_url = 1;
98 continue;
100 if (!strcmp("-v", arg)) {
101 args.verbose = 1;
102 continue;
104 if (starts_with(arg, "--depth=")) {
105 args.depth = strtol(arg + 8, NULL, 0);
106 continue;
108 if (!strcmp("--no-progress", arg)) {
109 args.no_progress = 1;
110 continue;
112 if (!strcmp("--stateless-rpc", arg)) {
113 args.stateless_rpc = 1;
114 continue;
116 if (!strcmp("--lock-pack", arg)) {
117 args.lock_pack = 1;
118 pack_lockfile_ptr = &pack_lockfile;
119 continue;
121 if (!strcmp("--check-self-contained-and-connected", arg)) {
122 args.check_self_contained_and_connected = 1;
123 continue;
125 if (!strcmp("--cloning", arg)) {
126 args.cloning = 1;
127 continue;
129 if (!strcmp("--update-shallow", arg)) {
130 args.update_shallow = 1;
131 continue;
133 usage(fetch_pack_usage);
136 if (i < argc)
137 dest = argv[i++];
138 else
139 usage(fetch_pack_usage);
142 * Copy refs from cmdline to growable list, then append any
143 * refs from the standard input:
145 for (; i < argc; i++)
146 add_sought_entry(&sought, &nr_sought, &alloc_sought, argv[i]);
147 if (args.stdin_refs) {
148 if (args.stateless_rpc) {
149 /* in stateless RPC mode we use pkt-line to read
150 * from stdin, until we get a flush packet
152 for (;;) {
153 char *line = packet_read_line(0, NULL);
154 if (!line)
155 break;
156 add_sought_entry(&sought, &nr_sought, &alloc_sought, line);
159 else {
160 /* read from stdin one ref per line, until EOF */
161 struct strbuf line = STRBUF_INIT;
162 while (strbuf_getline_lf(&line, stdin) != EOF)
163 add_sought_entry(&sought, &nr_sought, &alloc_sought, line.buf);
164 strbuf_release(&line);
168 if (args.stateless_rpc) {
169 conn = NULL;
170 fd[0] = 0;
171 fd[1] = 1;
172 } else {
173 int flags = args.verbose ? CONNECT_VERBOSE : 0;
174 if (args.diag_url)
175 flags |= CONNECT_DIAG_URL;
176 conn = git_connect(fd, dest, args.uploadpack,
177 flags);
178 if (!conn)
179 return args.diag_url ? 0 : 1;
181 get_remote_heads(fd[0], NULL, 0, &ref, 0, NULL, &shallow);
183 ref = fetch_pack(&args, fd, conn, ref, dest, sought, nr_sought,
184 &shallow, pack_lockfile_ptr);
185 if (pack_lockfile) {
186 printf("lock %s\n", pack_lockfile);
187 fflush(stdout);
189 if (args.check_self_contained_and_connected &&
190 args.self_contained_and_connected) {
191 printf("connectivity-ok\n");
192 fflush(stdout);
194 close(fd[0]);
195 close(fd[1]);
196 if (finish_connect(conn))
197 return 1;
199 ret = !ref;
202 * If the heads to pull were given, we should have consumed
203 * all of them by matching the remote. Otherwise, 'git fetch
204 * remote no-such-ref' would silently succeed without issuing
205 * an error.
207 for (i = 0; i < nr_sought; i++) {
208 if (!sought[i] || sought[i]->matched)
209 continue;
210 error("no such remote ref %s", sought[i]->name);
211 ret = 1;
214 while (ref) {
215 printf("%s %s\n",
216 oid_to_hex(&ref->old_oid), ref->name);
217 ref = ref->next;
220 return ret;