Commit a291790a8d42579dafe8684151931847921a9578

Carlos Martín Nieto 2015-02-15T05:18:01

Merge pull request #2831 from ethomson/merge_lock merge: lock index during the merge (not just checkout)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
diff --git a/include/git2/checkout.h b/include/git2/checkout.h
index 8314c62..4fe1340 100644
--- a/include/git2/checkout.h
+++ b/include/git2/checkout.h
@@ -135,7 +135,10 @@ typedef enum {
 	/** Only update existing files, don't create new ones */
 	GIT_CHECKOUT_UPDATE_ONLY = (1u << 7),
 
-	/** Normally checkout updates index entries as it goes; this stops that */
+	/**
+	 * Normally checkout updates index entries as it goes; this stops that.
+	 * Implies `GIT_CHECKOUT_DONT_WRITE_INDEX`.
+	 */
 	GIT_CHECKOUT_DONT_UPDATE_INDEX = (1u << 8),
 
 	/** Don't refresh index/config/etc before doing checkout */
@@ -166,6 +169,9 @@ typedef enum {
 	/** Don't overwrite existing files or folders */
 	GIT_CHECKOUT_DONT_REMOVE_EXISTING = (1u << 22),
 
+	/** Normally checkout writes the index upon completion; this prevents that. */
+	GIT_CHECKOUT_DONT_WRITE_INDEX = (1u << 23),
+
 	/**
 	 * THE FOLLOWING OPTIONS ARE NOT YET IMPLEMENTED
 	 */
diff --git a/src/checkout.c b/src/checkout.c
index 3f65a9e..3953840 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -2375,6 +2375,9 @@ cleanup:
 	return error;
 }
 
+#define CHECKOUT_INDEX_DONT_WRITE_MASK \
+	(GIT_CHECKOUT_DONT_UPDATE_INDEX | GIT_CHECKOUT_DONT_WRITE_INDEX)
+
 int git_checkout_iterator(
 	git_iterator *target,
 	git_index *index,
@@ -2481,7 +2484,7 @@ int git_checkout_iterator(
 
 cleanup:
 	if (!error && data.index != NULL &&
-		(data.strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0)
+		(data.strategy & CHECKOUT_INDEX_DONT_WRITE_MASK) == 0)
 		error = git_index_write(data.index);
 
 	git_diff_free(data.diff);
diff --git a/src/cherrypick.c b/src/cherrypick.c
index e58d0ab..ebc9fcd 100644
--- a/src/cherrypick.c
+++ b/src/cherrypick.c
@@ -10,6 +10,7 @@
 #include "filebuf.h"
 #include "merge.h"
 #include "vector.h"
+#include "index.h"
 
 #include "git2/types.h"
 #include "git2/merge.h"
@@ -171,7 +172,8 @@ int git_cherrypick(
 	char commit_oidstr[GIT_OID_HEXSZ + 1];
 	const char *commit_msg, *commit_summary;
 	git_buf their_label = GIT_BUF_INIT;
-	git_index *index_new = NULL;
+	git_index *index = NULL;
+	git_indexwriter indexwriter = GIT_INDEXWRITER_INIT;
 	int error = 0;
 
 	assert(repo && commit);
@@ -192,21 +194,25 @@ int git_cherrypick(
 	if ((error = write_merge_msg(repo, commit_msg)) < 0 ||
 		(error = git_buf_printf(&their_label, "%.7s... %s", commit_oidstr, commit_summary)) < 0 ||
 		(error = cherrypick_normalize_opts(repo, &opts, given_opts, git_buf_cstr(&their_label))) < 0 ||
+		(error = git_indexwriter_init_for_operation(&indexwriter, repo, &opts.checkout_opts.checkout_strategy)) < 0 ||
 		(error = write_cherrypick_head(repo, commit_oidstr)) < 0 ||
 		(error = git_repository_head(&our_ref, repo)) < 0 ||
 		(error = git_reference_peel((git_object **)&our_commit, our_ref, GIT_OBJ_COMMIT)) < 0 ||
-		(error = git_cherrypick_commit(&index_new, repo, commit, our_commit, opts.mainline, &opts.merge_opts)) < 0 ||
-		(error = git_merge__check_result(repo, index_new)) < 0 ||
-		(error = git_merge__append_conflicts_to_merge_msg(repo, index_new)) < 0 ||
-		(error = git_checkout_index(repo, index_new, &opts.checkout_opts)) < 0)
+		(error = git_cherrypick_commit(&index, repo, commit, our_commit, opts.mainline, &opts.merge_opts)) < 0 ||
+		(error = git_merge__check_result(repo, index)) < 0 ||
+		(error = git_merge__append_conflicts_to_merge_msg(repo, index)) < 0 ||
+		(error = git_checkout_index(repo, index, &opts.checkout_opts)) < 0 ||
+		(error = git_indexwriter_commit(&indexwriter)) < 0)
 		goto on_error;
+
 	goto done;
 
 on_error:
 	cherrypick_state_cleanup(repo);
 
 done:
-	git_index_free(index_new);
+	git_indexwriter_cleanup(&indexwriter);
+	git_index_free(index);
 	git_commit_free(our_commit);
 	git_reference_free(our_ref);
 	git_buf_free(&their_label);
diff --git a/src/index.c b/src/index.c
index 75700a7..c122cdf 100644
--- a/src/index.c
+++ b/src/index.c
@@ -656,39 +656,15 @@ int git_index__changed_relative_to(
 
 int git_index_write(git_index *index)
 {
-	git_filebuf file = GIT_FILEBUF_INIT;
+	git_indexwriter writer = GIT_INDEXWRITER_INIT;
 	int error;
 
-	if (!index->index_file_path)
-		return create_index_error(-1,
-			"Failed to read index: The index is in-memory only");
-
-	if (index_sort_if_needed(index, true) < 0)
-		return -1;
-	git_vector_sort(&index->reuc);
-
-	if ((error = git_filebuf_open(
-		&file, index->index_file_path, GIT_FILEBUF_HASH_CONTENTS, GIT_INDEX_FILE_MODE)) < 0) {
-		if (error == GIT_ELOCKED)
-			giterr_set(GITERR_INDEX, "The index is locked. This might be due to a concurrent or crashed process");
-
-		return error;
-	}
-
-	if ((error = write_index(index, &file)) < 0) {
-		git_filebuf_cleanup(&file);
-		return error;
-	}
+	if ((error = git_indexwriter_init(&writer, index)) == 0)
+		error = git_indexwriter_commit(&writer);
 
-	if ((error = git_filebuf_commit(&file)) < 0)
-		return error;
-
-	if (git_futils_filestamp_check(&index->stamp, index->index_file_path) < 0)
-		/* index could not be read from disk! */;
-	else
-		index->on_disk = 1;
+	git_indexwriter_cleanup(&writer);
 
-	return 0;
+	return error;
 }
 
 const char * git_index_path(const git_index *index)
@@ -2695,3 +2671,91 @@ int git_index_snapshot_find(
 {
 	return index_find_in_entries(out, entries, entry_srch, path, path_len, stage);
 }
+
+int git_indexwriter_init(
+	git_indexwriter *writer,
+	git_index *index)
+{
+	int error;
+
+	GIT_REFCOUNT_INC(index);
+
+	writer->index = index;
+
+	if (!index->index_file_path)
+		return create_index_error(-1,
+			"Failed to write index: The index is in-memory only");
+
+	if ((error = git_filebuf_open(
+		&writer->file, index->index_file_path, GIT_FILEBUF_HASH_CONTENTS, GIT_INDEX_FILE_MODE)) < 0) {
+
+		if (error == GIT_ELOCKED)
+			giterr_set(GITERR_INDEX, "The index is locked. This might be due to a concurrent or crashed process");
+
+		return error;
+	}
+
+	writer->should_write = 1;
+
+	return 0;
+}
+
+int git_indexwriter_init_for_operation(
+	git_indexwriter *writer,
+	git_repository *repo,
+	unsigned int *checkout_strategy)
+{
+	git_index *index;
+	int error;
+
+	if ((error = git_repository_index__weakptr(&index, repo)) < 0 ||
+		(error = git_indexwriter_init(writer, index)) < 0)
+		return error;
+
+	writer->should_write = (*checkout_strategy & GIT_CHECKOUT_DONT_WRITE_INDEX) == 0;
+	*checkout_strategy |= GIT_CHECKOUT_DONT_WRITE_INDEX;
+
+	return 0;
+}
+
+int git_indexwriter_commit(git_indexwriter *writer)
+{
+	int error;
+
+	if (!writer->should_write)
+		return 0;
+
+	if (index_sort_if_needed(writer->index, true) < 0)
+		return -1;
+
+	git_vector_sort(&writer->index->reuc);
+
+	if ((error = write_index(writer->index, &writer->file)) < 0) {
+		git_indexwriter_cleanup(writer);
+		return error;
+	}
+
+	if ((error = git_filebuf_commit(&writer->file)) < 0)
+		return error;
+
+	if ((error = git_futils_filestamp_check(
+		&writer->index->stamp, writer->index->index_file_path)) < 0) {
+		giterr_set(GITERR_OS, "Could not read index timestamp");
+		return -1;
+	}
+
+	writer->index->on_disk = 1;
+
+	git_index_free(writer->index);
+	writer->index = NULL;
+
+	return 0;
+}
+
+void git_indexwriter_cleanup(git_indexwriter *writer)
+{
+	git_filebuf_cleanup(&writer->file);
+
+	git_index_free(writer->index);
+	writer->index = NULL;
+}
diff --git a/src/index.h b/src/index.h
index 2eb93fb..6d2904f 100644
--- a/src/index.h
+++ b/src/index.h
@@ -94,4 +94,33 @@ extern int git_index_snapshot_find(
 	const char *path, size_t path_len, int stage);
 
 
+typedef struct {
+	git_index *index;
+	git_filebuf file;
+	unsigned int should_write:1;
+} git_indexwriter;
+
+#define GIT_INDEXWRITER_INIT { NULL, GIT_FILEBUF_INIT }
+
+/* Lock the index for eventual writing. */
+extern int git_indexwriter_init(git_indexwriter *writer, git_index *index);
+
+/* Lock the index for eventual writing by a repository operation: a merge,
+ * revert, cherry-pick or a rebase.  Note that the given checkout strategy
+ * will be updated for the operation's use so that checkout will not write
+ * the index.
+ */
+extern int git_indexwriter_init_for_operation(
+	git_indexwriter *writer,
+	git_repository *repo,
+	unsigned int *checkout_strategy);
+
+/* Write the index and unlock it. */
+extern int git_indexwriter_commit(git_indexwriter *writer);
+
+/* Cleanup an index writing session, unlocking the file (if it is still
+ * locked and freeing any data structures.
+ */
+extern void git_indexwriter_cleanup(git_indexwriter *writer);
+
 #endif
diff --git a/src/merge.c b/src/merge.c
index e4b60c8..75ad091 100644
--- a/src/merge.c
+++ b/src/merge.c
@@ -2652,7 +2652,8 @@ int git_merge(
 	git_checkout_options checkout_opts;
 	git_annotated_commit *ancestor_head = NULL, *our_head = NULL;
 	git_tree *ancestor_tree = NULL, *our_tree = NULL, **their_trees = NULL;
-	git_index *index_new = NULL;
+	git_index *index = NULL;
+	git_indexwriter indexwriter = GIT_INDEXWRITER_INIT;
 	size_t i;
 	int error = 0;
 
@@ -2666,11 +2667,10 @@ int git_merge(
 	their_trees = git__calloc(their_heads_len, sizeof(git_tree *));
 	GITERR_CHECK_ALLOC(their_trees);
 
-	if ((error = merge_heads(&ancestor_head, &our_head, repo, their_heads, their_heads_len)) < 0)
-		goto on_error;
-
-	if ((error = merge_normalize_checkout_opts(repo, &checkout_opts, given_checkout_opts,
-		ancestor_head, our_head, their_heads_len, their_heads)) < 0)
+	if ((error = merge_heads(&ancestor_head, &our_head, repo, their_heads, their_heads_len)) < 0 ||
+		(error = merge_normalize_checkout_opts(repo, &checkout_opts, given_checkout_opts,
+			ancestor_head, our_head, their_heads_len, their_heads)) < 0 ||
+		(error = git_indexwriter_init_for_operation(&indexwriter, repo, &checkout_opts.checkout_strategy)) < 0)
 		goto on_error;
 
 	/* Write the merge files to the repository. */
@@ -2691,10 +2691,11 @@ int git_merge(
 
 	/* TODO: recursive, octopus, etc... */
 
-	if ((error = git_merge_trees(&index_new, repo, ancestor_tree, our_tree, their_trees[0], merge_opts)) < 0 ||
-		(error = git_merge__check_result(repo, index_new)) < 0 ||
-		(error = git_merge__append_conflicts_to_merge_msg(repo, index_new)) < 0 ||
-		(error = git_checkout_index(repo, index_new, &checkout_opts)) < 0)
+	if ((error = git_merge_trees(&index, repo, ancestor_tree, our_tree, their_trees[0], merge_opts)) < 0 ||
+		(error = git_merge__check_result(repo, index)) < 0 ||
+		(error = git_merge__append_conflicts_to_merge_msg(repo, index)) < 0 ||
+		(error = git_checkout_index(repo, index, &checkout_opts)) < 0 ||
+		(error = git_indexwriter_commit(&indexwriter)) < 0)
 		goto on_error;
 
 	goto done;
@@ -2703,7 +2704,9 @@ on_error:
 	merge_state_cleanup(repo);
 
 done:
-	git_index_free(index_new);
+	git_indexwriter_cleanup(&indexwriter);
+
+	git_index_free(index);
 
 	git_tree_free(ancestor_tree);
 	git_tree_free(our_tree);
diff --git a/src/rebase.c b/src/rebase.c
index b0a93a6..8078eed 100644
--- a/src/rebase.c
+++ b/src/rebase.c
@@ -14,6 +14,7 @@
 #include "array.h"
 #include "config.h"
 #include "annotated_commit.h"
+#include "index.h"
 
 #include <git2/types.h>
 #include <git2/annotated_commit.h>
@@ -763,6 +764,7 @@ static int rebase_next_merge(
 	git_commit *current_commit = NULL, *parent_commit = NULL;
 	git_tree *current_tree = NULL, *head_tree = NULL, *parent_tree = NULL;
 	git_index *index = NULL;
+	git_indexwriter indexwriter = GIT_INDEXWRITER_INIT;
 	git_rebase_operation *operation;
 	char current_idstr[GIT_OID_HEXSZ];
 	unsigned int parent_count;
@@ -792,20 +794,21 @@ static int rebase_next_merge(
 
 	git_oid_fmt(current_idstr, &operation->id);
 
-	if ((error = rebase_setupfile(rebase, MSGNUM_FILE, -1, "%d\n", rebase->current+1)) < 0 ||
-		(error = rebase_setupfile(rebase, CURRENT_FILE, -1, "%.*s\n", GIT_OID_HEXSZ, current_idstr)) < 0)
-		goto done;
-
 	normalize_checkout_opts(rebase, current_commit, &checkout_opts, given_checkout_opts);
 
-	if ((error = git_merge_trees(&index, rebase->repo, parent_tree, head_tree, current_tree, NULL)) < 0 ||
+	if ((error = git_indexwriter_init_for_operation(&indexwriter, rebase->repo, &checkout_opts.checkout_strategy)) < 0 ||
+		(error = rebase_setupfile(rebase, MSGNUM_FILE, -1, "%d\n", rebase->current+1)) < 0 ||
+		(error = rebase_setupfile(rebase, CURRENT_FILE, -1, "%.*s\n", GIT_OID_HEXSZ, current_idstr)) < 0 ||
+		(error = git_merge_trees(&index, rebase->repo, parent_tree, head_tree, current_tree, NULL)) < 0 ||
 		(error = git_merge__check_result(rebase->repo, index)) < 0 ||
-		(error = git_checkout_index(rebase->repo, index, &checkout_opts)) < 0)
+		(error = git_checkout_index(rebase->repo, index, &checkout_opts)) < 0 ||
+		(error = git_indexwriter_commit(&indexwriter)) < 0)
 		goto done;
 
 	*out = operation;
 
 done:
+	git_indexwriter_cleanup(&indexwriter);
 	git_index_free(index);
 	git_tree_free(current_tree);
 	git_tree_free(head_tree);
diff --git a/src/revert.c b/src/revert.c
index 36560a7..f8a7f43 100644
--- a/src/revert.c
+++ b/src/revert.c
@@ -9,6 +9,7 @@
 #include "repository.h"
 #include "filebuf.h"
 #include "merge.h"
+#include "index.h"
 
 #include "git2/types.h"
 #include "git2/merge.h"
@@ -174,7 +175,8 @@ int git_revert(
 	char commit_oidstr[GIT_OID_HEXSZ + 1];
 	const char *commit_msg;
 	git_buf their_label = GIT_BUF_INIT;
-	git_index *index_new = NULL;
+	git_index *index = NULL;
+	git_indexwriter indexwriter = GIT_INDEXWRITER_INIT;
 	int error;
 
 	assert(repo && commit);
@@ -194,14 +196,16 @@ int git_revert(
 
 	if ((error = git_buf_printf(&their_label, "parent of %.7s... %s", commit_oidstr, commit_msg)) < 0 ||
 		(error = revert_normalize_opts(repo, &opts, given_opts, git_buf_cstr(&their_label))) < 0 ||
+		(error = git_indexwriter_init_for_operation(&indexwriter, repo, &opts.checkout_opts.checkout_strategy)) < 0 ||
 		(error = write_revert_head(repo, commit_oidstr)) < 0 ||
 		(error = write_merge_msg(repo, commit_oidstr, commit_msg)) < 0 ||
 		(error = git_repository_head(&our_ref, repo)) < 0 ||
 		(error = git_reference_peel((git_object **)&our_commit, our_ref, GIT_OBJ_COMMIT)) < 0 ||
-		(error = git_revert_commit(&index_new, repo, commit, our_commit, opts.mainline, &opts.merge_opts)) < 0 ||
-		(error = git_merge__check_result(repo, index_new)) < 0 ||
-		(error = git_merge__append_conflicts_to_merge_msg(repo, index_new)) < 0 ||
-		(error = git_checkout_index(repo, index_new, &opts.checkout_opts)) < 0)
+		(error = git_revert_commit(&index, repo, commit, our_commit, opts.mainline, &opts.merge_opts)) < 0 ||
+		(error = git_merge__check_result(repo, index)) < 0 ||
+		(error = git_merge__append_conflicts_to_merge_msg(repo, index)) < 0 ||
+		(error = git_checkout_index(repo, index, &opts.checkout_opts)) < 0 ||
+		(error = git_indexwriter_commit(&indexwriter)) < 0)
 		goto on_error;
 
 	goto done;
@@ -210,7 +214,8 @@ on_error:
 	revert_state_cleanup(repo);
 
 done:
-	git_index_free(index_new);
+	git_indexwriter_cleanup(&indexwriter);
+	git_index_free(index);
 	git_commit_free(our_commit);
 	git_reference_free(our_ref);
 	git_buf_free(&their_label);
diff --git a/tests/checkout/tree.c b/tests/checkout/tree.c
index 239ee55..0fabadc 100644
--- a/tests/checkout/tree.c
+++ b/tests/checkout/tree.c
@@ -1184,3 +1184,83 @@ void test_checkout_tree__caches_attributes_during_checkout(void)
 	git_buf_free(&ident2);
 	git_object_free(obj);
 }
+
+void test_checkout_tree__can_not_update_index(void)
+{
+	git_oid oid;
+	git_object *head;
+	unsigned int status;
+	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
+	git_index *index;
+
+	opts.checkout_strategy |=
+		GIT_CHECKOUT_FORCE | GIT_CHECKOUT_DONT_UPDATE_INDEX;
+
+	cl_git_pass(git_reference_name_to_id(&oid, g_repo, "HEAD"));
+	cl_git_pass(git_object_lookup(&head, g_repo, &oid, GIT_OBJ_ANY));
+
+	cl_git_pass(git_reset(g_repo, head, GIT_RESET_HARD, &g_opts, NULL, NULL));
+
+	cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/"));
+
+	cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees"));
+
+	cl_git_pass(git_checkout_tree(g_repo, g_object, &opts));
+
+	cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/2.txt"));
+	cl_git_pass(git_status_file(&status, g_repo, "ab/de/2.txt"));
+	cl_assert_equal_i(GIT_STATUS_WT_NEW, status);
+
+	cl_git_pass(git_repository_index(&index, g_repo));
+	cl_git_pass(git_index_write(index));
+
+	cl_git_pass(git_status_file(&status, g_repo, "ab/de/2.txt"));
+	cl_assert_equal_i(GIT_STATUS_WT_NEW, status);
+
+	git_object_free(head);
+	git_index_free(index);
+}
+
+void test_checkout_tree__can_update_but_not_write_index(void)
+{
+	git_oid oid;
+	git_object *head;
+	unsigned int status;
+	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
+	git_index *index;
+	git_repository *other;
+
+	opts.checkout_strategy |=
+		GIT_CHECKOUT_FORCE | GIT_CHECKOUT_DONT_WRITE_INDEX;
+
+	cl_git_pass(git_reference_name_to_id(&oid, g_repo, "HEAD"));
+	cl_git_pass(git_object_lookup(&head, g_repo, &oid, GIT_OBJ_ANY));
+
+	cl_git_pass(git_reset(g_repo, head, GIT_RESET_HARD, &g_opts, NULL, NULL));
+
+	cl_assert_equal_i(false, git_path_isdir("./testrepo/ab/"));
+
+	cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees"));
+
+	cl_git_pass(git_checkout_tree(g_repo, g_object, &opts));
+
+	cl_assert_equal_i(true, git_path_isfile("./testrepo/ab/de/2.txt"));
+	cl_git_pass(git_status_file(&status, g_repo, "ab/de/2.txt"));
+	cl_assert_equal_i(GIT_STATUS_INDEX_NEW, status);
+
+	cl_git_pass(git_repository_open(&other, "testrepo"));
+	cl_git_pass(git_status_file(&status, other, "ab/de/2.txt"));
+	cl_assert_equal_i(GIT_STATUS_WT_NEW, status);
+	git_repository_free(other);
+
+	cl_git_pass(git_repository_index(&index, g_repo));
+	cl_git_pass(git_index_write(index));
+
+	cl_git_pass(git_repository_open(&other, "testrepo"));
+	cl_git_pass(git_status_file(&status, other, "ab/de/2.txt"));
+	cl_assert_equal_i(GIT_STATUS_INDEX_NEW, status);
+	git_repository_free(other);
+
+	git_object_free(head);
+	git_index_free(index);
+}
diff --git a/tests/index/tests.c b/tests/index/tests.c
index a63183e..4cf7051 100644
--- a/tests/index/tests.c
+++ b/tests/index/tests.c
@@ -677,3 +677,24 @@ void test_index_tests__reload_while_ignoring_case(void)
 
 	git_index_free(index);
 }
+
+void test_index_tests__can_lock_index(void)
+{
+	git_index *index;
+	git_indexwriter one = GIT_INDEXWRITER_INIT,
+		two = GIT_INDEXWRITER_INIT;
+
+	cl_git_pass(git_index_open(&index, TEST_INDEX_PATH));
+	cl_git_pass(git_indexwriter_init(&one, index));
+
+	cl_git_fail_with(GIT_ELOCKED, git_indexwriter_init(&two, index));
+	cl_git_fail_with(GIT_ELOCKED, git_index_write(index));
+
+	cl_git_pass(git_indexwriter_commit(&one));
+
+	cl_git_pass(git_index_write(index));
+
+	git_indexwriter_cleanup(&one);
+	git_indexwriter_cleanup(&two);
+	git_index_free(index);
+}
diff --git a/tests/merge/workdir/setup.c b/tests/merge/workdir/setup.c
index 099bc12..4aebf87 100644
--- a/tests/merge/workdir/setup.c
+++ b/tests/merge/workdir/setup.c
@@ -1018,6 +1018,7 @@ void test_merge_workdir_setup__retained_after_success(void)
 	git_annotated_commit_free(their_heads[0]);
 }
 
+
 void test_merge_workdir_setup__removed_after_failure(void)
 {
 	git_oid our_oid;
@@ -1030,16 +1031,63 @@ void test_merge_workdir_setup__removed_after_failure(void)
 	cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
 	cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref));
 
+	cl_git_write2file("merge-resolve/.git/index.lock", "foo\n", 4, O_RDWR|O_CREAT, 0666);
+
+	cl_git_fail(git_merge(
+		repo, (const git_annotated_commit **)&their_heads[0], 1, NULL, NULL));
+
+	cl_assert(!git_path_exists("merge-resolve/.git/" GIT_MERGE_HEAD_FILE));
+	cl_assert(!git_path_exists("merge-resolve/.git/" GIT_MERGE_MODE_FILE));
+	cl_assert(!git_path_exists("merge-resolve/.git/" GIT_MERGE_MSG_FILE));
+
+	git_reference_free(octo1_ref);
+
+	git_annotated_commit_free(our_head);
+	git_annotated_commit_free(their_heads[0]);
+}
+
+void test_merge_workdir_setup__unlocked_after_success(void)
+{
+	git_oid our_oid;
+	git_reference *octo1_ref;
+	git_annotated_commit *our_head, *their_heads[1];
+
+	cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
+	cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
+
+	cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
+	cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref));
+
+	cl_git_pass(git_merge(
+		repo, (const git_annotated_commit **)&their_heads[0], 1, NULL, NULL));
+
+	cl_assert(!git_path_exists("merge-resolve/.git/index.lock"));
+
+	git_reference_free(octo1_ref);
+
+	git_annotated_commit_free(our_head);
+	git_annotated_commit_free(their_heads[0]);
+}
+
+void test_merge_workdir_setup__unlocked_after_conflict(void)
+{
+	git_oid our_oid;
+	git_reference *octo1_ref;
+	git_annotated_commit *our_head, *their_heads[1];
+
+	cl_git_pass(git_oid_fromstr(&our_oid, ORIG_HEAD));
+	cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid));
+
+	cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH));
+	cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref));
+
 	cl_git_rewritefile("merge-resolve/new-in-octo1.txt",
 		"Conflicting file!\n\nMerge will fail!\n");
 
 	cl_git_fail(git_merge(
 		repo, (const git_annotated_commit **)&their_heads[0], 1, NULL, NULL));
 
-	cl_assert(!git_path_exists("merge-resolve/" GIT_MERGE_HEAD_FILE));
-	cl_assert(!git_path_exists("merge-resolve/" GIT_ORIG_HEAD_FILE));
-	cl_assert(!git_path_exists("merge-resolve/" GIT_MERGE_MODE_FILE));
-	cl_assert(!git_path_exists("merge-resolve/" GIT_MERGE_MSG_FILE));
+	cl_assert(!git_path_exists("merge-resolve/.git/index.lock"));
 
 	git_reference_free(octo1_ref);