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