blob: 7d378b75480331b242410b9680399728efbae49d [file] [log] [blame]
Linus Torvalds8bc9a0c2005-04-07 15:16:10 -07001/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
Junio C Hamano4d3fe0c2005-09-06 12:53:56 -07006
Linus Torvaldse83c5162005-04-07 15:13:13 -07007#include "cache.h"
Daniel Barkalowee6566e2005-09-05 02:04:48 -04008#include "object.h"
9#include "tree.h"
Linus Torvalds1ccf5a32006-05-29 12:18:00 -070010#include "tree-walk.h"
Junio C Hamanobad68ec2006-04-24 21:18:58 -070011#include "cache-tree.h"
Johannes Schindelin16da1342006-07-30 20:25:18 +020012#include "unpack-trees.h"
Junio C Hamanof8a9d422006-12-04 16:00:46 -080013#include "dir.h"
Peter Eriksend147e502006-05-23 14:15:32 +020014#include "builtin.h"
Stephen Boyd5a56da52009-06-25 22:14:10 -070015#include "parse-options.h"
Junio C Hamanocfc57892009-12-25 00:30:51 -080016#include "resolve-undo.h"
Daniel Barkalowee6566e2005-09-05 02:04:48 -040017
Linus Torvalds933bf402007-08-09 22:21:29 -070018static int nr_trees;
Junio C Hamanoca885a42008-03-13 22:07:18 -070019static struct tree *trees[MAX_UNPACK_TREES];
Daniel Barkalowee6566e2005-09-05 02:04:48 -040020
Daniel Barkalowee6566e2005-09-05 02:04:48 -040021static int list_tree(unsigned char *sha1)
22{
Linus Torvalds933bf402007-08-09 22:21:29 -070023 struct tree *tree;
24
Junio C Hamanoca885a42008-03-13 22:07:18 -070025 if (nr_trees >= MAX_UNPACK_TREES)
26 die("I cannot read more than %d trees", MAX_UNPACK_TREES);
Linus Torvalds933bf402007-08-09 22:21:29 -070027 tree = parse_tree_indirect(sha1);
Daniel Barkalowee6566e2005-09-05 02:04:48 -040028 if (!tree)
29 return -1;
Linus Torvalds933bf402007-08-09 22:21:29 -070030 trees[nr_trees++] = tree;
Daniel Barkalowee6566e2005-09-05 02:04:48 -040031 return 0;
32}
33
Stephen Boyd5a56da52009-06-25 22:14:10 -070034static const char * const read_tree_usage[] = {
35 "git read-tree [[-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>] [-u [--exclude-per-directory=<gitignore>] | -i]] [--index-output=<file>] <tree-ish1> [<tree-ish2> [<tree-ish3>]]",
36 NULL
37};
38
39static int index_output_cb(const struct option *opt, const char *arg,
40 int unset)
41{
42 set_alternate_index_output(arg);
43 return 0;
44}
45
46static int exclude_per_directory_cb(const struct option *opt, const char *arg,
47 int unset)
48{
49 struct dir_struct *dir;
50 struct unpack_trees_options *opts;
51
52 opts = (struct unpack_trees_options *)opt->value;
53
54 if (opts->dir)
55 die("more than one --exclude-per-directory given.");
56
57 dir = xcalloc(1, sizeof(*opts->dir));
58 dir->flags |= DIR_SHOW_IGNORED;
59 dir->exclude_per_dir = arg;
60 opts->dir = dir;
61 /* We do not need to nor want to do read-directory
62 * here; we are merely interested in reusing the
63 * per directory ignore stack mechanism.
64 */
65 return 0;
66}
Junio C Hamanoc5bac172005-04-20 19:49:16 -070067
Junio C Hamano021b6e42006-06-06 12:51:49 -070068static struct lock_file lock_file;
Junio C Hamano96cd5422005-06-06 12:20:55 -070069
Junio C Hamanoa91af792006-08-03 13:44:09 -070070int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
Linus Torvaldse83c5162005-04-07 15:13:13 -070071{
Linus Torvalds6d6776c2006-05-15 08:09:31 -070072 int i, newfd, stage = 0;
Linus Torvaldse83c5162005-04-07 15:13:13 -070073 unsigned char sha1[20];
Junio C Hamanoca885a42008-03-13 22:07:18 -070074 struct tree_desc t[MAX_UNPACK_TREES];
Johannes Schindelin16da1342006-07-30 20:25:18 +020075 struct unpack_trees_options opts;
Stephen Boyd5a56da52009-06-25 22:14:10 -070076 int prefix_set = 0;
77 const struct option read_tree_options[] = {
78 { OPTION_CALLBACK, 0, "index-output", NULL, "FILE",
79 "write resulting index to <FILE>",
80 PARSE_OPT_NONEG, index_output_cb },
81 OPT__VERBOSE(&opts.verbose_update),
82 OPT_GROUP("Merging"),
83 OPT_SET_INT('m', NULL, &opts.merge,
84 "perform a merge in addition to a read", 1),
85 OPT_SET_INT(0, "trivial", &opts.trivial_merges_only,
86 "3-way merge if no file level merging required", 1),
87 OPT_SET_INT(0, "aggressive", &opts.aggressive,
88 "3-way merge in presence of adds and removes", 1),
89 OPT_SET_INT(0, "reset", &opts.reset,
90 "same as -m, but discard unmerged entries", 1),
91 { OPTION_STRING, 0, "prefix", &opts.prefix, "<subdirectory>/",
92 "read the tree into the index under <subdirectory>/",
93 PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP },
94 OPT_SET_INT('u', NULL, &opts.update,
95 "update working tree with merge result", 1),
96 { OPTION_CALLBACK, 0, "exclude-per-directory", &opts,
97 "gitignore",
98 "allow explicitly ignored files to be overwritten",
99 PARSE_OPT_NONEG, exclude_per_directory_cb },
100 OPT_SET_INT('i', NULL, &opts.index_only,
101 "don't check the working tree after merging", 1),
102 OPT_END()
103 };
Linus Torvaldse83c5162005-04-07 15:13:13 -0700104
Johannes Schindelin16da1342006-07-30 20:25:18 +0200105 memset(&opts, 0, sizeof(opts));
106 opts.head_idx = -1;
Linus Torvalds34110cd2008-03-06 18:12:28 -0800107 opts.src_index = &the_index;
108 opts.dst_index = &the_index;
Shawn Pearce344c52a2006-07-08 14:34:02 -0400109
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100110 git_config(git_default_config, NULL);
Junio C Hamano53228a52005-11-26 00:50:02 -0800111
Stephen Boyd5a56da52009-06-25 22:14:10 -0700112 argc = parse_options(argc, argv, unused_prefix, read_tree_options,
113 read_tree_usage, 0);
114
Jonathan Nieder99caeed2009-11-09 09:05:01 -0600115 newfd = hold_locked_index(&lock_file, 1);
116
Stephen Boyd5a56da52009-06-25 22:14:10 -0700117 prefix_set = opts.prefix ? 1 : 0;
118 if (1 < opts.merge + opts.reset + prefix_set)
119 die("Which one? -m, --reset, or --prefix?");
Alexandre Julliarddb137fe2009-08-17 17:35:44 +0200120
121 if (opts.reset || opts.merge || opts.prefix) {
122 if (read_cache_unmerged() && (opts.prefix || opts.merge))
123 die("You need to resolve your current index first");
124 stage = opts.merge = 1;
125 }
Junio C Hamanocfc57892009-12-25 00:30:51 -0800126 resolve_undo_clear();
Stephen Boyd5a56da52009-06-25 22:14:10 -0700127
128 for (i = 0; i < argc; i++) {
Linus Torvalds83adac32005-04-09 12:11:25 -0700129 const char *arg = argv[i];
130
Dmitry V. Levin31fff302006-05-09 01:43:38 +0400131 if (get_sha1(arg, sha1))
132 die("Not a valid object name %s", arg);
Daniel Barkalowee6566e2005-09-05 02:04:48 -0400133 if (list_tree(sha1) < 0)
Petr Baudis2de381f2005-04-13 02:28:48 -0700134 die("failed to unpack tree object %s", arg);
Linus Torvaldsd99082e2005-04-15 22:53:45 -0700135 stage++;
Linus Torvalds83adac32005-04-09 12:11:25 -0700136 }
Stephen Boyd5a56da52009-06-25 22:14:10 -0700137 if (1 < opts.index_only + opts.update)
138 die("-u and -i at the same time makes no sense");
Johannes Schindelin16da1342006-07-30 20:25:18 +0200139 if ((opts.update||opts.index_only) && !opts.merge)
Stephen Boyda429d2d2009-06-25 22:14:09 -0700140 die("%s is meaningless without -m, --reset, or --prefix",
141 opts.update ? "-u" : "-i");
Junio C Hamanof8a9d422006-12-04 16:00:46 -0800142 if ((opts.dir && !opts.update))
143 die("--exclude-per-directory is meaningless unless -u");
Nguyễn Thái Ngọc Duyb6469a82008-08-28 20:03:22 +0700144 if (opts.merge && !opts.index_only)
145 setup_work_tree();
Junio C Hamano7dd43572005-10-02 00:50:16 -0700146
Johannes Schindelin16da1342006-07-30 20:25:18 +0200147 if (opts.merge) {
Daniel Barkalowee6566e2005-09-05 02:04:48 -0400148 if (stage < 2)
Linus Torvaldsa3a65232005-04-19 11:41:18 -0700149 die("just how do you expect me to merge %d trees?", stage-1);
Daniel Barkalowee6566e2005-09-05 02:04:48 -0400150 switch (stage - 1) {
151 case 1:
Johannes Schindelin16da1342006-07-30 20:25:18 +0200152 opts.fn = opts.prefix ? bind_merge : oneway_merge;
Daniel Barkalowee6566e2005-09-05 02:04:48 -0400153 break;
154 case 2:
Johannes Schindelin16da1342006-07-30 20:25:18 +0200155 opts.fn = twoway_merge;
Junio C Hamanofa7b3c22008-11-12 11:52:35 -0800156 opts.initial_checkout = is_cache_unborn();
Daniel Barkalowee6566e2005-09-05 02:04:48 -0400157 break;
158 case 3:
Daniel Barkalowee6566e2005-09-05 02:04:48 -0400159 default:
Johannes Schindelin16da1342006-07-30 20:25:18 +0200160 opts.fn = threeway_merge;
Daniel Barkalowee6566e2005-09-05 02:04:48 -0400161 break;
Junio C Hamano03efa6d2005-06-10 18:36:08 -0700162 }
Daniel Barkalowee6566e2005-09-05 02:04:48 -0400163
Daniel Barkalowee6566e2005-09-05 02:04:48 -0400164 if (stage - 1 >= 3)
Johannes Schindelin16da1342006-07-30 20:25:18 +0200165 opts.head_idx = stage - 2;
Daniel Barkalowee6566e2005-09-05 02:04:48 -0400166 else
Johannes Schindelin16da1342006-07-30 20:25:18 +0200167 opts.head_idx = 1;
Daniel Barkalowee6566e2005-09-05 02:04:48 -0400168 }
169
Junio C Hamano8cc21ce2009-04-20 03:58:17 -0700170 cache_tree_free(&active_cache_tree);
Linus Torvalds933bf402007-08-09 22:21:29 -0700171 for (i = 0; i < nr_trees; i++) {
172 struct tree *tree = trees[i];
173 parse_tree(tree);
174 init_tree_desc(t+i, tree->buffer, tree->size);
175 }
Daniel Barkalow203a2fe2008-02-07 11:39:48 -0500176 if (unpack_trees(nr_trees, t, &opts))
177 return 128;
Junio C Hamano7927a552006-04-27 01:33:07 -0700178
179 /*
180 * When reading only one tree (either the most basic form,
181 * "-m ent" or "--reset ent" form), we can obtain a fully
182 * valid cache-tree because the index must match exactly
183 * what came from the tree.
Junio C Hamano456156d2009-04-20 03:58:19 -0700184 *
185 * The same holds true if we are switching between two trees
186 * using read-tree -m A B. The index must match B after that.
Junio C Hamano7927a552006-04-27 01:33:07 -0700187 */
Junio C Hamano8cc21ce2009-04-20 03:58:17 -0700188 if (nr_trees == 1 && !opts.prefix)
Junio C Hamanob9d37a52009-04-20 03:58:18 -0700189 prime_cache_tree(&active_cache_tree, trees[0]);
Junio C Hamano456156d2009-04-20 03:58:19 -0700190 else if (nr_trees == 2 && opts.merge)
191 prime_cache_tree(&active_cache_tree, trees[1]);
Junio C Hamano7927a552006-04-27 01:33:07 -0700192
Junio C Hamano96cd5422005-06-06 12:20:55 -0700193 if (write_cache(newfd, active_cache, active_nr) ||
Brandon Casey4ed7cd32008-01-16 13:12:46 -0600194 commit_locked_index(&lock_file))
Petr Baudis2de381f2005-04-13 02:28:48 -0700195 die("unable to write new index file");
Linus Torvalds9614b8d2005-04-11 15:39:26 -0700196 return 0;
Linus Torvaldse83c5162005-04-07 15:13:13 -0700197}