Code

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