Make builtin-count-objects.c use parse_options.
[git/dkf.git] / builtin-fetch--tool.c
blob6a78517958567e9dee4bfb00236caf5c3d1c3d67
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 = strchr(cp, '\n');
439 if (!np)
440 np = cp + strlen(cp);
441 if (pass) {
442 lrr_list[i].line = cp;
443 lrr_list[i].name = cp + 41;
444 lrr_list[i].namelen = np - (cp + 41);
446 i++;
447 cp = np;
449 if (!pass) {
450 lrr_count = i;
451 lrr_list = xcalloc(lrr_count, sizeof(*lrr_list));
455 while (1) {
456 const char *next;
457 int rreflen;
458 int i;
460 while (*rref && isspace(*rref))
461 rref++;
462 if (!*rref)
463 break;
464 next = strchr(rref, '\n');
465 if (!next)
466 next = rref + strlen(rref);
467 rreflen = next - rref;
469 for (i = 0; i < lrr_count; i++) {
470 struct lrr *lrr = &(lrr_list[i]);
472 if (rreflen == lrr->namelen &&
473 !memcmp(lrr->name, rref, rreflen)) {
474 if (!lrr->shown)
475 printf("%.*s\n",
476 sha1_only ? 40 : lrr->namelen + 41,
477 lrr->line);
478 lrr->shown = 1;
479 break;
482 if (lrr_count <= i) {
483 error("pick-rref: %.*s not found", rreflen, rref);
484 err = 1;
486 rref = next;
488 free(lrr_list);
489 return err;
492 int cmd_fetch__tool(int argc, const char **argv, const char *prefix)
494 int verbose = 0;
495 int force = 0;
496 int sopt = 0;
498 while (1 < argc) {
499 const char *arg = argv[1];
500 if (!strcmp("-v", arg))
501 verbose = 1;
502 else if (!strcmp("-f", arg))
503 force = 1;
504 else if (!strcmp("-s", arg))
505 sopt = 1;
506 else
507 break;
508 argc--;
509 argv++;
512 if (argc <= 1)
513 return error("Missing subcommand");
515 if (!strcmp("append-fetch-head", argv[1])) {
516 int result;
517 FILE *fp;
519 if (argc != 8)
520 return error("append-fetch-head takes 6 args");
521 fp = fopen(git_path("FETCH_HEAD"), "a");
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;
533 if (argc != 5)
534 return error("fetch-native-store takes 3 args");
535 fp = fopen(git_path("FETCH_HEAD"), "a");
536 result = fetch_native_store(fp, argv[2], argv[3], argv[4],
537 verbose, force);
538 fclose(fp);
539 return result;
541 if (!strcmp("parse-reflist", argv[1])) {
542 const char *reflist;
543 if (argc != 3)
544 return error("parse-reflist takes 1 arg");
545 reflist = argv[2];
546 if (!strcmp(reflist, "-"))
547 reflist = get_stdin();
548 return parse_reflist(reflist);
550 if (!strcmp("pick-rref", argv[1])) {
551 const char *ls_remote_result;
552 if (argc != 4)
553 return error("pick-rref takes 2 args");
554 ls_remote_result = argv[3];
555 if (!strcmp(ls_remote_result, "-"))
556 ls_remote_result = get_stdin();
557 return pick_rref(sopt, argv[2], ls_remote_result);
559 if (!strcmp("expand-refs-wildcard", argv[1])) {
560 const char *reflist;
561 if (argc < 4)
562 return error("expand-refs-wildcard takes at least 2 args");
563 reflist = argv[2];
564 if (!strcmp(reflist, "-"))
565 reflist = get_stdin();
566 return expand_refs_wildcard(reflist, argc - 3, argv + 3);
569 return error("Unknown subcommand: %s", argv[1]);