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"
15 #include "parse-options.h"
16 #include "resolve-undo.h"
19 static struct tree
*trees
[MAX_UNPACK_TREES
];
21 static int list_tree(unsigned char *sha1
)
25 if (nr_trees
>= MAX_UNPACK_TREES
)
26 die("I cannot read more than %d trees", MAX_UNPACK_TREES
);
27 tree
= parse_tree_indirect(sha1
);
30 trees
[nr_trees
++] = tree
;
34 static const char * const read_tree_usage
[] = {
35 "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>]]",
39 static int index_output_cb(const struct option
*opt
, const char *arg
,
42 set_alternate_index_output(arg
);
46 static int exclude_per_directory_cb(const struct option
*opt
, const char *arg
,
49 struct dir_struct
*dir
;
50 struct unpack_trees_options
*opts
;
52 opts
= (struct unpack_trees_options
*)opt
->value
;
55 die("more than one --exclude-per-directory given.");
57 dir
= xcalloc(1, sizeof(*opts
->dir
));
58 dir
->flags
|= DIR_SHOW_IGNORED
;
59 dir
->exclude_per_dir
= arg
;
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.
68 static struct lock_file lock_file
;
70 int cmd_read_tree(int argc
, const char **argv
, const char *unused_prefix
)
72 int i
, newfd
, stage
= 0;
73 unsigned char sha1
[20];
74 struct tree_desc t
[MAX_UNPACK_TREES
];
75 struct unpack_trees_options opts
;
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
),
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
,
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_SET_INT(0, "no-sparse-checkout", &opts
.skip_sparse_checkout
,
103 "skip applying sparse checkout filter", 1),
107 memset(&opts
, 0, sizeof(opts
));
109 opts
.src_index
= &the_index
;
110 opts
.dst_index
= &the_index
;
112 git_config(git_default_config
, NULL
);
114 argc
= parse_options(argc
, argv
, unused_prefix
, read_tree_options
,
117 newfd
= hold_locked_index(&lock_file
, 1);
119 prefix_set
= opts
.prefix
? 1 : 0;
120 if (1 < opts
.merge
+ opts
.reset
+ prefix_set
)
121 die("Which one? -m, --reset, or --prefix?");
123 if (opts
.reset
|| opts
.merge
|| opts
.prefix
) {
124 if (read_cache_unmerged() && (opts
.prefix
|| opts
.merge
))
125 die("You need to resolve your current index first");
126 stage
= opts
.merge
= 1;
128 resolve_undo_clear();
130 for (i
= 0; i
< argc
; i
++) {
131 const char *arg
= argv
[i
];
133 if (get_sha1(arg
, sha1
))
134 die("Not a valid object name %s", arg
);
135 if (list_tree(sha1
) < 0)
136 die("failed to unpack tree object %s", arg
);
139 if (1 < opts
.index_only
+ opts
.update
)
140 die("-u and -i at the same time makes no sense");
141 if ((opts
.update
||opts
.index_only
) && !opts
.merge
)
142 die("%s is meaningless without -m, --reset, or --prefix",
143 opts
.update
? "-u" : "-i");
144 if ((opts
.dir
&& !opts
.update
))
145 die("--exclude-per-directory is meaningless unless -u");
146 if (opts
.merge
&& !opts
.index_only
)
151 die("just how do you expect me to merge %d trees?", stage
-1);
154 opts
.fn
= opts
.prefix
? bind_merge
: oneway_merge
;
157 opts
.fn
= twoway_merge
;
158 opts
.initial_checkout
= is_cache_unborn();
162 opts
.fn
= threeway_merge
;
167 opts
.head_idx
= stage
- 2;
172 cache_tree_free(&active_cache_tree
);
173 for (i
= 0; i
< nr_trees
; i
++) {
174 struct tree
*tree
= trees
[i
];
176 init_tree_desc(t
+i
, tree
->buffer
, tree
->size
);
178 if (unpack_trees(nr_trees
, t
, &opts
))
182 * When reading only one tree (either the most basic form,
183 * "-m ent" or "--reset ent" form), we can obtain a fully
184 * valid cache-tree because the index must match exactly
185 * what came from the tree.
187 * The same holds true if we are switching between two trees
188 * using read-tree -m A B. The index must match B after that.
190 if (nr_trees
== 1 && !opts
.prefix
)
191 prime_cache_tree(&active_cache_tree
, trees
[0]);
192 else if (nr_trees
== 2 && opts
.merge
)
193 prime_cache_tree(&active_cache_tree
, trees
[1]);
195 if (write_cache(newfd
, active_cache
, active_nr
) ||
196 commit_locked_index(&lock_file
))
197 die("unable to write new index file");