builtin-fetch--tool.con commit git-fetch: rewrite expand_ref_wildcard in C (8655158)
   1#include "cache.h"
   2#include "refs.h"
   3#include "commit.h"
   4
   5static void show_new(char *type, unsigned char *sha1_new)
   6{
   7        fprintf(stderr, "  %s: %s\n", type,
   8                find_unique_abbrev(sha1_new, DEFAULT_ABBREV));
   9}
  10
  11static int update_ref(const char *action,
  12                      const char *refname,
  13                      unsigned char *sha1,
  14                      unsigned char *oldval)
  15{
  16        int len;
  17        char msg[1024];
  18        char *rla = getenv("GIT_REFLOG_ACTION");
  19        static struct ref_lock *lock;
  20
  21        if (!rla)
  22                rla = "(reflog update)";
  23        len = snprintf(msg, sizeof(msg), "%s: %s", rla, action);
  24        if (sizeof(msg) <= len)
  25                die("insanely long action");
  26        lock = lock_any_ref_for_update(refname, oldval);
  27        if (!lock)
  28                return 1;
  29        if (write_ref_sha1(lock, sha1, msg) < 0)
  30                return 1;
  31        return 0;
  32}
  33
  34static int update_local_ref(const char *name,
  35                            const char *new_head,
  36                            const char *note,
  37                            int verbose, int force)
  38{
  39        char type[20];
  40        unsigned char sha1_old[20], sha1_new[20];
  41        char oldh[41], newh[41];
  42        struct commit *current, *updated;
  43
  44        if (get_sha1_hex(new_head, sha1_new))
  45                die("malformed object name %s", new_head);
  46        if (sha1_object_info(sha1_new, type, NULL))
  47                die("object %s not found", new_head);
  48
  49        if (!*name) {
  50                /* Not storing */
  51                if (verbose) {
  52                        fprintf(stderr, "* fetched %s\n", note);
  53                        show_new(type, sha1_new);
  54                }
  55                return 0;
  56        }
  57
  58        if (get_sha1(name, sha1_old)) {
  59                char *msg;
  60        just_store:
  61                /* new ref */
  62                if (!strncmp(name, "refs/tags/", 10))
  63                        msg = "storing tag";
  64                else
  65                        msg = "storing head";
  66                fprintf(stderr, "* %s: storing %s\n",
  67                        name, note);
  68                show_new(type, sha1_new);
  69                return update_ref(msg, name, sha1_new, NULL);
  70        }
  71
  72        if (!hashcmp(sha1_old, sha1_new)) {
  73                if (verbose) {
  74                        fprintf(stderr, "* %s: same as %s\n", name, note);
  75                        show_new(type, sha1_new);
  76                }
  77                return 0;
  78        }
  79
  80        if (!strncmp(name, "refs/tags/", 10)) {
  81                fprintf(stderr, "* %s: updating with %s\n", name, note);
  82                show_new(type, sha1_new);
  83                return update_ref("updating tag", name, sha1_new, NULL);
  84        }
  85
  86        current = lookup_commit_reference(sha1_old);
  87        updated = lookup_commit_reference(sha1_new);
  88        if (!current || !updated)
  89                goto just_store;
  90
  91        strcpy(oldh, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
  92        strcpy(newh, find_unique_abbrev(sha1_new, DEFAULT_ABBREV));
  93
  94        if (in_merge_bases(current, &updated, 1)) {
  95                fprintf(stderr, "* %s: fast forward to %s\n",
  96                        name, note);
  97                fprintf(stderr, "  old..new: %s..%s\n", oldh, newh);
  98                return update_ref("fast forward", name, sha1_new, sha1_old);
  99        }
 100        if (!force) {
 101                fprintf(stderr,
 102                        "* %s: not updating to non-fast forward %s\n",
 103                        name, note);
 104                fprintf(stderr,
 105                        "  old...new: %s...%s\n", oldh, newh);
 106                return 1;
 107        }
 108        fprintf(stderr,
 109                "* %s: forcing update to non-fast forward %s\n",
 110                name, note);
 111        fprintf(stderr, "  old...new: %s...%s\n", oldh, newh);
 112        return update_ref("forced-update", name, sha1_new, sha1_old);
 113}
 114
 115static int append_fetch_head(FILE *fp,
 116                             const char *head, const char *remote,
 117                             const char *remote_name, const char *remote_nick,
 118                             const char *local_name, int not_for_merge,
 119                             int verbose, int force)
 120{
 121        struct commit *commit;
 122        int remote_len, i, note_len;
 123        unsigned char sha1[20];
 124        char note[1024];
 125        const char *what, *kind;
 126
 127        if (get_sha1(head, sha1))
 128                return error("Not a valid object name: %s", head);
 129        commit = lookup_commit_reference(sha1);
 130        if (!commit)
 131                not_for_merge = 1;
 132
 133        if (!strcmp(remote_name, "HEAD")) {
 134                kind = "";
 135                what = "";
 136        }
 137        else if (!strncmp(remote_name, "refs/heads/", 11)) {
 138                kind = "branch";
 139                what = remote_name + 11;
 140        }
 141        else if (!strncmp(remote_name, "refs/tags/", 10)) {
 142                kind = "tag";
 143                what = remote_name + 10;
 144        }
 145        else if (!strncmp(remote_name, "refs/remotes/", 13)) {
 146                kind = "remote branch";
 147                what = remote_name + 13;
 148        }
 149        else {
 150                kind = "";
 151                what = remote_name;
 152        }
 153
 154        remote_len = strlen(remote);
 155        for (i = remote_len - 1; remote[i] == '/' && 0 <= i; i--)
 156                ;
 157        remote_len = i + 1;
 158        if (4 < i && !strncmp(".git", remote + i - 3, 4))
 159                remote_len = i - 3;
 160        note_len = sprintf(note, "%s\t%s\t",
 161                           sha1_to_hex(commit ? commit->object.sha1 : sha1),
 162                           not_for_merge ? "not-for-merge" : "");
 163        if (*what) {
 164                if (*kind)
 165                        note_len += sprintf(note + note_len, "%s ", kind);
 166                note_len += sprintf(note + note_len, "'%s' of ", what);
 167        }
 168        note_len += sprintf(note + note_len, "%.*s", remote_len, remote);
 169        fprintf(fp, "%s\n", note);
 170        return update_local_ref(local_name, head, note, verbose, force);
 171}
 172
 173static char *keep;
 174static void remove_keep(void)
 175{
 176        if (keep && *keep)
 177                unlink(keep);
 178}
 179
 180static void remove_keep_on_signal(int signo)
 181{
 182        remove_keep();
 183        signal(SIGINT, SIG_DFL);
 184        raise(signo);
 185}
 186
 187static char *find_local_name(const char *remote_name, const char *refs,
 188                             int *force_p, int *not_for_merge_p)
 189{
 190        const char *ref = refs;
 191        int len = strlen(remote_name);
 192
 193        while (ref) {
 194                const char *next;
 195                int single_force, not_for_merge;
 196
 197                while (*ref == '\n')
 198                        ref++;
 199                if (!*ref)
 200                        break;
 201                next = strchr(ref, '\n');
 202
 203                single_force = not_for_merge = 0;
 204                if (*ref == '+') {
 205                        single_force = 1;
 206                        ref++;
 207                }
 208                if (*ref == '.') {
 209                        not_for_merge = 1;
 210                        ref++;
 211                        if (*ref == '+') {
 212                                single_force = 1;
 213                                ref++;
 214                        }
 215                }
 216                if (!strncmp(remote_name, ref, len) && ref[len] == ':') {
 217                        const char *local_part = ref + len + 1;
 218                        char *ret;
 219                        int retlen;
 220
 221                        if (!next)
 222                                retlen = strlen(local_part);
 223                        else
 224                                retlen = next - local_part;
 225                        ret = xmalloc(retlen + 1);
 226                        memcpy(ret, local_part, retlen);
 227                        ret[retlen] = 0;
 228                        *force_p = single_force;
 229                        *not_for_merge_p = not_for_merge;
 230                        return ret;
 231                }
 232                ref = next;
 233        }
 234        return NULL;
 235}
 236
 237static int fetch_native_store(FILE *fp,
 238                              const char *remote,
 239                              const char *remote_nick,
 240                              const char *refs,
 241                              int verbose, int force)
 242{
 243        char buffer[1024];
 244        int err = 0;
 245
 246        signal(SIGINT, remove_keep_on_signal);
 247        atexit(remove_keep);
 248
 249        while (fgets(buffer, sizeof(buffer), stdin)) {
 250                int len;
 251                char *cp;
 252                char *local_name;
 253                int single_force, not_for_merge;
 254
 255                for (cp = buffer; *cp && !isspace(*cp); cp++)
 256                        ;
 257                if (*cp)
 258                        *cp++ = 0;
 259                len = strlen(cp);
 260                if (len && cp[len-1] == '\n')
 261                        cp[--len] = 0;
 262                if (!strcmp(buffer, "failed"))
 263                        die("Fetch failure: %s", remote);
 264                if (!strcmp(buffer, "pack"))
 265                        continue;
 266                if (!strcmp(buffer, "keep")) {
 267                        char *od = get_object_directory();
 268                        int len = strlen(od) + strlen(cp) + 50;
 269                        keep = xmalloc(len);
 270                        sprintf(keep, "%s/pack/pack-%s.keep", od, cp);
 271                        continue;
 272                }
 273
 274                local_name = find_local_name(cp, refs,
 275                                             &single_force, &not_for_merge);
 276                if (!local_name)
 277                        continue;
 278                err |= append_fetch_head(fp,
 279                                         buffer, remote, cp, remote_nick,
 280                                         local_name, not_for_merge,
 281                                         verbose, force || single_force);
 282        }
 283        return err;
 284}
 285
 286static int parse_reflist(const char *reflist)
 287{
 288        const char *ref;
 289
 290        printf("refs='");
 291        for (ref = reflist; ref; ) {
 292                const char *next;
 293                while (*ref && isspace(*ref))
 294                        ref++;
 295                if (!*ref)
 296                        break;
 297                for (next = ref; *next && !isspace(*next); next++)
 298                        ;
 299                printf("\n%.*s", (int)(next - ref), ref);
 300                ref = next;
 301        }
 302        printf("'\n");
 303
 304        printf("rref='");
 305        for (ref = reflist; ref; ) {
 306                const char *next, *colon;
 307                while (*ref && isspace(*ref))
 308                        ref++;
 309                if (!*ref)
 310                        break;
 311                for (next = ref; *next && !isspace(*next); next++)
 312                        ;
 313                if (*ref == '.')
 314                        ref++;
 315                if (*ref == '+')
 316                        ref++;
 317                colon = strchr(ref, ':');
 318                putchar('\n');
 319                printf("%.*s", (int)((colon ? colon : next) - ref), ref);
 320                ref = next;
 321        }
 322        printf("'\n");
 323        return 0;
 324}
 325
 326static int expand_refs_wildcard(const char *ls_remote_result, int numrefs,
 327                                const char **refs)
 328{
 329        int i, matchlen, replacelen;
 330        int found_one = 0;
 331        const char *remote = *refs++;
 332        numrefs--;
 333
 334        if (numrefs == 0) {
 335                fprintf(stderr, "Nothing specified for fetching with remote.%s.fetch\n",
 336                        remote);
 337                printf("empty\n");
 338        }
 339
 340        for (i = 0; i < numrefs; i++) {
 341                const char *ref = refs[i];
 342                const char *lref = ref;
 343                const char *colon;
 344                const char *tail;
 345                const char *ls;
 346                const char *next;
 347
 348                if (*lref == '+')
 349                        lref++;
 350                colon = strchr(lref, ':');
 351                tail = lref + strlen(lref);
 352                if (!(colon &&
 353                      2 < colon - lref &&
 354                      colon[-1] == '*' &&
 355                      colon[-2] == '/' &&
 356                      2 < tail - (colon + 1) &&
 357                      tail[-1] == '*' &&
 358                      tail[-2] == '/')) {
 359                        /* not a glob */
 360                        if (!found_one++)
 361                                printf("explicit\n");
 362                        printf("%s\n", ref);
 363                        continue;
 364                }
 365
 366                /* glob */
 367                if (!found_one++)
 368                        printf("glob\n");
 369
 370                /* lref to colon-2 is remote hierarchy name;
 371                 * colon+1 to tail-2 is local.
 372                 */
 373                matchlen = (colon-1) - lref;
 374                replacelen = (tail-1) - (colon+1);
 375                for (ls = ls_remote_result; ls; ls = next) {
 376                        const char *eol;
 377                        unsigned char sha1[20];
 378                        int namelen;
 379
 380                        while (*ls && isspace(*ls))
 381                                ls++;
 382                        next = strchr(ls, '\n');
 383                        eol = !next ? (ls + strlen(ls)) : next;
 384                        if (!memcmp("^{}", eol-3, 3))
 385                                continue;
 386                        if (get_sha1_hex(ls, sha1))
 387                                continue;
 388                        ls += 40;
 389                        while (ls < eol && isspace(*ls))
 390                                ls++;
 391                        /* ls to next (or eol) is the name.
 392                         * is it identical to lref to colon-2?
 393                         */
 394                        if ((eol - ls) <= matchlen ||
 395                            strncmp(ls, lref, matchlen))
 396                                continue;
 397
 398                        /* Yes, it is a match */
 399                        namelen = eol - ls;
 400                        if (lref != ref)
 401                                putchar('+');
 402                        printf("%.*s:%.*s%.*s\n",
 403                               namelen, ls,
 404                               replacelen, colon + 1,
 405                               namelen - matchlen, ls + matchlen);
 406                }
 407        }
 408        return 0;
 409}
 410
 411int cmd_fetch__tool(int argc, const char **argv, const char *prefix)
 412{
 413        int verbose = 0;
 414        int force = 0;
 415
 416        while (1 < argc) {
 417                const char *arg = argv[1];
 418                if (!strcmp("-v", arg))
 419                        verbose = 1;
 420                else if (!strcmp("-f", arg))
 421                        force = 1;
 422                else
 423                        break;
 424                argc--;
 425                argv++;
 426        }
 427
 428        if (argc <= 1)
 429                return error("Missing subcommand");
 430
 431        if (!strcmp("append-fetch-head", argv[1])) {
 432                int result;
 433                FILE *fp;
 434
 435                if (argc != 8)
 436                        return error("append-fetch-head takes 6 args");
 437                fp = fopen(git_path("FETCH_HEAD"), "a");
 438                result = append_fetch_head(fp, argv[2], argv[3],
 439                                           argv[4], argv[5],
 440                                           argv[6], !!argv[7][0],
 441                                           verbose, force);
 442                fclose(fp);
 443                return result;
 444        }
 445        if (!strcmp("update-local-ref", argv[1])) {
 446                if (argc != 5)
 447                        return error("update-local-ref takes 3 args");
 448                return update_local_ref(argv[2], argv[3], argv[4],
 449                                        verbose, force);
 450        }
 451        if (!strcmp("native-store", argv[1])) {
 452                int result;
 453                FILE *fp;
 454
 455                if (argc != 5)
 456                        return error("fetch-native-store takes 3 args");
 457                fp = fopen(git_path("FETCH_HEAD"), "a");
 458                result = fetch_native_store(fp, argv[2], argv[3], argv[4],
 459                                            verbose, force);
 460                fclose(fp);
 461                return result;
 462        }
 463        if (!strcmp("parse-reflist", argv[1])) {
 464                if (argc != 3)
 465                        return error("parse-reflist takes 1 arg");
 466                return parse_reflist(argv[2]);
 467        }
 468        if (!strcmp("expand-refs-wildcard", argv[1])) {
 469                if (argc < 4)
 470                        return error("expand-refs-wildcard takes at least 2 args");
 471                return expand_refs_wildcard(argv[2], argc - 3, argv + 3);
 472        }
 473
 474        return error("Unknown subcommand: %s", argv[1]);
 475}