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