Use diff* with --exit-code in git-am, git-rebase and git-merge-ours
[git/repo.git] / builtin-fetch--tool.c
blobe9d676455078b40138ad4716a912c4b2b5e10c2e
1 #include "cache.h"
2 #include "refs.h"
3 #include "commit.h"
5 #define CHUNK_SIZE 1024
7 static char *get_stdin(void)
9 int offset = 0;
10 char *data = xmalloc(CHUNK_SIZE);
12 while (1) {
13 int cnt = xread(0, data + offset, CHUNK_SIZE);
14 if (cnt < 0)
15 die("error reading standard input: %s",
16 strerror(errno));
17 if (cnt == 0) {
18 data[offset] = 0;
19 break;
21 offset += cnt;
22 data = xrealloc(data, offset + CHUNK_SIZE);
24 return data;
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,
34 const char *refname,
35 unsigned char *sha1,
36 unsigned char *oldval)
38 int len;
39 char msg[1024];
40 char *rla = getenv("GIT_REFLOG_ACTION");
41 static struct ref_lock *lock;
43 if (!rla)
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);
49 if (!lock)
50 return 1;
51 if (write_ref_sha1(lock, sha1, msg) < 0)
52 return 1;
53 return 0;
56 static int update_local_ref(const char *name,
57 const char *new_head,
58 const char *note,
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);
70 if (type < 0)
71 die("object %s not found", new_head);
73 if (!*name) {
74 /* Not storing */
75 if (verbose) {
76 fprintf(stderr, "* fetched %s\n", note);
77 show_new(type, sha1_new);
79 return 0;
82 if (get_sha1(name, sha1_old)) {
83 char *msg;
84 just_store:
85 /* new ref */
86 if (!strncmp(name, "refs/tags/", 10))
87 msg = "storing tag";
88 else
89 msg = "storing head";
90 fprintf(stderr, "* %s: storing %s\n",
91 name, note);
92 show_new(type, sha1_new);
93 return update_ref(msg, name, sha1_new, NULL);
96 if (!hashcmp(sha1_old, sha1_new)) {
97 if (verbose) {
98 fprintf(stderr, "* %s: same as %s\n", name, note);
99 show_new(type, sha1_new);
101 return 0;
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)
113 goto just_store;
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",
120 name, note);
121 fprintf(stderr, " old..new: %s..%s\n", oldh, newh);
122 return update_ref("fast forward", name, sha1_new, sha1_old);
124 if (!force) {
125 fprintf(stderr,
126 "* %s: not updating to non-fast forward %s\n",
127 name, note);
128 fprintf(stderr,
129 " old...new: %s...%s\n", oldh, newh);
130 return 1;
132 fprintf(stderr,
133 "* %s: forcing update to non-fast forward %s\n",
134 name, note);
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];
148 char note[1024];
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);
154 if (!commit)
155 not_for_merge = 1;
157 if (!strcmp(remote_name, "HEAD")) {
158 kind = "";
159 what = "";
161 else if (!strncmp(remote_name, "refs/heads/", 11)) {
162 kind = "branch";
163 what = remote_name + 11;
165 else if (!strncmp(remote_name, "refs/tags/", 10)) {
166 kind = "tag";
167 what = remote_name + 10;
169 else if (!strncmp(remote_name, "refs/remotes/", 13)) {
170 kind = "remote branch";
171 what = remote_name + 13;
173 else {
174 kind = "";
175 what = remote_name;
178 remote_len = strlen(remote);
179 for (i = remote_len - 1; remote[i] == '/' && 0 <= i; i--)
181 remote_len = i + 1;
182 if (4 < i && !strncmp(".git", remote + i - 3, 4))
183 remote_len = i - 3;
185 note_len = 0;
186 if (*what) {
187 if (*kind)
188 note_len += sprintf(note + note_len, "%s ", kind);
189 note_len += sprintf(note + note_len, "'%s' of ", what);
191 note_len += sprintf(note + note_len, "%.*s", remote_len, remote);
192 fprintf(fp, "%s\t%s\t%s\n",
193 sha1_to_hex(commit ? commit->object.sha1 : sha1),
194 not_for_merge ? "not-for-merge" : "",
195 note);
196 return update_local_ref(local_name, head, note, verbose, force);
199 static char *keep;
200 static void remove_keep(void)
202 if (keep && *keep)
203 unlink(keep);
206 static void remove_keep_on_signal(int signo)
208 remove_keep();
209 signal(SIGINT, SIG_DFL);
210 raise(signo);
213 static char *find_local_name(const char *remote_name, const char *refs,
214 int *force_p, int *not_for_merge_p)
216 const char *ref = refs;
217 int len = strlen(remote_name);
219 while (ref) {
220 const char *next;
221 int single_force, not_for_merge;
223 while (*ref == '\n')
224 ref++;
225 if (!*ref)
226 break;
227 next = strchr(ref, '\n');
229 single_force = not_for_merge = 0;
230 if (*ref == '+') {
231 single_force = 1;
232 ref++;
234 if (*ref == '.') {
235 not_for_merge = 1;
236 ref++;
237 if (*ref == '+') {
238 single_force = 1;
239 ref++;
242 if (!strncmp(remote_name, ref, len) && ref[len] == ':') {
243 const char *local_part = ref + len + 1;
244 char *ret;
245 int retlen;
247 if (!next)
248 retlen = strlen(local_part);
249 else
250 retlen = next - local_part;
251 ret = xmalloc(retlen + 1);
252 memcpy(ret, local_part, retlen);
253 ret[retlen] = 0;
254 *force_p = single_force;
255 *not_for_merge_p = not_for_merge;
256 return ret;
258 ref = next;
260 return NULL;
263 static int fetch_native_store(FILE *fp,
264 const char *remote,
265 const char *remote_nick,
266 const char *refs,
267 int verbose, int force)
269 char buffer[1024];
270 int err = 0;
272 signal(SIGINT, remove_keep_on_signal);
273 atexit(remove_keep);
275 while (fgets(buffer, sizeof(buffer), stdin)) {
276 int len;
277 char *cp;
278 char *local_name;
279 int single_force, not_for_merge;
281 for (cp = buffer; *cp && !isspace(*cp); cp++)
283 if (*cp)
284 *cp++ = 0;
285 len = strlen(cp);
286 if (len && cp[len-1] == '\n')
287 cp[--len] = 0;
288 if (!strcmp(buffer, "failed"))
289 die("Fetch failure: %s", remote);
290 if (!strcmp(buffer, "pack"))
291 continue;
292 if (!strcmp(buffer, "keep")) {
293 char *od = get_object_directory();
294 int len = strlen(od) + strlen(cp) + 50;
295 keep = xmalloc(len);
296 sprintf(keep, "%s/pack/pack-%s.keep", od, cp);
297 continue;
300 local_name = find_local_name(cp, refs,
301 &single_force, &not_for_merge);
302 if (!local_name)
303 continue;
304 err |= append_fetch_head(fp,
305 buffer, remote, cp, remote_nick,
306 local_name, not_for_merge,
307 verbose, force || single_force);
309 return err;
312 static int parse_reflist(const char *reflist)
314 const char *ref;
316 printf("refs='");
317 for (ref = reflist; ref; ) {
318 const char *next;
319 while (*ref && isspace(*ref))
320 ref++;
321 if (!*ref)
322 break;
323 for (next = ref; *next && !isspace(*next); next++)
325 printf("\n%.*s", (int)(next - ref), ref);
326 ref = next;
328 printf("'\n");
330 printf("rref='");
331 for (ref = reflist; ref; ) {
332 const char *next, *colon;
333 while (*ref && isspace(*ref))
334 ref++;
335 if (!*ref)
336 break;
337 for (next = ref; *next && !isspace(*next); next++)
339 if (*ref == '.')
340 ref++;
341 if (*ref == '+')
342 ref++;
343 colon = strchr(ref, ':');
344 putchar('\n');
345 printf("%.*s", (int)((colon ? colon : next) - ref), ref);
346 ref = next;
348 printf("'\n");
349 return 0;
352 static int expand_refs_wildcard(const char *ls_remote_result, int numrefs,
353 const char **refs)
355 int i, matchlen, replacelen;
356 int found_one = 0;
357 const char *remote = *refs++;
358 numrefs--;
360 if (numrefs == 0) {
361 fprintf(stderr, "Nothing specified for fetching with remote.%s.fetch\n",
362 remote);
363 printf("empty\n");
366 for (i = 0; i < numrefs; i++) {
367 const char *ref = refs[i];
368 const char *lref = ref;
369 const char *colon;
370 const char *tail;
371 const char *ls;
372 const char *next;
374 if (*lref == '+')
375 lref++;
376 colon = strchr(lref, ':');
377 tail = lref + strlen(lref);
378 if (!(colon &&
379 2 < colon - lref &&
380 colon[-1] == '*' &&
381 colon[-2] == '/' &&
382 2 < tail - (colon + 1) &&
383 tail[-1] == '*' &&
384 tail[-2] == '/')) {
385 /* not a glob */
386 if (!found_one++)
387 printf("explicit\n");
388 printf("%s\n", ref);
389 continue;
392 /* glob */
393 if (!found_one++)
394 printf("glob\n");
396 /* lref to colon-2 is remote hierarchy name;
397 * colon+1 to tail-2 is local.
399 matchlen = (colon-1) - lref;
400 replacelen = (tail-1) - (colon+1);
401 for (ls = ls_remote_result; ls; ls = next) {
402 const char *eol;
403 unsigned char sha1[20];
404 int namelen;
406 while (*ls && isspace(*ls))
407 ls++;
408 next = strchr(ls, '\n');
409 eol = !next ? (ls + strlen(ls)) : next;
410 if (!memcmp("^{}", eol-3, 3))
411 continue;
412 if (eol - ls < 40)
413 continue;
414 if (get_sha1_hex(ls, sha1))
415 continue;
416 ls += 40;
417 while (ls < eol && isspace(*ls))
418 ls++;
419 /* ls to next (or eol) is the name.
420 * is it identical to lref to colon-2?
422 if ((eol - ls) <= matchlen ||
423 strncmp(ls, lref, matchlen))
424 continue;
426 /* Yes, it is a match */
427 namelen = eol - ls;
428 if (lref != ref)
429 putchar('+');
430 printf("%.*s:%.*s%.*s\n",
431 namelen, ls,
432 replacelen, colon + 1,
433 namelen - matchlen, ls + matchlen);
436 return 0;
439 int cmd_fetch__tool(int argc, const char **argv, const char *prefix)
441 int verbose = 0;
442 int force = 0;
444 while (1 < argc) {
445 const char *arg = argv[1];
446 if (!strcmp("-v", arg))
447 verbose = 1;
448 else if (!strcmp("-f", arg))
449 force = 1;
450 else
451 break;
452 argc--;
453 argv++;
456 if (argc <= 1)
457 return error("Missing subcommand");
459 if (!strcmp("append-fetch-head", argv[1])) {
460 int result;
461 FILE *fp;
463 if (argc != 8)
464 return error("append-fetch-head takes 6 args");
465 fp = fopen(git_path("FETCH_HEAD"), "a");
466 result = append_fetch_head(fp, argv[2], argv[3],
467 argv[4], argv[5],
468 argv[6], !!argv[7][0],
469 verbose, force);
470 fclose(fp);
471 return result;
473 if (!strcmp("native-store", argv[1])) {
474 int result;
475 FILE *fp;
477 if (argc != 5)
478 return error("fetch-native-store takes 3 args");
479 fp = fopen(git_path("FETCH_HEAD"), "a");
480 result = fetch_native_store(fp, argv[2], argv[3], argv[4],
481 verbose, force);
482 fclose(fp);
483 return result;
485 if (!strcmp("parse-reflist", argv[1])) {
486 const char *reflist;
487 if (argc != 3)
488 return error("parse-reflist takes 1 arg");
489 reflist = argv[2];
490 if (!strcmp(reflist, "-"))
491 reflist = get_stdin();
492 return parse_reflist(reflist);
494 if (!strcmp("expand-refs-wildcard", argv[1])) {
495 const char *reflist;
496 if (argc < 4)
497 return error("expand-refs-wildcard takes at least 2 args");
498 reflist = argv[2];
499 if (!strcmp(reflist, "-"))
500 reflist = get_stdin();
501 return expand_refs_wildcard(reflist, argc - 3, argv + 3);
504 return error("Unknown subcommand: %s", argv[1]);