Use a common function to get the pretty name of refs
[git] / builtin-send-pack.c
1 #include "cache.h"
2 #include "commit.h"
3 #include "refs.h"
4 #include "pkt-line.h"
5 #include "run-command.h"
6 #include "remote.h"
7 #include "send-pack.h"
8
9 static const char send_pack_usage[] =
10 "git send-pack [--all | --mirror] [--dry-run] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [<host>:]<directory> [<ref>...]\n"
11 "  --all and explicit <ref> specification are mutually exclusive.";
12
13 static struct send_pack_args args = {
14         /* .receivepack = */ "git-receive-pack",
15 };
16
17 static int feed_object(const unsigned char *sha1, int fd, int negative)
18 {
19         char buf[42];
20
21         if (negative && !has_sha1_file(sha1))
22                 return 1;
23
24         memcpy(buf + negative, sha1_to_hex(sha1), 40);
25         if (negative)
26                 buf[0] = '^';
27         buf[40 + negative] = '\n';
28         return write_or_whine(fd, buf, 41 + negative, "send-pack: send refs");
29 }
30
31 /*
32  * Make a pack stream and spit it out into file descriptor fd
33  */
34 static int pack_objects(int fd, struct ref *refs, struct extra_have_objects *extra)
35 {
36         /*
37          * The child becomes pack-objects --revs; we feed
38          * the revision parameters to it via its stdin and
39          * let its stdout go back to the other end.
40          */
41         const char *argv[] = {
42                 "pack-objects",
43                 "--all-progress",
44                 "--revs",
45                 "--stdout",
46                 NULL,
47                 NULL,
48         };
49         struct child_process po;
50         int i;
51
52         if (args.use_thin_pack)
53                 argv[4] = "--thin";
54         memset(&po, 0, sizeof(po));
55         po.argv = argv;
56         po.in = -1;
57         po.out = fd;
58         po.git_cmd = 1;
59         if (start_command(&po))
60                 die("git pack-objects failed (%s)", strerror(errno));
61
62         /*
63          * We feed the pack-objects we just spawned with revision
64          * parameters by writing to the pipe.
65          */
66         for (i = 0; i < extra->nr; i++)
67                 if (!feed_object(extra->array[i], po.in, 1))
68                         break;
69
70         while (refs) {
71                 if (!is_null_sha1(refs->old_sha1) &&
72                     !feed_object(refs->old_sha1, po.in, 1))
73                         break;
74                 if (!is_null_sha1(refs->new_sha1) &&
75                     !feed_object(refs->new_sha1, po.in, 0))
76                         break;
77                 refs = refs->next;
78         }
79
80         close(po.in);
81         if (finish_command(&po))
82                 return error("pack-objects died with strange error");
83         return 0;
84 }
85
86 static struct ref *remote_refs, **remote_tail;
87
88 static int receive_status(int in, struct ref *refs)
89 {
90         struct ref *hint;
91         char line[1000];
92         int ret = 0;
93         int len = packet_read_line(in, line, sizeof(line));
94         if (len < 10 || memcmp(line, "unpack ", 7))
95                 return error("did not receive remote status");
96         if (memcmp(line, "unpack ok\n", 10)) {
97                 char *p = line + strlen(line) - 1;
98                 if (*p == '\n')
99                         *p = '\0';
100                 error("unpack failed: %s", line + 7);
101                 ret = -1;
102         }
103         hint = NULL;
104         while (1) {
105                 char *refname;
106                 char *msg;
107                 len = packet_read_line(in, line, sizeof(line));
108                 if (!len)
109                         break;
110                 if (len < 3 ||
111                     (memcmp(line, "ok ", 3) && memcmp(line, "ng ", 3))) {
112                         fprintf(stderr, "protocol error: %s\n", line);
113                         ret = -1;
114                         break;
115                 }
116
117                 line[strlen(line)-1] = '\0';
118                 refname = line + 3;
119                 msg = strchr(refname, ' ');
120                 if (msg)
121                         *msg++ = '\0';
122
123                 /* first try searching at our hint, falling back to all refs */
124                 if (hint)
125                         hint = find_ref_by_name(hint, refname);
126                 if (!hint)
127                         hint = find_ref_by_name(refs, refname);
128                 if (!hint) {
129                         warning("remote reported status on unknown ref: %s",
130                                         refname);
131                         continue;
132                 }
133                 if (hint->status != REF_STATUS_EXPECTING_REPORT) {
134                         warning("remote reported status on unexpected ref: %s",
135                                         refname);
136                         continue;
137                 }
138
139                 if (line[0] == 'o' && line[1] == 'k')
140                         hint->status = REF_STATUS_OK;
141                 else {
142                         hint->status = REF_STATUS_REMOTE_REJECT;
143                         ret = -1;
144                 }
145                 if (msg)
146                         hint->remote_status = xstrdup(msg);
147                 /* start our next search from the next ref */
148                 hint = hint->next;
149         }
150         return ret;
151 }
152
153 static void update_tracking_ref(struct remote *remote, struct ref *ref)
154 {
155         struct refspec rs;
156
157         if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
158                 return;
159
160         rs.src = ref->name;
161         rs.dst = NULL;
162
163         if (!remote_find_tracking(remote, &rs)) {
164                 if (args.verbose)
165                         fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
166                 if (ref->deletion) {
167                         delete_ref(rs.dst, NULL, 0);
168                 } else
169                         update_ref("update by push", rs.dst,
170                                         ref->new_sha1, NULL, 0, 0);
171                 free(rs.dst);
172         }
173 }
174
175 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
176
177 static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg)
178 {
179         fprintf(stderr, " %c %-*s ", flag, SUMMARY_WIDTH, summary);
180         if (from)
181                 fprintf(stderr, "%s -> %s", prettify_ref(from), prettify_ref(to));
182         else
183                 fputs(prettify_ref(to), stderr);
184         if (msg) {
185                 fputs(" (", stderr);
186                 fputs(msg, stderr);
187                 fputc(')', stderr);
188         }
189         fputc('\n', stderr);
190 }
191
192 static const char *status_abbrev(unsigned char sha1[20])
193 {
194         return find_unique_abbrev(sha1, DEFAULT_ABBREV);
195 }
196
197 static void print_ok_ref_status(struct ref *ref)
198 {
199         if (ref->deletion)
200                 print_ref_status('-', "[deleted]", ref, NULL, NULL);
201         else if (is_null_sha1(ref->old_sha1))
202                 print_ref_status('*',
203                         (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" :
204                           "[new branch]"),
205                         ref, ref->peer_ref, NULL);
206         else {
207                 char quickref[84];
208                 char type;
209                 const char *msg;
210
211                 strcpy(quickref, status_abbrev(ref->old_sha1));
212                 if (ref->nonfastforward) {
213                         strcat(quickref, "...");
214                         type = '+';
215                         msg = "forced update";
216                 } else {
217                         strcat(quickref, "..");
218                         type = ' ';
219                         msg = NULL;
220                 }
221                 strcat(quickref, status_abbrev(ref->new_sha1));
222
223                 print_ref_status(type, quickref, ref, ref->peer_ref, msg);
224         }
225 }
226
227 static int print_one_push_status(struct ref *ref, const char *dest, int count)
228 {
229         if (!count)
230                 fprintf(stderr, "To %s\n", dest);
231
232         switch(ref->status) {
233         case REF_STATUS_NONE:
234                 print_ref_status('X', "[no match]", ref, NULL, NULL);
235                 break;
236         case REF_STATUS_REJECT_NODELETE:
237                 print_ref_status('!', "[rejected]", ref, NULL,
238                                 "remote does not support deleting refs");
239                 break;
240         case REF_STATUS_UPTODATE:
241                 print_ref_status('=', "[up to date]", ref,
242                                 ref->peer_ref, NULL);
243                 break;
244         case REF_STATUS_REJECT_NONFASTFORWARD:
245                 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
246                                 "non-fast forward");
247                 break;
248         case REF_STATUS_REMOTE_REJECT:
249                 print_ref_status('!', "[remote rejected]", ref,
250                                 ref->deletion ? NULL : ref->peer_ref,
251                                 ref->remote_status);
252                 break;
253         case REF_STATUS_EXPECTING_REPORT:
254                 print_ref_status('!', "[remote failure]", ref,
255                                 ref->deletion ? NULL : ref->peer_ref,
256                                 "remote failed to report status");
257                 break;
258         case REF_STATUS_OK:
259                 print_ok_ref_status(ref);
260                 break;
261         }
262
263         return 1;
264 }
265
266 static void print_push_status(const char *dest, struct ref *refs)
267 {
268         struct ref *ref;
269         int n = 0;
270
271         if (args.verbose) {
272                 for (ref = refs; ref; ref = ref->next)
273                         if (ref->status == REF_STATUS_UPTODATE)
274                                 n += print_one_push_status(ref, dest, n);
275         }
276
277         for (ref = refs; ref; ref = ref->next)
278                 if (ref->status == REF_STATUS_OK)
279                         n += print_one_push_status(ref, dest, n);
280
281         for (ref = refs; ref; ref = ref->next) {
282                 if (ref->status != REF_STATUS_NONE &&
283                     ref->status != REF_STATUS_UPTODATE &&
284                     ref->status != REF_STATUS_OK)
285                         n += print_one_push_status(ref, dest, n);
286         }
287 }
288
289 static int refs_pushed(struct ref *ref)
290 {
291         for (; ref; ref = ref->next) {
292                 switch(ref->status) {
293                 case REF_STATUS_NONE:
294                 case REF_STATUS_UPTODATE:
295                         break;
296                 default:
297                         return 1;
298                 }
299         }
300         return 0;
301 }
302
303 static int do_send_pack(int in, int out, struct remote *remote, const char *dest, int nr_refspec, const char **refspec)
304 {
305         struct ref *ref, *local_refs;
306         int new_refs;
307         int ask_for_status_report = 0;
308         int allow_deleting_refs = 0;
309         int expect_status_report = 0;
310         int flags = MATCH_REFS_NONE;
311         int ret;
312         struct extra_have_objects extra_have;
313
314         memset(&extra_have, 0, sizeof(extra_have));
315         if (args.send_all)
316                 flags |= MATCH_REFS_ALL;
317         if (args.send_mirror)
318                 flags |= MATCH_REFS_MIRROR;
319
320         /* No funny business with the matcher */
321         remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, REF_NORMAL,
322                                        &extra_have);
323         local_refs = get_local_heads();
324
325         /* Does the other end support the reporting? */
326         if (server_supports("report-status"))
327                 ask_for_status_report = 1;
328         if (server_supports("delete-refs"))
329                 allow_deleting_refs = 1;
330
331         /* match them up */
332         if (!remote_tail)
333                 remote_tail = &remote_refs;
334         if (match_refs(local_refs, remote_refs, &remote_tail,
335                        nr_refspec, refspec, flags)) {
336                 close(out);
337                 return -1;
338         }
339
340         if (!remote_refs) {
341                 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
342                         "Perhaps you should specify a branch such as 'master'.\n");
343                 close(out);
344                 return 0;
345         }
346
347         /*
348          * Finally, tell the other end!
349          */
350         new_refs = 0;
351         for (ref = remote_refs; ref; ref = ref->next) {
352
353                 if (ref->peer_ref)
354                         hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
355                 else if (!args.send_mirror)
356                         continue;
357
358                 ref->deletion = is_null_sha1(ref->new_sha1);
359                 if (ref->deletion && !allow_deleting_refs) {
360                         ref->status = REF_STATUS_REJECT_NODELETE;
361                         continue;
362                 }
363                 if (!ref->deletion &&
364                     !hashcmp(ref->old_sha1, ref->new_sha1)) {
365                         ref->status = REF_STATUS_UPTODATE;
366                         continue;
367                 }
368
369                 /* This part determines what can overwrite what.
370                  * The rules are:
371                  *
372                  * (0) you can always use --force or +A:B notation to
373                  *     selectively force individual ref pairs.
374                  *
375                  * (1) if the old thing does not exist, it is OK.
376                  *
377                  * (2) if you do not have the old thing, you are not allowed
378                  *     to overwrite it; you would not know what you are losing
379                  *     otherwise.
380                  *
381                  * (3) if both new and old are commit-ish, and new is a
382                  *     descendant of old, it is OK.
383                  *
384                  * (4) regardless of all of the above, removing :B is
385                  *     always allowed.
386                  */
387
388                 ref->nonfastforward =
389                     !ref->deletion &&
390                     !is_null_sha1(ref->old_sha1) &&
391                     (!has_sha1_file(ref->old_sha1)
392                       || !ref_newer(ref->new_sha1, ref->old_sha1));
393
394                 if (ref->nonfastforward && !ref->force && !args.force_update) {
395                         ref->status = REF_STATUS_REJECT_NONFASTFORWARD;
396                         continue;
397                 }
398
399                 if (!ref->deletion)
400                         new_refs++;
401
402                 if (!args.dry_run) {
403                         char *old_hex = sha1_to_hex(ref->old_sha1);
404                         char *new_hex = sha1_to_hex(ref->new_sha1);
405
406                         if (ask_for_status_report) {
407                                 packet_write(out, "%s %s %s%c%s",
408                                         old_hex, new_hex, ref->name, 0,
409                                         "report-status");
410                                 ask_for_status_report = 0;
411                                 expect_status_report = 1;
412                         }
413                         else
414                                 packet_write(out, "%s %s %s",
415                                         old_hex, new_hex, ref->name);
416                 }
417                 ref->status = expect_status_report ?
418                         REF_STATUS_EXPECTING_REPORT :
419                         REF_STATUS_OK;
420         }
421
422         packet_flush(out);
423         if (new_refs && !args.dry_run) {
424                 if (pack_objects(out, remote_refs, &extra_have) < 0)
425                         return -1;
426         }
427         else
428                 close(out);
429
430         if (expect_status_report)
431                 ret = receive_status(in, remote_refs);
432         else
433                 ret = 0;
434
435         print_push_status(dest, remote_refs);
436
437         if (!args.dry_run && remote) {
438                 for (ref = remote_refs; ref; ref = ref->next)
439                         update_tracking_ref(remote, ref);
440         }
441
442         if (!refs_pushed(remote_refs))
443                 fprintf(stderr, "Everything up-to-date\n");
444         if (ret < 0)
445                 return ret;
446         for (ref = remote_refs; ref; ref = ref->next) {
447                 switch (ref->status) {
448                 case REF_STATUS_NONE:
449                 case REF_STATUS_UPTODATE:
450                 case REF_STATUS_OK:
451                         break;
452                 default:
453                         return -1;
454                 }
455         }
456         return 0;
457 }
458
459 static void verify_remote_names(int nr_heads, const char **heads)
460 {
461         int i;
462
463         for (i = 0; i < nr_heads; i++) {
464                 const char *local = heads[i];
465                 const char *remote = strrchr(heads[i], ':');
466
467                 if (*local == '+')
468                         local++;
469
470                 /* A matching refspec is okay.  */
471                 if (remote == local && remote[1] == '\0')
472                         continue;
473
474                 remote = remote ? (remote + 1) : local;
475                 switch (check_ref_format(remote)) {
476                 case 0: /* ok */
477                 case CHECK_REF_FORMAT_ONELEVEL:
478                         /* ok but a single level -- that is fine for
479                          * a match pattern.
480                          */
481                 case CHECK_REF_FORMAT_WILDCARD:
482                         /* ok but ends with a pattern-match character */
483                         continue;
484                 }
485                 die("remote part of refspec is not a valid name in %s",
486                     heads[i]);
487         }
488 }
489
490 int cmd_send_pack(int argc, const char **argv, const char *prefix)
491 {
492         int i, nr_heads = 0;
493         const char **heads = NULL;
494         const char *remote_name = NULL;
495         struct remote *remote = NULL;
496         const char *dest = NULL;
497
498         argv++;
499         for (i = 1; i < argc; i++, argv++) {
500                 const char *arg = *argv;
501
502                 if (*arg == '-') {
503                         if (!prefixcmp(arg, "--receive-pack=")) {
504                                 args.receivepack = arg + 15;
505                                 continue;
506                         }
507                         if (!prefixcmp(arg, "--exec=")) {
508                                 args.receivepack = arg + 7;
509                                 continue;
510                         }
511                         if (!prefixcmp(arg, "--remote=")) {
512                                 remote_name = arg + 9;
513                                 continue;
514                         }
515                         if (!strcmp(arg, "--all")) {
516                                 args.send_all = 1;
517                                 continue;
518                         }
519                         if (!strcmp(arg, "--dry-run")) {
520                                 args.dry_run = 1;
521                                 continue;
522                         }
523                         if (!strcmp(arg, "--mirror")) {
524                                 args.send_mirror = 1;
525                                 continue;
526                         }
527                         if (!strcmp(arg, "--force")) {
528                                 args.force_update = 1;
529                                 continue;
530                         }
531                         if (!strcmp(arg, "--verbose")) {
532                                 args.verbose = 1;
533                                 continue;
534                         }
535                         if (!strcmp(arg, "--thin")) {
536                                 args.use_thin_pack = 1;
537                                 continue;
538                         }
539                         usage(send_pack_usage);
540                 }
541                 if (!dest) {
542                         dest = arg;
543                         continue;
544                 }
545                 heads = (const char **) argv;
546                 nr_heads = argc - i;
547                 break;
548         }
549         if (!dest)
550                 usage(send_pack_usage);
551         /*
552          * --all and --mirror are incompatible; neither makes sense
553          * with any refspecs.
554          */
555         if ((heads && (args.send_all || args.send_mirror)) ||
556                                         (args.send_all && args.send_mirror))
557                 usage(send_pack_usage);
558
559         if (remote_name) {
560                 remote = remote_get(remote_name);
561                 if (!remote_has_url(remote, dest)) {
562                         die("Destination %s is not a uri for %s",
563                             dest, remote_name);
564                 }
565         }
566
567         return send_pack(&args, dest, remote, nr_heads, heads);
568 }
569
570 int send_pack(struct send_pack_args *my_args,
571               const char *dest, struct remote *remote,
572               int nr_heads, const char **heads)
573 {
574         int fd[2], ret;
575         struct child_process *conn;
576
577         memcpy(&args, my_args, sizeof(args));
578
579         verify_remote_names(nr_heads, heads);
580
581         conn = git_connect(fd, dest, args.receivepack, args.verbose ? CONNECT_VERBOSE : 0);
582         ret = do_send_pack(fd[0], fd[1], remote, dest, nr_heads, heads);
583         close(fd[0]);
584         /* do_send_pack always closes fd[1] */
585         ret |= finish_connect(conn);
586         return !!ret;
587 }