Commit dfe8c8df3707b2773e376633c5908dc612e59d6a

Edward Thomson 2013-05-22T23:19:40

handle renames in status computation

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
diff --git a/include/git2/status.h b/include/git2/status.h
index ce1d44a..64ce675 100644
--- a/include/git2/status.h
+++ b/include/git2/status.h
@@ -42,6 +42,7 @@ typedef enum {
 	GIT_STATUS_WT_MODIFIED      = (1u << 8),
 	GIT_STATUS_WT_DELETED       = (1u << 9),
 	GIT_STATUS_WT_TYPECHANGE    = (1u << 10),
+	GIT_STATUS_WT_RENAMED       = (1u << 11),
 
 	GIT_STATUS_IGNORED          = (1u << 14),
 } git_status_t;
@@ -130,6 +131,10 @@ typedef enum {
  * - GIT_STATUS_OPT_RECURSE_IGNORED_DIRS indicates that the contents of
  *   ignored directories should be included in the status.  This is like
  *   doing `git ls-files -o -i --exclude-standard` with core git.
+ * - GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX indicates that items that are
+ *   renamed in the index will be reported as renames.
+ * - GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR indicates that items that
+ *   are renamed in the working directory will be reported as renames.
  *
  * Calling `git_status_foreach()` is like calling the extended version
  * with: GIT_STATUS_OPT_INCLUDE_IGNORED, GIT_STATUS_OPT_INCLUDE_UNTRACKED,
@@ -137,13 +142,15 @@ typedef enum {
  * together as `GIT_STATUS_OPT_DEFAULTS` if you want them as a baseline.
  */
 typedef enum {
-	GIT_STATUS_OPT_INCLUDE_UNTRACKED      = (1u << 0),
-	GIT_STATUS_OPT_INCLUDE_IGNORED        = (1u << 1),
-	GIT_STATUS_OPT_INCLUDE_UNMODIFIED     = (1u << 2),
-	GIT_STATUS_OPT_EXCLUDE_SUBMODULES     = (1u << 3),
-	GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS = (1u << 4),
-	GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH = (1u << 5),
-	GIT_STATUS_OPT_RECURSE_IGNORED_DIRS   = (1u << 6),
+	GIT_STATUS_OPT_INCLUDE_UNTRACKED        = (1u << 0),
+	GIT_STATUS_OPT_INCLUDE_IGNORED          = (1u << 1),
+	GIT_STATUS_OPT_INCLUDE_UNMODIFIED       = (1u << 2),
+	GIT_STATUS_OPT_EXCLUDE_SUBMODULES       = (1u << 3),
+	GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS   = (1u << 4),
+	GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH   = (1u << 5),
+	GIT_STATUS_OPT_RECURSE_IGNORED_DIRS     = (1u << 6),
+	GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX    = (1u << 7),
+	GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR = (1u << 8),
 } git_status_opt_t;
 
 #define GIT_STATUS_OPT_DEFAULTS \
diff --git a/src/status.c b/src/status.c
index 33e67ef..20e45b7 100644
--- a/src/status.c
+++ b/src/status.c
@@ -54,7 +54,6 @@ static unsigned int workdir_delta2status(git_delta_t workdir_status)
 
 	switch (workdir_status) {
 	case GIT_DELTA_ADDED:
-	case GIT_DELTA_RENAMED:
 	case GIT_DELTA_COPIED:
 	case GIT_DELTA_UNTRACKED:
 		st = GIT_STATUS_WT_NEW;
@@ -68,6 +67,9 @@ static unsigned int workdir_delta2status(git_delta_t workdir_status)
 	case GIT_DELTA_IGNORED:
 		st = GIT_STATUS_IGNORED;
 		break;
+	case GIT_DELTA_RENAMED:
+		st = GIT_STATUS_WT_RENAMED;
+		break;
 	case GIT_DELTA_TYPECHANGE:
 		st = GIT_STATUS_WT_TYPECHANGE;
 		break;
@@ -85,9 +87,9 @@ static bool status_is_included(
 {
 	/* if excluding submodules and this is a submodule everywhere */
 	if ((statuslist->opts.flags & GIT_STATUS_OPT_EXCLUDE_SUBMODULES) != 0) {
-		bool in_tree  = (head2idx && head2idx->status != GIT_DELTA_ADDED);
+		bool in_tree = (head2idx && head2idx->status != GIT_DELTA_ADDED);
 		bool in_index = (head2idx && head2idx->status != GIT_DELTA_DELETED);
-		bool in_wd    = (idx2wd && idx2wd->status != GIT_DELTA_DELETED);
+		bool in_wd = (idx2wd && idx2wd->status != GIT_DELTA_DELETED);
 
 		if ((!in_tree || head2idx->old_file.mode == GIT_FILEMODE_COMMIT) &&
 			(!in_index || head2idx->new_file.mode == GIT_FILEMODE_COMMIT) &&
@@ -136,24 +138,79 @@ static int status_collect(
 	return 0;
 }
 
-git_status_list *git_status_list_alloc(void)
+GIT_INLINE(int) status_entry_cmp_base(
+	const void *a,
+	const void *b,
+	int (*strcomp)(const char *a, const char *b))
+{
+	const git_status_entry *entry_a = a;
+	const git_status_entry *entry_b = b;
+	const git_diff_delta *delta_a, *delta_b;
+
+	delta_a = entry_a->index_to_workdir ? entry_a->index_to_workdir :
+		entry_a->head_to_index;
+	delta_b = entry_b->index_to_workdir ? entry_b->index_to_workdir :
+		entry_b->head_to_index;
+
+	if (!delta_a && delta_b)
+		return -1;
+	if (delta_a && !delta_b)
+		return 1;
+	if (!delta_a && !delta_b)
+		return 0;
+
+	return strcomp(delta_a->new_file.path, delta_b->new_file.path);
+}
+
+static int status_entry_icmp(const void *a, const void *b)
+{
+	return status_entry_cmp_base(a, b, git__strcasecmp);
+}
+
+static int status_entry_cmp(const void *a, const void *b)
+{
+	return status_entry_cmp_base(a, b, git__strcmp);
+}
+
+static git_status_list *git_status_list_alloc(git_index *index)
 {
 	git_status_list *statuslist = NULL;
+	int (*entrycmp)(const void *a, const void *b);
+
+	entrycmp = index->ignore_case ? status_entry_icmp : status_entry_cmp;
 
 	if ((statuslist = git__calloc(1, sizeof(git_status_list))) == NULL ||
-		git_vector_init(&statuslist->paired, 0, NULL) < 0)
+		git_vector_init(&statuslist->paired, 0, entrycmp) < 0)
 		return NULL;
 
 	return statuslist;
 }
 
+static int newfile_cmp(const void *a, const void *b)
+{
+	const git_diff_delta *delta_a = a;
+	const git_diff_delta *delta_b = b;
+
+	return git__strcmp(delta_a->new_file.path, delta_b->new_file.path);
+}
+
+static int newfile_casecmp(const void *a, const void *b)
+{
+	const git_diff_delta *delta_a = a;
+	const git_diff_delta *delta_b = b;
+
+	return git__strcasecmp(delta_a->new_file.path, delta_b->new_file.path);
+}
+
 int git_status_list_new(
 	git_status_list **out,
 	git_repository *repo,
 	const git_status_options *opts)
 {
+	git_index *index = NULL;
 	git_status_list *statuslist = NULL;
 	git_diff_options diffopt = GIT_DIFF_OPTIONS_INIT;
+	git_diff_find_options findopts_i2w = GIT_DIFF_FIND_OPTIONS_INIT;
 	git_tree *head = NULL;
 	git_status_show_t show =
 		opts ? opts->show : GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
@@ -165,7 +222,8 @@ int git_status_list_new(
 
 	GITERR_CHECK_VERSION(opts, GIT_STATUS_OPTIONS_VERSION, "git_status_options");
 
-	if ((error = git_repository__ensure_not_bare(repo, "status")) < 0)
+	if ((error = git_repository__ensure_not_bare(repo, "status")) < 0 ||
+		(error = git_repository_index(&index, repo)) < 0)
 		return error;
 
 	/* if there is no HEAD, that's okay - we'll make an empty iterator */
@@ -173,7 +231,7 @@ int git_status_list_new(
 		!(error == GIT_ENOTFOUND || error == GIT_EORPHANEDHEAD))
 		return error;
 
-	statuslist = git_status_list_alloc();
+	statuslist = git_status_list_alloc(index);
 	GITERR_CHECK_ALLOC(statuslist);
 
 	memcpy(&statuslist->opts, opts, sizeof(git_status_options));
@@ -197,17 +255,23 @@ int git_status_list_new(
 	if ((opts->flags & GIT_STATUS_OPT_EXCLUDE_SUBMODULES) != 0)
 		diffopt.flags = diffopt.flags | GIT_DIFF_IGNORE_SUBMODULES;
 
+	findopts_i2w.flags |= GIT_DIFF_FIND_FOR_UNTRACKED;
+
 	if (show != GIT_STATUS_SHOW_WORKDIR_ONLY) {
-		error = git_diff_tree_to_index(&statuslist->head2idx, repo, head, NULL, &diffopt);
+		if ((error = git_diff_tree_to_index(&statuslist->head2idx, repo, head, NULL, &diffopt)) < 0)
+			goto on_error;
 
-		if (error < 0)
+		if ((opts->flags & GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX) != 0 &&
+			(error = git_diff_find_similar(statuslist->head2idx, NULL)) < 0)
 			goto on_error;
 	}
 
 	if (show != GIT_STATUS_SHOW_INDEX_ONLY) {
-		error = git_diff_index_to_workdir(&statuslist->idx2wd, repo, NULL, &diffopt);
+		if ((error = git_diff_index_to_workdir(&statuslist->idx2wd, repo, NULL, &diffopt)) < 0)
+			goto on_error;
 
-		if (error < 0)
+		if ((opts->flags & GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR) != 0 &&
+			(error = git_diff_find_similar(statuslist->idx2wd, &findopts_i2w)) < 0)
 			goto on_error;
 	}
 
@@ -219,9 +283,22 @@ int git_status_list_new(
 		statuslist->head2idx = NULL;
 	}
 
-	if ((error = git_diff__paired_foreach(statuslist->head2idx, statuslist->idx2wd, status_collect, statuslist)) < 0)
+	if ((opts->flags & GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX) != 0) {
+		statuslist->head2idx->deltas._cmp =
+			(statuslist->head2idx->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0 ?
+			newfile_casecmp : newfile_cmp;
+
+		git_vector_sort(&statuslist->head2idx->deltas);
+	}
+
+	if ((error = git_diff__paired_foreach(statuslist->head2idx, statuslist->idx2wd,
+		status_collect, statuslist)) < 0)
 		goto on_error;
 
+	if ((opts->flags & GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX) != 0 ||
+		(opts->flags & GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR) != 0)
+		git_vector_sort(&statuslist->paired);
+
 	*out = statuslist;
 	goto done;
 
@@ -230,6 +307,7 @@ on_error:
 
 done:
 	git_tree_free(head);
+	git_index_free(index);
 
 	return error;
 }
@@ -307,7 +385,7 @@ int git_status_foreach(
 {
 	git_status_options opts = GIT_STATUS_OPTIONS_INIT;
 
-	opts.show  = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
+	opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
 	opts.flags = GIT_STATUS_OPT_INCLUDE_IGNORED |
 		GIT_STATUS_OPT_INCLUDE_UNTRACKED |
 		GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
@@ -364,7 +442,7 @@ int git_status_file(
 	if (index->ignore_case)
 		sfi.fnm_flags = FNM_CASEFOLD;
 
-	opts.show  = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
+	opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
 	opts.flags = GIT_STATUS_OPT_INCLUDE_IGNORED |
 		GIT_STATUS_OPT_RECURSE_IGNORED_DIRS |
 		GIT_STATUS_OPT_INCLUDE_UNTRACKED |
diff --git a/tests-clar/status/renames.c b/tests-clar/status/renames.c
new file mode 100644
index 0000000..d29c7bf
--- /dev/null
+++ b/tests-clar/status/renames.c
@@ -0,0 +1,265 @@
+#include "clar_libgit2.h"
+#include "buffer.h"
+#include "path.h"
+#include "posix.h"
+#include "status_helpers.h"
+#include "util.h"
+#include "status.h"
+
+static git_repository *g_repo = NULL;
+
+void test_status_renames__initialize(void)
+{
+	g_repo = cl_git_sandbox_init("renames");
+}
+
+void test_status_renames__cleanup(void)
+{
+	cl_git_sandbox_cleanup();
+}
+
+static void rename_file(git_repository *repo, const char *oldname, const char *newname)
+{
+	git_buf oldpath = GIT_BUF_INIT, newpath = GIT_BUF_INIT;
+
+	git_buf_joinpath(&oldpath, git_repository_workdir(repo), oldname);
+	git_buf_joinpath(&newpath, git_repository_workdir(repo), newname);
+
+	cl_git_pass(p_rename(oldpath.ptr, newpath.ptr));
+
+	git_buf_free(&oldpath);
+	git_buf_free(&newpath);
+}
+
+static void rename_and_edit_file(git_repository *repo, const char *oldname, const char *newname)
+{
+	git_buf oldpath = GIT_BUF_INIT, newpath = GIT_BUF_INIT;
+
+	git_buf_joinpath(&oldpath, git_repository_workdir(repo), oldname);
+	git_buf_joinpath(&newpath, git_repository_workdir(repo), newname);
+
+	cl_git_pass(p_rename(oldpath.ptr, newpath.ptr));
+	cl_git_append2file(newpath.ptr, "Added at the end to keep similarity!");
+
+	git_buf_free(&oldpath);
+	git_buf_free(&newpath);
+}
+
+struct status_entry {
+	git_status_t status;
+	const char *oldname;
+	const char *newname;
+};
+
+static void test_status(
+	git_status_list *status_list,
+	struct status_entry *expected_list,
+	size_t expected_len)
+{
+	const git_status_entry *actual;
+	const struct status_entry *expected;
+	const char *oldname, *newname;
+	size_t i;
+
+	cl_assert(expected_len == git_status_list_entrycount(status_list));
+
+	for (i = 0; i < expected_len; i++) {
+		actual = git_status_byindex(status_list, i);
+		expected = &expected_list[i];
+
+		cl_assert(actual->status == expected->status);
+
+		oldname = actual->head_to_index ? actual->head_to_index->old_file.path :
+			actual->index_to_workdir ? actual->index_to_workdir->old_file.path : NULL;
+
+		newname = actual->index_to_workdir ? actual->index_to_workdir->new_file.path :
+			actual->head_to_index ? actual->head_to_index->new_file.path : NULL;
+
+		if (oldname)
+			cl_assert(git__strcmp(oldname, expected->oldname) == 0);
+		else
+			cl_assert(expected->oldname == NULL);
+
+		if (newname)
+			cl_assert(git__strcmp(newname, expected->newname) == 0);
+		else
+			cl_assert(expected->newname == NULL);
+	}
+}
+
+void test_status_renames__head2index_one(void)
+{
+	git_index *index;
+	git_status_list *statuslist;
+	git_status_options opts = GIT_STATUS_OPTIONS_INIT;
+	struct status_entry expected[] = {
+		{ GIT_STATUS_INDEX_RENAMED, "ikeepsix.txt", "newname.txt" },
+	};
+
+	opts.flags |= GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX;
+
+	cl_git_pass(git_repository_index(&index, g_repo));
+
+	rename_file(g_repo, "ikeepsix.txt", "newname.txt");
+
+	cl_git_pass(git_index_remove_bypath(index, "ikeepsix.txt"));
+	cl_git_pass(git_index_add_bypath(index, "newname.txt"));
+	cl_git_pass(git_index_write(index));
+
+	cl_git_pass(git_status_list_new(&statuslist, g_repo, &opts));
+	test_status(statuslist, expected, 1);
+	git_status_list_free(statuslist);
+
+	git_index_free(index);
+}
+
+void test_status_renames__head2index_two(void)
+{
+	git_index *index;
+	git_status_list *statuslist;
+	git_status_options opts = GIT_STATUS_OPTIONS_INIT;
+	struct status_entry expected[] = {
+		{ GIT_STATUS_INDEX_RENAMED, "sixserving.txt", "aaa.txt" },
+		{ GIT_STATUS_INDEX_RENAMED, "untimely.txt", "bbb.txt" },
+		{ GIT_STATUS_INDEX_RENAMED, "songof7cities.txt", "ccc.txt" },
+		{ GIT_STATUS_INDEX_RENAMED, "ikeepsix.txt", "ddd.txt" },
+	};
+
+	opts.flags |= GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX;
+
+	cl_git_pass(git_repository_index(&index, g_repo));
+
+	rename_file(g_repo, "ikeepsix.txt", "ddd.txt");
+	rename_and_edit_file(g_repo, "sixserving.txt", "aaa.txt");
+	rename_file(g_repo, "songof7cities.txt", "ccc.txt");
+	rename_and_edit_file(g_repo, "untimely.txt", "bbb.txt");
+
+	cl_git_pass(git_index_remove_bypath(index, "ikeepsix.txt"));
+	cl_git_pass(git_index_remove_bypath(index, "sixserving.txt"));
+	cl_git_pass(git_index_remove_bypath(index, "songof7cities.txt"));
+	cl_git_pass(git_index_remove_bypath(index, "untimely.txt"));
+	cl_git_pass(git_index_add_bypath(index, "ddd.txt"));
+	cl_git_pass(git_index_add_bypath(index, "aaa.txt"));
+	cl_git_pass(git_index_add_bypath(index, "ccc.txt"));
+	cl_git_pass(git_index_add_bypath(index, "bbb.txt"));
+	cl_git_pass(git_index_write(index));
+
+	cl_git_pass(git_status_list_new(&statuslist, g_repo, &opts));
+	test_status(statuslist, expected, 4);
+	git_status_list_free(statuslist);
+
+	git_index_free(index);
+}
+
+void test_status_renames__index2workdir_one(void)
+{
+	git_status_list *statuslist;
+	git_status_options opts = GIT_STATUS_OPTIONS_INIT;
+	struct status_entry expected[] = {
+		{ GIT_STATUS_WT_RENAMED, "ikeepsix.txt", "newname.txt" },
+	};
+
+	opts.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED;
+	opts.flags |= GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR;
+
+	rename_file(g_repo, "ikeepsix.txt", "newname.txt");
+
+	cl_git_pass(git_status_list_new(&statuslist, g_repo, &opts));
+	test_status(statuslist, expected, 1);
+	git_status_list_free(statuslist);
+}
+
+void test_status_renames__index2workdir_two(void)
+{
+	git_status_list *statuslist;
+	git_status_options opts = GIT_STATUS_OPTIONS_INIT;
+	struct status_entry expected[] = {
+		{ GIT_STATUS_WT_RENAMED, "sixserving.txt", "aaa.txt" },
+		{ GIT_STATUS_WT_RENAMED, "untimely.txt", "bbb.txt" },
+		{ GIT_STATUS_WT_RENAMED, "songof7cities.txt", "ccc.txt" },
+		{ GIT_STATUS_WT_RENAMED, "ikeepsix.txt", "ddd.txt" },
+	};
+
+	opts.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED;
+	opts.flags |= GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR;
+
+	rename_file(g_repo, "ikeepsix.txt", "ddd.txt");
+	rename_and_edit_file(g_repo, "sixserving.txt", "aaa.txt");
+	rename_file(g_repo, "songof7cities.txt", "ccc.txt");
+	rename_and_edit_file(g_repo, "untimely.txt", "bbb.txt");
+
+	cl_git_pass(git_status_list_new(&statuslist, g_repo, &opts));
+	test_status(statuslist, expected, 4);
+	git_status_list_free(statuslist);
+}
+
+void test_status_renames__both_one(void)
+{
+	git_index *index;
+	git_status_list *statuslist;
+	git_status_options opts = GIT_STATUS_OPTIONS_INIT;
+	struct status_entry expected[] = {
+		{ GIT_STATUS_INDEX_RENAMED | GIT_STATUS_WT_RENAMED, "ikeepsix.txt", "newname-workdir.txt" },
+	};
+
+	opts.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED;
+	opts.flags |= GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX;
+	opts.flags |= GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR;
+
+	cl_git_pass(git_repository_index(&index, g_repo));
+
+	rename_file(g_repo, "ikeepsix.txt", "newname-index.txt");
+
+	cl_git_pass(git_index_remove_bypath(index, "ikeepsix.txt"));
+	cl_git_pass(git_index_add_bypath(index, "newname-index.txt"));
+	cl_git_pass(git_index_write(index));
+
+	rename_file(g_repo, "newname-index.txt", "newname-workdir.txt");
+
+	cl_git_pass(git_status_list_new(&statuslist, g_repo, &opts));
+	test_status(statuslist, expected, 1);
+	git_status_list_free(statuslist);
+
+	git_index_free(index);
+}
+
+void test_status_renames__both_two(void)
+{
+	git_index *index;
+	git_status_list *statuslist;
+	git_status_options opts = GIT_STATUS_OPTIONS_INIT;
+	struct status_entry expected[] = {
+		{ GIT_STATUS_INDEX_RENAMED | GIT_STATUS_WT_RENAMED, "ikeepsix.txt", "ikeepsix-both.txt" },
+		{ GIT_STATUS_INDEX_RENAMED, "sixserving.txt", "sixserving-index.txt" },
+		{ GIT_STATUS_WT_RENAMED, "songof7cities.txt", "songof7cities-workdir.txt" },
+		{ GIT_STATUS_INDEX_RENAMED | GIT_STATUS_WT_RENAMED, "untimely.txt", "untimely-both.txt" },
+	};
+
+	opts.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED;
+	opts.flags |= GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX;
+	opts.flags |= GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR;
+
+	cl_git_pass(git_repository_index(&index, g_repo));
+
+	rename_and_edit_file(g_repo, "ikeepsix.txt", "ikeepsix-index.txt");
+	rename_and_edit_file(g_repo, "sixserving.txt", "sixserving-index.txt");
+	rename_file(g_repo, "untimely.txt", "untimely-index.txt");
+
+	cl_git_pass(git_index_remove_bypath(index, "ikeepsix.txt"));
+	cl_git_pass(git_index_remove_bypath(index, "sixserving.txt"));
+	cl_git_pass(git_index_remove_bypath(index, "untimely.txt"));
+	cl_git_pass(git_index_add_bypath(index, "ikeepsix-index.txt"));
+	cl_git_pass(git_index_add_bypath(index, "sixserving-index.txt"));
+	cl_git_pass(git_index_add_bypath(index, "untimely-index.txt"));
+	cl_git_pass(git_index_write(index));
+
+	rename_and_edit_file(g_repo, "ikeepsix-index.txt", "ikeepsix-both.txt");
+	rename_and_edit_file(g_repo, "songof7cities.txt", "songof7cities-workdir.txt");
+	rename_file(g_repo, "untimely-index.txt", "untimely-both.txt");
+
+	cl_git_pass(git_status_list_new(&statuslist, g_repo, &opts));
+	test_status(statuslist, expected, 4);
+	git_status_list_free(statuslist);
+
+	git_index_free(index);
+}