read-cache: move shared commit and ls-files code

The function overlay_tree_on_index(), plus associated helper functions, were defined in builtin/ls-files.c, but also shared with builtin/commit.c. Move these shared functions to read-cache.c. Diff best viewed with `--color-moved`. Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elijah Newren committed May 16, 2023 at 06:33 UTC 1a40e7be6cb7dda07d23d8beb84622e63d1aa42c
2 files changed +137 -137
builtin/ls-files.c
-137
@@ -543,143 +543,6 @@ static int get_common_prefix_len(const char *common_prefix)
543 return common_prefix_len;
544 }
545
546 -static int read_one_entry_opt(struct index_state *istate,
547 - const struct object_id *oid,
548 - struct strbuf *base,
549 - const char *pathname,
550 - unsigned mode, int opt)
551 -{
552 - int len;
553 - struct cache_entry *ce;
554 -
555 - if (S_ISDIR(mode))
556 - return READ_TREE_RECURSIVE;
557 -
558 - len = strlen(pathname);
559 - ce = make_empty_cache_entry(istate, base->len + len);
560 -
561 - ce->ce_mode = create_ce_mode(mode);
562 - ce->ce_flags = create_ce_flags(1);
563 - ce->ce_namelen = base->len + len;
564 - memcpy(ce->name, base->buf, base->len);
565 - memcpy(ce->name + base->len, pathname, len+1);
566 - oidcpy(&ce->oid, oid);
567 - return add_index_entry(istate, ce, opt);
568 -}
569 -
570 -static int read_one_entry(const struct object_id *oid, struct strbuf *base,
571 - const char *pathname, unsigned mode,
572 - void *context)
573 -{
574 - struct index_state *istate = context;
575 - return read_one_entry_opt(istate, oid, base, pathname,
576 - mode,
577 - ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
578 -}
579 -
580 -/*
581 - * This is used when the caller knows there is no existing entries at
582 - * the stage that will conflict with the entry being added.
583 - */
584 -static int read_one_entry_quick(const struct object_id *oid, struct strbuf *base,
585 - const char *pathname, unsigned mode,
586 - void *context)
587 -{
588 - struct index_state *istate = context;
589 - return read_one_entry_opt(istate, oid, base, pathname,
590 - mode, ADD_CACHE_JUST_APPEND);
591 -}
592 -
593 -/*
594 - * Read the tree specified with --with-tree option
595 - * (typically, HEAD) into stage #1 and then
596 - * squash them down to stage #0. This is used for
597 - * --error-unmatch to list and check the path patterns
598 - * that were given from the command line. We are not
599 - * going to write this index out.
600 - */
601 -void overlay_tree_on_index(struct index_state *istate,
602 - const char *tree_name, const char *prefix)
603 -{
604 - struct tree *tree;
605 - struct object_id oid;
606 - struct pathspec pathspec;
607 - struct cache_entry *last_stage0 = NULL;
608 - int i;
609 - read_tree_fn_t fn = NULL;
610 - int err;
611 -
612 - if (repo_get_oid(the_repository, tree_name, &oid))
613 - die("tree-ish %s not found.", tree_name);
614 - tree = parse_tree_indirect(&oid);
615 - if (!tree)
616 - die("bad tree-ish %s", tree_name);
617 -
618 - /* Hoist the unmerged entries up to stage #3 to make room */
619 - /* TODO: audit for interaction with sparse-index. */
620 - ensure_full_index(istate);
621 - for (i = 0; i < istate->cache_nr; i++) {
622 - struct cache_entry *ce = istate->cache[i];
623 - if (!ce_stage(ce))
624 - continue;
625 - ce->ce_flags |= CE_STAGEMASK;
626 - }
627 -
628 - if (prefix) {
629 - static const char *(matchbuf[1]);
630 - matchbuf[0] = NULL;
631 - parse_pathspec(&pathspec, PATHSPEC_ALL_MAGIC,
632 - PATHSPEC_PREFER_CWD, prefix, matchbuf);
633 - } else
634 - memset(&pathspec, 0, sizeof(pathspec));
635 -
636 - /*
637 - * See if we have cache entry at the stage. If so,
638 - * do it the original slow way, otherwise, append and then
639 - * sort at the end.
640 - */
641 - for (i = 0; !fn && i < istate->cache_nr; i++) {
642 - const struct cache_entry *ce = istate->cache[i];
643 - if (ce_stage(ce) == 1)
644 - fn = read_one_entry;
645 - }
646 -
647 - if (!fn)
648 - fn = read_one_entry_quick;
649 - err = read_tree(the_repository, tree, &pathspec, fn, istate);
650 - clear_pathspec(&pathspec);
651 - if (err)
652 - die("unable to read tree entries %s", tree_name);
653 -
654 - /*
655 - * Sort the cache entry -- we need to nuke the cache tree, though.
656 - */
657 - if (fn == read_one_entry_quick) {
658 - cache_tree_free(&istate->cache_tree);
659 - QSORT(istate->cache, istate->cache_nr, cmp_cache_name_compare);
660 - }
661 -
662 - for (i = 0; i < istate->cache_nr; i++) {
663 - struct cache_entry *ce = istate->cache[i];
664 - switch (ce_stage(ce)) {
665 - case 0:
666 - last_stage0 = ce;
667 - /* fallthru */
668 - default:
669 - continue;
670 - case 1:
671 - /*
672 - * If there is stage #0 entry for this, we do not
673 - * need to show it. We use CE_UPDATE bit to mark
674 - * such an entry.
675 - */
676 - if (last_stage0 &&
677 - !strcmp(last_stage0->name, ce->name))
678 - ce->ce_flags |= CE_UPDATE;
679 - }
680 - }
681 -}
682 -
546 static const char * const ls_files_usage[] = {
547 N_("git ls-files [<options>] [<file>...]"),
548 NULL
read-cache.c
+137
@@ -3806,3 +3806,140 @@ void prefetch_cache_entries(const struct index_state *istate,
3806 to_fetch.oid, to_fetch.nr);
3807 oid_array_clear(&to_fetch);
3808 }
3809 +
3810 +static int read_one_entry_opt(struct index_state *istate,
3811 + const struct object_id *oid,
3812 + struct strbuf *base,
3813 + const char *pathname,
3814 + unsigned mode, int opt)
3815 +{
3816 + int len;
3817 + struct cache_entry *ce;
3818 +
3819 + if (S_ISDIR(mode))
3820 + return READ_TREE_RECURSIVE;
3821 +
3822 + len = strlen(pathname);
3823 + ce = make_empty_cache_entry(istate, base->len + len);
3824 +
3825 + ce->ce_mode = create_ce_mode(mode);
3826 + ce->ce_flags = create_ce_flags(1);
3827 + ce->ce_namelen = base->len + len;
3828 + memcpy(ce->name, base->buf, base->len);
3829 + memcpy(ce->name + base->len, pathname, len+1);
3830 + oidcpy(&ce->oid, oid);
3831 + return add_index_entry(istate, ce, opt);
3832 +}
3833 +
3834 +static int read_one_entry(const struct object_id *oid, struct strbuf *base,
3835 + const char *pathname, unsigned mode,
3836 + void *context)
3837 +{
3838 + struct index_state *istate = context;
3839 + return read_one_entry_opt(istate, oid, base, pathname,
3840 + mode,
3841 + ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
3842 +}
3843 +
3844 +/*
3845 + * This is used when the caller knows there is no existing entries at
3846 + * the stage that will conflict with the entry being added.
3847 + */
3848 +static int read_one_entry_quick(const struct object_id *oid, struct strbuf *base,
3849 + const char *pathname, unsigned mode,
3850 + void *context)
3851 +{
3852 + struct index_state *istate = context;
3853 + return read_one_entry_opt(istate, oid, base, pathname,
3854 + mode, ADD_CACHE_JUST_APPEND);
3855 +}
3856 +
3857 +/*
3858 + * Read the tree specified with --with-tree option
3859 + * (typically, HEAD) into stage #1 and then
3860 + * squash them down to stage #0. This is used for
3861 + * --error-unmatch to list and check the path patterns
3862 + * that were given from the command line. We are not
3863 + * going to write this index out.
3864 + */
3865 +void overlay_tree_on_index(struct index_state *istate,
3866 + const char *tree_name, const char *prefix)
3867 +{
3868 + struct tree *tree;
3869 + struct object_id oid;
3870 + struct pathspec pathspec;
3871 + struct cache_entry *last_stage0 = NULL;
3872 + int i;
3873 + read_tree_fn_t fn = NULL;
3874 + int err;
3875 +
3876 + if (repo_get_oid(the_repository, tree_name, &oid))
3877 + die("tree-ish %s not found.", tree_name);
3878 + tree = parse_tree_indirect(&oid);
3879 + if (!tree)
3880 + die("bad tree-ish %s", tree_name);
3881 +
3882 + /* Hoist the unmerged entries up to stage #3 to make room */
3883 + /* TODO: audit for interaction with sparse-index. */
3884 + ensure_full_index(istate);
3885 + for (i = 0; i < istate->cache_nr; i++) {
3886 + struct cache_entry *ce = istate->cache[i];
3887 + if (!ce_stage(ce))
3888 + continue;
3889 + ce->ce_flags |= CE_STAGEMASK;
3890 + }
3891 +
3892 + if (prefix) {
3893 + static const char *(matchbuf[1]);
3894 + matchbuf[0] = NULL;
3895 + parse_pathspec(&pathspec, PATHSPEC_ALL_MAGIC,
3896 + PATHSPEC_PREFER_CWD, prefix, matchbuf);
3897 + } else
3898 + memset(&pathspec, 0, sizeof(pathspec));
3899 +
3900 + /*
3901 + * See if we have cache entry at the stage. If so,
3902 + * do it the original slow way, otherwise, append and then
3903 + * sort at the end.
3904 + */
3905 + for (i = 0; !fn && i < istate->cache_nr; i++) {
3906 + const struct cache_entry *ce = istate->cache[i];
3907 + if (ce_stage(ce) == 1)
3908 + fn = read_one_entry;
3909 + }
3910 +
3911 + if (!fn)
3912 + fn = read_one_entry_quick;
3913 + err = read_tree(the_repository, tree, &pathspec, fn, istate);
3914 + clear_pathspec(&pathspec);
3915 + if (err)
3916 + die("unable to read tree entries %s", tree_name);
3917 +
3918 + /*
3919 + * Sort the cache entry -- we need to nuke the cache tree, though.
3920 + */
3921 + if (fn == read_one_entry_quick) {
3922 + cache_tree_free(&istate->cache_tree);
3923 + QSORT(istate->cache, istate->cache_nr, cmp_cache_name_compare);
3924 + }
3925 +
3926 + for (i = 0; i < istate->cache_nr; i++) {
3927 + struct cache_entry *ce = istate->cache[i];
3928 + switch (ce_stage(ce)) {
3929 + case 0:
3930 + last_stage0 = ce;
3931 + /* fallthru */
3932 + default:
3933 + continue;
3934 + case 1:
3935 + /*
3936 + * If there is stage #0 entry for this, we do not
3937 + * need to show it. We use CE_UPDATE bit to mark
3938 + * such an entry.
3939 + */
3940 + if (last_stage0 &&
3941 + !strcmp(last_stage0->name, ce->name))
3942 + ce->ce_flags |= CE_UPDATE;
3943 + }
3944 + }
3945 +}