git-diff: squelch "empty" diffs
[alt-git.git] / builtin-fetch--tool.c
blobe2f8ede9ae4507ed1e431f9d14fc649f6475627d
1 #include "builtin.h"
2 #include "cache.h"
3 #include "refs.h"
4 #include "commit.h"
6 #define CHUNK_SIZE 1024
8 static char *get_stdin(void)
10 size_t offset = 0;
11 char *data = xmalloc(CHUNK_SIZE);
13 while (1) {
14 ssize_t cnt = xread(0, data + offset, CHUNK_SIZE);
15 if (cnt < 0)
16 die("error reading standard input: %s",
17 strerror(errno));
18 if (cnt == 0) {
19 data[offset] = 0;
20 break;
22 offset += cnt;
23 data = xrealloc(data, offset + CHUNK_SIZE);
25 return data;
28 static void show_new(enum object_type type, unsigned char *sha1_new)
30 fprintf(stderr, " %s: %s\n", typename(type),
31 find_unique_abbrev(sha1_new, DEFAULT_ABBREV));
34 static int update_ref(const char *action,
35 const char *refname,
36 unsigned char *sha1,
37 unsigned char *oldval)
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 snprintf(msg, sizeof(msg), "%s: %s", rla, action);
46 lock = lock_any_ref_for_update(refname, oldval, 0);
47 if (!lock)
48 return 1;
49 if (write_ref_sha1(lock, sha1, msg) < 0)
50 return 1;
51 return 0;
54 static int update_local_ref(const char *name,
55 const char *new_head,
56 const char *note,
57 int verbose, int force)
59 unsigned char sha1_old[20], sha1_new[20];
60 char oldh[41], newh[41];
61 struct commit *current, *updated;
62 enum object_type type;
64 if (get_sha1_hex(new_head, sha1_new))
65 die("malformed object name %s", new_head);
67 type = sha1_object_info(sha1_new, NULL);
68 if (type < 0)
69 die("object %s not found", new_head);
71 if (!*name) {
72 /* Not storing */
73 if (verbose) {
74 fprintf(stderr, "* fetched %s\n", note);
75 show_new(type, sha1_new);
77 return 0;
80 if (get_sha1(name, sha1_old)) {
81 char *msg;
82 just_store:
83 /* new ref */
84 if (!strncmp(name, "refs/tags/", 10))
85 msg = "storing tag";
86 else
87 msg = "storing head";
88 fprintf(stderr, "* %s: storing %s\n",
89 name, note);
90 show_new(type, sha1_new);
91 return update_ref(msg, name, sha1_new, NULL);
94 if (!hashcmp(sha1_old, sha1_new)) {
95 if (verbose) {
96 fprintf(stderr, "* %s: same as %s\n", name, note);
97 show_new(type, sha1_new);
99 return 0;
102 if (!strncmp(name, "refs/tags/", 10)) {
103 fprintf(stderr, "* %s: updating with %s\n", name, note);
104 show_new(type, sha1_new);
105 return update_ref("updating tag", name, sha1_new, NULL);
108 current = lookup_commit_reference(sha1_old);
109 updated = lookup_commit_reference(sha1_new);
110 if (!current || !updated)
111 goto just_store;
113 strcpy(oldh, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
114 strcpy(newh, find_unique_abbrev(sha1_new, DEFAULT_ABBREV));
116 if (in_merge_bases(current, &updated, 1)) {
117 fprintf(stderr, "* %s: fast forward to %s\n",
118 name, note);
119 fprintf(stderr, " old..new: %s..%s\n", oldh, newh);
120 return update_ref("fast forward", name, sha1_new, sha1_old);
122 if (!force) {
123 fprintf(stderr,
124 "* %s: not updating to non-fast forward %s\n",
125 name, note);
126 fprintf(stderr,
127 " old...new: %s...%s\n", oldh, newh);
128 return 1;
130 fprintf(stderr,
131 "* %s: forcing update to non-fast forward %s\n",
132 name, note);
133 fprintf(stderr, " old...new: %s...%s\n", oldh, newh);
134 return update_ref("forced-update", name, sha1_new, sha1_old);
137 static int append_fetch_head(FILE *fp,
138 const char *head, const char *remote,
139 const char *remote_name, const char *remote_nick,
140 const char *local_name, int not_for_merge,
141 int verbose, int force)
143 struct commit *commit;
144 int remote_len, i, note_len;
145 unsigned char sha1[20];
146 char note[1024];
147 const char *what, *kind;
149 if (get_sha1(head, sha1))
150 return error("Not a valid object name: %s", head);
151 commit = lookup_commit_reference(sha1);
152 if (!commit)
153 not_for_merge = 1;
155 if (!strcmp(remote_name, "HEAD")) {
156 kind = "";
157 what = "";
159 else if (!strncmp(remote_name, "refs/heads/", 11)) {
160 kind = "branch";
161 what = remote_name + 11;
163 else if (!strncmp(remote_name, "refs/tags/", 10)) {
164 kind = "tag";
165 what = remote_name + 10;
167 else if (!strncmp(remote_name, "refs/remotes/", 13)) {
168 kind = "remote branch";
169 what = remote_name + 13;
171 else {
172 kind = "";
173 what = remote_name;
176 remote_len = strlen(remote);
177 for (i = remote_len - 1; remote[i] == '/' && 0 <= i; i--)
179 remote_len = i + 1;
180 if (4 < i && !strncmp(".git", remote + i - 3, 4))
181 remote_len = i - 3;
183 note_len = 0;
184 if (*what) {
185 if (*kind)
186 note_len += sprintf(note + note_len, "%s ", kind);
187 note_len += sprintf(note + note_len, "'%s' of ", what);
189 note_len += sprintf(note + note_len, "%.*s", remote_len, remote);
190 fprintf(fp, "%s\t%s\t%s\n",
191 sha1_to_hex(commit ? commit->object.sha1 : sha1),
192 not_for_merge ? "not-for-merge" : "",
193 note);
194 return update_local_ref(local_name, head, note, verbose, force);
197 static char *keep;
198 static void remove_keep(void)
200 if (keep && *keep)
201 unlink(keep);
204 static void remove_keep_on_signal(int signo)
206 remove_keep();
207 signal(SIGINT, SIG_DFL);
208 raise(signo);
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);
217 while (ref) {
218 const char *next;
219 int single_force, not_for_merge;
221 while (*ref == '\n')
222 ref++;
223 if (!*ref)
224 break;
225 next = strchr(ref, '\n');
227 single_force = not_for_merge = 0;
228 if (*ref == '+') {
229 single_force = 1;
230 ref++;
232 if (*ref == '.') {
233 not_for_merge = 1;
234 ref++;
235 if (*ref == '+') {
236 single_force = 1;
237 ref++;
240 if (!strncmp(remote_name, ref, len) && ref[len] == ':') {
241 const char *local_part = ref + len + 1;
242 char *ret;
243 int retlen;
245 if (!next)
246 retlen = strlen(local_part);
247 else
248 retlen = next - local_part;
249 ret = xmalloc(retlen + 1);
250 memcpy(ret, local_part, retlen);
251 ret[retlen] = 0;
252 *force_p = single_force;
253 *not_for_merge_p = not_for_merge;
254 return ret;
256 ref = next;
258 return NULL;
261 static int fetch_native_store(FILE *fp,
262 const char *remote,
263 const char *remote_nick,
264 const char *refs,
265 int verbose, int force)
267 char buffer[1024];
268 int err = 0;
270 signal(SIGINT, remove_keep_on_signal);
271 atexit(remove_keep);
273 while (fgets(buffer, sizeof(buffer), stdin)) {
274 int len;
275 char *cp;
276 char *local_name;
277 int single_force, not_for_merge;
279 for (cp = buffer; *cp && !isspace(*cp); cp++)
281 if (*cp)
282 *cp++ = 0;
283 len = strlen(cp);
284 if (len && cp[len-1] == '\n')
285 cp[--len] = 0;
286 if (!strcmp(buffer, "failed"))
287 die("Fetch failure: %s", remote);
288 if (!strcmp(buffer, "pack"))
289 continue;
290 if (!strcmp(buffer, "keep")) {
291 char *od = get_object_directory();
292 int len = strlen(od) + strlen(cp) + 50;
293 keep = xmalloc(len);
294 sprintf(keep, "%s/pack/pack-%s.keep", od, cp);
295 continue;
298 local_name = find_local_name(cp, refs,
299 &single_force, &not_for_merge);
300 if (!local_name)
301 continue;
302 err |= append_fetch_head(fp,
303 buffer, remote, cp, remote_nick,
304 local_name, not_for_merge,
305 verbose, force || single_force);
307 return err;
310 static int parse_reflist(const char *reflist)
312 const char *ref;
314 printf("refs='");
315 for (ref = reflist; ref; ) {
316 const char *next;
317 while (*ref && isspace(*ref))
318 ref++;
319 if (!*ref)
320 break;
321 for (next = ref; *next && !isspace(*next); next++)
323 printf("\n%.*s", (int)(next - ref), ref);
324 ref = next;
326 printf("'\n");
328 printf("rref='");
329 for (ref = reflist; ref; ) {
330 const char *next, *colon;
331 while (*ref && isspace(*ref))
332 ref++;
333 if (!*ref)
334 break;
335 for (next = ref; *next && !isspace(*next); next++)
337 if (*ref == '.')
338 ref++;
339 if (*ref == '+')
340 ref++;
341 colon = strchr(ref, ':');
342 putchar('\n');
343 printf("%.*s", (int)((colon ? colon : next) - ref), ref);
344 ref = next;
346 printf("'\n");
347 return 0;
350 static int expand_refs_wildcard(const char *ls_remote_result, int numrefs,
351 const char **refs)
353 int i, matchlen, replacelen;
354 int found_one = 0;
355 const char *remote = *refs++;
356 numrefs--;
358 if (numrefs == 0) {
359 fprintf(stderr, "Nothing specified for fetching with remote.%s.fetch\n",
360 remote);
361 printf("empty\n");
364 for (i = 0; i < numrefs; i++) {
365 const char *ref = refs[i];
366 const char *lref = ref;
367 const char *colon;
368 const char *tail;
369 const char *ls;
370 const char *next;
372 if (*lref == '+')
373 lref++;
374 colon = strchr(lref, ':');
375 tail = lref + strlen(lref);
376 if (!(colon &&
377 2 < colon - lref &&
378 colon[-1] == '*' &&
379 colon[-2] == '/' &&
380 2 < tail - (colon + 1) &&
381 tail[-1] == '*' &&
382 tail[-2] == '/')) {
383 /* not a glob */
384 if (!found_one++)
385 printf("explicit\n");
386 printf("%s\n", ref);
387 continue;
390 /* glob */
391 if (!found_one++)
392 printf("glob\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) {
400 const char *eol;
401 unsigned char sha1[20];
402 int namelen;
404 while (*ls && isspace(*ls))
405 ls++;
406 next = strchr(ls, '\n');
407 eol = !next ? (ls + strlen(ls)) : next;
408 if (!memcmp("^{}", eol-3, 3))
409 continue;
410 if (eol - ls < 40)
411 continue;
412 if (get_sha1_hex(ls, sha1))
413 continue;
414 ls += 40;
415 while (ls < eol && isspace(*ls))
416 ls++;
417 /* ls to next (or eol) is the name.
418 * is it identical to lref to colon-2?
420 if ((eol - ls) <= matchlen ||
421 strncmp(ls, lref, matchlen))
422 continue;
424 /* Yes, it is a match */
425 namelen = eol - ls;
426 if (lref != ref)
427 putchar('+');
428 printf("%.*s:%.*s%.*s\n",
429 namelen, ls,
430 replacelen, colon + 1,
431 namelen - matchlen, ls + matchlen);
434 return 0;
437 static int pick_rref(int sha1_only, const char *rref, const char *ls_remote_result)
439 int err = 0;
440 int lrr_count = lrr_count, i, pass;
441 const char *cp;
442 struct lrr {
443 const char *line;
444 const char *name;
445 int namelen;
446 int shown;
447 } *lrr_list = lrr_list;
449 for (pass = 0; pass < 2; pass++) {
450 /* pass 0 counts and allocates, pass 1 fills... */
451 cp = ls_remote_result;
452 i = 0;
453 while (1) {
454 const char *np;
455 while (*cp && isspace(*cp))
456 cp++;
457 if (!*cp)
458 break;
459 np = strchr(cp, '\n');
460 if (!np)
461 np = cp + strlen(cp);
462 if (pass) {
463 lrr_list[i].line = cp;
464 lrr_list[i].name = cp + 41;
465 lrr_list[i].namelen = np - (cp + 41);
467 i++;
468 cp = np;
470 if (!pass) {
471 lrr_count = i;
472 lrr_list = xcalloc(lrr_count, sizeof(*lrr_list));
476 while (1) {
477 const char *next;
478 int rreflen;
479 int i;
481 while (*rref && isspace(*rref))
482 rref++;
483 if (!*rref)
484 break;
485 next = strchr(rref, '\n');
486 if (!next)
487 next = rref + strlen(rref);
488 rreflen = next - rref;
490 for (i = 0; i < lrr_count; i++) {
491 struct lrr *lrr = &(lrr_list[i]);
493 if (rreflen == lrr->namelen &&
494 !memcmp(lrr->name, rref, rreflen)) {
495 if (!lrr->shown)
496 printf("%.*s\n",
497 sha1_only ? 40 : lrr->namelen + 41,
498 lrr->line);
499 lrr->shown = 1;
500 break;
503 if (lrr_count <= i) {
504 error("pick-rref: %.*s not found", rreflen, rref);
505 err = 1;
507 rref = next;
509 free(lrr_list);
510 return err;
513 int cmd_fetch__tool(int argc, const char **argv, const char *prefix)
515 int verbose = 0;
516 int force = 0;
517 int sopt = 0;
519 while (1 < argc) {
520 const char *arg = argv[1];
521 if (!strcmp("-v", arg))
522 verbose = 1;
523 else if (!strcmp("-f", arg))
524 force = 1;
525 else if (!strcmp("-s", arg))
526 sopt = 1;
527 else
528 break;
529 argc--;
530 argv++;
533 if (argc <= 1)
534 return error("Missing subcommand");
536 if (!strcmp("append-fetch-head", argv[1])) {
537 int result;
538 FILE *fp;
540 if (argc != 8)
541 return error("append-fetch-head takes 6 args");
542 fp = fopen(git_path("FETCH_HEAD"), "a");
543 result = append_fetch_head(fp, argv[2], argv[3],
544 argv[4], argv[5],
545 argv[6], !!argv[7][0],
546 verbose, force);
547 fclose(fp);
548 return result;
550 if (!strcmp("native-store", argv[1])) {
551 int result;
552 FILE *fp;
554 if (argc != 5)
555 return error("fetch-native-store takes 3 args");
556 fp = fopen(git_path("FETCH_HEAD"), "a");
557 result = fetch_native_store(fp, argv[2], argv[3], argv[4],
558 verbose, force);
559 fclose(fp);
560 return result;
562 if (!strcmp("parse-reflist", argv[1])) {
563 const char *reflist;
564 if (argc != 3)
565 return error("parse-reflist takes 1 arg");
566 reflist = argv[2];
567 if (!strcmp(reflist, "-"))
568 reflist = get_stdin();
569 return parse_reflist(reflist);
571 if (!strcmp("pick-rref", argv[1])) {
572 const char *ls_remote_result;
573 if (argc != 4)
574 return error("pick-rref takes 2 args");
575 ls_remote_result = argv[3];
576 if (!strcmp(ls_remote_result, "-"))
577 ls_remote_result = get_stdin();
578 return pick_rref(sopt, argv[2], ls_remote_result);
580 if (!strcmp("expand-refs-wildcard", argv[1])) {
581 const char *reflist;
582 if (argc < 4)
583 return error("expand-refs-wildcard takes at least 2 args");
584 reflist = argv[2];
585 if (!strcmp(reflist, "-"))
586 reflist = get_stdin();
587 return expand_refs_wildcard(reflist, argc - 3, argv + 3);
590 return error("Unknown subcommand: %s", argv[1]);