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