provide a facility for "delayed" progress reporting
[git] / send-pack.c
1 #include "cache.h"
2 #include "commit.h"
3 #include "tag.h"
4 #include "refs.h"
5 #include "pkt-line.h"
6 #include "run-command.h"
7
8 static const char send_pack_usage[] =
9 "git-send-pack [--all] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [<host>:]<directory> [<ref>...]\n"
10 "  --all and explicit <ref> specification are mutually exclusive.";
11 static const char *receivepack = "git-receive-pack";
12 static int verbose;
13 static int send_all;
14 static int force_update;
15 static int use_thin_pack;
16
17 /*
18  * Make a pack stream and spit it out into file descriptor fd
19  */
20 static int pack_objects(int fd, struct ref *refs)
21 {
22         /*
23          * The child becomes pack-objects --revs; we feed
24          * the revision parameters to it via its stdin and
25          * let its stdout go back to the other end.
26          */
27         const char *args[] = {
28                 "pack-objects",
29                 "--all-progress",
30                 "--revs",
31                 "--stdout",
32                 NULL,
33                 NULL,
34         };
35         struct child_process po;
36
37         if (use_thin_pack)
38                 args[4] = "--thin";
39         memset(&po, 0, sizeof(po));
40         po.argv = args;
41         po.in = -1;
42         po.out = fd;
43         po.git_cmd = 1;
44         if (start_command(&po))
45                 die("git-pack-objects failed (%s)", strerror(errno));
46
47         /*
48          * We feed the pack-objects we just spawned with revision
49          * parameters by writing to the pipe.
50          */
51         while (refs) {
52                 char buf[42];
53
54                 if (!is_null_sha1(refs->old_sha1) &&
55                     has_sha1_file(refs->old_sha1)) {
56                         memcpy(buf + 1, sha1_to_hex(refs->old_sha1), 40);
57                         buf[0] = '^';
58                         buf[41] = '\n';
59                         if (!write_or_whine(po.in, buf, 42,
60                                                 "send-pack: send refs"))
61                                 break;
62                 }
63                 if (!is_null_sha1(refs->new_sha1)) {
64                         memcpy(buf, sha1_to_hex(refs->new_sha1), 40);
65                         buf[40] = '\n';
66                         if (!write_or_whine(po.in, buf, 41,
67                                                 "send-pack: send refs"))
68                                 break;
69                 }
70                 refs = refs->next;
71         }
72
73         if (finish_command(&po))
74                 return error("pack-objects died with strange error");
75         return 0;
76 }
77
78 static void unmark_and_free(struct commit_list *list, unsigned int mark)
79 {
80         while (list) {
81                 struct commit_list *temp = list;
82                 temp->item->object.flags &= ~mark;
83                 list = temp->next;
84                 free(temp);
85         }
86 }
87
88 static int ref_newer(const unsigned char *new_sha1,
89                      const unsigned char *old_sha1)
90 {
91         struct object *o;
92         struct commit *old, *new;
93         struct commit_list *list, *used;
94         int found = 0;
95
96         /* Both new and old must be commit-ish and new is descendant of
97          * old.  Otherwise we require --force.
98          */
99         o = deref_tag(parse_object(old_sha1), NULL, 0);
100         if (!o || o->type != OBJ_COMMIT)
101                 return 0;
102         old = (struct commit *) o;
103
104         o = deref_tag(parse_object(new_sha1), NULL, 0);
105         if (!o || o->type != OBJ_COMMIT)
106                 return 0;
107         new = (struct commit *) o;
108
109         if (parse_commit(new) < 0)
110                 return 0;
111
112         used = list = NULL;
113         commit_list_insert(new, &list);
114         while (list) {
115                 new = pop_most_recent_commit(&list, 1);
116                 commit_list_insert(new, &used);
117                 if (new == old) {
118                         found = 1;
119                         break;
120                 }
121         }
122         unmark_and_free(list, 1);
123         unmark_and_free(used, 1);
124         return found;
125 }
126
127 static struct ref *local_refs, **local_tail;
128 static struct ref *remote_refs, **remote_tail;
129
130 static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
131 {
132         struct ref *ref;
133         int len = strlen(refname) + 1;
134         ref = xcalloc(1, sizeof(*ref) + len);
135         hashcpy(ref->new_sha1, sha1);
136         memcpy(ref->name, refname, len);
137         *local_tail = ref;
138         local_tail = &ref->next;
139         return 0;
140 }
141
142 static void get_local_heads(void)
143 {
144         local_tail = &local_refs;
145         for_each_ref(one_local_ref, NULL);
146 }
147
148 static int receive_status(int in)
149 {
150         char line[1000];
151         int ret = 0;
152         int len = packet_read_line(in, line, sizeof(line));
153         if (len < 10 || memcmp(line, "unpack ", 7)) {
154                 fprintf(stderr, "did not receive status back\n");
155                 return -1;
156         }
157         if (memcmp(line, "unpack ok\n", 10)) {
158                 fputs(line, stderr);
159                 ret = -1;
160         }
161         while (1) {
162                 len = packet_read_line(in, line, sizeof(line));
163                 if (!len)
164                         break;
165                 if (len < 3 ||
166                     (memcmp(line, "ok", 2) && memcmp(line, "ng", 2))) {
167                         fprintf(stderr, "protocol error: %s\n", line);
168                         ret = -1;
169                         break;
170                 }
171                 if (!memcmp(line, "ok", 2))
172                         continue;
173                 fputs(line, stderr);
174                 ret = -1;
175         }
176         return ret;
177 }
178
179 static int send_pack(int in, int out, int nr_refspec, char **refspec)
180 {
181         struct ref *ref;
182         int new_refs;
183         int ret = 0;
184         int ask_for_status_report = 0;
185         int allow_deleting_refs = 0;
186         int expect_status_report = 0;
187
188         /* No funny business with the matcher */
189         remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, REF_NORMAL);
190         get_local_heads();
191
192         /* Does the other end support the reporting? */
193         if (server_supports("report-status"))
194                 ask_for_status_report = 1;
195         if (server_supports("delete-refs"))
196                 allow_deleting_refs = 1;
197
198         /* match them up */
199         if (!remote_tail)
200                 remote_tail = &remote_refs;
201         if (match_refs(local_refs, remote_refs, &remote_tail,
202                        nr_refspec, refspec, send_all))
203                 return -1;
204
205         if (!remote_refs) {
206                 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
207                 return 0;
208         }
209
210         /*
211          * Finally, tell the other end!
212          */
213         new_refs = 0;
214         for (ref = remote_refs; ref; ref = ref->next) {
215                 char old_hex[60], *new_hex;
216                 int delete_ref;
217
218                 if (!ref->peer_ref)
219                         continue;
220
221                 delete_ref = is_null_sha1(ref->peer_ref->new_sha1);
222                 if (delete_ref && !allow_deleting_refs) {
223                         error("remote does not support deleting refs");
224                         ret = -2;
225                         continue;
226                 }
227                 if (!delete_ref &&
228                     !hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
229                         if (verbose)
230                                 fprintf(stderr, "'%s': up-to-date\n", ref->name);
231                         continue;
232                 }
233
234                 /* This part determines what can overwrite what.
235                  * The rules are:
236                  *
237                  * (0) you can always use --force or +A:B notation to
238                  *     selectively force individual ref pairs.
239                  *
240                  * (1) if the old thing does not exist, it is OK.
241                  *
242                  * (2) if you do not have the old thing, you are not allowed
243                  *     to overwrite it; you would not know what you are losing
244                  *     otherwise.
245                  *
246                  * (3) if both new and old are commit-ish, and new is a
247                  *     descendant of old, it is OK.
248                  *
249                  * (4) regardless of all of the above, removing :B is
250                  *     always allowed.
251                  */
252
253                 if (!force_update &&
254                     !delete_ref &&
255                     !is_null_sha1(ref->old_sha1) &&
256                     !ref->force) {
257                         if (!has_sha1_file(ref->old_sha1) ||
258                             !ref_newer(ref->peer_ref->new_sha1,
259                                        ref->old_sha1)) {
260                                 /* We do not have the remote ref, or
261                                  * we know that the remote ref is not
262                                  * an ancestor of what we are trying to
263                                  * push.  Either way this can be losing
264                                  * commits at the remote end and likely
265                                  * we were not up to date to begin with.
266                                  */
267                                 error("remote '%s' is not a strict "
268                                       "subset of local ref '%s'. "
269                                       "maybe you are not up-to-date and "
270                                       "need to pull first?",
271                                       ref->name,
272                                       ref->peer_ref->name);
273                                 ret = -2;
274                                 continue;
275                         }
276                 }
277                 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
278                 if (!delete_ref)
279                         new_refs++;
280                 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
281                 new_hex = sha1_to_hex(ref->new_sha1);
282
283                 if (ask_for_status_report) {
284                         packet_write(out, "%s %s %s%c%s",
285                                      old_hex, new_hex, ref->name, 0,
286                                      "report-status");
287                         ask_for_status_report = 0;
288                         expect_status_report = 1;
289                 }
290                 else
291                         packet_write(out, "%s %s %s",
292                                      old_hex, new_hex, ref->name);
293                 if (delete_ref)
294                         fprintf(stderr, "deleting '%s'\n", ref->name);
295                 else {
296                         fprintf(stderr, "updating '%s'", ref->name);
297                         if (strcmp(ref->name, ref->peer_ref->name))
298                                 fprintf(stderr, " using '%s'",
299                                         ref->peer_ref->name);
300                         fprintf(stderr, "\n  from %s\n  to   %s\n",
301                                 old_hex, new_hex);
302                 }
303         }
304
305         packet_flush(out);
306         if (new_refs)
307                 ret = pack_objects(out, remote_refs);
308         close(out);
309
310         if (expect_status_report) {
311                 if (receive_status(in))
312                         ret = -4;
313         }
314
315         if (!new_refs && ret == 0)
316                 fprintf(stderr, "Everything up-to-date\n");
317         return ret;
318 }
319
320 static void verify_remote_names(int nr_heads, char **heads)
321 {
322         int i;
323
324         for (i = 0; i < nr_heads; i++) {
325                 const char *remote = strchr(heads[i], ':');
326
327                 remote = remote ? (remote + 1) : heads[i];
328                 switch (check_ref_format(remote)) {
329                 case 0: /* ok */
330                 case -2: /* ok but a single level -- that is fine for
331                           * a match pattern.
332                           */
333                         continue;
334                 }
335                 die("remote part of refspec is not a valid name in %s",
336                     heads[i]);
337         }
338 }
339
340 int main(int argc, char **argv)
341 {
342         int i, nr_heads = 0;
343         char *dest = NULL;
344         char **heads = NULL;
345         int fd[2], ret;
346         pid_t pid;
347
348         setup_git_directory();
349         git_config(git_default_config);
350
351         argv++;
352         for (i = 1; i < argc; i++, argv++) {
353                 char *arg = *argv;
354
355                 if (*arg == '-') {
356                         if (!prefixcmp(arg, "--receive-pack=")) {
357                                 receivepack = arg + 15;
358                                 continue;
359                         }
360                         if (!prefixcmp(arg, "--exec=")) {
361                                 receivepack = arg + 7;
362                                 continue;
363                         }
364                         if (!strcmp(arg, "--all")) {
365                                 send_all = 1;
366                                 continue;
367                         }
368                         if (!strcmp(arg, "--force")) {
369                                 force_update = 1;
370                                 continue;
371                         }
372                         if (!strcmp(arg, "--verbose")) {
373                                 verbose = 1;
374                                 continue;
375                         }
376                         if (!strcmp(arg, "--thin")) {
377                                 use_thin_pack = 1;
378                                 continue;
379                         }
380                         usage(send_pack_usage);
381                 }
382                 if (!dest) {
383                         dest = arg;
384                         continue;
385                 }
386                 heads = argv;
387                 nr_heads = argc - i;
388                 break;
389         }
390         if (!dest)
391                 usage(send_pack_usage);
392         if (heads && send_all)
393                 usage(send_pack_usage);
394         verify_remote_names(nr_heads, heads);
395
396         pid = git_connect(fd, dest, receivepack);
397         if (pid < 0)
398                 return 1;
399         ret = send_pack(fd[0], fd[1], nr_heads, heads);
400         close(fd[0]);
401         close(fd[1]);
402         ret |= finish_connect(pid);
403         return !!ret;
404 }