]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/fetch-pack.c
Git 2.14.4
[thirdparty/git.git] / builtin / fetch-pack.c
CommitLineData
c2e86add 1#include "builtin.h"
def88e9a 2#include "pkt-line.h"
2d4177c0 3#include "fetch-pack.h"
47a59185
JH
4#include "remote.h"
5#include "connect.h"
16094885 6#include "sha1-array.h"
fa740529 7
33b83034 8static const char fetch_pack_usage[] =
9c9b4f2f 9"git fetch-pack [--all] [--stdin] [--quiet | -q] [--keep | -k] [--thin] "
078b895f 10"[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] "
5610b7c0 11"[--no-progress] [--diag-url] [-v] [<host>:]<directory> [<refs>...]";
def88e9a 12
5545f057
JK
13static void add_sought_entry(struct ref ***sought, int *nr, int *alloc,
14 const char *name)
f2db854d 15{
5545f057 16 struct ref *ref;
854ecb9c 17 struct object_id oid;
58f2ed05 18
4a8d202c
GSF
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);
28 }
29 } else {
30 /* <ref>, clear cruft from get_oid_hex */
5545f057 31 oidclr(&oid);
4a8d202c 32 }
f2db854d 33
5545f057
JK
34 ref = alloc_ref(name);
35 oidcpy(&ref->old_oid, &oid);
f2db854d
JH
36 (*nr)++;
37 ALLOC_GROW(*sought, *nr, *alloc);
38 (*sought)[*nr - 1] = ref;
39}
40
50ab5fd3
SP
41int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
42{
57e6fc69 43 int i, ret;
ba227857 44 struct ref *ref = NULL;
9d19c6ea 45 const char *dest = NULL;
f2db854d
JH
46 struct ref **sought = NULL;
47 int nr_sought = 0, alloc_sought = 0;
ba227857 48 int fd[2];
249b2004
SP
49 char *pack_lockfile = NULL;
50 char **pack_lockfile_ptr = NULL;
ba227857 51 struct child_process *conn;
f8eb3036 52 struct fetch_pack_args args;
910650d2 53 struct oid_array shallow = OID_ARRAY_INIT;
a45a2600 54 struct string_list deepen_not = STRING_LIST_INIT_DUP;
e28714c5 55
bbc30f99
JK
56 packet_trace_identity("fetch-pack");
57
f8eb3036
NTND
58 memset(&args, 0, sizeof(args));
59 args.uploadpack = "git-upload-pack";
60
ff22ff99 61 for (i = 1; i < argc && *argv[i] == '-'; i++) {
2d4177c0 62 const char *arg = argv[i];
def88e9a 63
45a3e526
NTND
64 if (skip_prefix(arg, "--upload-pack=", &arg)) {
65 args.uploadpack = arg;
ff22ff99
MH
66 continue;
67 }
45a3e526
NTND
68 if (skip_prefix(arg, "--exec=", &arg)) {
69 args.uploadpack = arg;
ff22ff99 70 continue;
def88e9a 71 }
ff22ff99
MH
72 if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
73 args.quiet = 1;
74 continue;
75 }
76 if (!strcmp("--keep", arg) || !strcmp("-k", arg)) {
77 args.lock_pack = args.keep_pack;
78 args.keep_pack = 1;
79 continue;
80 }
81 if (!strcmp("--thin", arg)) {
82 args.use_thin_pack = 1;
83 continue;
84 }
85 if (!strcmp("--include-tag", arg)) {
86 args.include_tag = 1;
87 continue;
88 }
89 if (!strcmp("--all", arg)) {
90 args.fetch_all = 1;
91 continue;
def88e9a 92 }
ff22ff99
MH
93 if (!strcmp("--stdin", arg)) {
94 args.stdin_refs = 1;
95 continue;
96 }
5610b7c0
TB
97 if (!strcmp("--diag-url", arg)) {
98 args.diag_url = 1;
99 continue;
100 }
ff22ff99
MH
101 if (!strcmp("-v", arg)) {
102 args.verbose = 1;
103 continue;
104 }
45a3e526
NTND
105 if (skip_prefix(arg, "--depth=", &arg)) {
106 args.depth = strtol(arg, NULL, 0);
ff22ff99
MH
107 continue;
108 }
508ea882
NTND
109 if (skip_prefix(arg, "--shallow-since=", &arg)) {
110 args.deepen_since = xstrdup(arg);
111 continue;
112 }
a45a2600
NTND
113 if (skip_prefix(arg, "--shallow-exclude=", &arg)) {
114 string_list_append(&deepen_not, arg);
115 continue;
116 }
cccf74e2
NTND
117 if (!strcmp(arg, "--deepen-relative")) {
118 args.deepen_relative = 1;
ff22ff99
MH
119 continue;
120 }
121 if (!strcmp("--no-progress", arg)) {
122 args.no_progress = 1;
123 continue;
124 }
125 if (!strcmp("--stateless-rpc", arg)) {
126 args.stateless_rpc = 1;
127 continue;
128 }
129 if (!strcmp("--lock-pack", arg)) {
130 args.lock_pack = 1;
131 pack_lockfile_ptr = &pack_lockfile;
132 continue;
133 }
9ba38048
NTND
134 if (!strcmp("--check-self-contained-and-connected", arg)) {
135 args.check_self_contained_and_connected = 1;
136 continue;
137 }
16094885
NTND
138 if (!strcmp("--cloning", arg)) {
139 args.cloning = 1;
140 continue;
141 }
142 if (!strcmp("--update-shallow", arg)) {
143 args.update_shallow = 1;
144 continue;
145 }
ff22ff99 146 usage(fetch_pack_usage);
def88e9a 147 }
a45a2600
NTND
148 if (deepen_not.nr)
149 args.deepen_not = &deepen_not;
4cc00fcf
MH
150
151 if (i < argc)
152 dest = argv[i++];
153 else
def88e9a 154 usage(fetch_pack_usage);
2d4177c0 155
57e6fc69
MH
156 /*
157 * Copy refs from cmdline to growable list, then append any
158 * refs from the standard input:
159 */
57e6fc69 160 for (; i < argc; i++)
f2db854d 161 add_sought_entry(&sought, &nr_sought, &alloc_sought, argv[i]);
078b895f 162 if (args.stdin_refs) {
078b895f
IT
163 if (args.stateless_rpc) {
164 /* in stateless RPC mode we use pkt-line to read
165 * from stdin, until we get a flush packet
166 */
078b895f 167 for (;;) {
74543a04
JK
168 char *line = packet_read_line(0, NULL);
169 if (!line)
078b895f 170 break;
e013bdab 171 add_sought_entry(&sought, &nr_sought, &alloc_sought, line);
078b895f
IT
172 }
173 }
174 else {
175 /* read from stdin one ref per line, until EOF */
176 struct strbuf line = STRBUF_INIT;
8f309aeb 177 while (strbuf_getline_lf(&line, stdin) != EOF)
f2db854d 178 add_sought_entry(&sought, &nr_sought, &alloc_sought, line.buf);
078b895f
IT
179 strbuf_release(&line);
180 }
181 }
182
249b2004
SP
183 if (args.stateless_rpc) {
184 conn = NULL;
185 fd[0] = 0;
186 fd[1] = 1;
ba227857 187 } else {
5610b7c0
TB
188 int flags = args.verbose ? CONNECT_VERBOSE : 0;
189 if (args.diag_url)
190 flags |= CONNECT_DIAG_URL;
9d19c6ea 191 conn = git_connect(fd, dest, args.uploadpack,
5610b7c0
TB
192 flags);
193 if (!conn)
194 return args.diag_url ? 0 : 1;
249b2004 195 }
16094885 196 get_remote_heads(fd[0], NULL, 0, &ref, 0, NULL, &shallow);
249b2004 197
16094885
NTND
198 ref = fetch_pack(&args, fd, conn, ref, dest, sought, nr_sought,
199 &shallow, pack_lockfile_ptr);
249b2004
SP
200 if (pack_lockfile) {
201 printf("lock %s\n", pack_lockfile);
202 fflush(stdout);
ba227857 203 }
9ba38048
NTND
204 if (args.check_self_contained_and_connected &&
205 args.self_contained_and_connected) {
206 printf("connectivity-ok\n");
207 fflush(stdout);
208 }
249b2004
SP
209 close(fd[0]);
210 close(fd[1]);
211 if (finish_connect(conn))
7418f1a0 212 return 1;
2d4177c0 213
f2db854d 214 ret = !ref;
b285668d
MH
215
216 /*
217 * If the heads to pull were given, we should have consumed
218 * all of them by matching the remote. Otherwise, 'git fetch
219 * remote no-such-ref' would silently succeed without issuing
220 * an error.
221 */
e860d96b 222 ret |= report_unmatched_refs(sought, nr_sought);
f2db854d 223
2d4177c0
DB
224 while (ref) {
225 printf("%s %s\n",
f4e54d02 226 oid_to_hex(&ref->old_oid), ref->name);
2d4177c0
DB
227 ref = ref->next;
228 }
229
230 return ret;
231}