Modularize commit-walker
[git] / builtin-fetch--tool.c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "refs.h"
4 #include "commit.h"
5
6 #define CHUNK_SIZE 1024
7
8 static char *get_stdin(void)
9 {
10         size_t offset = 0;
11         char *data = xmalloc(CHUNK_SIZE);
12
13         while (1) {
14                 ssize_t cnt = xread(0, data + offset, CHUNK_SIZE);
15                 if (cnt < 0)
16                         die("error reading standard input: %s",
17                             strerror(errno));
18                 if (cnt == 0) {
19                         data[offset] = 0;
20                         break;
21                 }
22                 offset += cnt;
23                 data = xrealloc(data, offset + CHUNK_SIZE);
24         }
25         return data;
26 }
27
28 static void show_new(enum object_type type, unsigned char *sha1_new)
29 {
30         fprintf(stderr, "  %s: %s\n", typename(type),
31                 find_unique_abbrev(sha1_new, DEFAULT_ABBREV));
32 }
33
34 static int update_ref_env(const char *action,
35                       const char *refname,
36                       unsigned char *sha1,
37                       unsigned char *oldval)
38 {
39         char msg[1024];
40         char *rla = getenv("GIT_REFLOG_ACTION");
41
42         if (!rla)
43                 rla = "(reflog update)";
44         if (snprintf(msg, sizeof(msg), "%s: %s", rla, action) >= sizeof(msg))
45                 warning("reflog message too long: %.*s...", 50, msg);
46         return update_ref(msg, refname, sha1, oldval, 0, QUIET_ON_ERR);
47 }
48
49 static int update_local_ref(const char *name,
50                             const char *new_head,
51                             const char *note,
52                             int verbose, int force)
53 {
54         unsigned char sha1_old[20], sha1_new[20];
55         char oldh[41], newh[41];
56         struct commit *current, *updated;
57         enum object_type type;
58
59         if (get_sha1_hex(new_head, sha1_new))
60                 die("malformed object name %s", new_head);
61
62         type = sha1_object_info(sha1_new, NULL);
63         if (type < 0)
64                 die("object %s not found", new_head);
65
66         if (!*name) {
67                 /* Not storing */
68                 if (verbose) {
69                         fprintf(stderr, "* fetched %s\n", note);
70                         show_new(type, sha1_new);
71                 }
72                 return 0;
73         }
74
75         if (get_sha1(name, sha1_old)) {
76                 char *msg;
77         just_store:
78                 /* new ref */
79                 if (!strncmp(name, "refs/tags/", 10))
80                         msg = "storing tag";
81                 else
82                         msg = "storing head";
83                 fprintf(stderr, "* %s: storing %s\n",
84                         name, note);
85                 show_new(type, sha1_new);
86                 return update_ref_env(msg, name, sha1_new, NULL);
87         }
88
89         if (!hashcmp(sha1_old, sha1_new)) {
90                 if (verbose) {
91                         fprintf(stderr, "* %s: same as %s\n", name, note);
92                         show_new(type, sha1_new);
93                 }
94                 return 0;
95         }
96
97         if (!strncmp(name, "refs/tags/", 10)) {
98                 fprintf(stderr, "* %s: updating with %s\n", name, note);
99                 show_new(type, sha1_new);
100                 return update_ref_env("updating tag", name, sha1_new, NULL);
101         }
102
103         current = lookup_commit_reference(sha1_old);
104         updated = lookup_commit_reference(sha1_new);
105         if (!current || !updated)
106                 goto just_store;
107
108         strcpy(oldh, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
109         strcpy(newh, find_unique_abbrev(sha1_new, DEFAULT_ABBREV));
110
111         if (in_merge_bases(current, &updated, 1)) {
112                 fprintf(stderr, "* %s: fast forward to %s\n",
113                         name, note);
114                 fprintf(stderr, "  old..new: %s..%s\n", oldh, newh);
115                 return update_ref_env("fast forward", name, sha1_new, sha1_old);
116         }
117         if (!force) {
118                 fprintf(stderr,
119                         "* %s: not updating to non-fast forward %s\n",
120                         name, note);
121                 fprintf(stderr,
122                         "  old...new: %s...%s\n", oldh, newh);
123                 return 1;
124         }
125         fprintf(stderr,
126                 "* %s: forcing update to non-fast forward %s\n",
127                 name, note);
128         fprintf(stderr, "  old...new: %s...%s\n", oldh, newh);
129         return update_ref_env("forced-update", name, sha1_new, sha1_old);
130 }
131
132 static int append_fetch_head(FILE *fp,
133                              const char *head, const char *remote,
134                              const char *remote_name, const char *remote_nick,
135                              const char *local_name, int not_for_merge,
136                              int verbose, int force)
137 {
138         struct commit *commit;
139         int remote_len, i, note_len;
140         unsigned char sha1[20];
141         char note[1024];
142         const char *what, *kind;
143
144         if (get_sha1(head, sha1))
145                 return error("Not a valid object name: %s", head);
146         commit = lookup_commit_reference(sha1);
147         if (!commit)
148                 not_for_merge = 1;
149
150         if (!strcmp(remote_name, "HEAD")) {
151                 kind = "";
152                 what = "";
153         }
154         else if (!strncmp(remote_name, "refs/heads/", 11)) {
155                 kind = "branch";
156                 what = remote_name + 11;
157         }
158         else if (!strncmp(remote_name, "refs/tags/", 10)) {
159                 kind = "tag";
160                 what = remote_name + 10;
161         }
162         else if (!strncmp(remote_name, "refs/remotes/", 13)) {
163                 kind = "remote branch";
164                 what = remote_name + 13;
165         }
166         else {
167                 kind = "";
168                 what = remote_name;
169         }
170
171         remote_len = strlen(remote);
172         for (i = remote_len - 1; remote[i] == '/' && 0 <= i; i--)
173                 ;
174         remote_len = i + 1;
175         if (4 < i && !strncmp(".git", remote + i - 3, 4))
176                 remote_len = i - 3;
177
178         note_len = 0;
179         if (*what) {
180                 if (*kind)
181                         note_len += sprintf(note + note_len, "%s ", kind);
182                 note_len += sprintf(note + note_len, "'%s' of ", what);
183         }
184         note_len += sprintf(note + note_len, "%.*s", remote_len, remote);
185         fprintf(fp, "%s\t%s\t%s\n",
186                 sha1_to_hex(commit ? commit->object.sha1 : sha1),
187                 not_for_merge ? "not-for-merge" : "",
188                 note);
189         return update_local_ref(local_name, head, note, verbose, force);
190 }
191
192 static char *keep;
193 static void remove_keep(void)
194 {
195         if (keep && *keep)
196                 unlink(keep);
197 }
198
199 static void remove_keep_on_signal(int signo)
200 {
201         remove_keep();
202         signal(SIGINT, SIG_DFL);
203         raise(signo);
204 }
205
206 static char *find_local_name(const char *remote_name, const char *refs,
207                              int *force_p, int *not_for_merge_p)
208 {
209         const char *ref = refs;
210         int len = strlen(remote_name);
211
212         while (ref) {
213                 const char *next;
214                 int single_force, not_for_merge;
215
216                 while (*ref == '\n')
217                         ref++;
218                 if (!*ref)
219                         break;
220                 next = strchr(ref, '\n');
221
222                 single_force = not_for_merge = 0;
223                 if (*ref == '+') {
224                         single_force = 1;
225                         ref++;
226                 }
227                 if (*ref == '.') {
228                         not_for_merge = 1;
229                         ref++;
230                         if (*ref == '+') {
231                                 single_force = 1;
232                                 ref++;
233                         }
234                 }
235                 if (!strncmp(remote_name, ref, len) && ref[len] == ':') {
236                         const char *local_part = ref + len + 1;
237                         char *ret;
238                         int retlen;
239
240                         if (!next)
241                                 retlen = strlen(local_part);
242                         else
243                                 retlen = next - local_part;
244                         ret = xmalloc(retlen + 1);
245                         memcpy(ret, local_part, retlen);
246                         ret[retlen] = 0;
247                         *force_p = single_force;
248                         *not_for_merge_p = not_for_merge;
249                         return ret;
250                 }
251                 ref = next;
252         }
253         return NULL;
254 }
255
256 static int fetch_native_store(FILE *fp,
257                               const char *remote,
258                               const char *remote_nick,
259                               const char *refs,
260                               int verbose, int force)
261 {
262         char buffer[1024];
263         int err = 0;
264
265         signal(SIGINT, remove_keep_on_signal);
266         atexit(remove_keep);
267
268         while (fgets(buffer, sizeof(buffer), stdin)) {
269                 int len;
270                 char *cp;
271                 char *local_name;
272                 int single_force, not_for_merge;
273
274                 for (cp = buffer; *cp && !isspace(*cp); cp++)
275                         ;
276                 if (*cp)
277                         *cp++ = 0;
278                 len = strlen(cp);
279                 if (len && cp[len-1] == '\n')
280                         cp[--len] = 0;
281                 if (!strcmp(buffer, "failed"))
282                         die("Fetch failure: %s", remote);
283                 if (!strcmp(buffer, "pack"))
284                         continue;
285                 if (!strcmp(buffer, "keep")) {
286                         char *od = get_object_directory();
287                         int len = strlen(od) + strlen(cp) + 50;
288                         keep = xmalloc(len);
289                         sprintf(keep, "%s/pack/pack-%s.keep", od, cp);
290                         continue;
291                 }
292
293                 local_name = find_local_name(cp, refs,
294                                              &single_force, &not_for_merge);
295                 if (!local_name)
296                         continue;
297                 err |= append_fetch_head(fp,
298                                          buffer, remote, cp, remote_nick,
299                                          local_name, not_for_merge,
300                                          verbose, force || single_force);
301         }
302         return err;
303 }
304
305 static int parse_reflist(const char *reflist)
306 {
307         const char *ref;
308
309         printf("refs='");
310         for (ref = reflist; ref; ) {
311                 const char *next;
312                 while (*ref && isspace(*ref))
313                         ref++;
314                 if (!*ref)
315                         break;
316                 for (next = ref; *next && !isspace(*next); next++)
317                         ;
318                 printf("\n%.*s", (int)(next - ref), ref);
319                 ref = next;
320         }
321         printf("'\n");
322
323         printf("rref='");
324         for (ref = reflist; ref; ) {
325                 const char *next, *colon;
326                 while (*ref && isspace(*ref))
327                         ref++;
328                 if (!*ref)
329                         break;
330                 for (next = ref; *next && !isspace(*next); next++)
331                         ;
332                 if (*ref == '.')
333                         ref++;
334                 if (*ref == '+')
335                         ref++;
336                 colon = strchr(ref, ':');
337                 putchar('\n');
338                 printf("%.*s", (int)((colon ? colon : next) - ref), ref);
339                 ref = next;
340         }
341         printf("'\n");
342         return 0;
343 }
344
345 static int expand_refs_wildcard(const char *ls_remote_result, int numrefs,
346                                 const char **refs)
347 {
348         int i, matchlen, replacelen;
349         int found_one = 0;
350         const char *remote = *refs++;
351         numrefs--;
352
353         if (numrefs == 0) {
354                 fprintf(stderr, "Nothing specified for fetching with remote.%s.fetch\n",
355                         remote);
356                 printf("empty\n");
357         }
358
359         for (i = 0; i < numrefs; i++) {
360                 const char *ref = refs[i];
361                 const char *lref = ref;
362                 const char *colon;
363                 const char *tail;
364                 const char *ls;
365                 const char *next;
366
367                 if (*lref == '+')
368                         lref++;
369                 colon = strchr(lref, ':');
370                 tail = lref + strlen(lref);
371                 if (!(colon &&
372                       2 < colon - lref &&
373                       colon[-1] == '*' &&
374                       colon[-2] == '/' &&
375                       2 < tail - (colon + 1) &&
376                       tail[-1] == '*' &&
377                       tail[-2] == '/')) {
378                         /* not a glob */
379                         if (!found_one++)
380                                 printf("explicit\n");
381                         printf("%s\n", ref);
382                         continue;
383                 }
384
385                 /* glob */
386                 if (!found_one++)
387                         printf("glob\n");
388
389                 /* lref to colon-2 is remote hierarchy name;
390                  * colon+1 to tail-2 is local.
391                  */
392                 matchlen = (colon-1) - lref;
393                 replacelen = (tail-1) - (colon+1);
394                 for (ls = ls_remote_result; ls; ls = next) {
395                         const char *eol;
396                         unsigned char sha1[20];
397                         int namelen;
398
399                         while (*ls && isspace(*ls))
400                                 ls++;
401                         next = strchr(ls, '\n');
402                         eol = !next ? (ls + strlen(ls)) : next;
403                         if (!memcmp("^{}", eol-3, 3))
404                                 continue;
405                         if (eol - ls < 40)
406                                 continue;
407                         if (get_sha1_hex(ls, sha1))
408                                 continue;
409                         ls += 40;
410                         while (ls < eol && isspace(*ls))
411                                 ls++;
412                         /* ls to next (or eol) is the name.
413                          * is it identical to lref to colon-2?
414                          */
415                         if ((eol - ls) <= matchlen ||
416                             strncmp(ls, lref, matchlen))
417                                 continue;
418
419                         /* Yes, it is a match */
420                         namelen = eol - ls;
421                         if (lref != ref)
422                                 putchar('+');
423                         printf("%.*s:%.*s%.*s\n",
424                                namelen, ls,
425                                replacelen, colon + 1,
426                                namelen - matchlen, ls + matchlen);
427                 }
428         }
429         return 0;
430 }
431
432 static int pick_rref(int sha1_only, const char *rref, const char *ls_remote_result)
433 {
434         int err = 0;
435         int lrr_count = lrr_count, i, pass;
436         const char *cp;
437         struct lrr {
438                 const char *line;
439                 const char *name;
440                 int namelen;
441                 int shown;
442         } *lrr_list = lrr_list;
443
444         for (pass = 0; pass < 2; pass++) {
445                 /* pass 0 counts and allocates, pass 1 fills... */
446                 cp = ls_remote_result;
447                 i = 0;
448                 while (1) {
449                         const char *np;
450                         while (*cp && isspace(*cp))
451                                 cp++;
452                         if (!*cp)
453                                 break;
454                         np = strchr(cp, '\n');
455                         if (!np)
456                                 np = cp + strlen(cp);
457                         if (pass) {
458                                 lrr_list[i].line = cp;
459                                 lrr_list[i].name = cp + 41;
460                                 lrr_list[i].namelen = np - (cp + 41);
461                         }
462                         i++;
463                         cp = np;
464                 }
465                 if (!pass) {
466                         lrr_count = i;
467                         lrr_list = xcalloc(lrr_count, sizeof(*lrr_list));
468                 }
469         }
470
471         while (1) {
472                 const char *next;
473                 int rreflen;
474                 int i;
475
476                 while (*rref && isspace(*rref))
477                         rref++;
478                 if (!*rref)
479                         break;
480                 next = strchr(rref, '\n');
481                 if (!next)
482                         next = rref + strlen(rref);
483                 rreflen = next - rref;
484
485                 for (i = 0; i < lrr_count; i++) {
486                         struct lrr *lrr = &(lrr_list[i]);
487
488                         if (rreflen == lrr->namelen &&
489                             !memcmp(lrr->name, rref, rreflen)) {
490                                 if (!lrr->shown)
491                                         printf("%.*s\n",
492                                                sha1_only ? 40 : lrr->namelen + 41,
493                                                lrr->line);
494                                 lrr->shown = 1;
495                                 break;
496                         }
497                 }
498                 if (lrr_count <= i) {
499                         error("pick-rref: %.*s not found", rreflen, rref);
500                         err = 1;
501                 }
502                 rref = next;
503         }
504         free(lrr_list);
505         return err;
506 }
507
508 int cmd_fetch__tool(int argc, const char **argv, const char *prefix)
509 {
510         int verbose = 0;
511         int force = 0;
512         int sopt = 0;
513
514         while (1 < argc) {
515                 const char *arg = argv[1];
516                 if (!strcmp("-v", arg))
517                         verbose = 1;
518                 else if (!strcmp("-f", arg))
519                         force = 1;
520                 else if (!strcmp("-s", arg))
521                         sopt = 1;
522                 else
523                         break;
524                 argc--;
525                 argv++;
526         }
527
528         if (argc <= 1)
529                 return error("Missing subcommand");
530
531         if (!strcmp("append-fetch-head", argv[1])) {
532                 int result;
533                 FILE *fp;
534
535                 if (argc != 8)
536                         return error("append-fetch-head takes 6 args");
537                 fp = fopen(git_path("FETCH_HEAD"), "a");
538                 result = append_fetch_head(fp, argv[2], argv[3],
539                                            argv[4], argv[5],
540                                            argv[6], !!argv[7][0],
541                                            verbose, force);
542                 fclose(fp);
543                 return result;
544         }
545         if (!strcmp("native-store", argv[1])) {
546                 int result;
547                 FILE *fp;
548
549                 if (argc != 5)
550                         return error("fetch-native-store takes 3 args");
551                 fp = fopen(git_path("FETCH_HEAD"), "a");
552                 result = fetch_native_store(fp, argv[2], argv[3], argv[4],
553                                             verbose, force);
554                 fclose(fp);
555                 return result;
556         }
557         if (!strcmp("parse-reflist", argv[1])) {
558                 const char *reflist;
559                 if (argc != 3)
560                         return error("parse-reflist takes 1 arg");
561                 reflist = argv[2];
562                 if (!strcmp(reflist, "-"))
563                         reflist = get_stdin();
564                 return parse_reflist(reflist);
565         }
566         if (!strcmp("pick-rref", argv[1])) {
567                 const char *ls_remote_result;
568                 if (argc != 4)
569                         return error("pick-rref takes 2 args");
570                 ls_remote_result = argv[3];
571                 if (!strcmp(ls_remote_result, "-"))
572                         ls_remote_result = get_stdin();
573                 return pick_rref(sopt, argv[2], ls_remote_result);
574         }
575         if (!strcmp("expand-refs-wildcard", argv[1])) {
576                 const char *reflist;
577                 if (argc < 4)
578                         return error("expand-refs-wildcard takes at least 2 args");
579                 reflist = argv[2];
580                 if (!strcmp(reflist, "-"))
581                         reflist = get_stdin();
582                 return expand_refs_wildcard(reflist, argc - 3, argv + 3);
583         }
584
585         return error("Unknown subcommand: %s", argv[1]);
586 }