5 #define CHUNK_SIZE (1048576)
7 static char *get_stdin(void)
9 char *data = xmalloc(CHUNK_SIZE);
10 int offset = 0, read = 0;
11 read = xread(0, data, CHUNK_SIZE);
12 while (read == CHUNK_SIZE) {
14 data = xrealloc(data, offset + CHUNK_SIZE);
15 read = xread(0, data + offset, CHUNK_SIZE);
20 static void show_new(char *type, unsigned char *sha1_new)
22 fprintf(stderr, " %s: %s\n", type,
23 find_unique_abbrev(sha1_new, DEFAULT_ABBREV));
26 static int update_ref(const char *action,
29 unsigned char *oldval)
33 char *rla = getenv("GIT_REFLOG_ACTION");
34 static struct ref_lock *lock;
37 rla = "(reflog update)";
38 len = snprintf(msg, sizeof(msg), "%s: %s", rla, action);
39 if (sizeof(msg) <= len)
40 die("insanely long action");
41 lock = lock_any_ref_for_update(refname, oldval);
44 if (write_ref_sha1(lock, sha1, msg) < 0)
49 static int update_local_ref(const char *name,
52 int verbose, int force)
55 unsigned char sha1_old[20], sha1_new[20];
56 char oldh[41], newh[41];
57 struct commit *current, *updated;
59 if (get_sha1_hex(new_head, sha1_new))
60 die("malformed object name %s", new_head);
61 if (sha1_object_info(sha1_new, type, NULL))
62 die("object %s not found", new_head);
67 fprintf(stderr, "* fetched %s\n", note);
68 show_new(type, sha1_new);
73 if (get_sha1(name, sha1_old)) {
77 if (!strncmp(name, "refs/tags/", 10))
81 fprintf(stderr, "* %s: storing %s\n",
83 show_new(type, sha1_new);
84 return update_ref(msg, name, sha1_new, NULL);
87 if (!hashcmp(sha1_old, sha1_new)) {
89 fprintf(stderr, "* %s: same as %s\n", name, note);
90 show_new(type, sha1_new);
95 if (!strncmp(name, "refs/tags/", 10)) {
96 fprintf(stderr, "* %s: updating with %s\n", name, note);
97 show_new(type, sha1_new);
98 return update_ref("updating tag", name, sha1_new, NULL);
101 current = lookup_commit_reference(sha1_old);
102 updated = lookup_commit_reference(sha1_new);
103 if (!current || !updated)
106 strcpy(oldh, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
107 strcpy(newh, find_unique_abbrev(sha1_new, DEFAULT_ABBREV));
109 if (in_merge_bases(current, &updated, 1)) {
110 fprintf(stderr, "* %s: fast forward to %s\n",
112 fprintf(stderr, " old..new: %s..%s\n", oldh, newh);
113 return update_ref("fast forward", name, sha1_new, sha1_old);
117 "* %s: not updating to non-fast forward %s\n",
120 " old...new: %s...%s\n", oldh, newh);
124 "* %s: forcing update to non-fast forward %s\n",
126 fprintf(stderr, " old...new: %s...%s\n", oldh, newh);
127 return update_ref("forced-update", name, sha1_new, sha1_old);
130 static int append_fetch_head(FILE *fp,
131 const char *head, const char *remote,
132 const char *remote_name, const char *remote_nick,
133 const char *local_name, int not_for_merge,
134 int verbose, int force)
136 struct commit *commit;
137 int remote_len, i, note_len;
138 unsigned char sha1[20];
140 const char *what, *kind;
142 if (get_sha1(head, sha1))
143 return error("Not a valid object name: %s", head);
144 commit = lookup_commit_reference(sha1);
148 if (!strcmp(remote_name, "HEAD")) {
152 else if (!strncmp(remote_name, "refs/heads/", 11)) {
154 what = remote_name + 11;
156 else if (!strncmp(remote_name, "refs/tags/", 10)) {
158 what = remote_name + 10;
160 else if (!strncmp(remote_name, "refs/remotes/", 13)) {
161 kind = "remote branch";
162 what = remote_name + 13;
169 remote_len = strlen(remote);
170 for (i = remote_len - 1; remote[i] == '/' && 0 <= i; i--)
173 if (4 < i && !strncmp(".git", remote + i - 3, 4))
175 note_len = sprintf(note, "%s\t%s\t",
176 sha1_to_hex(commit ? commit->object.sha1 : sha1),
177 not_for_merge ? "not-for-merge" : "");
180 note_len += sprintf(note + note_len, "%s ", kind);
181 note_len += sprintf(note + note_len, "'%s' of ", what);
183 note_len += sprintf(note + note_len, "%.*s", remote_len, remote);
184 fprintf(fp, "%s\n", note);
185 return update_local_ref(local_name, head, note, verbose, force);
189 static void remove_keep(void)
195 static void remove_keep_on_signal(int signo)
198 signal(SIGINT, SIG_DFL);
202 static char *find_local_name(const char *remote_name, const char *refs,
203 int *force_p, int *not_for_merge_p)
205 const char *ref = refs;
206 int len = strlen(remote_name);
210 int single_force, not_for_merge;
216 next = strchr(ref, '\n');
218 single_force = not_for_merge = 0;
231 if (!strncmp(remote_name, ref, len) && ref[len] == ':') {
232 const char *local_part = ref + len + 1;
237 retlen = strlen(local_part);
239 retlen = next - local_part;
240 ret = xmalloc(retlen + 1);
241 memcpy(ret, local_part, retlen);
243 *force_p = single_force;
244 *not_for_merge_p = not_for_merge;
252 static int fetch_native_store(FILE *fp,
254 const char *remote_nick,
256 int verbose, int force)
261 signal(SIGINT, remove_keep_on_signal);
264 while (fgets(buffer, sizeof(buffer), stdin)) {
268 int single_force, not_for_merge;
270 for (cp = buffer; *cp && !isspace(*cp); cp++)
275 if (len && cp[len-1] == '\n')
277 if (!strcmp(buffer, "failed"))
278 die("Fetch failure: %s", remote);
279 if (!strcmp(buffer, "pack"))
281 if (!strcmp(buffer, "keep")) {
282 char *od = get_object_directory();
283 int len = strlen(od) + strlen(cp) + 50;
285 sprintf(keep, "%s/pack/pack-%s.keep", od, cp);
289 local_name = find_local_name(cp, refs,
290 &single_force, ¬_for_merge);
293 err |= append_fetch_head(fp,
294 buffer, remote, cp, remote_nick,
295 local_name, not_for_merge,
296 verbose, force || single_force);
301 static int parse_reflist(const char *reflist)
306 for (ref = reflist; ref; ) {
308 while (*ref && isspace(*ref))
312 for (next = ref; *next && !isspace(*next); next++)
314 printf("\n%.*s", (int)(next - ref), ref);
320 for (ref = reflist; ref; ) {
321 const char *next, *colon;
322 while (*ref && isspace(*ref))
326 for (next = ref; *next && !isspace(*next); next++)
332 colon = strchr(ref, ':');
334 printf("%.*s", (int)((colon ? colon : next) - ref), ref);
341 static int expand_refs_wildcard(const char *ls_remote_result, int numrefs,
344 int i, matchlen, replacelen;
346 const char *remote = *refs++;
350 fprintf(stderr, "Nothing specified for fetching with remote.%s.fetch\n",
355 for (i = 0; i < numrefs; i++) {
356 const char *ref = refs[i];
357 const char *lref = ref;
365 colon = strchr(lref, ':');
366 tail = lref + strlen(lref);
371 2 < tail - (colon + 1) &&
376 printf("explicit\n");
385 /* lref to colon-2 is remote hierarchy name;
386 * colon+1 to tail-2 is local.
388 matchlen = (colon-1) - lref;
389 replacelen = (tail-1) - (colon+1);
390 for (ls = ls_remote_result; ls; ls = next) {
392 unsigned char sha1[20];
395 while (*ls && isspace(*ls))
397 next = strchr(ls, '\n');
398 eol = !next ? (ls + strlen(ls)) : next;
399 if (!memcmp("^{}", eol-3, 3))
401 if (get_sha1_hex(ls, sha1))
404 while (ls < eol && isspace(*ls))
406 /* ls to next (or eol) is the name.
407 * is it identical to lref to colon-2?
409 if ((eol - ls) <= matchlen ||
410 strncmp(ls, lref, matchlen))
413 /* Yes, it is a match */
417 printf("%.*s:%.*s%.*s\n",
419 replacelen, colon + 1,
420 namelen - matchlen, ls + matchlen);
426 int cmd_fetch__tool(int argc, const char **argv, const char *prefix)
432 const char *arg = argv[1];
433 if (!strcmp("-v", arg))
435 else if (!strcmp("-f", arg))
444 return error("Missing subcommand");
446 if (!strcmp("append-fetch-head", argv[1])) {
451 return error("append-fetch-head takes 6 args");
452 fp = fopen(git_path("FETCH_HEAD"), "a");
453 result = append_fetch_head(fp, argv[2], argv[3],
455 argv[6], !!argv[7][0],
460 if (!strcmp("update-local-ref", argv[1])) {
462 return error("update-local-ref takes 3 args");
463 return update_local_ref(argv[2], argv[3], argv[4],
466 if (!strcmp("native-store", argv[1])) {
471 return error("fetch-native-store takes 3 args");
472 fp = fopen(git_path("FETCH_HEAD"), "a");
473 result = fetch_native_store(fp, argv[2], argv[3], argv[4],
478 if (!strcmp("parse-reflist", argv[1])) {
481 return error("parse-reflist takes 1 arg");
483 if (!strcmp(reflist, "-"))
484 reflist = get_stdin();
485 return parse_reflist(reflist);
487 if (!strcmp("expand-refs-wildcard", argv[1])) {
490 return error("expand-refs-wildcard takes at least 2 args");
492 if (!strcmp(reflist, "-"))
493 reflist = get_stdin();
494 return expand_refs_wildcard(reflist, argc - 3, argv + 3);
497 return error("Unknown subcommand: %s", argv[1]);