7 static char *get_stdin(void)
11 if (strbuf_read(&buf, 0, 1024) < 0) {
12 die("error reading standard input: %s", strerror(errno));
14 return strbuf_detach(&buf);
17 static void show_new(enum object_type type, unsigned char *sha1_new)
19 fprintf(stderr, " %s: %s\n", typename(type),
20 find_unique_abbrev(sha1_new, DEFAULT_ABBREV));
23 static int update_ref_env(const char *action,
26 unsigned char *oldval)
29 char *rla = getenv("GIT_REFLOG_ACTION");
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);
38 static int update_local_ref(const char *name,
41 int verbose, int force)
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;
48 if (get_sha1_hex(new_head, sha1_new))
49 die("malformed object name %s", new_head);
51 type = sha1_object_info(sha1_new, NULL);
53 die("object %s not found", new_head);
58 fprintf(stderr, "* fetched %s\n", note);
59 show_new(type, sha1_new);
64 if (get_sha1(name, sha1_old)) {
68 if (!strncmp(name, "refs/tags/", 10))
72 fprintf(stderr, "* %s: storing %s\n",
74 show_new(type, sha1_new);
75 return update_ref_env(msg, name, sha1_new, NULL);
78 if (!hashcmp(sha1_old, sha1_new)) {
80 fprintf(stderr, "* %s: same as %s\n", name, note);
81 show_new(type, sha1_new);
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);
92 current = lookup_commit_reference(sha1_old);
93 updated = lookup_commit_reference(sha1_new);
94 if (!current || !updated)
97 strcpy(oldh, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
98 strcpy(newh, find_unique_abbrev(sha1_new, DEFAULT_ABBREV));
100 if (in_merge_bases(current, &updated, 1)) {
101 fprintf(stderr, "* %s: fast forward to %s\n",
103 fprintf(stderr, " old..new: %s..%s\n", oldh, newh);
104 return update_ref_env("fast forward", name, sha1_new, sha1_old);
108 "* %s: not updating to non-fast forward %s\n",
111 " old...new: %s...%s\n", oldh, newh);
115 "* %s: forcing update to non-fast forward %s\n",
117 fprintf(stderr, " old...new: %s...%s\n", oldh, newh);
118 return update_ref_env("forced-update", name, sha1_new, sha1_old);
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)
127 struct commit *commit;
128 int remote_len, i, note_len;
129 unsigned char sha1[20];
131 const char *what, *kind;
133 if (get_sha1(head, sha1))
134 return error("Not a valid object name: %s", head);
135 commit = lookup_commit_reference(sha1);
139 if (!strcmp(remote_name, "HEAD")) {
143 else if (!strncmp(remote_name, "refs/heads/", 11)) {
145 what = remote_name + 11;
147 else if (!strncmp(remote_name, "refs/tags/", 10)) {
149 what = remote_name + 10;
151 else if (!strncmp(remote_name, "refs/remotes/", 13)) {
152 kind = "remote branch";
153 what = remote_name + 13;
160 remote_len = strlen(remote);
161 for (i = remote_len - 1; remote[i] == '/' && 0 <= i; i--)
164 if (4 < i && !strncmp(".git", remote + i - 3, 4))
170 note_len += sprintf(note + note_len, "%s ", kind);
171 note_len += sprintf(note + note_len, "'%s' of ", what);
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" : "",
178 return update_local_ref(local_name, head, note, verbose, force);
182 static void remove_keep(void)
188 static void remove_keep_on_signal(int signo)
191 signal(SIGINT, SIG_DFL);
195 static char *find_local_name(const char *remote_name, const char *refs,
196 int *force_p, int *not_for_merge_p)
198 const char *ref = refs;
199 int len = strlen(remote_name);
203 int single_force, not_for_merge;
209 next = strchr(ref, '\n');
211 single_force = not_for_merge = 0;
224 if (!strncmp(remote_name, ref, len) && ref[len] == ':') {
225 const char *local_part = ref + len + 1;
230 retlen = strlen(local_part);
232 retlen = next - local_part;
233 ret = xmalloc(retlen + 1);
234 memcpy(ret, local_part, retlen);
236 *force_p = single_force;
237 *not_for_merge_p = not_for_merge;
245 static int fetch_native_store(FILE *fp,
247 const char *remote_nick,
249 int verbose, int force)
254 signal(SIGINT, remove_keep_on_signal);
257 while (fgets(buffer, sizeof(buffer), stdin)) {
261 int single_force, not_for_merge;
263 for (cp = buffer; *cp && !isspace(*cp); cp++)
268 if (len && cp[len-1] == '\n')
270 if (!strcmp(buffer, "failed"))
271 die("Fetch failure: %s", remote);
272 if (!strcmp(buffer, "pack"))
274 if (!strcmp(buffer, "keep")) {
275 char *od = get_object_directory();
276 int len = strlen(od) + strlen(cp) + 50;
278 sprintf(keep, "%s/pack/pack-%s.keep", od, cp);
282 local_name = find_local_name(cp, refs,
283 &single_force, ¬_for_merge);
286 err |= append_fetch_head(fp,
287 buffer, remote, cp, remote_nick,
288 local_name, not_for_merge,
289 verbose, force || single_force);
294 static int parse_reflist(const char *reflist)
299 for (ref = reflist; ref; ) {
301 while (*ref && isspace(*ref))
305 for (next = ref; *next && !isspace(*next); next++)
307 printf("\n%.*s", (int)(next - ref), ref);
313 for (ref = reflist; ref; ) {
314 const char *next, *colon;
315 while (*ref && isspace(*ref))
319 for (next = ref; *next && !isspace(*next); next++)
325 colon = strchr(ref, ':');
327 printf("%.*s", (int)((colon ? colon : next) - ref), ref);
334 static int expand_refs_wildcard(const char *ls_remote_result, int numrefs,
337 int i, matchlen, replacelen;
339 const char *remote = *refs++;
343 fprintf(stderr, "Nothing specified for fetching with remote.%s.fetch\n",
348 for (i = 0; i < numrefs; i++) {
349 const char *ref = refs[i];
350 const char *lref = ref;
358 colon = strchr(lref, ':');
359 tail = lref + strlen(lref);
364 2 < tail - (colon + 1) &&
369 printf("explicit\n");
378 /* lref to colon-2 is remote hierarchy name;
379 * colon+1 to tail-2 is local.
381 matchlen = (colon-1) - lref;
382 replacelen = (tail-1) - (colon+1);
383 for (ls = ls_remote_result; ls; ls = next) {
385 unsigned char sha1[20];
388 while (*ls && isspace(*ls))
390 next = strchr(ls, '\n');
391 eol = !next ? (ls + strlen(ls)) : next;
392 if (!memcmp("^{}", eol-3, 3))
396 if (get_sha1_hex(ls, sha1))
399 while (ls < eol && isspace(*ls))
401 /* ls to next (or eol) is the name.
402 * is it identical to lref to colon-2?
404 if ((eol - ls) <= matchlen ||
405 strncmp(ls, lref, matchlen))
408 /* Yes, it is a match */
412 printf("%.*s:%.*s%.*s\n",
414 replacelen, colon + 1,
415 namelen - matchlen, ls + matchlen);
421 static int pick_rref(int sha1_only, const char *rref, const char *ls_remote_result)
424 int lrr_count = lrr_count, i, pass;
431 } *lrr_list = lrr_list;
433 for (pass = 0; pass < 2; pass++) {
434 /* pass 0 counts and allocates, pass 1 fills... */
435 cp = ls_remote_result;
439 while (*cp && isspace(*cp))
443 np = strchr(cp, '\n');
445 np = cp + strlen(cp);
447 lrr_list[i].line = cp;
448 lrr_list[i].name = cp + 41;
449 lrr_list[i].namelen = np - (cp + 41);
456 lrr_list = xcalloc(lrr_count, sizeof(*lrr_list));
465 while (*rref && isspace(*rref))
469 next = strchr(rref, '\n');
471 next = rref + strlen(rref);
472 rreflen = next - rref;
474 for (i = 0; i < lrr_count; i++) {
475 struct lrr *lrr = &(lrr_list[i]);
477 if (rreflen == lrr->namelen &&
478 !memcmp(lrr->name, rref, rreflen)) {
481 sha1_only ? 40 : lrr->namelen + 41,
487 if (lrr_count <= i) {
488 error("pick-rref: %.*s not found", rreflen, rref);
497 int cmd_fetch__tool(int argc, const char **argv, const char *prefix)
504 const char *arg = argv[1];
505 if (!strcmp("-v", arg))
507 else if (!strcmp("-f", arg))
509 else if (!strcmp("-s", arg))
518 return error("Missing subcommand");
520 if (!strcmp("append-fetch-head", argv[1])) {
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],
529 argv[6], !!argv[7][0],
534 if (!strcmp("native-store", argv[1])) {
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],
546 if (!strcmp("parse-reflist", argv[1])) {
549 return error("parse-reflist takes 1 arg");
551 if (!strcmp(reflist, "-"))
552 reflist = get_stdin();
553 return parse_reflist(reflist);
555 if (!strcmp("pick-rref", argv[1])) {
556 const char *ls_remote_result;
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);
564 if (!strcmp("expand-refs-wildcard", argv[1])) {
567 return error("expand-refs-wildcard takes at least 2 args");
569 if (!strcmp(reflist, "-"))
570 reflist = get_stdin();
571 return expand_refs_wildcard(reflist, argc - 3, argv + 3);
574 return error("Unknown subcommand: %s", argv[1]);