read-tree: convert unhelpful usage()'s to helpful die()'s
[git/git.git] / builtin-read-tree.c
CommitLineData
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"
ee6566e8 15
933bf40a 16static int nr_trees;
ca885a4f 17static struct tree *trees[MAX_UNPACK_TREES];
ee6566e8 18
ee6566e8
DB
19static int list_tree(unsigned char *sha1)
20{
933bf40a
LT
21 struct tree *tree;
22
ca885a4f
JH
23 if (nr_trees >= MAX_UNPACK_TREES)
24 die("I cannot read more than %d trees", MAX_UNPACK_TREES);
933bf40a 25 tree = parse_tree_indirect(sha1);
ee6566e8
DB
26 if (!tree)
27 return -1;
933bf40a 28 trees[nr_trees++] = tree;
ee6566e8
DB
29 return 0;
30}
31
f18d244a 32static const char read_tree_usage[] = "git read-tree (<sha> | [[-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>] [-u | -i]] [--exclude-per-directory=<gitignore>] [--index-output=<file>] <sha1> [<sha2> [<sha3>]])";
c5bac17a 33
021b6e45 34static struct lock_file lock_file;
96cd5429 35
a91af794 36int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
e83c5163 37{
6d6776cb 38 int i, newfd, stage = 0;
e83c5163 39 unsigned char sha1[20];
ca885a4f 40 struct tree_desc t[MAX_UNPACK_TREES];
16da134b 41 struct unpack_trees_options opts;
bb233d69 42
16da134b
JS
43 memset(&opts, 0, sizeof(opts));
44 opts.head_idx = -1;
34110cd4
LT
45 opts.src_index = &the_index;
46 opts.dst_index = &the_index;
344c52ae 47
ef90d6d4 48 git_config(git_default_config, NULL);
53228a5f 49
30ca07a2 50 newfd = hold_locked_index(&lock_file, 1);
83adac3c
LT
51
52 for (i = 1; i < argc; i++) {
53 const char *arg = argv[i];
54
720d150c
JH
55 /* "-u" means "update", meaning that a merge will update
56 * the working tree.
57 */
220a0b52 58 if (!strcmp(arg, "-u")) {
16da134b 59 opts.update = 1;
220a0b52
LT
60 continue;
61 }
62
744633cb 63 if (!strcmp(arg, "-v")) {
16da134b 64 opts.verbose_update = 1;
744633cb
JH
65 continue;
66 }
67
720d150c
JH
68 /* "-i" means "index only", meaning that a merge will
69 * not even look at the working tree.
70 */
71 if (!strcmp(arg, "-i")) {
16da134b 72 opts.index_only = 1;
720d150c
JH
73 continue;
74 }
75
5e7f56ac
JH
76 if (!prefixcmp(arg, "--index-output=")) {
77 set_alternate_index_output(arg + 15);
78 continue;
79 }
80
f4c6f2d3
JH
81 /* "--prefix=<subdirectory>/" means keep the current index
82 * entries and put the entries from the tree under the
83 * given subdirectory.
84 */
cc44c765 85 if (!prefixcmp(arg, "--prefix=")) {
16da134b 86 if (stage || opts.merge || opts.prefix)
f4c6f2d3 87 usage(read_tree_usage);
16da134b
JS
88 opts.prefix = arg + 9;
89 opts.merge = 1;
f4c6f2d3
JH
90 stage = 1;
91 if (read_cache_unmerged())
92 die("you need to resolve your current index first");
93 continue;
94 }
95
b0d6e646
JH
96 /* This differs from "-m" in that we'll silently ignore
97 * unmerged entries and overwrite working tree files that
98 * correspond to them.
99 */
438195cc 100 if (!strcmp(arg, "--reset")) {
16da134b 101 if (stage || opts.merge || opts.prefix)
438195cc 102 usage(read_tree_usage);
16da134b
JS
103 opts.reset = 1;
104 opts.merge = 1;
438195cc
LT
105 stage = 1;
106 read_cache_unmerged();
7875b50d 107 continue;
438195cc
LT
108 }
109
23822a35 110 if (!strcmp(arg, "--trivial")) {
16da134b 111 opts.trivial_merges_only = 1;
23822a35
LT
112 continue;
113 }
114
1b1fdf8c 115 if (!strcmp(arg, "--aggressive")) {
16da134b 116 opts.aggressive = 1;
1b1fdf8c
JH
117 continue;
118 }
119
d99082e0 120 /* "-m" stands for "merge", meaning we start in stage 1 */
83adac3c 121 if (!strcmp(arg, "-m")) {
16da134b 122 if (stage || opts.merge || opts.prefix)
438195cc
LT
123 usage(read_tree_usage);
124 if (read_cache_unmerged())
125 die("you need to resolve your current index first");
d99082e0 126 stage = 1;
16da134b 127 opts.merge = 1;
83adac3c
LT
128 continue;
129 }
03efa6d9 130
cc44c765 131 if (!prefixcmp(arg, "--exclude-per-directory=")) {
f8a9d428
JH
132 struct dir_struct *dir;
133
134 if (opts.dir)
135 die("more than one --exclude-per-directory are given.");
136
aa4cfa85 137 dir = xcalloc(1, sizeof(*opts.dir));
7c4c97c0 138 dir->flags |= DIR_SHOW_IGNORED;
f8a9d428
JH
139 dir->exclude_per_dir = arg + 24;
140 opts.dir = dir;
141 /* We do not need to nor want to do read-directory
142 * here; we are merely interested in reusing the
143 * per directory ignore stack mechanism.
144 */
145 continue;
146 }
147
16da134b 148 if (1 < opts.index_only + opts.update)
a429d2dd 149 die("-u and -i at the same time makes no sense");
720d150c 150
31fff305
DL
151 if (get_sha1(arg, sha1))
152 die("Not a valid object name %s", arg);
ee6566e8 153 if (list_tree(sha1) < 0)
2de381f9 154 die("failed to unpack tree object %s", arg);
d99082e0 155 stage++;
83adac3c 156 }
16da134b 157 if ((opts.update||opts.index_only) && !opts.merge)
a429d2dd
SB
158 die("%s is meaningless without -m, --reset, or --prefix",
159 opts.update ? "-u" : "-i");
f8a9d428
JH
160 if ((opts.dir && !opts.update))
161 die("--exclude-per-directory is meaningless unless -u");
b6469a81
NTND
162 if (opts.merge && !opts.index_only)
163 setup_work_tree();
7dd43575 164
16da134b 165 if (opts.merge) {
ee6566e8 166 if (stage < 2)
a3a65234 167 die("just how do you expect me to merge %d trees?", stage-1);
ee6566e8
DB
168 switch (stage - 1) {
169 case 1:
16da134b 170 opts.fn = opts.prefix ? bind_merge : oneway_merge;
ee6566e8
DB
171 break;
172 case 2:
16da134b 173 opts.fn = twoway_merge;
fa7b3c2f 174 opts.initial_checkout = is_cache_unborn();
ee6566e8
DB
175 break;
176 case 3:
ee6566e8 177 default:
16da134b 178 opts.fn = threeway_merge;
ee6566e8 179 break;
03efa6d9 180 }
ee6566e8 181
ee6566e8 182 if (stage - 1 >= 3)
16da134b 183 opts.head_idx = stage - 2;
ee6566e8 184 else
16da134b 185 opts.head_idx = 1;
ee6566e8
DB
186 }
187
8cc21ce7 188 cache_tree_free(&active_cache_tree);
933bf40a
LT
189 for (i = 0; i < nr_trees; i++) {
190 struct tree *tree = trees[i];
191 parse_tree(tree);
192 init_tree_desc(t+i, tree->buffer, tree->size);
193 }
203a2fe1
DB
194 if (unpack_trees(nr_trees, t, &opts))
195 return 128;
7927a55d
JH
196
197 /*
198 * When reading only one tree (either the most basic form,
199 * "-m ent" or "--reset ent" form), we can obtain a fully
200 * valid cache-tree because the index must match exactly
201 * what came from the tree.
456156dc
JH
202 *
203 * The same holds true if we are switching between two trees
204 * using read-tree -m A B. The index must match B after that.
7927a55d 205 */
8cc21ce7 206 if (nr_trees == 1 && !opts.prefix)
b9d37a54 207 prime_cache_tree(&active_cache_tree, trees[0]);
456156dc
JH
208 else if (nr_trees == 2 && opts.merge)
209 prime_cache_tree(&active_cache_tree, trees[1]);
7927a55d 210
96cd5429 211 if (write_cache(newfd, active_cache, active_nr) ||
4ed7cd3a 212 commit_locked_index(&lock_file))
2de381f9 213 die("unable to write new index file");
9614b8dc 214 return 0;
e83c5163 215}