Support 'diff=pgm' attribute
[git/jrn.git] / builtin-fetch--tool.c
blobbe341c159fb17117c4cc55a8efa9e2107375a06b
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 static int pick_rref(int sha1_only, const char *rref, const char *ls_remote_result)
441 int err = 0;
442 int lrr_count = lrr_count, i, pass;
443 const char *cp;
444 struct lrr {
445 const char *line;
446 const char *name;
447 int namelen;
448 int shown;
449 } *lrr_list = lrr_list;
451 for (pass = 0; pass < 2; pass++) {
452 /* pass 0 counts and allocates, pass 1 fills... */
453 cp = ls_remote_result;
454 i = 0;
455 while (1) {
456 const char *np;
457 while (*cp && isspace(*cp))
458 cp++;
459 if (!*cp)
460 break;
461 np = strchr(cp, '\n');
462 if (!np)
463 np = cp + strlen(cp);
464 if (pass) {
465 lrr_list[i].line = cp;
466 lrr_list[i].name = cp + 41;
467 lrr_list[i].namelen = np - (cp + 41);
469 i++;
470 cp = np;
472 if (!pass) {
473 lrr_count = i;
474 lrr_list = xcalloc(lrr_count, sizeof(*lrr_list));
478 while (1) {
479 const char *next;
480 int rreflen;
481 int i;
483 while (*rref && isspace(*rref))
484 rref++;
485 if (!*rref)
486 break;
487 next = strchr(rref, '\n');
488 if (!next)
489 next = rref + strlen(rref);
490 rreflen = next - rref;
492 for (i = 0; i < lrr_count; i++) {
493 struct lrr *lrr = &(lrr_list[i]);
495 if (rreflen == lrr->namelen &&
496 !memcmp(lrr->name, rref, rreflen)) {
497 if (!lrr->shown)
498 printf("%.*s\n",
499 sha1_only ? 40 : lrr->namelen + 41,
500 lrr->line);
501 lrr->shown = 1;
502 break;
505 if (lrr_count <= i) {
506 error("pick-rref: %.*s not found", rreflen, rref);
507 err = 1;
509 rref = next;
511 free(lrr_list);
512 return err;
515 int cmd_fetch__tool(int argc, const char **argv, const char *prefix)
517 int verbose = 0;
518 int force = 0;
519 int sopt = 0;
521 while (1 < argc) {
522 const char *arg = argv[1];
523 if (!strcmp("-v", arg))
524 verbose = 1;
525 else if (!strcmp("-f", arg))
526 force = 1;
527 else if (!strcmp("-s", arg))
528 sopt = 1;
529 else
530 break;
531 argc--;
532 argv++;
535 if (argc <= 1)
536 return error("Missing subcommand");
538 if (!strcmp("append-fetch-head", argv[1])) {
539 int result;
540 FILE *fp;
542 if (argc != 8)
543 return error("append-fetch-head takes 6 args");
544 fp = fopen(git_path("FETCH_HEAD"), "a");
545 result = append_fetch_head(fp, argv[2], argv[3],
546 argv[4], argv[5],
547 argv[6], !!argv[7][0],
548 verbose, force);
549 fclose(fp);
550 return result;
552 if (!strcmp("native-store", argv[1])) {
553 int result;
554 FILE *fp;
556 if (argc != 5)
557 return error("fetch-native-store takes 3 args");
558 fp = fopen(git_path("FETCH_HEAD"), "a");
559 result = fetch_native_store(fp, argv[2], argv[3], argv[4],
560 verbose, force);
561 fclose(fp);
562 return result;
564 if (!strcmp("parse-reflist", argv[1])) {
565 const char *reflist;
566 if (argc != 3)
567 return error("parse-reflist takes 1 arg");
568 reflist = argv[2];
569 if (!strcmp(reflist, "-"))
570 reflist = get_stdin();
571 return parse_reflist(reflist);
573 if (!strcmp("pick-rref", argv[1])) {
574 if (argc != 4)
575 return error("pick-rref takes 2 args");
576 return pick_rref(sopt, argv[2], argv[3]);
578 if (!strcmp("expand-refs-wildcard", argv[1])) {
579 const char *reflist;
580 if (argc < 4)
581 return error("expand-refs-wildcard takes at least 2 args");
582 reflist = argv[2];
583 if (!strcmp(reflist, "-"))
584 reflist = get_stdin();
585 return expand_refs_wildcard(reflist, argc - 3, argv + 3);
588 return error("Unknown subcommand: %s", argv[1]);