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