Code

builtin-fetch--tool: make sure not to overstep ls-remote-result buffer.
[git.git] / builtin-fetch--tool.c
1 #include "cache.h"
2 #include "refs.h"
3 #include "commit.h"
5 #define CHUNK_SIZE 1024
7 static char *get_stdin(void)
8 {
9         int offset = 0;
10         char *data = xmalloc(CHUNK_SIZE);
12         while (1) {
13                 int cnt = xread(0, data + offset, CHUNK_SIZE);
14                 if (cnt < 0)
15                         die("error reading standard input: %s",
16                             strerror(errno));
17                 if (cnt == 0) {
18                         data[offset] = 0;
19                         break;
20                 }
21                 offset += cnt;
22                 data = xrealloc(data, offset + CHUNK_SIZE);
23         }
24         return data;
25 }
27 static void show_new(enum object_type type, unsigned char *sha1_new)
28 {
29         fprintf(stderr, "  %s: %s\n", typename(type),
30                 find_unique_abbrev(sha1_new, DEFAULT_ABBREV));
31 }
33 static int update_ref(const char *action,
34                       const char *refname,
35                       unsigned char *sha1,
36                       unsigned char *oldval)
37 {
38         int len;
39         char msg[1024];
40         char *rla = getenv("GIT_REFLOG_ACTION");
41         static struct ref_lock *lock;
43         if (!rla)
44                 rla = "(reflog update)";
45         len = snprintf(msg, sizeof(msg), "%s: %s", rla, action);
46         if (sizeof(msg) <= len)
47                 die("insanely long action");
48         lock = lock_any_ref_for_update(refname, oldval);
49         if (!lock)
50                 return 1;
51         if (write_ref_sha1(lock, sha1, msg) < 0)
52                 return 1;
53         return 0;
54 }
56 static int update_local_ref(const char *name,
57                             const char *new_head,
58                             const char *note,
59                             int verbose, int force)
60 {
61         unsigned char sha1_old[20], sha1_new[20];
62         char oldh[41], newh[41];
63         struct commit *current, *updated;
64         enum object_type type;
66         if (get_sha1_hex(new_head, sha1_new))
67                 die("malformed object name %s", new_head);
69         type = sha1_object_info(sha1_new, NULL);
70         if (type < 0)
71                 die("object %s not found", new_head);
73         if (!*name) {
74                 /* Not storing */
75                 if (verbose) {
76                         fprintf(stderr, "* fetched %s\n", note);
77                         show_new(type, sha1_new);
78                 }
79                 return 0;
80         }
82         if (get_sha1(name, sha1_old)) {
83                 char *msg;
84         just_store:
85                 /* new ref */
86                 if (!strncmp(name, "refs/tags/", 10))
87                         msg = "storing tag";
88                 else
89                         msg = "storing head";
90                 fprintf(stderr, "* %s: storing %s\n",
91                         name, note);
92                 show_new(type, sha1_new);
93                 return update_ref(msg, name, sha1_new, NULL);
94         }
96         if (!hashcmp(sha1_old, sha1_new)) {
97                 if (verbose) {
98                         fprintf(stderr, "* %s: same as %s\n", name, note);
99                         show_new(type, sha1_new);
100                 }
101                 return 0;
102         }
104         if (!strncmp(name, "refs/tags/", 10)) {
105                 fprintf(stderr, "* %s: updating with %s\n", name, note);
106                 show_new(type, sha1_new);
107                 return update_ref("updating tag", name, sha1_new, NULL);
108         }
110         current = lookup_commit_reference(sha1_old);
111         updated = lookup_commit_reference(sha1_new);
112         if (!current || !updated)
113                 goto just_store;
115         strcpy(oldh, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
116         strcpy(newh, find_unique_abbrev(sha1_new, DEFAULT_ABBREV));
118         if (in_merge_bases(current, &updated, 1)) {
119                 fprintf(stderr, "* %s: fast forward to %s\n",
120                         name, note);
121                 fprintf(stderr, "  old..new: %s..%s\n", oldh, newh);
122                 return update_ref("fast forward", name, sha1_new, sha1_old);
123         }
124         if (!force) {
125                 fprintf(stderr,
126                         "* %s: not updating to non-fast forward %s\n",
127                         name, note);
128                 fprintf(stderr,
129                         "  old...new: %s...%s\n", oldh, newh);
130                 return 1;
131         }
132         fprintf(stderr,
133                 "* %s: forcing update to non-fast forward %s\n",
134                 name, note);
135         fprintf(stderr, "  old...new: %s...%s\n", oldh, newh);
136         return update_ref("forced-update", name, sha1_new, sha1_old);
139 static int append_fetch_head(FILE *fp,
140                              const char *head, const char *remote,
141                              const char *remote_name, const char *remote_nick,
142                              const char *local_name, int not_for_merge,
143                              int verbose, int force)
145         struct commit *commit;
146         int remote_len, i, note_len;
147         unsigned char sha1[20];
148         char note[1024];
149         const char *what, *kind;
151         if (get_sha1(head, sha1))
152                 return error("Not a valid object name: %s", head);
153         commit = lookup_commit_reference(sha1);
154         if (!commit)
155                 not_for_merge = 1;
157         if (!strcmp(remote_name, "HEAD")) {
158                 kind = "";
159                 what = "";
160         }
161         else if (!strncmp(remote_name, "refs/heads/", 11)) {
162                 kind = "branch";
163                 what = remote_name + 11;
164         }
165         else if (!strncmp(remote_name, "refs/tags/", 10)) {
166                 kind = "tag";
167                 what = remote_name + 10;
168         }
169         else if (!strncmp(remote_name, "refs/remotes/", 13)) {
170                 kind = "remote branch";
171                 what = remote_name + 13;
172         }
173         else {
174                 kind = "";
175                 what = remote_name;
176         }
178         remote_len = strlen(remote);
179         for (i = remote_len - 1; remote[i] == '/' && 0 <= i; i--)
180                 ;
181         remote_len = i + 1;
182         if (4 < i && !strncmp(".git", remote + i - 3, 4))
183                 remote_len = i - 3;
184         note_len = sprintf(note, "%s\t%s\t",
185                            sha1_to_hex(commit ? commit->object.sha1 : sha1),
186                            not_for_merge ? "not-for-merge" : "");
187         if (*what) {
188                 if (*kind)
189                         note_len += sprintf(note + note_len, "%s ", kind);
190                 note_len += sprintf(note + note_len, "'%s' of ", what);
191         }
192         note_len += sprintf(note + note_len, "%.*s", remote_len, remote);
193         fprintf(fp, "%s\n", note);
194         return update_local_ref(local_name, head, note, verbose, force);
197 static char *keep;
198 static void remove_keep(void)
200         if (keep && *keep)
201                 unlink(keep);
204 static void remove_keep_on_signal(int signo)
206         remove_keep();
207         signal(SIGINT, SIG_DFL);
208         raise(signo);
211 static char *find_local_name(const char *remote_name, const char *refs,
212                              int *force_p, int *not_for_merge_p)
214         const char *ref = refs;
215         int len = strlen(remote_name);
217         while (ref) {
218                 const char *next;
219                 int single_force, not_for_merge;
221                 while (*ref == '\n')
222                         ref++;
223                 if (!*ref)
224                         break;
225                 next = strchr(ref, '\n');
227                 single_force = not_for_merge = 0;
228                 if (*ref == '+') {
229                         single_force = 1;
230                         ref++;
231                 }
232                 if (*ref == '.') {
233                         not_for_merge = 1;
234                         ref++;
235                         if (*ref == '+') {
236                                 single_force = 1;
237                                 ref++;
238                         }
239                 }
240                 if (!strncmp(remote_name, ref, len) && ref[len] == ':') {
241                         const char *local_part = ref + len + 1;
242                         char *ret;
243                         int retlen;
245                         if (!next)
246                                 retlen = strlen(local_part);
247                         else
248                                 retlen = next - local_part;
249                         ret = xmalloc(retlen + 1);
250                         memcpy(ret, local_part, retlen);
251                         ret[retlen] = 0;
252                         *force_p = single_force;
253                         *not_for_merge_p = not_for_merge;
254                         return ret;
255                 }
256                 ref = next;
257         }
258         return NULL;
261 static int fetch_native_store(FILE *fp,
262                               const char *remote,
263                               const char *remote_nick,
264                               const char *refs,
265                               int verbose, int force)
267         char buffer[1024];
268         int err = 0;
270         signal(SIGINT, remove_keep_on_signal);
271         atexit(remove_keep);
273         while (fgets(buffer, sizeof(buffer), stdin)) {
274                 int len;
275                 char *cp;
276                 char *local_name;
277                 int single_force, not_for_merge;
279                 for (cp = buffer; *cp && !isspace(*cp); cp++)
280                         ;
281                 if (*cp)
282                         *cp++ = 0;
283                 len = strlen(cp);
284                 if (len && cp[len-1] == '\n')
285                         cp[--len] = 0;
286                 if (!strcmp(buffer, "failed"))
287                         die("Fetch failure: %s", remote);
288                 if (!strcmp(buffer, "pack"))
289                         continue;
290                 if (!strcmp(buffer, "keep")) {
291                         char *od = get_object_directory();
292                         int len = strlen(od) + strlen(cp) + 50;
293                         keep = xmalloc(len);
294                         sprintf(keep, "%s/pack/pack-%s.keep", od, cp);
295                         continue;
296                 }
298                 local_name = find_local_name(cp, refs,
299                                              &single_force, &not_for_merge);
300                 if (!local_name)
301                         continue;
302                 err |= append_fetch_head(fp,
303                                          buffer, remote, cp, remote_nick,
304                                          local_name, not_for_merge,
305                                          verbose, force || single_force);
306         }
307         return err;
310 static int parse_reflist(const char *reflist)
312         const char *ref;
314         printf("refs='");
315         for (ref = reflist; ref; ) {
316                 const char *next;
317                 while (*ref && isspace(*ref))
318                         ref++;
319                 if (!*ref)
320                         break;
321                 for (next = ref; *next && !isspace(*next); next++)
322                         ;
323                 printf("\n%.*s", (int)(next - ref), ref);
324                 ref = next;
325         }
326         printf("'\n");
328         printf("rref='");
329         for (ref = reflist; ref; ) {
330                 const char *next, *colon;
331                 while (*ref && isspace(*ref))
332                         ref++;
333                 if (!*ref)
334                         break;
335                 for (next = ref; *next && !isspace(*next); next++)
336                         ;
337                 if (*ref == '.')
338                         ref++;
339                 if (*ref == '+')
340                         ref++;
341                 colon = strchr(ref, ':');
342                 putchar('\n');
343                 printf("%.*s", (int)((colon ? colon : next) - ref), ref);
344                 ref = next;
345         }
346         printf("'\n");
347         return 0;
350 static int expand_refs_wildcard(const char *ls_remote_result, int numrefs,
351                                 const char **refs)
353         int i, matchlen, replacelen;
354         int found_one = 0;
355         const char *remote = *refs++;
356         numrefs--;
358         if (numrefs == 0) {
359                 fprintf(stderr, "Nothing specified for fetching with remote.%s.fetch\n",
360                         remote);
361                 printf("empty\n");
362         }
364         for (i = 0; i < numrefs; i++) {
365                 const char *ref = refs[i];
366                 const char *lref = ref;
367                 const char *colon;
368                 const char *tail;
369                 const char *ls;
370                 const char *next;
372                 if (*lref == '+')
373                         lref++;
374                 colon = strchr(lref, ':');
375                 tail = lref + strlen(lref);
376                 if (!(colon &&
377                       2 < colon - lref &&
378                       colon[-1] == '*' &&
379                       colon[-2] == '/' &&
380                       2 < tail - (colon + 1) &&
381                       tail[-1] == '*' &&
382                       tail[-2] == '/')) {
383                         /* not a glob */
384                         if (!found_one++)
385                                 printf("explicit\n");
386                         printf("%s\n", ref);
387                         continue;
388                 }
390                 /* glob */
391                 if (!found_one++)
392                         printf("glob\n");
394                 /* lref to colon-2 is remote hierarchy name;
395                  * colon+1 to tail-2 is local.
396                  */
397                 matchlen = (colon-1) - lref;
398                 replacelen = (tail-1) - (colon+1);
399                 for (ls = ls_remote_result; ls; ls = next) {
400                         const char *eol;
401                         unsigned char sha1[20];
402                         int namelen;
404                         while (*ls && isspace(*ls))
405                                 ls++;
406                         next = strchr(ls, '\n');
407                         eol = !next ? (ls + strlen(ls)) : next;
408                         if (!memcmp("^{}", eol-3, 3))
409                                 continue;
410                         if (eol - ls < 40)
411                                 continue;
412                         if (get_sha1_hex(ls, sha1))
413                                 continue;
414                         ls += 40;
415                         while (ls < eol && isspace(*ls))
416                                 ls++;
417                         /* ls to next (or eol) is the name.
418                          * is it identical to lref to colon-2?
419                          */
420                         if ((eol - ls) <= matchlen ||
421                             strncmp(ls, lref, matchlen))
422                                 continue;
424                         /* Yes, it is a match */
425                         namelen = eol - ls;
426                         if (lref != ref)
427                                 putchar('+');
428                         printf("%.*s:%.*s%.*s\n",
429                                namelen, ls,
430                                replacelen, colon + 1,
431                                namelen - matchlen, ls + matchlen);
432                 }
433         }
434         return 0;
437 int cmd_fetch__tool(int argc, const char **argv, const char *prefix)
439         int verbose = 0;
440         int force = 0;
442         while (1 < argc) {
443                 const char *arg = argv[1];
444                 if (!strcmp("-v", arg))
445                         verbose = 1;
446                 else if (!strcmp("-f", arg))
447                         force = 1;
448                 else
449                         break;
450                 argc--;
451                 argv++;
452         }
454         if (argc <= 1)
455                 return error("Missing subcommand");
457         if (!strcmp("append-fetch-head", argv[1])) {
458                 int result;
459                 FILE *fp;
461                 if (argc != 8)
462                         return error("append-fetch-head takes 6 args");
463                 fp = fopen(git_path("FETCH_HEAD"), "a");
464                 result = append_fetch_head(fp, argv[2], argv[3],
465                                            argv[4], argv[5],
466                                            argv[6], !!argv[7][0],
467                                            verbose, force);
468                 fclose(fp);
469                 return result;
470         }
471         if (!strcmp("update-local-ref", argv[1])) {
472                 if (argc != 5)
473                         return error("update-local-ref takes 3 args");
474                 return update_local_ref(argv[2], argv[3], argv[4],
475                                         verbose, force);
476         }
477         if (!strcmp("native-store", argv[1])) {
478                 int result;
479                 FILE *fp;
481                 if (argc != 5)
482                         return error("fetch-native-store takes 3 args");
483                 fp = fopen(git_path("FETCH_HEAD"), "a");
484                 result = fetch_native_store(fp, argv[2], argv[3], argv[4],
485                                             verbose, force);
486                 fclose(fp);
487                 return result;
488         }
489         if (!strcmp("parse-reflist", argv[1])) {
490                 const char *reflist;
491                 if (argc != 3)
492                         return error("parse-reflist takes 1 arg");
493                 reflist = argv[2];
494                 if (!strcmp(reflist, "-"))
495                         reflist = get_stdin();
496                 return parse_reflist(reflist);
497         }
498         if (!strcmp("expand-refs-wildcard", argv[1])) {
499                 const char *reflist;
500                 if (argc < 4)
501                         return error("expand-refs-wildcard takes at least 2 args");
502                 reflist = argv[2];
503                 if (!strcmp(reflist, "-"))
504                         reflist = get_stdin();
505                 return expand_refs_wildcard(reflist, argc - 3, argv + 3);
506         }
508         return error("Unknown subcommand: %s", argv[1]);