[PATCH 13/16] ls-files: add --narrow-match=spec option for testing narrow matching

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@xxxxxxxxx>
---
 Documentation/git-ls-files.txt   |    7 +++++-
 builtin-ls-files.c               |   14 +++++++++++-
 t/t3003-ls-files-narrow-match.sh |   45 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 64 insertions(+), 2 deletions(-)
 create mode 100755 t/t3003-ls-files-narrow-match.sh

diff --git a/Documentation/git-ls-files.txt b/Documentation/git-ls-files.txt
index f74b212..4f5a37e 100644
--- a/Documentation/git-ls-files.txt
+++ b/Documentation/git-ls-files.txt
@@ -12,7 +12,7 @@ SYNOPSIS
 'git ls-files' [-z] [-t] [-v]
 		(--[cached|deleted|others|ignored|stage|unmerged|killed|modified])\*
 		(-[c|d|o|i|s|u|k|m])\*
-		[--narrow-checkout]
+		[--narrow-checkout] [--narrow-match=<narrow_spec>]
 		[-x <pattern>|--exclude=<pattern>]
 		[-X <file>|--exclude-from=<file>]
 		[--exclude-per-directory=<file>]
@@ -78,6 +78,11 @@ OPTIONS
 	index-based selection options like --cached or --stage, only narrowed
 	portion will be printed out.
 
+--narrow-match=<narrow_spec>::
+	Narrow spec can be applied on ls-files output so that you can test
+	your spec. Can only be used with --cached. See linkgit:git-checkout[1]
+	for more information about narrow spec.
+
 -z::
 	\0 line termination on output.
 
diff --git a/builtin-ls-files.c b/builtin-ls-files.c
index 02fa00b..f48a157 100644
--- a/builtin-ls-files.c
+++ b/builtin-ls-files.c
@@ -10,6 +10,8 @@
 #include "dir.h"
 #include "builtin.h"
 #include "tree.h"
+#include "tree-walk.h"
+#include "unpack-trees.h"
 
 static int abbrev;
 static int show_deleted;
@@ -29,6 +31,7 @@ static const char **pathspec;
 static int error_unmatch;
 static char *ps_matched;
 static const char *with_tree;
+static const char *narrow_spec;
 
 static const char *tag_cached = "";
 static const char *tag_unmerged = "";
@@ -248,6 +251,8 @@ static void show_files(struct dir_struct *dir, const char *prefix)
 				continue;
 			if (narrow_checkout && ce_no_checkout(ce))
 				continue;
+			if (narrow_spec && !match_narrow_spec(narrow_spec, ce->name, prefix))
+				continue;
 			show_ce_entry(ce_stage(ce) ? tag_unmerged : tag_cached, ce);
 		}
 	}
@@ -427,7 +432,7 @@ int report_path_error(const char *ps_matched, const char **pathspec, int prefix_
 
 static const char ls_files_usage[] =
 	"git ls-files [-z] [-t] [-v] (--[cached|deleted|others|stage|unmerged|killed|modified])* "
-	"[ --narrow-checkout ] "
+	"[ --narrow-checkout ] [--narrow-match=<narrow_spec>] "
 	"[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
 	"[ --exclude-per-directory=<filename> ] [--exclude-standard] "
 	"[--full-name] [--abbrev] [--] [<file>]*";
@@ -473,6 +478,10 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)
 			narrow_checkout = 1;
 			continue;
 		}
+		if (!prefixcmp(arg, "--narrow-match=")) {
+			narrow_spec = arg+15;
+			continue;
+		}
 		if (!strcmp(arg, "-d") || !strcmp(arg, "--deleted")) {
 			show_deleted = 1;
 			continue;
@@ -607,6 +616,9 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)
 	if (narrow_checkout && !show_cached && !show_stage)
 		die("ls-files: --narrow-checkout can only be used with either --cached or --stage");
 
+	if (narrow_spec && !show_cached && !show_stage)
+		die("ls-files: --narrow-match can only be used with either --cached or --stage");
+
 	read_cache();
 	if (prefix)
 		prune_cache(prefix);
diff --git a/t/t3003-ls-files-narrow-match.sh b/t/t3003-ls-files-narrow-match.sh
new file mode 100755
index 0000000..b48fdb2
--- /dev/null
+++ b/t/t3003-ls-files-narrow-match.sh
@@ -0,0 +1,45 @@
+#!/bin/sh
+
+test_description='This test is for narrow spec matching'
+
+. test-lib.sh
+
+test_expect_success 'setup' '
+	touch 1 2 3 &&
+	mkdir -p sub/subsub &&
+	touch sub/1 sub/2 sub/3 &&
+	touch sub/subsub/1 sub/subsub/2 sub/subsub/3 &&
+	git add .
+'
+
+test_expect_success 'exact matches' '
+	test "$(git ls-files --narrow-match=1)" = 1 &&
+	test -z "$(git ls-files --narrow-match=sub)" && # exact match on a directory gives nothing
+	(cd sub &&
+	test "$(git ls-files --full-name --narrow-match=subsub/1)" = sub/subsub/1 &&
+	test "$(git ls-files --narrow-match=subsub/1)" = subsub/1)
+'
+
+test_expect_success 'star matches' '
+	test "$(git ls-files --narrow-match=\*1)" = 1 &&
+	(cd sub &&
+	test "$(git ls-files --full-name --narrow-match=subsub/\*1)" = sub/subsub/1 &&
+	test "$(git ls-files --narrow-match=subsub/\*1)" = subsub/1)
+'
+
+cat >starplus-1.expected <<\EOF
+1
+sub/1
+sub/subsub/1
+EOF
+cat >starplus-2.expected <<\EOF
+sub/1
+sub/subsub/1
+EOF
+test_expect_success 'starplus matches' '
+	git ls-files --narrow-match=+\*1 | cmp starplus-1.expected
+	(cd sub &&
+	git ls-files --full-name --narrow-match=+\*1 | cmp ../starplus-2.expected)
+'
+
+test_done
-- 
1.6.0.96.g2fad1.dirty

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux