fetch, upload-pack: --deepen=N extends shallow boundary by N commits
[git] / builtin / fetch-pack.c
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"
7
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>...]";
12
13 static 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);
17         struct object_id oid;
18         const int chunksz = GIT_SHA1_HEXSZ + 1;
19
20         if (namelen > chunksz && name[chunksz - 1] == ' ' &&
21                 !get_oid_hex(name, &oid)) {
22                 oidcpy(&ref->old_oid, &oid);
23                 name += chunksz;
24                 namelen -= chunksz;
25         }
26
27         memcpy(ref->name, name, namelen);
28         ref->name[namelen] = '\0';
29         (*nr)++;
30         ALLOC_GROW(*sought, *nr, *alloc);
31         (*sought)[*nr - 1] = ref;
32 }
33
34 static void add_sought_entry(struct ref ***sought, int *nr, int *alloc,
35                              const char *string)
36 {
37         add_sought_entry_mem(sought, nr, alloc, string, strlen(string));
38 }
39
40 int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
41 {
42         int i, ret;
43         struct ref *ref = NULL;
44         const char *dest = NULL;
45         struct ref **sought = NULL;
46         int nr_sought = 0, alloc_sought = 0;
47         int fd[2];
48         char *pack_lockfile = NULL;
49         char **pack_lockfile_ptr = NULL;
50         struct child_process *conn;
51         struct fetch_pack_args args;
52         struct sha1_array shallow = SHA1_ARRAY_INIT;
53         struct string_list deepen_not = STRING_LIST_INIT_DUP;
54
55         packet_trace_identity("fetch-pack");
56
57         memset(&args, 0, sizeof(args));
58         args.uploadpack = "git-upload-pack";
59
60         for (i = 1; i < argc && *argv[i] == '-'; i++) {
61                 const char *arg = argv[i];
62
63                 if (skip_prefix(arg, "--upload-pack=", &arg)) {
64                         args.uploadpack = arg;
65                         continue;
66                 }
67                 if (skip_prefix(arg, "--exec=", &arg)) {
68                         args.uploadpack = arg;
69                         continue;
70                 }
71                 if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
72                         args.quiet = 1;
73                         continue;
74                 }
75                 if (!strcmp("--keep", arg) || !strcmp("-k", arg)) {
76                         args.lock_pack = args.keep_pack;
77                         args.keep_pack = 1;
78                         continue;
79                 }
80                 if (!strcmp("--thin", arg)) {
81                         args.use_thin_pack = 1;
82                         continue;
83                 }
84                 if (!strcmp("--include-tag", arg)) {
85                         args.include_tag = 1;
86                         continue;
87                 }
88                 if (!strcmp("--all", arg)) {
89                         args.fetch_all = 1;
90                         continue;
91                 }
92                 if (!strcmp("--stdin", arg)) {
93                         args.stdin_refs = 1;
94                         continue;
95                 }
96                 if (!strcmp("--diag-url", arg)) {
97                         args.diag_url = 1;
98                         continue;
99                 }
100                 if (!strcmp("-v", arg)) {
101                         args.verbose = 1;
102                         continue;
103                 }
104                 if (skip_prefix(arg, "--depth=", &arg)) {
105                         args.depth = strtol(arg, NULL, 0);
106                         continue;
107                 }
108                 if (skip_prefix(arg, "--shallow-since=", &arg)) {
109                         args.deepen_since = xstrdup(arg);
110                         continue;
111                 }
112                 if (skip_prefix(arg, "--shallow-exclude=", &arg)) {
113                         string_list_append(&deepen_not, arg);
114                         continue;
115                 }
116                 if (!strcmp(arg, "--deepen-relative")) {
117                         args.deepen_relative = 1;
118                         continue;
119                 }
120                 if (!strcmp("--no-progress", arg)) {
121                         args.no_progress = 1;
122                         continue;
123                 }
124                 if (!strcmp("--stateless-rpc", arg)) {
125                         args.stateless_rpc = 1;
126                         continue;
127                 }
128                 if (!strcmp("--lock-pack", arg)) {
129                         args.lock_pack = 1;
130                         pack_lockfile_ptr = &pack_lockfile;
131                         continue;
132                 }
133                 if (!strcmp("--check-self-contained-and-connected", arg)) {
134                         args.check_self_contained_and_connected = 1;
135                         continue;
136                 }
137                 if (!strcmp("--cloning", arg)) {
138                         args.cloning = 1;
139                         continue;
140                 }
141                 if (!strcmp("--update-shallow", arg)) {
142                         args.update_shallow = 1;
143                         continue;
144                 }
145                 usage(fetch_pack_usage);
146         }
147         if (deepen_not.nr)
148                 args.deepen_not = &deepen_not;
149
150         if (i < argc)
151                 dest = argv[i++];
152         else
153                 usage(fetch_pack_usage);
154
155         /*
156          * Copy refs from cmdline to growable list, then append any
157          * refs from the standard input:
158          */
159         for (; i < argc; i++)
160                 add_sought_entry(&sought, &nr_sought, &alloc_sought, argv[i]);
161         if (args.stdin_refs) {
162                 if (args.stateless_rpc) {
163                         /* in stateless RPC mode we use pkt-line to read
164                          * from stdin, until we get a flush packet
165                          */
166                         for (;;) {
167                                 char *line = packet_read_line(0, NULL);
168                                 if (!line)
169                                         break;
170                                 add_sought_entry(&sought, &nr_sought,  &alloc_sought, line);
171                         }
172                 }
173                 else {
174                         /* read from stdin one ref per line, until EOF */
175                         struct strbuf line = STRBUF_INIT;
176                         while (strbuf_getline_lf(&line, stdin) != EOF)
177                                 add_sought_entry(&sought, &nr_sought, &alloc_sought, line.buf);
178                         strbuf_release(&line);
179                 }
180         }
181
182         if (args.stateless_rpc) {
183                 conn = NULL;
184                 fd[0] = 0;
185                 fd[1] = 1;
186         } else {
187                 int flags = args.verbose ? CONNECT_VERBOSE : 0;
188                 if (args.diag_url)
189                         flags |= CONNECT_DIAG_URL;
190                 conn = git_connect(fd, dest, args.uploadpack,
191                                    flags);
192                 if (!conn)
193                         return args.diag_url ? 0 : 1;
194         }
195         get_remote_heads(fd[0], NULL, 0, &ref, 0, NULL, &shallow);
196
197         ref = fetch_pack(&args, fd, conn, ref, dest, sought, nr_sought,
198                          &shallow, pack_lockfile_ptr);
199         if (pack_lockfile) {
200                 printf("lock %s\n", pack_lockfile);
201                 fflush(stdout);
202         }
203         if (args.check_self_contained_and_connected &&
204             args.self_contained_and_connected) {
205                 printf("connectivity-ok\n");
206                 fflush(stdout);
207         }
208         close(fd[0]);
209         close(fd[1]);
210         if (finish_connect(conn))
211                 return 1;
212
213         ret = !ref;
214
215         /*
216          * If the heads to pull were given, we should have consumed
217          * all of them by matching the remote.  Otherwise, 'git fetch
218          * remote no-such-ref' would silently succeed without issuing
219          * an error.
220          */
221         for (i = 0; i < nr_sought; i++) {
222                 if (!sought[i] || sought[i]->matched)
223                         continue;
224                 error("no such remote ref %s", sought[i]->name);
225                 ret = 1;
226         }
227
228         while (ref) {
229                 printf("%s %s\n",
230                        oid_to_hex(&ref->old_oid), ref->name);
231                 ref = ref->next;
232         }
233
234         return ret;
235 }