gitweb: use new Git::Repo API, and add optional caching
[git/gitweb-caching.git] / builtin-fetch--tool.c
blob7460ab7fce2a4e6a7e014f448819140e2204ccb7
1 #include "builtin.h"
2 #include "cache.h"
3 #include "refs.h"
4 #include "commit.h"
6 static char *get_stdin(void)
8 struct strbuf buf;
9 strbuf_init(&buf, 0);
10 if (strbuf_read(&buf, 0, 1024) < 0) {
11 die("error reading standard input: %s", strerror(errno));
13 return strbuf_detach(&buf, NULL);
16 static void show_new(enum object_type type, unsigned char *sha1_new)
18 fprintf(stderr, " %s: %s\n", typename(type),
19 find_unique_abbrev(sha1_new, DEFAULT_ABBREV));
22 static int update_ref_env(const char *action,
23 const char *refname,
24 unsigned char *sha1,
25 unsigned char *oldval)
27 char msg[1024];
28 const char *rla = getenv("GIT_REFLOG_ACTION");
30 if (!rla)
31 rla = "(reflog update)";
32 if (snprintf(msg, sizeof(msg), "%s: %s", rla, action) >= sizeof(msg))
33 warning("reflog message too long: %.*s...", 50, msg);
34 return update_ref(msg, refname, sha1, oldval, 0, QUIET_ON_ERR);
37 static int update_local_ref(const char *name,
38 const char *new_head,
39 const char *note,
40 int verbose, int force)
42 unsigned char sha1_old[20], sha1_new[20];
43 char oldh[41], newh[41];
44 struct commit *current, *updated;
45 enum object_type type;
47 if (get_sha1_hex(new_head, sha1_new))
48 die("malformed object name %s", new_head);
50 type = sha1_object_info(sha1_new, NULL);
51 if (type < 0)
52 die("object %s not found", new_head);
54 if (!*name) {
55 /* Not storing */
56 if (verbose) {
57 fprintf(stderr, "* fetched %s\n", note);
58 show_new(type, sha1_new);
60 return 0;
63 if (get_sha1(name, sha1_old)) {
64 const char *msg;
65 just_store:
66 /* new ref */
67 if (!strncmp(name, "refs/tags/", 10))
68 msg = "storing tag";
69 else
70 msg = "storing head";
71 fprintf(stderr, "* %s: storing %s\n",
72 name, note);
73 show_new(type, sha1_new);
74 return update_ref_env(msg, name, sha1_new, NULL);
77 if (!hashcmp(sha1_old, sha1_new)) {
78 if (verbose) {
79 fprintf(stderr, "* %s: same as %s\n", name, note);
80 show_new(type, sha1_new);
82 return 0;
85 if (!strncmp(name, "refs/tags/", 10)) {
86 fprintf(stderr, "* %s: updating with %s\n", name, note);
87 show_new(type, sha1_new);
88 return update_ref_env("updating tag", name, sha1_new, NULL);
91 current = lookup_commit_reference(sha1_old);
92 updated = lookup_commit_reference(sha1_new);
93 if (!current || !updated)
94 goto just_store;
96 strcpy(oldh, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
97 strcpy(newh, find_unique_abbrev(sha1_new, DEFAULT_ABBREV));
99 if (in_merge_bases(current, &updated, 1)) {
100 fprintf(stderr, "* %s: fast forward to %s\n",
101 name, note);
102 fprintf(stderr, " old..new: %s..%s\n", oldh, newh);
103 return update_ref_env("fast forward", name, sha1_new, sha1_old);
105 if (!force) {
106 fprintf(stderr,
107 "* %s: not updating to non-fast forward %s\n",
108 name, note);
109 fprintf(stderr,
110 " old...new: %s...%s\n", oldh, newh);
111 return 1;
113 fprintf(stderr,
114 "* %s: forcing update to non-fast forward %s\n",
115 name, note);
116 fprintf(stderr, " old...new: %s...%s\n", oldh, newh);
117 return update_ref_env("forced-update", name, sha1_new, sha1_old);
120 static int append_fetch_head(FILE *fp,
121 const char *head, const char *remote,
122 const char *remote_name, const char *remote_nick,
123 const char *local_name, int not_for_merge,
124 int verbose, int force)
126 struct commit *commit;
127 int remote_len, i, note_len;
128 unsigned char sha1[20];
129 char note[1024];
130 const char *what, *kind;
132 if (get_sha1(head, sha1))
133 return error("Not a valid object name: %s", head);
134 commit = lookup_commit_reference_gently(sha1, 1);
135 if (!commit)
136 not_for_merge = 1;
138 if (!strcmp(remote_name, "HEAD")) {
139 kind = "";
140 what = "";
142 else if (!strncmp(remote_name, "refs/heads/", 11)) {
143 kind = "branch";
144 what = remote_name + 11;
146 else if (!strncmp(remote_name, "refs/tags/", 10)) {
147 kind = "tag";
148 what = remote_name + 10;
150 else if (!strncmp(remote_name, "refs/remotes/", 13)) {
151 kind = "remote branch";
152 what = remote_name + 13;
154 else {
155 kind = "";
156 what = remote_name;
159 remote_len = strlen(remote);
160 for (i = remote_len - 1; remote[i] == '/' && 0 <= i; i--)
162 remote_len = i + 1;
163 if (4 < i && !strncmp(".git", remote + i - 3, 4))
164 remote_len = i - 3;
166 note_len = 0;
167 if (*what) {
168 if (*kind)
169 note_len += sprintf(note + note_len, "%s ", kind);
170 note_len += sprintf(note + note_len, "'%s' of ", what);
172 note_len += sprintf(note + note_len, "%.*s", remote_len, remote);
173 fprintf(fp, "%s\t%s\t%s\n",
174 sha1_to_hex(commit ? commit->object.sha1 : sha1),
175 not_for_merge ? "not-for-merge" : "",
176 note);
177 return update_local_ref(local_name, head, note, verbose, force);
180 static char *keep;
181 static void remove_keep(void)
183 if (keep && *keep)
184 unlink(keep);
187 static void remove_keep_on_signal(int signo)
189 remove_keep();
190 signal(SIGINT, SIG_DFL);
191 raise(signo);
194 static char *find_local_name(const char *remote_name, const char *refs,
195 int *force_p, int *not_for_merge_p)
197 const char *ref = refs;
198 int len = strlen(remote_name);
200 while (ref) {
201 const char *next;
202 int single_force, not_for_merge;
204 while (*ref == '\n')
205 ref++;
206 if (!*ref)
207 break;
208 next = strchr(ref, '\n');
210 single_force = not_for_merge = 0;
211 if (*ref == '+') {
212 single_force = 1;
213 ref++;
215 if (*ref == '.') {
216 not_for_merge = 1;
217 ref++;
218 if (*ref == '+') {
219 single_force = 1;
220 ref++;
223 if (!strncmp(remote_name, ref, len) && ref[len] == ':') {
224 const char *local_part = ref + len + 1;
225 int retlen;
227 if (!next)
228 retlen = strlen(local_part);
229 else
230 retlen = next - local_part;
231 *force_p = single_force;
232 *not_for_merge_p = not_for_merge;
233 return xmemdupz(local_part, retlen);
235 ref = next;
237 return NULL;
240 static int fetch_native_store(FILE *fp,
241 const char *remote,
242 const char *remote_nick,
243 const char *refs,
244 int verbose, int force)
246 char buffer[1024];
247 int err = 0;
249 signal(SIGINT, remove_keep_on_signal);
250 atexit(remove_keep);
252 while (fgets(buffer, sizeof(buffer), stdin)) {
253 int len;
254 char *cp;
255 char *local_name;
256 int single_force, not_for_merge;
258 for (cp = buffer; *cp && !isspace(*cp); cp++)
260 if (*cp)
261 *cp++ = 0;
262 len = strlen(cp);
263 if (len && cp[len-1] == '\n')
264 cp[--len] = 0;
265 if (!strcmp(buffer, "failed"))
266 die("Fetch failure: %s", remote);
267 if (!strcmp(buffer, "pack"))
268 continue;
269 if (!strcmp(buffer, "keep")) {
270 char *od = get_object_directory();
271 int len = strlen(od) + strlen(cp) + 50;
272 keep = xmalloc(len);
273 sprintf(keep, "%s/pack/pack-%s.keep", od, cp);
274 continue;
277 local_name = find_local_name(cp, refs,
278 &single_force, &not_for_merge);
279 if (!local_name)
280 continue;
281 err |= append_fetch_head(fp,
282 buffer, remote, cp, remote_nick,
283 local_name, not_for_merge,
284 verbose, force || single_force);
286 return err;
289 static int parse_reflist(const char *reflist)
291 const char *ref;
293 printf("refs='");
294 for (ref = reflist; ref; ) {
295 const char *next;
296 while (*ref && isspace(*ref))
297 ref++;
298 if (!*ref)
299 break;
300 for (next = ref; *next && !isspace(*next); next++)
302 printf("\n%.*s", (int)(next - ref), ref);
303 ref = next;
305 printf("'\n");
307 printf("rref='");
308 for (ref = reflist; ref; ) {
309 const char *next, *colon;
310 while (*ref && isspace(*ref))
311 ref++;
312 if (!*ref)
313 break;
314 for (next = ref; *next && !isspace(*next); next++)
316 if (*ref == '.')
317 ref++;
318 if (*ref == '+')
319 ref++;
320 colon = strchr(ref, ':');
321 putchar('\n');
322 printf("%.*s", (int)((colon ? colon : next) - ref), ref);
323 ref = next;
325 printf("'\n");
326 return 0;
329 static int expand_refs_wildcard(const char *ls_remote_result, int numrefs,
330 const char **refs)
332 int i, matchlen, replacelen;
333 int found_one = 0;
334 const char *remote = *refs++;
335 numrefs--;
337 if (numrefs == 0) {
338 fprintf(stderr, "Nothing specified for fetching with remote.%s.fetch\n",
339 remote);
340 printf("empty\n");
343 for (i = 0; i < numrefs; i++) {
344 const char *ref = refs[i];
345 const char *lref = ref;
346 const char *colon;
347 const char *tail;
348 const char *ls;
349 const char *next;
351 if (*lref == '+')
352 lref++;
353 colon = strchr(lref, ':');
354 tail = lref + strlen(lref);
355 if (!(colon &&
356 2 < colon - lref &&
357 colon[-1] == '*' &&
358 colon[-2] == '/' &&
359 2 < tail - (colon + 1) &&
360 tail[-1] == '*' &&
361 tail[-2] == '/')) {
362 /* not a glob */
363 if (!found_one++)
364 printf("explicit\n");
365 printf("%s\n", ref);
366 continue;
369 /* glob */
370 if (!found_one++)
371 printf("glob\n");
373 /* lref to colon-2 is remote hierarchy name;
374 * colon+1 to tail-2 is local.
376 matchlen = (colon-1) - lref;
377 replacelen = (tail-1) - (colon+1);
378 for (ls = ls_remote_result; ls; ls = next) {
379 const char *eol;
380 unsigned char sha1[20];
381 int namelen;
383 while (*ls && isspace(*ls))
384 ls++;
385 next = strchr(ls, '\n');
386 eol = !next ? (ls + strlen(ls)) : next;
387 if (!memcmp("^{}", eol-3, 3))
388 continue;
389 if (eol - ls < 40)
390 continue;
391 if (get_sha1_hex(ls, sha1))
392 continue;
393 ls += 40;
394 while (ls < eol && isspace(*ls))
395 ls++;
396 /* ls to next (or eol) is the name.
397 * is it identical to lref to colon-2?
399 if ((eol - ls) <= matchlen ||
400 strncmp(ls, lref, matchlen))
401 continue;
403 /* Yes, it is a match */
404 namelen = eol - ls;
405 if (lref != ref)
406 putchar('+');
407 printf("%.*s:%.*s%.*s\n",
408 namelen, ls,
409 replacelen, colon + 1,
410 namelen - matchlen, ls + matchlen);
413 return 0;
416 static int pick_rref(int sha1_only, const char *rref, const char *ls_remote_result)
418 int err = 0;
419 int lrr_count = lrr_count, i, pass;
420 const char *cp;
421 struct lrr {
422 const char *line;
423 const char *name;
424 int namelen;
425 int shown;
426 } *lrr_list = lrr_list;
428 for (pass = 0; pass < 2; pass++) {
429 /* pass 0 counts and allocates, pass 1 fills... */
430 cp = ls_remote_result;
431 i = 0;
432 while (1) {
433 const char *np;
434 while (*cp && isspace(*cp))
435 cp++;
436 if (!*cp)
437 break;
438 np = strchrnul(cp, '\n');
439 if (pass) {
440 lrr_list[i].line = cp;
441 lrr_list[i].name = cp + 41;
442 lrr_list[i].namelen = np - (cp + 41);
444 i++;
445 cp = np;
447 if (!pass) {
448 lrr_count = i;
449 lrr_list = xcalloc(lrr_count, sizeof(*lrr_list));
453 while (1) {
454 const char *next;
455 int rreflen;
456 int i;
458 while (*rref && isspace(*rref))
459 rref++;
460 if (!*rref)
461 break;
462 next = strchrnul(rref, '\n');
463 rreflen = next - rref;
465 for (i = 0; i < lrr_count; i++) {
466 struct lrr *lrr = &(lrr_list[i]);
468 if (rreflen == lrr->namelen &&
469 !memcmp(lrr->name, rref, rreflen)) {
470 if (!lrr->shown)
471 printf("%.*s\n",
472 sha1_only ? 40 : lrr->namelen + 41,
473 lrr->line);
474 lrr->shown = 1;
475 break;
478 if (lrr_count <= i) {
479 error("pick-rref: %.*s not found", rreflen, rref);
480 err = 1;
482 rref = next;
484 free(lrr_list);
485 return err;
488 int cmd_fetch__tool(int argc, const char **argv, const char *prefix)
490 int verbose = 0;
491 int force = 0;
492 int sopt = 0;
494 while (1 < argc) {
495 const char *arg = argv[1];
496 if (!strcmp("-v", arg))
497 verbose = 1;
498 else if (!strcmp("-f", arg))
499 force = 1;
500 else if (!strcmp("-s", arg))
501 sopt = 1;
502 else
503 break;
504 argc--;
505 argv++;
508 if (argc <= 1)
509 return error("Missing subcommand");
511 if (!strcmp("append-fetch-head", argv[1])) {
512 int result;
513 FILE *fp;
514 char *filename;
516 if (argc != 8)
517 return error("append-fetch-head takes 6 args");
518 filename = git_path("FETCH_HEAD");
519 fp = fopen(filename, "a");
520 if (!fp)
521 return error("cannot open %s: %s\n", filename, strerror(errno));
522 result = append_fetch_head(fp, argv[2], argv[3],
523 argv[4], argv[5],
524 argv[6], !!argv[7][0],
525 verbose, force);
526 fclose(fp);
527 return result;
529 if (!strcmp("native-store", argv[1])) {
530 int result;
531 FILE *fp;
532 char *filename;
534 if (argc != 5)
535 return error("fetch-native-store takes 3 args");
536 filename = git_path("FETCH_HEAD");
537 fp = fopen(filename, "a");
538 if (!fp)
539 return error("cannot open %s: %s\n", filename, strerror(errno));
540 result = fetch_native_store(fp, argv[2], argv[3], argv[4],
541 verbose, force);
542 fclose(fp);
543 return result;
545 if (!strcmp("parse-reflist", argv[1])) {
546 const char *reflist;
547 if (argc != 3)
548 return error("parse-reflist takes 1 arg");
549 reflist = argv[2];
550 if (!strcmp(reflist, "-"))
551 reflist = get_stdin();
552 return parse_reflist(reflist);
554 if (!strcmp("pick-rref", argv[1])) {
555 const char *ls_remote_result;
556 if (argc != 4)
557 return error("pick-rref takes 2 args");
558 ls_remote_result = argv[3];
559 if (!strcmp(ls_remote_result, "-"))
560 ls_remote_result = get_stdin();
561 return pick_rref(sopt, argv[2], ls_remote_result);
563 if (!strcmp("expand-refs-wildcard", argv[1])) {
564 const char *reflist;
565 if (argc < 4)
566 return error("expand-refs-wildcard takes at least 2 args");
567 reflist = argv[2];
568 if (!strcmp(reflist, "-"))
569 reflist = get_stdin();
570 return expand_refs_wildcard(reflist, argc - 3, argv + 3);
573 return error("Unknown subcommand: %s", argv[1]);