]>
Commit | Line | Data |
---|---|---|
8bc9a0c7 LT |
1 | /* |
2 | * GIT - The information manager from hell | |
3 | * | |
4 | * Copyright (C) Linus Torvalds, 2005 | |
5 | */ | |
4d3fe0c5 | 6 | |
e83c5163 | 7 | #include "cache.h" |
ee6566e8 DB |
8 | #include "object.h" |
9 | #include "tree.h" | |
1ccf5a34 | 10 | #include "tree-walk.h" |
bad68ec9 | 11 | #include "cache-tree.h" |
16da134b | 12 | #include "unpack-trees.h" |
f8a9d428 | 13 | #include "dir.h" |
d147e501 | 14 | #include "builtin.h" |
5a56da58 | 15 | #include "parse-options.h" |
ee6566e8 | 16 | |
933bf40a | 17 | static int nr_trees; |
ca885a4f | 18 | static struct tree *trees[MAX_UNPACK_TREES]; |
ee6566e8 | 19 | |
ee6566e8 DB |
20 | static int list_tree(unsigned char *sha1) |
21 | { | |
933bf40a LT |
22 | struct tree *tree; |
23 | ||
ca885a4f JH |
24 | if (nr_trees >= MAX_UNPACK_TREES) |
25 | die("I cannot read more than %d trees", MAX_UNPACK_TREES); | |
933bf40a | 26 | tree = parse_tree_indirect(sha1); |
ee6566e8 DB |
27 | if (!tree) |
28 | return -1; | |
933bf40a | 29 | trees[nr_trees++] = tree; |
ee6566e8 DB |
30 | return 0; |
31 | } | |
32 | ||
5a56da58 | 33 | static const char * const read_tree_usage[] = { |
a5d07d0f | 34 | "git read-tree [[-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>] [-u [--exclude-per-directory=<gitignore>] | -i]] [--no-sparse-checkout] [--index-output=<file>] <tree-ish1> [<tree-ish2> [<tree-ish3>]]", |
5a56da58 SB |
35 | NULL |
36 | }; | |
37 | ||
38 | static int index_output_cb(const struct option *opt, const char *arg, | |
39 | int unset) | |
40 | { | |
41 | set_alternate_index_output(arg); | |
42 | return 0; | |
43 | } | |
44 | ||
45 | static int exclude_per_directory_cb(const struct option *opt, const char *arg, | |
46 | int unset) | |
47 | { | |
48 | struct dir_struct *dir; | |
49 | struct unpack_trees_options *opts; | |
50 | ||
51 | opts = (struct unpack_trees_options *)opt->value; | |
52 | ||
53 | if (opts->dir) | |
54 | die("more than one --exclude-per-directory given."); | |
55 | ||
56 | dir = xcalloc(1, sizeof(*opts->dir)); | |
57 | dir->flags |= DIR_SHOW_IGNORED; | |
58 | dir->exclude_per_dir = arg; | |
59 | opts->dir = dir; | |
60 | /* We do not need to nor want to do read-directory | |
61 | * here; we are merely interested in reusing the | |
62 | * per directory ignore stack mechanism. | |
63 | */ | |
64 | return 0; | |
65 | } | |
c5bac17a | 66 | |
021b6e45 | 67 | static struct lock_file lock_file; |
96cd5429 | 68 | |
a91af794 | 69 | int cmd_read_tree(int argc, const char **argv, const char *unused_prefix) |
e83c5163 | 70 | { |
6d6776cb | 71 | int i, newfd, stage = 0; |
e83c5163 | 72 | unsigned char sha1[20]; |
ca885a4f | 73 | struct tree_desc t[MAX_UNPACK_TREES]; |
16da134b | 74 | struct unpack_trees_options opts; |
5a56da58 SB |
75 | int prefix_set = 0; |
76 | const struct option read_tree_options[] = { | |
77 | { OPTION_CALLBACK, 0, "index-output", NULL, "FILE", | |
78 | "write resulting index to <FILE>", | |
79 | PARSE_OPT_NONEG, index_output_cb }, | |
80 | OPT__VERBOSE(&opts.verbose_update), | |
81 | OPT_GROUP("Merging"), | |
82 | OPT_SET_INT('m', NULL, &opts.merge, | |
83 | "perform a merge in addition to a read", 1), | |
84 | OPT_SET_INT(0, "trivial", &opts.trivial_merges_only, | |
85 | "3-way merge if no file level merging required", 1), | |
86 | OPT_SET_INT(0, "aggressive", &opts.aggressive, | |
87 | "3-way merge in presence of adds and removes", 1), | |
88 | OPT_SET_INT(0, "reset", &opts.reset, | |
89 | "same as -m, but discard unmerged entries", 1), | |
90 | { OPTION_STRING, 0, "prefix", &opts.prefix, "<subdirectory>/", | |
91 | "read the tree into the index under <subdirectory>/", | |
92 | PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP }, | |
93 | OPT_SET_INT('u', NULL, &opts.update, | |
94 | "update working tree with merge result", 1), | |
95 | { OPTION_CALLBACK, 0, "exclude-per-directory", &opts, | |
96 | "gitignore", | |
97 | "allow explicitly ignored files to be overwritten", | |
98 | PARSE_OPT_NONEG, exclude_per_directory_cb }, | |
99 | OPT_SET_INT('i', NULL, &opts.index_only, | |
100 | "don't check the working tree after merging", 1), | |
a5d07d0f NTND |
101 | OPT_SET_INT(0, "no-sparse-checkout", &opts.skip_sparse_checkout, |
102 | "skip applying sparse checkout filter", 1), | |
5a56da58 SB |
103 | OPT_END() |
104 | }; | |
bb233d69 | 105 | |
16da134b JS |
106 | memset(&opts, 0, sizeof(opts)); |
107 | opts.head_idx = -1; | |
34110cd4 LT |
108 | opts.src_index = &the_index; |
109 | opts.dst_index = &the_index; | |
344c52ae | 110 | |
ef90d6d4 | 111 | git_config(git_default_config, NULL); |
53228a5f | 112 | |
5a56da58 SB |
113 | argc = parse_options(argc, argv, unused_prefix, read_tree_options, |
114 | read_tree_usage, 0); | |
83adac3c | 115 | |
99caeed0 JN |
116 | newfd = hold_locked_index(&lock_file, 1); |
117 | ||
5a56da58 SB |
118 | prefix_set = opts.prefix ? 1 : 0; |
119 | if (1 < opts.merge + opts.reset + prefix_set) | |
120 | die("Which one? -m, --reset, or --prefix?"); | |
db137fe9 AJ |
121 | |
122 | if (opts.reset || opts.merge || opts.prefix) { | |
123 | if (read_cache_unmerged() && (opts.prefix || opts.merge)) | |
124 | die("You need to resolve your current index first"); | |
125 | stage = opts.merge = 1; | |
126 | } | |
5a56da58 SB |
127 | |
128 | for (i = 0; i < argc; i++) { | |
129 | const char *arg = argv[i]; | |
720d150c | 130 | |
31fff305 DL |
131 | if (get_sha1(arg, sha1)) |
132 | die("Not a valid object name %s", arg); | |
ee6566e8 | 133 | if (list_tree(sha1) < 0) |
2de381f9 | 134 | die("failed to unpack tree object %s", arg); |
d99082e0 | 135 | stage++; |
83adac3c | 136 | } |
5a56da58 SB |
137 | if (1 < opts.index_only + opts.update) |
138 | die("-u and -i at the same time makes no sense"); | |
16da134b | 139 | if ((opts.update||opts.index_only) && !opts.merge) |
a429d2dd SB |
140 | die("%s is meaningless without -m, --reset, or --prefix", |
141 | opts.update ? "-u" : "-i"); | |
f8a9d428 JH |
142 | if ((opts.dir && !opts.update)) |
143 | die("--exclude-per-directory is meaningless unless -u"); | |
b6469a81 NTND |
144 | if (opts.merge && !opts.index_only) |
145 | setup_work_tree(); | |
7dd43575 | 146 | |
16da134b | 147 | if (opts.merge) { |
ee6566e8 | 148 | if (stage < 2) |
a3a65234 | 149 | die("just how do you expect me to merge %d trees?", stage-1); |
ee6566e8 DB |
150 | switch (stage - 1) { |
151 | case 1: | |
16da134b | 152 | opts.fn = opts.prefix ? bind_merge : oneway_merge; |
ee6566e8 DB |
153 | break; |
154 | case 2: | |
16da134b | 155 | opts.fn = twoway_merge; |
fa7b3c2f | 156 | opts.initial_checkout = is_cache_unborn(); |
ee6566e8 DB |
157 | break; |
158 | case 3: | |
ee6566e8 | 159 | default: |
16da134b | 160 | opts.fn = threeway_merge; |
ee6566e8 | 161 | break; |
03efa6d9 | 162 | } |
ee6566e8 | 163 | |
ee6566e8 | 164 | if (stage - 1 >= 3) |
16da134b | 165 | opts.head_idx = stage - 2; |
ee6566e8 | 166 | else |
16da134b | 167 | opts.head_idx = 1; |
ee6566e8 DB |
168 | } |
169 | ||
8cc21ce7 | 170 | cache_tree_free(&active_cache_tree); |
933bf40a LT |
171 | 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 | } | |
203a2fe1 DB |
176 | if (unpack_trees(nr_trees, t, &opts)) |
177 | return 128; | |
7927a55d JH |
178 | |
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. | |
456156dc JH |
184 | * |
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. | |
7927a55d | 187 | */ |
8cc21ce7 | 188 | if (nr_trees == 1 && !opts.prefix) |
b9d37a54 | 189 | prime_cache_tree(&active_cache_tree, trees[0]); |
456156dc JH |
190 | else if (nr_trees == 2 && opts.merge) |
191 | prime_cache_tree(&active_cache_tree, trees[1]); | |
7927a55d | 192 | |
96cd5429 | 193 | if (write_cache(newfd, active_cache, active_nr) || |
4ed7cd3a | 194 | commit_locked_index(&lock_file)) |
2de381f9 | 195 | die("unable to write new index file"); |
9614b8dc | 196 | return 0; |
e83c5163 | 197 | } |