8a889ef4c3cc79ea6d5d173aa16e3ae42d383d57
   1/*
   2 * GIT - The information manager from hell
   3 *
   4 * Copyright (C) Linus Torvalds, 2005
   5 */
   6
   7#include "cache.h"
   8#include "lockfile.h"
   9#include "object.h"
  10#include "tree.h"
  11#include "tree-walk.h"
  12#include "cache-tree.h"
  13#include "unpack-trees.h"
  14#include "dir.h"
  15#include "builtin.h"
  16#include "parse-options.h"
  17#include "resolve-undo.h"
  18#include "submodule.h"
  19#include "submodule-config.h"
  20
  21static int nr_trees;
  22static int read_empty;
  23static struct tree *trees[MAX_UNPACK_TREES];
  24
  25static int list_tree(unsigned char *sha1)
  26{
  27        struct tree *tree;
  28
  29        if (nr_trees >= MAX_UNPACK_TREES)
  30                die("I cannot read more than %d trees", MAX_UNPACK_TREES);
  31        tree = parse_tree_indirect(sha1);
  32        if (!tree)
  33                return -1;
  34        trees[nr_trees++] = tree;
  35        return 0;
  36}
  37
  38static const char * const read_tree_usage[] = {
  39        N_("git read-tree [(-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>) [-u [--exclude-per-directory=<gitignore>] | -i]] [--no-sparse-checkout] [--index-output=<file>] (--empty | <tree-ish1> [<tree-ish2> [<tree-ish3>]])"),
  40        NULL
  41};
  42
  43static int index_output_cb(const struct option *opt, const char *arg,
  44                                 int unset)
  45{
  46        set_alternate_index_output(arg);
  47        return 0;
  48}
  49
  50static int exclude_per_directory_cb(const struct option *opt, const char *arg,
  51                                    int unset)
  52{
  53        struct dir_struct *dir;
  54        struct unpack_trees_options *opts;
  55
  56        opts = (struct unpack_trees_options *)opt->value;
  57
  58        if (opts->dir)
  59                die("more than one --exclude-per-directory given.");
  60
  61        dir = xcalloc(1, sizeof(*opts->dir));
  62        dir->flags |= DIR_SHOW_IGNORED;
  63        dir->exclude_per_dir = arg;
  64        opts->dir = dir;
  65        /* We do not need to nor want to do read-directory
  66         * here; we are merely interested in reusing the
  67         * per directory ignore stack mechanism.
  68         */
  69        return 0;
  70}
  71
  72static void debug_stage(const char *label, const struct cache_entry *ce,
  73                        struct unpack_trees_options *o)
  74{
  75        printf("%s ", label);
  76        if (!ce)
  77                printf("(missing)\n");
  78        else if (ce == o->df_conflict_entry)
  79                printf("(conflict)\n");
  80        else
  81                printf("%06o #%d %s %.8s\n",
  82                       ce->ce_mode, ce_stage(ce), ce->name,
  83                       oid_to_hex(&ce->oid));
  84}
  85
  86static int debug_merge(const struct cache_entry * const *stages,
  87                       struct unpack_trees_options *o)
  88{
  89        int i;
  90
  91        printf("* %d-way merge\n", o->merge_size);
  92        debug_stage("index", stages[0], o);
  93        for (i = 1; i <= o->merge_size; i++) {
  94                char buf[24];
  95                xsnprintf(buf, sizeof(buf), "ent#%d", i);
  96                debug_stage(buf, stages[i], o);
  97        }
  98        return 0;
  99}
 100
 101static struct lock_file lock_file;
 102
 103int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
 104{
 105        int i, stage = 0;
 106        unsigned char sha1[20];
 107        struct tree_desc t[MAX_UNPACK_TREES];
 108        struct unpack_trees_options opts;
 109        int prefix_set = 0;
 110        const struct option read_tree_options[] = {
 111                { OPTION_CALLBACK, 0, "index-output", NULL, N_("file"),
 112                  N_("write resulting index to <file>"),
 113                  PARSE_OPT_NONEG, index_output_cb },
 114                OPT_BOOL(0, "empty", &read_empty,
 115                            N_("only empty the index")),
 116                OPT__VERBOSE(&opts.verbose_update, N_("be verbose")),
 117                OPT_GROUP(N_("Merging")),
 118                OPT_BOOL('m', NULL, &opts.merge,
 119                         N_("perform a merge in addition to a read")),
 120                OPT_BOOL(0, "trivial", &opts.trivial_merges_only,
 121                         N_("3-way merge if no file level merging required")),
 122                OPT_BOOL(0, "aggressive", &opts.aggressive,
 123                         N_("3-way merge in presence of adds and removes")),
 124                OPT_BOOL(0, "reset", &opts.reset,
 125                         N_("same as -m, but discard unmerged entries")),
 126                { OPTION_STRING, 0, "prefix", &opts.prefix, N_("<subdirectory>/"),
 127                  N_("read the tree into the index under <subdirectory>/"),
 128                  PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP },
 129                OPT_BOOL('u', NULL, &opts.update,
 130                         N_("update working tree with merge result")),
 131                { OPTION_CALLBACK, 0, "exclude-per-directory", &opts,
 132                  N_("gitignore"),
 133                  N_("allow explicitly ignored files to be overwritten"),
 134                  PARSE_OPT_NONEG, exclude_per_directory_cb },
 135                OPT_BOOL('i', NULL, &opts.index_only,
 136                         N_("don't check the working tree after merging")),
 137                OPT__DRY_RUN(&opts.dry_run, N_("don't update the index or the work tree")),
 138                OPT_BOOL(0, "no-sparse-checkout", &opts.skip_sparse_checkout,
 139                         N_("skip applying sparse checkout filter")),
 140                OPT_BOOL(0, "debug-unpack", &opts.debug_unpack,
 141                         N_("debug unpack-trees")),
 142                { OPTION_CALLBACK, 0, "recurse-submodules", NULL,
 143                            "checkout", "control recursive updating of submodules",
 144                            PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater },
 145                OPT_END()
 146        };
 147
 148        memset(&opts, 0, sizeof(opts));
 149        opts.head_idx = -1;
 150        opts.src_index = &the_index;
 151        opts.dst_index = &the_index;
 152
 153        git_config(git_default_config, NULL);
 154
 155        argc = parse_options(argc, argv, unused_prefix, read_tree_options,
 156                             read_tree_usage, 0);
 157
 158        load_submodule_cache();
 159
 160        hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
 161
 162        prefix_set = opts.prefix ? 1 : 0;
 163        if (1 < opts.merge + opts.reset + prefix_set)
 164                die("Which one? -m, --reset, or --prefix?");
 165
 166        /*
 167         * NEEDSWORK
 168         *
 169         * The old index should be read anyway even if we're going to
 170         * destroy all index entries because we still need to preserve
 171         * certain information such as index version or split-index
 172         * mode.
 173         */
 174
 175        if (opts.reset || opts.merge || opts.prefix) {
 176                if (read_cache_unmerged() && (opts.prefix || opts.merge))
 177                        die("You need to resolve your current index first");
 178                stage = opts.merge = 1;
 179        }
 180        resolve_undo_clear();
 181
 182        for (i = 0; i < argc; i++) {
 183                const char *arg = argv[i];
 184
 185                if (get_sha1(arg, sha1))
 186                        die("Not a valid object name %s", arg);
 187                if (list_tree(sha1) < 0)
 188                        die("failed to unpack tree object %s", arg);
 189                stage++;
 190        }
 191        if (nr_trees == 0 && !read_empty)
 192                warning("read-tree: emptying the index with no arguments is deprecated; use --empty");
 193        else if (nr_trees > 0 && read_empty)
 194                die("passing trees as arguments contradicts --empty");
 195
 196        if (1 < opts.index_only + opts.update)
 197                die("-u and -i at the same time makes no sense");
 198        if ((opts.update || opts.index_only) && !opts.merge)
 199                die("%s is meaningless without -m, --reset, or --prefix",
 200                    opts.update ? "-u" : "-i");
 201        if ((opts.dir && !opts.update))
 202                die("--exclude-per-directory is meaningless unless -u");
 203        if (opts.merge && !opts.index_only)
 204                setup_work_tree();
 205
 206        if (opts.merge) {
 207                if (stage < 2)
 208                        die("just how do you expect me to merge %d trees?", stage-1);
 209                switch (stage - 1) {
 210                case 1:
 211                        opts.fn = opts.prefix ? bind_merge : oneway_merge;
 212                        break;
 213                case 2:
 214                        opts.fn = twoway_merge;
 215                        opts.initial_checkout = is_cache_unborn();
 216                        break;
 217                case 3:
 218                default:
 219                        opts.fn = threeway_merge;
 220                        break;
 221                }
 222
 223                if (stage - 1 >= 3)
 224                        opts.head_idx = stage - 2;
 225                else
 226                        opts.head_idx = 1;
 227        }
 228
 229        if (opts.debug_unpack)
 230                opts.fn = debug_merge;
 231
 232        cache_tree_free(&active_cache_tree);
 233        for (i = 0; i < nr_trees; i++) {
 234                struct tree *tree = trees[i];
 235                parse_tree(tree);
 236                init_tree_desc(t+i, tree->buffer, tree->size);
 237        }
 238        if (unpack_trees(nr_trees, t, &opts))
 239                return 128;
 240
 241        if (opts.debug_unpack || opts.dry_run)
 242                return 0; /* do not write the index out */
 243
 244        /*
 245         * When reading only one tree (either the most basic form,
 246         * "-m ent" or "--reset ent" form), we can obtain a fully
 247         * valid cache-tree because the index must match exactly
 248         * what came from the tree.
 249         */
 250        if (nr_trees == 1 && !opts.prefix)
 251                prime_cache_tree(&the_index, trees[0]);
 252
 253        if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
 254                die("unable to write new index file");
 255        return 0;
 256}