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"
17 static struct tree
*trees
[MAX_UNPACK_TREES
];
19 static int list_tree(unsigned char *sha1
)
23 if (nr_trees
>= MAX_UNPACK_TREES
)
24 die("I cannot read more than %d trees", MAX_UNPACK_TREES
);
25 tree
= parse_tree_indirect(sha1
);
28 trees
[nr_trees
++] = tree
;
32 static void prime_cache_tree_rec(struct cache_tree
*it
, struct tree
*tree
)
34 struct tree_desc desc
;
35 struct name_entry entry
;
38 hashcpy(it
->sha1
, tree
->object
.sha1
);
39 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
41 while (tree_entry(&desc
, &entry
)) {
42 if (!S_ISDIR(entry
.mode
))
45 struct cache_tree_sub
*sub
;
46 struct tree
*subtree
= lookup_tree(entry
.sha1
);
47 if (!subtree
->object
.parsed
)
49 sub
= cache_tree_sub(it
, entry
.path
);
50 sub
->cache_tree
= cache_tree();
51 prime_cache_tree_rec(sub
->cache_tree
, subtree
);
52 cnt
+= sub
->cache_tree
->entry_count
;
55 it
->entry_count
= cnt
;
58 static void prime_cache_tree(void)
62 active_cache_tree
= cache_tree();
63 prime_cache_tree_rec(active_cache_tree
, trees
[0]);
67 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>]])";
69 static struct lock_file lock_file
;
71 int cmd_read_tree(int argc
, const char **argv
, const char *unused_prefix
)
73 int i
, newfd
, stage
= 0;
74 unsigned char sha1
[20];
75 struct tree_desc t
[MAX_UNPACK_TREES
];
76 struct unpack_trees_options opts
;
78 memset(&opts
, 0, sizeof(opts
));
80 opts
.src_index
= &the_index
;
81 opts
.dst_index
= &the_index
;
83 git_config(git_default_config
, NULL
);
85 newfd
= hold_locked_index(&lock_file
, 1);
87 for (i
= 1; i
< argc
; i
++) {
88 const char *arg
= argv
[i
];
90 /* "-u" means "update", meaning that a merge will update
93 if (!strcmp(arg
, "-u")) {
98 if (!strcmp(arg
, "-v")) {
99 opts
.verbose_update
= 1;
103 /* "-i" means "index only", meaning that a merge will
104 * not even look at the working tree.
106 if (!strcmp(arg
, "-i")) {
111 if (!prefixcmp(arg
, "--index-output=")) {
112 set_alternate_index_output(arg
+ 15);
116 /* "--prefix=<subdirectory>/" means keep the current index
117 * entries and put the entries from the tree under the
118 * given subdirectory.
120 if (!prefixcmp(arg
, "--prefix=")) {
121 if (stage
|| opts
.merge
|| opts
.prefix
)
122 usage(read_tree_usage
);
123 opts
.prefix
= arg
+ 9;
126 if (read_cache_unmerged())
127 die("you need to resolve your current index first");
131 /* This differs from "-m" in that we'll silently ignore
132 * unmerged entries and overwrite working tree files that
133 * correspond to them.
135 if (!strcmp(arg
, "--reset")) {
136 if (stage
|| opts
.merge
|| opts
.prefix
)
137 usage(read_tree_usage
);
141 read_cache_unmerged();
145 if (!strcmp(arg
, "--trivial")) {
146 opts
.trivial_merges_only
= 1;
150 if (!strcmp(arg
, "--aggressive")) {
155 /* "-m" stands for "merge", meaning we start in stage 1 */
156 if (!strcmp(arg
, "-m")) {
157 if (stage
|| opts
.merge
|| opts
.prefix
)
158 usage(read_tree_usage
);
159 if (read_cache_unmerged())
160 die("you need to resolve your current index first");
166 if (!prefixcmp(arg
, "--exclude-per-directory=")) {
167 struct dir_struct
*dir
;
170 die("more than one --exclude-per-directory are given.");
172 dir
= xcalloc(1, sizeof(*opts
.dir
));
173 dir
->show_ignored
= 1;
174 dir
->exclude_per_dir
= arg
+ 24;
176 /* We do not need to nor want to do read-directory
177 * here; we are merely interested in reusing the
178 * per directory ignore stack mechanism.
183 /* using -u and -i at the same time makes no sense */
184 if (1 < opts
.index_only
+ opts
.update
)
185 usage(read_tree_usage
);
187 if (get_sha1(arg
, sha1
))
188 die("Not a valid object name %s", arg
);
189 if (list_tree(sha1
) < 0)
190 die("failed to unpack tree object %s", arg
);
193 if ((opts
.update
||opts
.index_only
) && !opts
.merge
)
194 usage(read_tree_usage
);
195 if ((opts
.dir
&& !opts
.update
))
196 die("--exclude-per-directory is meaningless unless -u");
197 if (opts
.merge
&& !opts
.index_only
)
202 die("just how do you expect me to merge %d trees?", stage
-1);
205 opts
.fn
= opts
.prefix
? bind_merge
: oneway_merge
;
208 opts
.fn
= twoway_merge
;
209 opts
.initial_checkout
= !active_nr
;
213 opts
.fn
= threeway_merge
;
214 cache_tree_free(&active_cache_tree
);
219 opts
.head_idx
= stage
- 2;
224 for (i
= 0; i
< nr_trees
; i
++) {
225 struct tree
*tree
= trees
[i
];
227 init_tree_desc(t
+i
, tree
->buffer
, tree
->size
);
229 if (unpack_trees(nr_trees
, t
, &opts
))
233 * When reading only one tree (either the most basic form,
234 * "-m ent" or "--reset ent" form), we can obtain a fully
235 * valid cache-tree because the index must match exactly
236 * what came from the tree.
238 if (nr_trees
&& !opts
.prefix
&& (!opts
.merge
|| (stage
== 2))) {
239 cache_tree_free(&active_cache_tree
);
243 if (write_cache(newfd
, active_cache
, active_nr
) ||
244 commit_locked_index(&lock_file
))
245 die("unable to write new index file");