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