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