merge-recursive: do not use on-file index when not needed.
This revamps the merge-recursive implementation following the outline in: Message-ID: <7v8xgileza.fsf@assigned-by-dhcp.cox.net> There is no need to write out the index until the very end just once from merge-recursive. Also there is no need to write out the resulting tree object for the simple case of merging with a single merge base. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Jan 10, 2007 at 11:20 UTC
8b944b5678efcbbbedfd555e5eef1500dc6074d1
1 file changed
+46
-123
merge-recursive.c
+46
-123
@@ -110,35 +110,6 @@ static void output_commit_title(struct commit *commit)
110
}
111
}
112
113
-static const char *current_index_file = NULL;
114
-static const char *original_index_file;
115
-static const char *temporary_index_file;
116
-static int cache_dirty = 0;
117
-
118
-static int flush_cache(void)
119
-{
120
- /* flush temporary index */
121
- struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
122
- int fd = hold_lock_file_for_update(lock, current_index_file, 1);
123
- if (write_cache(fd, active_cache, active_nr) ||
124
- close(fd) || commit_lock_file(lock))
125
- die ("unable to write %s", current_index_file);
126
- discard_cache();
127
- cache_dirty = 0;
128
- return 0;
129
-}
130
-
131
-static void setup_index(int temp)
132
-{
133
- current_index_file = temp ? temporary_index_file: original_index_file;
134
- if (cache_dirty) {
135
- discard_cache();
136
- cache_dirty = 0;
137
- }
138
- unlink(temporary_index_file);
139
- discard_cache();
140
-}
141
-
113
static struct cache_entry *make_cache_entry(unsigned int mode,
114
const unsigned char *sha1, const char *path, int stage, int refresh)
115
{
@@ -167,9 +138,6 @@ static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
138
const char *path, int stage, int refresh, int options)
139
{
140
struct cache_entry *ce;
170
- if (!cache_dirty)
171
- read_cache_from(current_index_file);
172
- cache_dirty++;
141
ce = make_cache_entry(mode, sha1 ? sha1 : null_sha1, path, stage, refresh);
142
if (!ce)
143
return error("cache_addinfo failed: %s", strerror(cache_errno));
@@ -187,26 +155,6 @@ static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
155
*/
156
static int index_only = 0;
157
190
-static int git_read_tree(struct tree *tree)
191
-{
192
- int rc;
193
- struct object_list *trees = NULL;
194
- struct unpack_trees_options opts;
195
-
196
- if (cache_dirty)
197
- die("read-tree with dirty cache");
198
-
199
- memset(&opts, 0, sizeof(opts));
200
- object_list_append(&tree->object, &trees);
201
- rc = unpack_trees(trees, &opts);
202
- cache_tree_free(&active_cache_tree);
203
-
204
- if (rc == 0)
205
- cache_dirty = 1;
206
-
207
- return rc;
208
-}
209
-
158
static int git_merge_trees(int index_only,
159
struct tree *common,
160
struct tree *head,
@@ -216,11 +164,6 @@ static int git_merge_trees(int index_only,
164
struct object_list *trees = NULL;
165
struct unpack_trees_options opts;
166
219
- if (!cache_dirty) {
220
- read_cache_from(current_index_file);
221
- cache_dirty = 1;
222
- }
223
-
167
memset(&opts, 0, sizeof(opts));
168
if (index_only)
169
opts.index_only = 1;
@@ -236,39 +179,37 @@ static int git_merge_trees(int index_only,
179
180
rc = unpack_trees(trees, &opts);
181
cache_tree_free(&active_cache_tree);
239
-
240
- cache_dirty = 1;
241
-
182
return rc;
183
}
184
185
+static int unmerged_index(void)
186
+{
187
+ int i;
188
+ for (i = 0; i < active_nr; i++) {
189
+ struct cache_entry *ce = active_cache[i];
190
+ if (ce_stage(ce))
191
+ return 1;
192
+ }
193
+ return 0;
194
+}
195
+
196
static struct tree *git_write_tree(void)
197
{
198
struct tree *result = NULL;
199
249
- if (cache_dirty) {
250
- unsigned i;
251
- for (i = 0; i < active_nr; i++) {
252
- struct cache_entry *ce = active_cache[i];
253
- if (ce_stage(ce))
254
- return NULL;
255
- }
256
- } else
257
- read_cache_from(current_index_file);
200
+ if (unmerged_index())
201
+ return NULL;
202
203
if (!active_cache_tree)
204
active_cache_tree = cache_tree();
205
206
if (!cache_tree_fully_valid(active_cache_tree) &&
263
- cache_tree_update(active_cache_tree,
264
- active_cache, active_nr, 0, 0) < 0)
207
+ cache_tree_update(active_cache_tree,
208
+ active_cache, active_nr, 0, 0) < 0)
209
die("error building trees");
210
211
result = lookup_tree(active_cache_tree->sha1);
212
269
- flush_cache();
270
- cache_dirty = 0;
271
-
213
return result;
214
}
215
@@ -331,10 +272,7 @@ static struct path_list *get_unmerged(void)
272
int i;
273
274
unmerged->strdup_paths = 1;
334
- if (!cache_dirty) {
335
- read_cache_from(current_index_file);
336
- cache_dirty++;
337
- }
275
+
276
for (i = 0; i < active_nr; i++) {
277
struct path_list_item *item;
278
struct stage_data *e;
@@ -469,9 +407,6 @@ static int remove_file(int clean, const char *path, int no_wd)
407
int update_working_directory = !index_only && !no_wd;
408
409
if (update_cache) {
472
- if (!cache_dirty)
473
- read_cache_from(current_index_file);
474
- cache_dirty++;
410
if (remove_file_from_cache(path))
411
return -1;
412
}
@@ -1105,9 +1040,7 @@ static int merge_trees(struct tree *head,
1040
sha1_to_hex(head->object.sha1),
1041
sha1_to_hex(merge->object.sha1));
1042
1108
- *result = git_write_tree();
1109
-
1110
- if (!*result) {
1043
+ if (unmerged_index()) {
1044
struct path_list *entries, *re_head, *re_merge;
1045
int i;
1046
path_list_clear(¤t_file_set, 1);
@@ -1128,17 +1061,11 @@ static int merge_trees(struct tree *head,
1061
if (!process_entry(path, e, branch1, branch2))
1062
clean = 0;
1063
}
1131
- if (cache_dirty)
1132
- flush_cache();
1064
1065
path_list_clear(re_merge, 0);
1066
path_list_clear(re_head, 0);
1067
path_list_clear(entries, 1);
1068
1138
- if (clean || index_only)
1139
- *result = git_write_tree();
1140
- else
1141
- *result = NULL;
1069
} else {
1070
clean = 1;
1071
printf("merging of trees %s and %s resulted in %s\n",
@@ -1146,6 +1073,8 @@ static int merge_trees(struct tree *head,
1073
sha1_to_hex(merge->object.sha1),
1074
sha1_to_hex((*result)->object.sha1));
1075
}
1076
+ if (index_only)
1077
+ *result = git_write_tree();
1078
1079
return clean;
1080
}
@@ -1170,10 +1099,10 @@ static int merge(struct commit *h1,
1099
const char *branch1,
1100
const char *branch2,
1101
int call_depth /* =0 */,
1173
- struct commit *ancestor /* =None */,
1102
+ struct commit_list *ca,
1103
struct commit **result)
1104
{
1176
- struct commit_list *ca = NULL, *iter;
1105
+ struct commit_list *iter;
1106
struct commit *merged_common_ancestors;
1107
struct tree *mrtree;
1108
int clean;
@@ -1182,10 +1111,10 @@ static int merge(struct commit *h1,
1111
output_commit_title(h1);
1112
output_commit_title(h2);
1113
1185
- if (ancestor)
1186
- commit_list_insert(ancestor, &ca);
1187
- else
1188
- ca = reverse_commit_list(get_merge_bases(h1, h2, 1));
1114
+ if (!ca) {
1115
+ ca = get_merge_bases(h1, h2, 1);
1116
+ ca = reverse_commit_list(ca);
1117
+ }
1118
1119
output("found %u common ancestor(s):", commit_list_count(ca));
1120
for (iter = ca; iter; iter = iter->next)
@@ -1211,6 +1140,7 @@ static int merge(struct commit *h1,
1140
* merge_trees has always overwritten it: the commited
1141
* "conflicts" were already resolved.
1142
*/
1143
+ discard_cache();
1144
merge(merged_common_ancestors, iter->item,
1145
"Temporary merge branch 1",
1146
"Temporary merge branch 2",
@@ -1223,25 +1153,21 @@ static int merge(struct commit *h1,
1153
die("merge returned no commit");
1154
}
1155
1156
+ discard_cache();
1157
if (call_depth == 0) {
1227
- setup_index(0 /* $GIT_DIR/index */);
1158
+ read_cache();
1159
index_only = 0;
1229
- } else {
1230
- setup_index(1 /* temporary index */);
1231
- git_read_tree(h1->tree);
1160
+ } else
1161
index_only = 1;
1233
- }
1162
1163
clean = merge_trees(h1->tree, h2->tree, merged_common_ancestors->tree,
1164
branch1, branch2, &mrtree);
1165
1238
- if (!ancestor && (clean || index_only)) {
1166
+ if (index_only) {
1167
*result = make_virtual_commit(mrtree, "merged tree");
1168
commit_list_insert(h1, &(*result)->parents);
1169
commit_list_insert(h2, &(*result)->parents->next);
1242
- } else
1243
- *result = NULL;
1244
-
1170
+ }
1171
return clean;
1172
}
1173
@@ -1277,19 +1203,16 @@ static struct commit *get_ref(const char *ref)
1203
1204
int main(int argc, char *argv[])
1205
{
1280
- static const char *bases[2];
1206
+ static const char *bases[20];
1207
static unsigned bases_count = 0;
1208
int i, clean;
1209
const char *branch1, *branch2;
1210
struct commit *result, *h1, *h2;
1211
+ struct commit_list *ca = NULL;
1212
+ struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
1213
+ int index_fd;
1214
1215
git_config(git_default_config); /* core.filemode */
1287
- original_index_file = getenv(INDEX_ENVIRONMENT);
1288
-
1289
- if (!original_index_file)
1290
- original_index_file = xstrdup(git_path("index"));
1291
-
1292
- temporary_index_file = xstrdup(git_path("mrg-rcrsv-tmp-idx"));
1216
1217
if (argc < 4)
1218
die("Usage: %s <base>... -- <head> <remote> ...\n", argv[0]);
@@ -1313,18 +1236,18 @@ int main(int argc, char *argv[])
1236
branch2 = better_branch_name(branch2);
1237
printf("Merging %s with %s\n", branch1, branch2);
1238
1316
- if (bases_count == 1) {
1317
- struct commit *ancestor = get_ref(bases[0]);
1318
- clean = merge(h1, h2, branch1, branch2, 0, ancestor, &result);
1319
- } else
1320
- clean = merge(h1, h2, branch1, branch2, 0, NULL, &result);
1239
+ index_fd = hold_lock_file_for_update(lock, get_index_file(), 1);
1240
1322
- if (cache_dirty)
1323
- flush_cache();
1241
+ for (i = 0; i < bases_count; i++) {
1242
+ struct commit *ancestor = get_ref(bases[i]);
1243
+ ca = commit_list_insert(ancestor, &ca);
1244
+ }
1245
+ clean = merge(h1, h2, branch1, branch2, 0, ca, &result);
1246
+
1247
+ if (active_cache_changed &&
1248
+ (write_cache(index_fd, active_cache, active_nr) ||
1249
+ close(index_fd) || commit_lock_file(lock)))
1250
+ die ("unable to write %s", get_index_file());
1251
1252
return clean ? 0: 1;
1253
}
1327
-
1328
-/*
1329
-vim: sw=8 noet
1330
-*/