Subject: [PATCH 18/25] list-files: delete redundant cached entries

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Mon, 6 Apr 2015 20:52:27 +0700

When both --cached and one of -amdAMD is used together we may have two
entries of the same path, e.g. "  foo" and "MM foo". In this case it's
pretty clear that "foo" must be tracked, no need to display "   foo".
The new function does that.

Helped-by: Eric Sunshine <[email-masked]>
Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 builtin/list-files.c  | 12 ++++++++++--
 t/t7013-list-files.sh | 12 ++++++++++++
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/builtin/list-files.c b/builtin/list-files.c
index 14ffd62..31c2336 100644
--- a/builtin/list-files.c
+++ b/builtin/list-files.c
@@ -93,7 +93,10 @@ static int compare_item(const void *a_, const void *b_)
 {
 	const struct item *a = a_;
 	const struct item *b = b_;
-	return strcmp(a->path, b->path);
+	int ret = strcmp(a->path, b->path);
+	if (ret)
+		return ret;
+	return strncmp(a->tag, b->tag, 2);
 }
 
 static void free_item(struct item *item)
@@ -132,7 +135,12 @@ static void remove_duplicates(struct item_list *list)
 	for (src = dst = 1; src < list->nr; src++) {
 		if (!compare_item(list->items + dst - 1, list->items + src))
 			free_item(list->items + src);
-		else
+		else if ((list->items[dst - 1].tag[0] == ' ' &&
+			  list->items[dst - 1].tag[1] == ' ' &&
+			  !strcmp(list->items[src].path, list->items[dst - 1].path))) {
+			free_item(list->items + dst - 1);
+			list->items[dst - 1] = list->items[src];
+		} else
 			list->items[dst++] = list->items[src];
 	}
 	list->nr = dst;
diff --git a/t/t7013-list-files.sh b/t/t7013-list-files.sh
index c747453..392cce1 100755
--- a/t/t7013-list-files.sh
+++ b/t/t7013-list-files.sh
@@ -315,4 +315,16 @@ test_expect_success 'list-files --wt-modifed --modified' '
 	test_cmp expected actual
 '
 
+test_expect_success 'list-files -mMc' '
+	git list-files -mMc >actual &&
+	cat >expected <<-\EOF &&
+	D  a
+	AD b
+	AM c
+	   sa
+	   sc
+	EOF
+	test_cmp expected actual
+'
+
 test_done
-- 
2.3.0.rc1.137.g477eb31