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