Subject: [PATCH 00/21] nd/list-files updates

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Sun, 8 Feb 2015 16:01:18 +0700

- 02/21: fix a bug that ignores config keys if $LS_COLORS is not defined
- 09/21: use listFiles instead of list-files in config keys
- 17/21: incorporate match_pathspec changes from Junio,
    use strbuf_swap in show_as_directory()
- 21/21: use test_decode_color and test_when_finished in t3080

Full diff of changes is

-- 8< --
diff --git a/Documentation/config.txt b/Documentation/config.txt
index 74da715..2dc4788 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -940,7 +940,7 @@ color.status.<slot>::
 	to red). The values of these variables may be specified as in
 	color.branch.<slot>.
 
-color.list-files::
+color.listFiles::
 	A boolean to enable/disable color in the output of
 	linkgit:git-list-files[1]. May be set to `always`, `false` (or
 	`never`) or `auto` (or `true`), in which case colors are used
@@ -1018,7 +1018,7 @@ column.clean::
 	Specify the layout when list items in `git clean -i`, which always
 	shows files and directories in columns. See `column.ui` for details.
 
-column.list-files::
+column.listFiles::
 	Specify whether to output tag listing in `git list-files` in columns.
 	See `column.ui` for details.
 
diff --git a/builtin/ls-files.c b/builtin/ls-files.c
index b04c712..b5e1a59 100644
--- a/builtin/ls-files.c
+++ b/builtin/ls-files.c
@@ -229,8 +229,8 @@ static int show_as_directory(const struct cache_entry *ce)
 		if (want_color(use_color)) {
 			struct strbuf sb3 = STRBUF_INIT;
 			color_filename(&sb3, ce->name, sb2.buf, S_IFDIR, 1);
-			strbuf_release(&sb2);
-			sb2 = sb3;
+			strbuf_swap(&sb2, &sb3);
+			strbuf_release(&sb3);
 		}
 		if (show_tag)
 			strbuf_insert(&sb2, 0, tag_cached, strlen(tag_cached));
@@ -258,33 +258,41 @@ static void write_ce_name(struct strbuf *sb, const struct cache_entry *ce)
 	strbuf_release(&quoted);
 }
 
+static int match_pathspec_with_depth(struct pathspec *ps,
+				     const char *name, int namelen,
+				     int prefix, char *seen, int is_dir,
+				     const int *custom_depth)
+{
+	int saved_depth = ps->max_depth;
+	int result;
+
+	if (custom_depth)
+		ps->max_depth = *custom_depth;
+	result = match_pathspec(ps, name, namelen, prefix, seen, is_dir);
+	if (custom_depth)
+		ps->max_depth = saved_depth;
+	return result;
+}
+
 static void show_ce_entry(const char *tag, const struct cache_entry *ce)
 {
 	static struct strbuf sb = STRBUF_INIT;
-	int len = max_prefix_len, saved_max_depth;
+	static const int infinite_depth = -1;
+	int len = max_prefix_len;
 
 	if (len >= ce_namelen(ce))
 		die("git ls-files: internal error - cache entry not superset of prefix");
 
-	if (show_dirs) {
-		/* ignore depth to catch dirs that contain matched entries */
-		saved_max_depth = pathspec.max_depth;
-		pathspec.max_depth = -1;
-	}
-
-	if (!match_pathspec(&pathspec, ce->name, ce_namelen(ce),
-			    len, ps_matched,
-			    S_ISDIR(ce->ce_mode) || S_ISGITLINK(ce->ce_mode)))
+	if (!match_pathspec_with_depth(&pathspec, ce->name, ce_namelen(ce),
+				       len, ps_matched,
+				       S_ISDIR(ce->ce_mode) || S_ISGITLINK(ce->ce_mode),
+				       show_dirs ? &infinite_depth : NULL))
 		return;
 
-	if (show_dirs) {
-		pathspec.max_depth = saved_max_depth;
-		if (strchr(ce->name, '/') &&
-		    !match_pathspec(&pathspec, ce->name, ce_namelen(ce),
-				    prefix_len, NULL, 1) &&
-		    show_as_directory(ce))
-			return;
-	}
+	if (show_dirs &&strchr(ce->name, '/') &&
+	    !match_pathspec(&pathspec, ce->name, ce_namelen(ce), prefix_len, NULL, 1) &&
+	    show_as_directory(ce))
+		return;
 
 	if (tag && *tag && show_valid_bit &&
 	    (ce->ce_flags & CE_VALID)) {
@@ -697,8 +705,8 @@ static int option_parse_exclude_standard(const struct option *opt,
 static int git_ls_config(const char *var, const char *value, void *cb)
 {
 	if (starts_with(var, "column."))
-		return git_column_config(var, value, "list-files", &colopts);
-	if (!strcmp(var, "color.list-files")) {
+		return git_column_config(var, value, "listfiles", &colopts);
+	if (!strcmp(var, "color.listfiles")) {
 		use_color = git_config_colorbool(var, value);
 		return 0;
 	}
diff --git a/ls_colors.c b/ls_colors.c
index 9259ad3..011a8b9 100644
--- a/ls_colors.c
+++ b/ls_colors.c
@@ -326,8 +326,10 @@ void parse_ls_color(void)
 	char *start;
 	size_t len;
 
-	if ((p = getenv("LS_COLORS")) == NULL || *p == '\0')
+	if ((p = getenv("LS_COLORS")) == NULL || *p == '\0') {
+		git_config(ls_colors_config, NULL);
 		return;
+	}
 
 	ext = NULL;
 	strcpy(label, "??");
diff --git a/t/t3080-list-files.sh b/t/t3080-list-files.sh
index 6313dd9..01b9662 100755
--- a/t/t3080-list-files.sh
+++ b/t/t3080-list-files.sh
@@ -15,16 +15,26 @@ test_expect_success 'setup' '
 
 test_expect_success 'LS_COLORS env variable' '
 	LS_COLORS="rs=0:fi=31:di=32" \
-		git list-files --color=always | grep -v gitlink >actual &&
-	test_cmp "$TEST_DIRECTORY"/t3080/ls_colors actual
+		git list-files --color=always | test_decode_color | \
+		grep -v gitlink >actual &&
+	cat >expected <<-\EOF &&
+	<GREEN>dir<RESET>
+	<RED>file<RESET>
+	EOF
+	test_cmp expected actual
 '
 
 test_expect_success 'color.ls.*' '
 	test_config color.ls.file red &&
 	test_config color.ls.directory green &&
 	test_config color.ls.submodule yellow &&
-	git list-files --color=always >actual &&
-	test_cmp "$TEST_DIRECTORY"/t3080/color_ls actual
+	git list-files --color=always | test_decode_color >actual &&
+	cat >expected <<-\EOF &&
+	<GREEN>dir<RESET>
+	<RED>file<RESET>
+	<YELLOW>gitlink<RESET>
+	EOF
+	test_cmp expected actual
 '
 
 test_expect_success 'column output' '
@@ -78,6 +88,7 @@ test_expect_success 'globbing' '
 '
 
 test_expect_success 'no dups' '
+	test_when_finished "git checkout file" &&
 	echo dirty >>file &&
 	git list-files -m file >actual &&
 	echo "file" >expected &&
@@ -100,6 +111,7 @@ test_expect_success '--classify' '
 '
 
 test_expect_success 'diff-cached' '
+	test_when_finished "git checkout file" &&
 	echo dirty >>file &&
 	git add file &&
 	git list-files -M >actual &&
diff --git a/t/t3080/color_ls b/t/t3080/color_ls
deleted file mode 100644
index 47f77ad..0000000
--- a/t/t3080/color_ls
+++ /dev/null
@@ -1,3 +0,0 @@
-[32mdir[m
-[31mfile[m
-[33mgitlink[m
diff --git a/t/t3080/ls_colors b/t/t3080/ls_colors
deleted file mode 100644
index 423c016..0000000
--- a/t/t3080/ls_colors
+++ /dev/null
@@ -1,2 +0,0 @@
-[32mdir[m
-[31mfile[m
-- 8< --
-- 
2.3.0.rc1.137.g477eb31