6 #define CHUNK_SIZE 1024
8 static char *get_stdin(void)
11 char *data = xmalloc(CHUNK_SIZE);
14 ssize_t cnt = xread(0, data + offset, CHUNK_SIZE);
16 die("error reading standard input: %s",
23 data = xrealloc(data, offset + CHUNK_SIZE);
28 static void show_new(enum object_type type, unsigned char *sha1_new)
30 fprintf(stderr, " %s: %s\n", typename(type),
31 find_unique_abbrev(sha1_new, DEFAULT_ABBREV));
34 static int update_ref_env(const char *action,
37 unsigned char *oldval)
40 char *rla = getenv("GIT_REFLOG_ACTION");
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);
49 static int update_local_ref(const char *name,
52 int verbose, int force)
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;
59 if (get_sha1_hex(new_head, sha1_new))
60 die("malformed object name %s", new_head);
62 type = sha1_object_info(sha1_new, NULL);
64 die("object %s not found", new_head);
69 fprintf(stderr, "* fetched %s\n", note);
70 show_new(type, sha1_new);
75 if (get_sha1(name, sha1_old)) {
79 if (!strncmp(name, "refs/tags/", 10))
83 fprintf(stderr, "* %s: storing %s\n",
85 show_new(type, sha1_new);
86 return update_ref_env(msg, name, sha1_new, NULL);
89 if (!hashcmp(sha1_old, sha1_new)) {
91 fprintf(stderr, "* %s: same as %s\n", name, note);
92 show_new(type, sha1_new);
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);
103 current = lookup_commit_reference(sha1_old);
104 updated = lookup_commit_reference(sha1_new);
105 if (!current || !updated)
108 strcpy(oldh, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
109 strcpy(newh, find_unique_abbrev(sha1_new, DEFAULT_ABBREV));
111 if (in_merge_bases(current, &updated, 1)) {
112 fprintf(stderr, "* %s: fast forward to %s\n",
114 fprintf(stderr, " old..new: %s..%s\n", oldh, newh);
115 return update_ref_env("fast forward", name, sha1_new, sha1_old);
119 "* %s: not updating to non-fast forward %s\n",
122 " old...new: %s...%s\n", oldh, newh);
126 "* %s: forcing update to non-fast forward %s\n",
128 fprintf(stderr, " old...new: %s...%s\n", oldh, newh);
129 return update_ref_env("forced-update", name, sha1_new, sha1_old);
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)
138 struct commit *commit;
139 int remote_len, i, note_len;
140 unsigned char sha1[20];
142 const char *what, *kind;
144 if (get_sha1(head, sha1))
145 return error("Not a valid object name: %s", head);
146 commit = lookup_commit_reference(sha1);
150 if (!strcmp(remote_name, "HEAD")) {
154 else if (!strncmp(remote_name, "refs/heads/", 11)) {
156 what = remote_name + 11;
158 else if (!strncmp(remote_name, "refs/tags/", 10)) {
160 what = remote_name + 10;
162 else if (!strncmp(remote_name, "refs/remotes/", 13)) {
163 kind = "remote branch";
164 what = remote_name + 13;
171 remote_len = strlen(remote);
172 for (i = remote_len - 1; remote[i] == '/' && 0 <= i; i--)
175 if (4 < i && !strncmp(".git", remote + i - 3, 4))
181 note_len += sprintf(note + note_len, "%s ", kind);
182 note_len += sprintf(note + note_len, "'%s' of ", what);
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" : "",
189 return update_local_ref(local_name, head, note, verbose, force);
193 static void remove_keep(void)
199 static void remove_keep_on_signal(int signo)
202 signal(SIGINT, SIG_DFL);
206 static char *find_local_name(const char *remote_name, const char *refs,
207 int *force_p, int *not_for_merge_p)
209 const char *ref = refs;
210 int len = strlen(remote_name);
214 int single_force, not_for_merge;
220 next = strchr(ref, '\n');
222 single_force = not_for_merge = 0;
235 if (!strncmp(remote_name, ref, len) && ref[len] == ':') {
236 const char *local_part = ref + len + 1;
241 retlen = strlen(local_part);
243 retlen = next - local_part;
244 ret = xmalloc(retlen + 1);
245 memcpy(ret, local_part, retlen);
247 *force_p = single_force;
248 *not_for_merge_p = not_for_merge;
256 static int fetch_native_store(FILE *fp,
258 const char *remote_nick,
260 int verbose, int force)
265 signal(SIGINT, remove_keep_on_signal);
268 while (fgets(buffer, sizeof(buffer), stdin)) {
272 int single_force, not_for_merge;
274 for (cp = buffer; *cp && !isspace(*cp); cp++)
279 if (len && cp[len-1] == '\n')
281 if (!strcmp(buffer, "failed"))
282 die("Fetch failure: %s", remote);
283 if (!strcmp(buffer, "pack"))
285 if (!strcmp(buffer, "keep")) {
286 char *od = get_object_directory();
287 int len = strlen(od) + strlen(cp) + 50;
289 sprintf(keep, "%s/pack/pack-%s.keep", od, cp);
293 local_name = find_local_name(cp, refs,
294 &single_force, ¬_for_merge);
297 err |= append_fetch_head(fp,
298 buffer, remote, cp, remote_nick,
299 local_name, not_for_merge,
300 verbose, force || single_force);
305 static int parse_reflist(const char *reflist)
310 for (ref = reflist; ref; ) {
312 while (*ref && isspace(*ref))
316 for (next = ref; *next && !isspace(*next); next++)
318 printf("\n%.*s", (int)(next - ref), ref);
324 for (ref = reflist; ref; ) {
325 const char *next, *colon;
326 while (*ref && isspace(*ref))
330 for (next = ref; *next && !isspace(*next); next++)
336 colon = strchr(ref, ':');
338 printf("%.*s", (int)((colon ? colon : next) - ref), ref);
345 static int expand_refs_wildcard(const char *ls_remote_result, int numrefs,
348 int i, matchlen, replacelen;
350 const char *remote = *refs++;
354 fprintf(stderr, "Nothing specified for fetching with remote.%s.fetch\n",
359 for (i = 0; i < numrefs; i++) {
360 const char *ref = refs[i];
361 const char *lref = ref;
369 colon = strchr(lref, ':');
370 tail = lref + strlen(lref);
375 2 < tail - (colon + 1) &&
380 printf("explicit\n");
389 /* lref to colon-2 is remote hierarchy name;
390 * colon+1 to tail-2 is local.
392 matchlen = (colon-1) - lref;
393 replacelen = (tail-1) - (colon+1);
394 for (ls = ls_remote_result; ls; ls = next) {
396 unsigned char sha1[20];
399 while (*ls && isspace(*ls))
401 next = strchr(ls, '\n');
402 eol = !next ? (ls + strlen(ls)) : next;
403 if (!memcmp("^{}", eol-3, 3))
407 if (get_sha1_hex(ls, sha1))
410 while (ls < eol && isspace(*ls))
412 /* ls to next (or eol) is the name.
413 * is it identical to lref to colon-2?
415 if ((eol - ls) <= matchlen ||
416 strncmp(ls, lref, matchlen))
419 /* Yes, it is a match */
423 printf("%.*s:%.*s%.*s\n",
425 replacelen, colon + 1,
426 namelen - matchlen, ls + matchlen);
432 static int pick_rref(int sha1_only, const char *rref, const char *ls_remote_result)
435 int lrr_count = lrr_count, i, pass;
442 } *lrr_list = lrr_list;
444 for (pass = 0; pass < 2; pass++) {
445 /* pass 0 counts and allocates, pass 1 fills... */
446 cp = ls_remote_result;
450 while (*cp && isspace(*cp))
454 np = strchr(cp, '\n');
456 np = cp + strlen(cp);
458 lrr_list[i].line = cp;
459 lrr_list[i].name = cp + 41;
460 lrr_list[i].namelen = np - (cp + 41);
467 lrr_list = xcalloc(lrr_count, sizeof(*lrr_list));
476 while (*rref && isspace(*rref))
480 next = strchr(rref, '\n');
482 next = rref + strlen(rref);
483 rreflen = next - rref;
485 for (i = 0; i < lrr_count; i++) {
486 struct lrr *lrr = &(lrr_list[i]);
488 if (rreflen == lrr->namelen &&
489 !memcmp(lrr->name, rref, rreflen)) {
492 sha1_only ? 40 : lrr->namelen + 41,
498 if (lrr_count <= i) {
499 error("pick-rref: %.*s not found", rreflen, rref);
508 int cmd_fetch__tool(int argc, const char **argv, const char *prefix)
515 const char *arg = argv[1];
516 if (!strcmp("-v", arg))
518 else if (!strcmp("-f", arg))
520 else if (!strcmp("-s", arg))
529 return error("Missing subcommand");
531 if (!strcmp("append-fetch-head", argv[1])) {
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],
540 argv[6], !!argv[7][0],
545 if (!strcmp("native-store", argv[1])) {
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],
557 if (!strcmp("parse-reflist", argv[1])) {
560 return error("parse-reflist takes 1 arg");
562 if (!strcmp(reflist, "-"))
563 reflist = get_stdin();
564 return parse_reflist(reflist);
566 if (!strcmp("pick-rref", argv[1])) {
567 const char *ls_remote_result;
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);
575 if (!strcmp("expand-refs-wildcard", argv[1])) {
578 return error("expand-refs-wildcard takes at least 2 args");
580 if (!strcmp(reflist, "-"))
581 reflist = get_stdin();
582 return expand_refs_wildcard(reflist, argc - 3, argv + 3);
585 return error("Unknown subcommand: %s", argv[1]);