]> asedeno.scripts.mit.edu Git - git.git/blob - builtin-read-tree.c
read-tree --debug-unpack
[git.git] / builtin-read-tree.c
1 /*
2  * GIT - The information manager from hell
3  *
4  * Copyright (C) Linus Torvalds, 2005
5  */
6
7 #include "cache.h"
8 #include "object.h"
9 #include "tree.h"
10 #include "tree-walk.h"
11 #include "cache-tree.h"
12 #include "unpack-trees.h"
13 #include "dir.h"
14 #include "builtin.h"
15 #include "parse-options.h"
16
17 static int nr_trees;
18 static struct tree *trees[MAX_UNPACK_TREES];
19
20 static int list_tree(unsigned char *sha1)
21 {
22         struct tree *tree;
23
24         if (nr_trees >= MAX_UNPACK_TREES)
25                 die("I cannot read more than %d trees", MAX_UNPACK_TREES);
26         tree = parse_tree_indirect(sha1);
27         if (!tree)
28                 return -1;
29         trees[nr_trees++] = tree;
30         return 0;
31 }
32
33 static const char * const read_tree_usage[] = {
34         "git read-tree [[-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>] [-u [--exclude-per-directory=<gitignore>] | -i]]  [--index-output=<file>] <tree-ish1> [<tree-ish2> [<tree-ish3>]]",
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 }
66
67 static void debug_stage(const char *label, struct cache_entry *ce,
68                         struct unpack_trees_options *o)
69 {
70         printf("%s ", label);
71         if (!ce)
72                 printf("(missing)\n");
73         else if (ce == o->df_conflict_entry)
74                 printf("(conflict)\n");
75         else
76                 printf("%06o #%d %s %.8s\n",
77                        ce->ce_mode, ce_stage(ce), ce->name,
78                        sha1_to_hex(ce->sha1));
79 }
80
81 static int debug_merge(struct cache_entry **stages, struct unpack_trees_options *o)
82 {
83         int i;
84
85         printf("* %d-way merge\n", o->merge_size);
86         debug_stage("index", stages[0], o);
87         for (i = 1; i <= o->merge_size; i++) {
88                 char buf[24];
89                 sprintf(buf, "ent#%d", i);
90                 debug_stage(buf, stages[i], o);
91         }
92         return 0;
93 }
94
95 static struct lock_file lock_file;
96
97 int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
98 {
99         int i, newfd, stage = 0;
100         unsigned char sha1[20];
101         struct tree_desc t[MAX_UNPACK_TREES];
102         struct unpack_trees_options opts;
103         int prefix_set = 0;
104         const struct option read_tree_options[] = {
105                 { OPTION_CALLBACK, 0, "index-output", NULL, "FILE",
106                   "write resulting index to <FILE>",
107                   PARSE_OPT_NONEG, index_output_cb },
108                 OPT__VERBOSE(&opts.verbose_update),
109                 OPT_GROUP("Merging"),
110                 OPT_SET_INT('m', NULL, &opts.merge,
111                             "perform a merge in addition to a read", 1),
112                 OPT_SET_INT(0, "trivial", &opts.trivial_merges_only,
113                             "3-way merge if no file level merging required", 1),
114                 OPT_SET_INT(0, "aggressive", &opts.aggressive,
115                             "3-way merge in presence of adds and removes", 1),
116                 OPT_SET_INT(0, "reset", &opts.reset,
117                             "same as -m, but discard unmerged entries", 1),
118                 { OPTION_STRING, 0, "prefix", &opts.prefix, "<subdirectory>/",
119                   "read the tree into the index under <subdirectory>/",
120                   PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP },
121                 OPT_SET_INT('u', NULL, &opts.update,
122                             "update working tree with merge result", 1),
123                 { OPTION_CALLBACK, 0, "exclude-per-directory", &opts,
124                   "gitignore",
125                   "allow explicitly ignored files to be overwritten",
126                   PARSE_OPT_NONEG, exclude_per_directory_cb },
127                 OPT_SET_INT('i', NULL, &opts.index_only,
128                             "don't check the working tree after merging", 1),
129                 OPT_SET_INT(0, "debug-unpack", &opts.debug_unpack,
130                             "debug unpack-trees", 1),
131                 OPT_END()
132         };
133
134         memset(&opts, 0, sizeof(opts));
135         opts.head_idx = -1;
136         opts.src_index = &the_index;
137         opts.dst_index = &the_index;
138
139         git_config(git_default_config, NULL);
140
141         argc = parse_options(argc, argv, unused_prefix, read_tree_options,
142                              read_tree_usage, 0);
143
144         newfd = hold_locked_index(&lock_file, 1);
145
146         prefix_set = opts.prefix ? 1 : 0;
147         if (1 < opts.merge + opts.reset + prefix_set)
148                 die("Which one? -m, --reset, or --prefix?");
149
150         if (opts.reset || opts.merge || opts.prefix) {
151                 if (read_cache_unmerged() && (opts.prefix || opts.merge))
152                         die("You need to resolve your current index first");
153                 stage = opts.merge = 1;
154         }
155
156         for (i = 0; i < argc; i++) {
157                 const char *arg = argv[i];
158
159                 if (get_sha1(arg, sha1))
160                         die("Not a valid object name %s", arg);
161                 if (list_tree(sha1) < 0)
162                         die("failed to unpack tree object %s", arg);
163                 stage++;
164         }
165         if (1 < opts.index_only + opts.update)
166                 die("-u and -i at the same time makes no sense");
167         if ((opts.update||opts.index_only) && !opts.merge)
168                 die("%s is meaningless without -m, --reset, or --prefix",
169                     opts.update ? "-u" : "-i");
170         if ((opts.dir && !opts.update))
171                 die("--exclude-per-directory is meaningless unless -u");
172         if (opts.merge && !opts.index_only)
173                 setup_work_tree();
174
175         if (opts.merge) {
176                 if (stage < 2)
177                         die("just how do you expect me to merge %d trees?", stage-1);
178                 switch (stage - 1) {
179                 case 1:
180                         opts.fn = opts.prefix ? bind_merge : oneway_merge;
181                         break;
182                 case 2:
183                         opts.fn = twoway_merge;
184                         opts.initial_checkout = is_cache_unborn();
185                         break;
186                 case 3:
187                 default:
188                         opts.fn = threeway_merge;
189                         break;
190                 }
191
192                 if (stage - 1 >= 3)
193                         opts.head_idx = stage - 2;
194                 else
195                         opts.head_idx = 1;
196         }
197
198         if (opts.debug_unpack)
199                 opts.fn = debug_merge;
200
201         cache_tree_free(&active_cache_tree);
202         for (i = 0; i < nr_trees; i++) {
203                 struct tree *tree = trees[i];
204                 parse_tree(tree);
205                 init_tree_desc(t+i, tree->buffer, tree->size);
206         }
207         if (unpack_trees(nr_trees, t, &opts))
208                 return 128;
209
210         if (opts.debug_unpack)
211                 return 0; /* do not write the index out */
212
213         /*
214          * When reading only one tree (either the most basic form,
215          * "-m ent" or "--reset ent" form), we can obtain a fully
216          * valid cache-tree because the index must match exactly
217          * what came from the tree.
218          *
219          * The same holds true if we are switching between two trees
220          * using read-tree -m A B.  The index must match B after that.
221          */
222         if (nr_trees == 1 && !opts.prefix)
223                 prime_cache_tree(&active_cache_tree, trees[0]);
224         else if (nr_trees == 2 && opts.merge)
225                 prime_cache_tree(&active_cache_tree, trees[1]);
226
227         if (write_cache(newfd, active_cache, active_nr) ||
228             commit_locked_index(&lock_file))
229                 die("unable to write new index file");
230         return 0;
231 }