6 static char *get_stdin(void)
8 struct strbuf buf = STRBUF_INIT;
9 if (strbuf_read(&buf, 0, 1024) < 0) {
10 die("error reading standard input: %s", strerror(errno));
12 return strbuf_detach(&buf, NULL);
15 static void show_new(enum object_type type, unsigned char *sha1_new)
17 fprintf(stderr, " %s: %s\n", typename(type),
18 find_unique_abbrev(sha1_new, DEFAULT_ABBREV));
21 static int update_ref_env(const char *action,
24 unsigned char *oldval)
27 const char *rla = getenv("GIT_REFLOG_ACTION");
30 rla = "(reflog update)";
31 if (snprintf(msg, sizeof(msg), "%s: %s", rla, action) >= sizeof(msg))
32 warning("reflog message too long: %.*s...", 50, msg);
33 return update_ref(msg, refname, sha1, oldval, 0, QUIET_ON_ERR);
36 static int update_local_ref(const char *name,
39 int verbose, int force)
41 unsigned char sha1_old[20], sha1_new[20];
42 char oldh[41], newh[41];
43 struct commit *current, *updated;
44 enum object_type type;
46 if (get_sha1_hex(new_head, sha1_new))
47 die("malformed object name %s", new_head);
49 type = sha1_object_info(sha1_new, NULL);
51 die("object %s not found", new_head);
56 fprintf(stderr, "* fetched %s\n", note);
57 show_new(type, sha1_new);
62 if (get_sha1(name, sha1_old)) {
66 if (!strncmp(name, "refs/tags/", 10))
70 fprintf(stderr, "* %s: storing %s\n",
72 show_new(type, sha1_new);
73 return update_ref_env(msg, name, sha1_new, NULL);
76 if (!hashcmp(sha1_old, sha1_new)) {
78 fprintf(stderr, "* %s: same as %s\n", name, note);
79 show_new(type, sha1_new);
84 if (!strncmp(name, "refs/tags/", 10)) {
85 fprintf(stderr, "* %s: updating with %s\n", name, note);
86 show_new(type, sha1_new);
87 return update_ref_env("updating tag", name, sha1_new, NULL);
90 current = lookup_commit_reference(sha1_old);
91 updated = lookup_commit_reference(sha1_new);
92 if (!current || !updated)
95 strcpy(oldh, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
96 strcpy(newh, find_unique_abbrev(sha1_new, DEFAULT_ABBREV));
98 if (in_merge_bases(current, &updated, 1)) {
99 fprintf(stderr, "* %s: fast forward to %s\n",
101 fprintf(stderr, " old..new: %s..%s\n", oldh, newh);
102 return update_ref_env("fast forward", name, sha1_new, sha1_old);
106 "* %s: not updating to non-fast forward %s\n",
109 " old...new: %s...%s\n", oldh, newh);
113 "* %s: forcing update to non-fast forward %s\n",
115 fprintf(stderr, " old...new: %s...%s\n", oldh, newh);
116 return update_ref_env("forced-update", name, sha1_new, sha1_old);
119 static int append_fetch_head(FILE *fp,
120 const char *head, const char *remote,
121 const char *remote_name, const char *remote_nick,
122 const char *local_name, int not_for_merge,
123 int verbose, int force)
125 struct commit *commit;
126 int remote_len, i, note_len;
127 unsigned char sha1[20];
129 const char *what, *kind;
131 if (get_sha1(head, sha1))
132 return error("Not a valid object name: %s", head);
133 commit = lookup_commit_reference_gently(sha1, 1);
137 if (!strcmp(remote_name, "HEAD")) {
141 else if (!strncmp(remote_name, "refs/heads/", 11)) {
143 what = remote_name + 11;
145 else if (!strncmp(remote_name, "refs/tags/", 10)) {
147 what = remote_name + 10;
149 else if (!strncmp(remote_name, "refs/remotes/", 13)) {
150 kind = "remote branch";
151 what = remote_name + 13;
158 remote_len = strlen(remote);
159 for (i = remote_len - 1; remote[i] == '/' && 0 <= i; i--)
162 if (4 < i && !strncmp(".git", remote + i - 3, 4))
168 note_len += sprintf(note + note_len, "%s ", kind);
169 note_len += sprintf(note + note_len, "'%s' of ", what);
171 note_len += sprintf(note + note_len, "%.*s", remote_len, remote);
172 fprintf(fp, "%s\t%s\t%s\n",
173 sha1_to_hex(commit ? commit->object.sha1 : sha1),
174 not_for_merge ? "not-for-merge" : "",
176 return update_local_ref(local_name, head, note, verbose, force);
180 static void remove_keep(void)
186 static void remove_keep_on_signal(int signo)
189 signal(SIGINT, SIG_DFL);
193 static char *find_local_name(const char *remote_name, const char *refs,
194 int *force_p, int *not_for_merge_p)
196 const char *ref = refs;
197 int len = strlen(remote_name);
201 int single_force, not_for_merge;
207 next = strchr(ref, '\n');
209 single_force = not_for_merge = 0;
222 if (!strncmp(remote_name, ref, len) && ref[len] == ':') {
223 const char *local_part = ref + len + 1;
227 retlen = strlen(local_part);
229 retlen = next - local_part;
230 *force_p = single_force;
231 *not_for_merge_p = not_for_merge;
232 return xmemdupz(local_part, retlen);
239 static int fetch_native_store(FILE *fp,
241 const char *remote_nick,
243 int verbose, int force)
248 signal(SIGINT, remove_keep_on_signal);
251 while (fgets(buffer, sizeof(buffer), stdin)) {
255 int single_force, not_for_merge;
257 for (cp = buffer; *cp && !isspace(*cp); cp++)
262 if (len && cp[len-1] == '\n')
264 if (!strcmp(buffer, "failed"))
265 die("Fetch failure: %s", remote);
266 if (!strcmp(buffer, "pack"))
268 if (!strcmp(buffer, "keep")) {
269 char *od = get_object_directory();
270 int len = strlen(od) + strlen(cp) + 50;
272 sprintf(keep, "%s/pack/pack-%s.keep", od, cp);
276 local_name = find_local_name(cp, refs,
277 &single_force, ¬_for_merge);
280 err |= append_fetch_head(fp,
281 buffer, remote, cp, remote_nick,
282 local_name, not_for_merge,
283 verbose, force || single_force);
288 static int parse_reflist(const char *reflist)
293 for (ref = reflist; ref; ) {
295 while (*ref && isspace(*ref))
299 for (next = ref; *next && !isspace(*next); next++)
301 printf("\n%.*s", (int)(next - ref), ref);
307 for (ref = reflist; ref; ) {
308 const char *next, *colon;
309 while (*ref && isspace(*ref))
313 for (next = ref; *next && !isspace(*next); next++)
319 colon = strchr(ref, ':');
321 printf("%.*s", (int)((colon ? colon : next) - ref), ref);
328 static int expand_refs_wildcard(const char *ls_remote_result, int numrefs,
331 int i, matchlen, replacelen;
333 const char *remote = *refs++;
337 fprintf(stderr, "Nothing specified for fetching with remote.%s.fetch\n",
342 for (i = 0; i < numrefs; i++) {
343 const char *ref = refs[i];
344 const char *lref = ref;
352 colon = strchr(lref, ':');
353 tail = lref + strlen(lref);
358 2 < tail - (colon + 1) &&
363 printf("explicit\n");
372 /* lref to colon-2 is remote hierarchy name;
373 * colon+1 to tail-2 is local.
375 matchlen = (colon-1) - lref;
376 replacelen = (tail-1) - (colon+1);
377 for (ls = ls_remote_result; ls; ls = next) {
379 unsigned char sha1[20];
382 while (*ls && isspace(*ls))
384 next = strchr(ls, '\n');
385 eol = !next ? (ls + strlen(ls)) : next;
386 if (!memcmp("^{}", eol-3, 3))
390 if (get_sha1_hex(ls, sha1))
393 while (ls < eol && isspace(*ls))
395 /* ls to next (or eol) is the name.
396 * is it identical to lref to colon-2?
398 if ((eol - ls) <= matchlen ||
399 strncmp(ls, lref, matchlen))
402 /* Yes, it is a match */
406 printf("%.*s:%.*s%.*s\n",
408 replacelen, colon + 1,
409 namelen - matchlen, ls + matchlen);
415 static int pick_rref(int sha1_only, const char *rref, const char *ls_remote_result)
418 int lrr_count = lrr_count, i, pass;
425 } *lrr_list = lrr_list;
427 for (pass = 0; pass < 2; pass++) {
428 /* pass 0 counts and allocates, pass 1 fills... */
429 cp = ls_remote_result;
433 while (*cp && isspace(*cp))
437 np = strchrnul(cp, '\n');
439 lrr_list[i].line = cp;
440 lrr_list[i].name = cp + 41;
441 lrr_list[i].namelen = np - (cp + 41);
448 lrr_list = xcalloc(lrr_count, sizeof(*lrr_list));
457 while (*rref && isspace(*rref))
461 next = strchrnul(rref, '\n');
462 rreflen = next - rref;
464 for (i = 0; i < lrr_count; i++) {
465 struct lrr *lrr = &(lrr_list[i]);
467 if (rreflen == lrr->namelen &&
468 !memcmp(lrr->name, rref, rreflen)) {
471 sha1_only ? 40 : lrr->namelen + 41,
477 if (lrr_count <= i) {
478 error("pick-rref: %.*s not found", rreflen, rref);
487 int cmd_fetch__tool(int argc, const char **argv, const char *prefix)
494 const char *arg = argv[1];
495 if (!strcmp("-v", arg))
497 else if (!strcmp("-f", arg))
499 else if (!strcmp("-s", arg))
508 return error("Missing subcommand");
510 if (!strcmp("append-fetch-head", argv[1])) {
516 return error("append-fetch-head takes 6 args");
517 filename = git_path("FETCH_HEAD");
518 fp = fopen(filename, "a");
520 return error("cannot open %s: %s\n", filename, strerror(errno));
521 result = append_fetch_head(fp, argv[2], argv[3],
523 argv[6], !!argv[7][0],
528 if (!strcmp("native-store", argv[1])) {
534 return error("fetch-native-store takes 3 args");
535 filename = git_path("FETCH_HEAD");
536 fp = fopen(filename, "a");
538 return error("cannot open %s: %s\n", filename, strerror(errno));
539 result = fetch_native_store(fp, argv[2], argv[3], argv[4],
544 if (!strcmp("parse-reflist", argv[1])) {
547 return error("parse-reflist takes 1 arg");
549 if (!strcmp(reflist, "-"))
550 reflist = get_stdin();
551 return parse_reflist(reflist);
553 if (!strcmp("pick-rref", argv[1])) {
554 const char *ls_remote_result;
556 return error("pick-rref takes 2 args");
557 ls_remote_result = argv[3];
558 if (!strcmp(ls_remote_result, "-"))
559 ls_remote_result = get_stdin();
560 return pick_rref(sopt, argv[2], ls_remote_result);
562 if (!strcmp("expand-refs-wildcard", argv[1])) {
565 return error("expand-refs-wildcard takes at least 2 args");
567 if (!strcmp(reflist, "-"))
568 reflist = get_stdin();
569 return expand_refs_wildcard(reflist, argc - 3, argv + 3);
572 return error("Unknown subcommand: %s", argv[1]);