@matheusbsbs / matheus-bagliano / commits / 895a1d1e57
Fork 0

git-fetch--tool pick-rref

This script helper takes list of fully qualified refnames and results from ls-remote and grabs only the lines for the named refs from the latter. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Apr 5, 2007 at 03:22 UTC 895a1d1e57c8dfb8470bb742f05f1969b9961e07
1 file changed +84
builtin-fetch--tool.c
+84
@@ -436,10 +436,87 @@ static int expand_refs_wildcard(const char *ls_remote_result, int numrefs,
436 return 0;
437 }
438
439 +static int pick_rref(int sha1_only, const char *rref, const char *ls_remote_result)
440 +{
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;
450 +
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);
468 + }
469 + i++;
470 + cp = np;
471 + }
472 + if (!pass) {
473 + lrr_count = i;
474 + lrr_list = xcalloc(lrr_count, sizeof(*lrr_list));
475 + }
476 + }
477 +
478 + while (1) {
479 + const char *next;
480 + int rreflen;
481 + int i;
482 +
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;
491 +
492 + for (i = 0; i < lrr_count; i++) {
493 + struct lrr *lrr = &(lrr_list[i]);
494 +
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;
503 + }
504 + }
505 + if (lrr_count <= i) {
506 + error("pick-rref: %.*s not found", rreflen, rref);
507 + err = 1;
508 + }
509 + rref = next;
510 + }
511 + free(lrr_list);
512 + return err;
513 +}
514 +
515 int cmd_fetch__tool(int argc, const char **argv, const char *prefix)
516 {
517 int verbose = 0;
518 int force = 0;
519 + int sopt = 0;
520
521 while (1 < argc) {
522 const char *arg = argv[1];
@@ -447,6 +524,8 @@ int cmd_fetch__tool(int argc, const char **argv, const char *prefix)
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--;
@@ -491,6 +570,11 @@ int cmd_fetch__tool(int argc, const char **argv, const char *prefix)
570 reflist = get_stdin();
571 return parse_reflist(reflist);
572 }
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]);
577 + }
578 if (!strcmp("expand-refs-wildcard", argv[1])) {
579 const char *reflist;
580 if (argc < 4)