5 #define CHUNK_SIZE 1024
7 static char *get_stdin(void)
10 char *data = xmalloc(CHUNK_SIZE);
13 int cnt = xread(0, data + offset, CHUNK_SIZE);
15 die("error reading standard input: %s",
22 data = xrealloc(data, offset + CHUNK_SIZE);
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,
36 unsigned char *oldval)
40 char *rla = getenv("GIT_REFLOG_ACTION");
41 static struct ref_lock *lock;
44 rla = "(reflog update)";
45 len = snprintf(msg, sizeof(msg), "%s: %s", rla, action);
46 if (sizeof(msg) <= len)
47 die("insanely long action");
48 lock = lock_any_ref_for_update(refname, oldval);
51 if (write_ref_sha1(lock, sha1, msg) < 0)
56 static int update_local_ref(const char *name,
59 int verbose, int force)
61 unsigned char sha1_old[20], sha1_new[20];
62 char oldh[41], newh[41];
63 struct commit *current, *updated;
64 enum object_type type;
66 if (get_sha1_hex(new_head, sha1_new))
67 die("malformed object name %s", new_head);
69 type = sha1_object_info(sha1_new, NULL);
71 die("object %s not found", new_head);
76 fprintf(stderr, "* fetched %s\n", note);
77 show_new(type, sha1_new);
82 if (get_sha1(name, sha1_old)) {
86 if (!strncmp(name, "refs/tags/", 10))
90 fprintf(stderr, "* %s: storing %s\n",
92 show_new(type, sha1_new);
93 return update_ref(msg, name, sha1_new, NULL);
96 if (!hashcmp(sha1_old, sha1_new)) {
98 fprintf(stderr, "* %s: same as %s\n", name, note);
99 show_new(type, sha1_new);
104 if (!strncmp(name, "refs/tags/", 10)) {
105 fprintf(stderr, "* %s: updating with %s\n", name, note);
106 show_new(type, sha1_new);
107 return update_ref("updating tag", name, sha1_new, NULL);
110 current = lookup_commit_reference(sha1_old);
111 updated = lookup_commit_reference(sha1_new);
112 if (!current || !updated)
115 strcpy(oldh, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
116 strcpy(newh, find_unique_abbrev(sha1_new, DEFAULT_ABBREV));
118 if (in_merge_bases(current, &updated, 1)) {
119 fprintf(stderr, "* %s: fast forward to %s\n",
121 fprintf(stderr, " old..new: %s..%s\n", oldh, newh);
122 return update_ref("fast forward", name, sha1_new, sha1_old);
126 "* %s: not updating to non-fast forward %s\n",
129 " old...new: %s...%s\n", oldh, newh);
133 "* %s: forcing update to non-fast forward %s\n",
135 fprintf(stderr, " old...new: %s...%s\n", oldh, newh);
136 return update_ref("forced-update", name, sha1_new, sha1_old);
139 static int append_fetch_head(FILE *fp,
140 const char *head, const char *remote,
141 const char *remote_name, const char *remote_nick,
142 const char *local_name, int not_for_merge,
143 int verbose, int force)
145 struct commit *commit;
146 int remote_len, i, note_len;
147 unsigned char sha1[20];
149 const char *what, *kind;
151 if (get_sha1(head, sha1))
152 return error("Not a valid object name: %s", head);
153 commit = lookup_commit_reference(sha1);
157 if (!strcmp(remote_name, "HEAD")) {
161 else if (!strncmp(remote_name, "refs/heads/", 11)) {
163 what = remote_name + 11;
165 else if (!strncmp(remote_name, "refs/tags/", 10)) {
167 what = remote_name + 10;
169 else if (!strncmp(remote_name, "refs/remotes/", 13)) {
170 kind = "remote branch";
171 what = remote_name + 13;
178 remote_len = strlen(remote);
179 for (i = remote_len - 1; remote[i] == '/' && 0 <= i; i--)
182 if (4 < i && !strncmp(".git", remote + i - 3, 4))
184 note_len = sprintf(note, "%s\t%s\t",
185 sha1_to_hex(commit ? commit->object.sha1 : sha1),
186 not_for_merge ? "not-for-merge" : "");
189 note_len += sprintf(note + note_len, "%s ", kind);
190 note_len += sprintf(note + note_len, "'%s' of ", what);
192 note_len += sprintf(note + note_len, "%.*s", remote_len, remote);
193 fprintf(fp, "%s\n", note);
194 return update_local_ref(local_name, head, note, verbose, force);
198 static void remove_keep(void)
204 static void remove_keep_on_signal(int signo)
207 signal(SIGINT, SIG_DFL);
211 static char *find_local_name(const char *remote_name, const char *refs,
212 int *force_p, int *not_for_merge_p)
214 const char *ref = refs;
215 int len = strlen(remote_name);
219 int single_force, not_for_merge;
225 next = strchr(ref, '\n');
227 single_force = not_for_merge = 0;
240 if (!strncmp(remote_name, ref, len) && ref[len] == ':') {
241 const char *local_part = ref + len + 1;
246 retlen = strlen(local_part);
248 retlen = next - local_part;
249 ret = xmalloc(retlen + 1);
250 memcpy(ret, local_part, retlen);
252 *force_p = single_force;
253 *not_for_merge_p = not_for_merge;
261 static int fetch_native_store(FILE *fp,
263 const char *remote_nick,
265 int verbose, int force)
270 signal(SIGINT, remove_keep_on_signal);
273 while (fgets(buffer, sizeof(buffer), stdin)) {
277 int single_force, not_for_merge;
279 for (cp = buffer; *cp && !isspace(*cp); cp++)
284 if (len && cp[len-1] == '\n')
286 if (!strcmp(buffer, "failed"))
287 die("Fetch failure: %s", remote);
288 if (!strcmp(buffer, "pack"))
290 if (!strcmp(buffer, "keep")) {
291 char *od = get_object_directory();
292 int len = strlen(od) + strlen(cp) + 50;
294 sprintf(keep, "%s/pack/pack-%s.keep", od, cp);
298 local_name = find_local_name(cp, refs,
299 &single_force, ¬_for_merge);
302 err |= append_fetch_head(fp,
303 buffer, remote, cp, remote_nick,
304 local_name, not_for_merge,
305 verbose, force || single_force);
310 static int parse_reflist(const char *reflist)
315 for (ref = reflist; ref; ) {
317 while (*ref && isspace(*ref))
321 for (next = ref; *next && !isspace(*next); next++)
323 printf("\n%.*s", (int)(next - ref), ref);
329 for (ref = reflist; ref; ) {
330 const char *next, *colon;
331 while (*ref && isspace(*ref))
335 for (next = ref; *next && !isspace(*next); next++)
341 colon = strchr(ref, ':');
343 printf("%.*s", (int)((colon ? colon : next) - ref), ref);
350 static int expand_refs_wildcard(const char *ls_remote_result, int numrefs,
353 int i, matchlen, replacelen;
355 const char *remote = *refs++;
359 fprintf(stderr, "Nothing specified for fetching with remote.%s.fetch\n",
364 for (i = 0; i < numrefs; i++) {
365 const char *ref = refs[i];
366 const char *lref = ref;
374 colon = strchr(lref, ':');
375 tail = lref + strlen(lref);
380 2 < tail - (colon + 1) &&
385 printf("explicit\n");
394 /* lref to colon-2 is remote hierarchy name;
395 * colon+1 to tail-2 is local.
397 matchlen = (colon-1) - lref;
398 replacelen = (tail-1) - (colon+1);
399 for (ls = ls_remote_result; ls; ls = next) {
401 unsigned char sha1[20];
404 while (*ls && isspace(*ls))
406 next = strchr(ls, '\n');
407 eol = !next ? (ls + strlen(ls)) : next;
408 if (!memcmp("^{}", eol-3, 3))
410 if (get_sha1_hex(ls, sha1))
413 while (ls < eol && isspace(*ls))
415 /* ls to next (or eol) is the name.
416 * is it identical to lref to colon-2?
418 if ((eol - ls) <= matchlen ||
419 strncmp(ls, lref, matchlen))
422 /* Yes, it is a match */
426 printf("%.*s:%.*s%.*s\n",
428 replacelen, colon + 1,
429 namelen - matchlen, ls + matchlen);
435 int cmd_fetch__tool(int argc, const char **argv, const char *prefix)
441 const char *arg = argv[1];
442 if (!strcmp("-v", arg))
444 else if (!strcmp("-f", arg))
453 return error("Missing subcommand");
455 if (!strcmp("append-fetch-head", argv[1])) {
460 return error("append-fetch-head takes 6 args");
461 fp = fopen(git_path("FETCH_HEAD"), "a");
462 result = append_fetch_head(fp, argv[2], argv[3],
464 argv[6], !!argv[7][0],
469 if (!strcmp("update-local-ref", argv[1])) {
471 return error("update-local-ref takes 3 args");
472 return update_local_ref(argv[2], argv[3], argv[4],
475 if (!strcmp("native-store", argv[1])) {
480 return error("fetch-native-store takes 3 args");
481 fp = fopen(git_path("FETCH_HEAD"), "a");
482 result = fetch_native_store(fp, argv[2], argv[3], argv[4],
487 if (!strcmp("parse-reflist", argv[1])) {
490 return error("parse-reflist takes 1 arg");
492 if (!strcmp(reflist, "-"))
493 reflist = get_stdin();
494 return parse_reflist(reflist);
496 if (!strcmp("expand-refs-wildcard", argv[1])) {
499 return error("expand-refs-wildcard takes at least 2 args");
501 if (!strcmp(reflist, "-"))
502 reflist = get_stdin();
503 return expand_refs_wildcard(reflist, argc - 3, argv + 3);
506 return error("Unknown subcommand: %s", argv[1]);