2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
10 #include "tree-walk.h"
11 #include "cache-tree.h"
12 #include "unpack-trees.h"
18 static struct tree
*trees
[MAX_TREES
];
20 static int list_tree(unsigned char *sha1
)
24 if (nr_trees
>= MAX_TREES
)
25 die("I cannot read more than %d trees", MAX_TREES
);
26 tree
= parse_tree_indirect(sha1
);
29 trees
[nr_trees
++] = tree
;
33 static int read_cache_unmerged(void)
36 struct cache_entry
**dst
;
37 struct cache_entry
*last
= NULL
;
41 for (i
= 0; i
< active_nr
; i
++) {
42 struct cache_entry
*ce
= active_cache
[i
];
44 remove_index_entry(ce
);
45 if (last
&& !strcmp(ce
->name
, last
->name
))
47 cache_tree_invalidate_path(active_cache_tree
, ce
->name
);
53 active_nr
= dst
- active_cache
;
57 static void prime_cache_tree_rec(struct cache_tree
*it
, struct tree
*tree
)
59 struct tree_desc desc
;
60 struct name_entry entry
;
63 hashcpy(it
->sha1
, tree
->object
.sha1
);
64 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
66 while (tree_entry(&desc
, &entry
)) {
67 if (!S_ISDIR(entry
.mode
))
70 struct cache_tree_sub
*sub
;
71 struct tree
*subtree
= lookup_tree(entry
.sha1
);
72 if (!subtree
->object
.parsed
)
74 sub
= cache_tree_sub(it
, entry
.path
);
75 sub
->cache_tree
= cache_tree();
76 prime_cache_tree_rec(sub
->cache_tree
, subtree
);
77 cnt
+= sub
->cache_tree
->entry_count
;
80 it
->entry_count
= cnt
;
83 static void prime_cache_tree(void)
87 active_cache_tree
= cache_tree();
88 prime_cache_tree_rec(active_cache_tree
, trees
[0]);
92 static 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>]])";
94 static struct lock_file lock_file
;
96 int cmd_read_tree(int argc
, const char **argv
, const char *unused_prefix
)
98 int i
, newfd
, stage
= 0;
99 unsigned char sha1
[20];
100 struct tree_desc t
[MAX_TREES
];
101 struct unpack_trees_options opts
;
103 memset(&opts
, 0, sizeof(opts
));
105 opts
.src_index
= &the_index
;
106 opts
.dst_index
= &the_index
;
108 git_config(git_default_config
);
110 newfd
= hold_locked_index(&lock_file
, 1);
112 git_config(git_default_config
);
114 for (i
= 1; i
< argc
; i
++) {
115 const char *arg
= argv
[i
];
117 /* "-u" means "update", meaning that a merge will update
120 if (!strcmp(arg
, "-u")) {
125 if (!strcmp(arg
, "-v")) {
126 opts
.verbose_update
= 1;
130 /* "-i" means "index only", meaning that a merge will
131 * not even look at the working tree.
133 if (!strcmp(arg
, "-i")) {
138 if (!prefixcmp(arg
, "--index-output=")) {
139 set_alternate_index_output(arg
+ 15);
143 /* "--prefix=<subdirectory>/" means keep the current index
144 * entries and put the entries from the tree under the
145 * given subdirectory.
147 if (!prefixcmp(arg
, "--prefix=")) {
148 if (stage
|| opts
.merge
|| opts
.prefix
)
149 usage(read_tree_usage
);
150 opts
.prefix
= arg
+ 9;
153 if (read_cache_unmerged())
154 die("you need to resolve your current index first");
158 /* This differs from "-m" in that we'll silently ignore
159 * unmerged entries and overwrite working tree files that
160 * correspond to them.
162 if (!strcmp(arg
, "--reset")) {
163 if (stage
|| opts
.merge
|| opts
.prefix
)
164 usage(read_tree_usage
);
168 read_cache_unmerged();
172 if (!strcmp(arg
, "--trivial")) {
173 opts
.trivial_merges_only
= 1;
177 if (!strcmp(arg
, "--aggressive")) {
182 /* "-m" stands for "merge", meaning we start in stage 1 */
183 if (!strcmp(arg
, "-m")) {
184 if (stage
|| opts
.merge
|| opts
.prefix
)
185 usage(read_tree_usage
);
186 if (read_cache_unmerged())
187 die("you need to resolve your current index first");
193 if (!prefixcmp(arg
, "--exclude-per-directory=")) {
194 struct dir_struct
*dir
;
197 die("more than one --exclude-per-directory are given.");
199 dir
= xcalloc(1, sizeof(*opts
.dir
));
200 dir
->show_ignored
= 1;
201 dir
->exclude_per_dir
= arg
+ 24;
203 /* We do not need to nor want to do read-directory
204 * here; we are merely interested in reusing the
205 * per directory ignore stack mechanism.
210 /* using -u and -i at the same time makes no sense */
211 if (1 < opts
.index_only
+ opts
.update
)
212 usage(read_tree_usage
);
214 if (get_sha1(arg
, sha1
))
215 die("Not a valid object name %s", arg
);
216 if (list_tree(sha1
) < 0)
217 die("failed to unpack tree object %s", arg
);
220 if ((opts
.update
||opts
.index_only
) && !opts
.merge
)
221 usage(read_tree_usage
);
222 if ((opts
.dir
&& !opts
.update
))
223 die("--exclude-per-directory is meaningless unless -u");
227 die("just how do you expect me to merge %d trees?", stage
-1);
230 opts
.fn
= opts
.prefix
? bind_merge
: oneway_merge
;
233 opts
.fn
= twoway_merge
;
237 opts
.fn
= threeway_merge
;
238 cache_tree_free(&active_cache_tree
);
243 opts
.head_idx
= stage
- 2;
248 for (i
= 0; i
< nr_trees
; i
++) {
249 struct tree
*tree
= trees
[i
];
251 init_tree_desc(t
+i
, tree
->buffer
, tree
->size
);
253 if (unpack_trees(nr_trees
, t
, &opts
))
257 * When reading only one tree (either the most basic form,
258 * "-m ent" or "--reset ent" form), we can obtain a fully
259 * valid cache-tree because the index must match exactly
260 * what came from the tree.
262 if (nr_trees
&& !opts
.prefix
&& (!opts
.merge
|| (stage
== 2))) {
263 cache_tree_free(&active_cache_tree
);
267 if (write_cache(newfd
, active_cache
, active_nr
) ||
268 commit_locked_index(&lock_file
))
269 die("unable to write new index file");