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