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(enum object_type type, unsigned char *sha1_new)
22 fprintf(stderr, " %s: %s\n", typename(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)
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(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("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("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("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))
177 note_len = sprintf(note, "%s\t%s\t",
178 sha1_to_hex(commit ? commit->object.sha1 : sha1),
179 not_for_merge ? "not-for-merge" : "");
182 note_len += sprintf(note + note_len, "%s ", kind);
183 note_len += sprintf(note + note_len, "'%s' of ", what);
185 note_len += sprintf(note + note_len, "%.*s", remote_len, remote);
186 fprintf(fp, "%s\n", note);
187 return update_local_ref(local_name, head, note, verbose, force);
191 static void remove_keep(void)
197 static void remove_keep_on_signal(int signo)
200 signal(SIGINT, SIG_DFL);
204 static char *find_local_name(const char *remote_name, const char *refs,
205 int *force_p, int *not_for_merge_p)
207 const char *ref = refs;
208 int len = strlen(remote_name);
212 int single_force, not_for_merge;
218 next = strchr(ref, '\n');
220 single_force = not_for_merge = 0;
233 if (!strncmp(remote_name, ref, len) && ref[len] == ':') {
234 const char *local_part = ref + len + 1;
239 retlen = strlen(local_part);
241 retlen = next - local_part;
242 ret = xmalloc(retlen + 1);
243 memcpy(ret, local_part, retlen);
245 *force_p = single_force;
246 *not_for_merge_p = not_for_merge;
254 static int fetch_native_store(FILE *fp,
256 const char *remote_nick,
258 int verbose, int force)
263 signal(SIGINT, remove_keep_on_signal);
266 while (fgets(buffer, sizeof(buffer), stdin)) {
270 int single_force, not_for_merge;
272 for (cp = buffer; *cp && !isspace(*cp); cp++)
277 if (len && cp[len-1] == '\n')
279 if (!strcmp(buffer, "failed"))
280 die("Fetch failure: %s", remote);
281 if (!strcmp(buffer, "pack"))
283 if (!strcmp(buffer, "keep")) {
284 char *od = get_object_directory();
285 int len = strlen(od) + strlen(cp) + 50;
287 sprintf(keep, "%s/pack/pack-%s.keep", od, cp);
291 local_name = find_local_name(cp, refs,
292 &single_force, ¬_for_merge);
295 err |= append_fetch_head(fp,
296 buffer, remote, cp, remote_nick,
297 local_name, not_for_merge,
298 verbose, force || single_force);
303 static int parse_reflist(const char *reflist)
308 for (ref = reflist; ref; ) {
310 while (*ref && isspace(*ref))
314 for (next = ref; *next && !isspace(*next); next++)
316 printf("\n%.*s", (int)(next - ref), ref);
322 for (ref = reflist; ref; ) {
323 const char *next, *colon;
324 while (*ref && isspace(*ref))
328 for (next = ref; *next && !isspace(*next); next++)
334 colon = strchr(ref, ':');
336 printf("%.*s", (int)((colon ? colon : next) - ref), ref);
343 static int expand_refs_wildcard(const char *ls_remote_result, int numrefs,
346 int i, matchlen, replacelen;
348 const char *remote = *refs++;
352 fprintf(stderr, "Nothing specified for fetching with remote.%s.fetch\n",
357 for (i = 0; i < numrefs; i++) {
358 const char *ref = refs[i];
359 const char *lref = ref;
367 colon = strchr(lref, ':');
368 tail = lref + strlen(lref);
373 2 < tail - (colon + 1) &&
378 printf("explicit\n");
387 /* lref to colon-2 is remote hierarchy name;
388 * colon+1 to tail-2 is local.
390 matchlen = (colon-1) - lref;
391 replacelen = (tail-1) - (colon+1);
392 for (ls = ls_remote_result; ls; ls = next) {
394 unsigned char sha1[20];
397 while (*ls && isspace(*ls))
399 next = strchr(ls, '\n');
400 eol = !next ? (ls + strlen(ls)) : next;
401 if (!memcmp("^{}", eol-3, 3))
403 if (get_sha1_hex(ls, sha1))
406 while (ls < eol && isspace(*ls))
408 /* ls to next (or eol) is the name.
409 * is it identical to lref to colon-2?
411 if ((eol - ls) <= matchlen ||
412 strncmp(ls, lref, matchlen))
415 /* Yes, it is a match */
419 printf("%.*s:%.*s%.*s\n",
421 replacelen, colon + 1,
422 namelen - matchlen, ls + matchlen);
428 int cmd_fetch__tool(int argc, const char **argv, const char *prefix)
434 const char *arg = argv[1];
435 if (!strcmp("-v", arg))
437 else if (!strcmp("-f", arg))
446 return error("Missing subcommand");
448 if (!strcmp("append-fetch-head", argv[1])) {
453 return error("append-fetch-head takes 6 args");
454 fp = fopen(git_path("FETCH_HEAD"), "a");
455 result = append_fetch_head(fp, argv[2], argv[3],
457 argv[6], !!argv[7][0],
462 if (!strcmp("update-local-ref", argv[1])) {
464 return error("update-local-ref takes 3 args");
465 return update_local_ref(argv[2], argv[3], argv[4],
468 if (!strcmp("native-store", argv[1])) {
473 return error("fetch-native-store takes 3 args");
474 fp = fopen(git_path("FETCH_HEAD"), "a");
475 result = fetch_native_store(fp, argv[2], argv[3], argv[4],
480 if (!strcmp("parse-reflist", argv[1])) {
483 return error("parse-reflist takes 1 arg");
485 if (!strcmp(reflist, "-"))
486 reflist = get_stdin();
487 return parse_reflist(reflist);
489 if (!strcmp("expand-refs-wildcard", argv[1])) {
492 return error("expand-refs-wildcard takes at least 2 args");
494 if (!strcmp(reflist, "-"))
495 reflist = get_stdin();
496 return expand_refs_wildcard(reflist, argc - 3, argv + 3);
499 return error("Unknown subcommand: %s", argv[1]);