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