Skip to contentSkip to conversation

check-ignore: --non-matching without --verbose

PATCH Superseded Carl Smedstad via GitGitGadget 5 participants 9 messages May 23, 2022 – May 24, 2022 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 check-ignore: --non-matching without --verbose

From: Carl Smedstad <[email protected]>

Allow --non-matching to be specified without the --verbose flag to make git only list files that are not matching any pattern. The behaviour when specifying both --non-matching and --verbose is unchanged.

The motivation for this is to use the underutilized invocation of --non-matching without --verbose to create a shorthand for what could be achieved by:

    find . |
      git check-ignore --verbose --non-matching |
      grep $'^::\t' |
      sed -e 's/.*\t//'

---
check-ignore: --non-matching without --verbose

Allow --non-matching to be specified without the --verbose flag to make git only list files that are not matching any pattern. The behaviour when specifying both --non-matching and --verbose is unchanged.

The motivation for this is to use the underutilized invocation of --non-matching without --verbose to create a shorthand for what could be achieved by:

find . |
git check-ignore --verbose --non-matching |
grep $'^::\t' |
sed -e 's/.*\t//'

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1236%2Fcarlsmedstad%2Fcheck-ignore-non-matching-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1236/carlsmedstad/check-ignore-non-matching-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/1236

 Documentation/git-check-ignore.txt |  6 ++----
 builtin/check-ignore.c             | 15 +++++++++++----
 t/t0008-ignores.sh                 | 17 +++++++++++++++--
 3 files changed, 28 insertions(+), 10 deletions(-)
Documentation/git-check-ignore.txt +2 -4
index 2892799e32f..69d12fd22e1 100644
@@ -50,10 +50,8 @@ linkgit:gitignore[5].
 	with a NUL character instead of a linefeed character.
 
 -n, --non-matching::
-	Show given paths which don't match any pattern.	 This only
-	makes sense when `--verbose` is enabled, otherwise it would
-	not be possible to distinguish between paths which match a
-	pattern and those which don't.
+	Only show given paths which don't match any pattern. If `--verbose` is
+	enabled, show both paths that match a pattern and those which don't.
 
 --no-index::
 	Don't look in the index when undertaking the checks. This can
builtin/check-ignore.c +11 -4
index 21912569650..10e8bba1057 100644
@@ -113,8 +113,17 @@ static int check_ignore(struct dir_struct *dir,
 			    pattern->flags & PATTERN_FLAG_NEGATIVE)
 				pattern = NULL;
 		}
-		if (!quiet && (pattern || show_non_matching))
-			output_pattern(pathspec.items[i].original, pattern);
+		if (!quiet) {
+			if (verbose) {
+				if (show_non_matching || pattern)
+					output_pattern(pathspec.items[i].original, pattern);
+			} else {
+				if (show_non_matching && !pattern)
+					output_pattern(pathspec.items[i].original, pattern);
+				if (!show_non_matching && pattern)
+					output_pattern(pathspec.items[i].original, pattern);
+			}
+		}
 		if (pattern)
 			num_ignored++;
 	}
@@ -175,8 +184,6 @@ int cmd_check_ignore(int argc, const char **argv, const char *prefix)
 		if (verbose)
 			die(_("cannot have both --quiet and --verbose"));
 	}
-	if (show_non_matching && !verbose)
-		die(_("--non-matching is only valid with --verbose"));
 
 	/* read_cache() is only necessary so we can watch out for submodules. */
 	if (!no_index && read_cache() < 0)
t/t0008-ignores.sh +15 -2
index 5575dade8ee..2ea178797bc 100755
@@ -90,7 +90,8 @@ test_check_ignore () {
 #   1. with -q / --quiet
 #   2. with default verbosity
 #   3. with -v / --verbose
-#   4. with -v / --verbose, *and* -n / --non-matching
+#   4. with -n / --non-matching
+#   5. with -v / --verbose, *and* -n / --non-matching
 #
 # expecting success each time.  Takes advantage of the fact that
 # check-ignore --verbose output is the same as normal output except
@@ -102,7 +103,7 @@ test_check_ignore () {
 # Arguments:
 #   - (optional) prereqs for this test, e.g. 'SYMLINKS'
 #   - test name
-#   - output to expect from the fourth verbosity mode (the output
+#   - output to expect from the fifth verbosity mode (the output
 #     from the other verbosity modes is automatically inferred
 #     from this value)
 #   - code to run (should invoke test_check_ignore)
@@ -124,6 +125,7 @@ test_expect_success_multiple () {
 
 	expect_verbose=$( echo "$expect_all" | grep -v '^::	' )
 	expect=$( echo "$expect_verbose" | sed -e 's/.*	//' )
+	expect_non_matching=$( echo "$expect_all" | grep '^::	' | sed -e 's/.*	//')
 
 	test_expect_success $prereq "$testname${no_index_opt:+ with $no_index_opt}" '
 		expect "$expect" &&
@@ -144,6 +146,17 @@ test_expect_success_multiple () {
 		quiet_opt=
 	fi
 
+	for non_matching_opt in '-n' '--non-matching'
+	do
+		test_code="
+			expect '$expect_non_matching' &&
+			$code
+		"
+		opts="${no_index_opt:+$no_index_opt }$non_matching_opt"
+		test_expect_success $prereq "$testname${opts:+ with $opts}" "$test_code"
+	done
+	non_matching_opt=
+
 	for verbose_opt in '-v' '--verbose'
 	do
 		for non_matching_opt in '' '-n' '--non-matching'

base-commit: f9b95943b68b6b8ca5a6072f50a08411c6449b55

Signature · 1 line
gitgitgadget
#1 lore raw
Quoted 3 lines · Carl Smedstad via GitGitGadget

The motivation for this is to use the underutilized invocation of --non-matching without --verbose to create a shorthand for what could be achieved by:

Not commenting on the patch itself, but ...

Quoted 5 lines
    find . |
      git check-ignore --verbose --non-matching |
      grep $'^::\t' |
      sed -e 's/.*\t//'

... feeding grep output to sed caught my eyes. Isn't that equivalent to

	git ... |
	sed -n '/^::\t/s/.*\t//p'

?

#2 lore raw
Quoted 11 lines

Not commenting on the patch itself, but ...

Quoted 4 lines

find . |
git check-ignore --verbose --non-matching |
grep $'^::\t' |
sed -e 's/.*\t//'

... feeding grep output to sed caught my eyes. Isn't that equivalent to

git ... |
sed -n '/^::\t/s/.*\t//p'

That is indeed equivalent. TIL about regular-expression addresses in sed, thanks!

#3 lore raw
Quoted 7 lines · Carl Smedstad
Quoted 7 lines

Not commenting on the patch itself, but ...

Quoted 4 lines

find . |
git check-ignore --verbose --non-matching |
grep $'^::\t' |
sed -e 's/.*\t//'

Quoted 7 lines
Quoted 4 lines

... feeding grep output to sed caught my eyes. Isn't that equivalent to

git ... |
sed -n '/^::\t/s/.*\t//p'

That is indeed equivalent. TIL about regular-expression addresses in sed, thanks!

I however suspect that these "\t"'s should be spelled out as an actual TAB in the final production version for portability.

Thanks.

#4 lore raw
Quoted 11 lines · Junio C Hamano
Quoted 9 lines · Carl Smedstad
Quoted 9 lines

Not commenting on the patch itself, but ...

Quoted 4 lines

find . |
git check-ignore --verbose --non-matching |
grep $'^::\t' |
sed -e 's/.*\t//'

... feeding grep output to sed caught my eyes. Isn't that equivalent to

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

git ... |
sed -n '/^::\t/s/.*\t//p'

That is indeed equivalent. TIL about regular-expression addresses in sed, thanks!

I however suspect that these "\t"'s should be spelled out as an actual TAB in the final production version for portability.

Make that a "for sure", it's a commonly unsupported feature of some sed's, it's actually more common that not that it isn't supported. See $HT in t4213-log-tabexpand.sh.

#5 lore raw
In reply to Carl Smedstad via GitGitGadget

Documentation/git-check-ignore.txt linkgit:gitignore[5]. · line 54

Quoted 9 lines · Carl Smedstad via GitGitGadget

From: Carl Smedstad <[email protected]>

-n, --non-matching::

Documentation/git-check-ignore.txt +2 -4
-	Show given paths which don't match any pattern.	 This only
-	makes sense when `--verbose` is enabled, otherwise it would
-	not be possible to distinguish between paths which match a
-	pattern and those which don't.
+	Only show given paths which don't match any pattern. If `--verbose` is
+	enabled, show both paths that match a pattern and those which don't.

This may be better to indicate the behavior as predicated on the existence of --verbose:

If --verbose is enabled, then all paths are listed along with an indicator (::) that no matching pattern was found. Without --verbose, list only the paths that do not match any pattern.

builtin/check-ignore.c static int check_ignore(struct dir_struct *dir, · line 124

Quoted 11 lines
builtin/check-ignore.c +9 -2
-		if (!quiet && (pattern || show_non_matching))
-			output_pattern(pathspec.items[i].original, pattern);
+		if (!quiet) {
+			if (verbose) {
+				if (show_non_matching || pattern)
+					output_pattern(pathspec.items[i].original, pattern);
+			} else {
+				if (show_non_matching && !pattern)
+					output_pattern(pathspec.items[i].original, pattern);
+				if (!show_non_matching && pattern)
+					output_pattern(pathspec.items[i].original, pattern);

These three blocks all call the same code line. So really you want to avoid a single case:

	if (!quiet &&
	    ((verbose && (show_non_matching || pattern)) ||
	     (!verbose && !!show_non_matching != !!pattern)))

This is the most direct way to write what you had above. However, we could do this more simply:

	/* If --non-matching, then show if verbose or the pattern is missing. */
	if (!quiet && show_non_matching && (verbose || !pattern))
		output_pattern(...);

	/* If not --non-matching, then show if the pattern exists. */
	if (!quiet && !show_non_matching && pattern)
		output_pattern(...);

Hopefully that's a bit easier to parse. I believe it is equivalent.

Thanks,
-Stolee

#6 lore raw
Quoted 7 lines

This may be better to indicate the behavior as predicated on the existence of --verbose:

If --verbose is enabled, then all paths are listed along with an indicator (::) that no matching pattern was found. Without --verbose, list only the paths that do not match any pattern.

Good point. I've adopted you changes but made some slight changes to the first line:

If --verbose is enabled, list both matching and non-matching paths (non-matching paths along with the indicator ::). Without --verbose, list only the paths that do not match any pattern.

Quoted 18 lines

These three blocks all call the same code line. So really you want to avoid a single case:

	if (!quiet &&
	    ((verbose && (show_non_matching || pattern)) ||
	     (!verbose && !!show_non_matching != !!pattern)))

This is the most direct way to write what you had above. However, we could do this more simply:

	/* If --non-matching, then show if verbose or the pattern is missing. */
	if (!quiet && show_non_matching && (verbose || !pattern))
		output_pattern(...);

	/* If not --non-matching, then show if the pattern exists. */
	if (!quiet && !show_non_matching && pattern)
		output_pattern(...);
Quoted 2 lines

Hopefully that's a bit easier to parse. I believe it is equivalent.

That is indeed equivalent and a lot easier to read. Implemented the changes in full. Thanks!

#7 lore raw

PATCH v2 1/1 check-ignore: --non-matching without --verbose

In reply to Carl Smedstad via GitGitGadget

From: Carl Smedstad <[email protected]>

Allow --non-matching to be specified without the --verbose flag to make git only list files that are not matching any pattern. The behaviour when specifying both --non-matching and --verbose is unchanged.

The motivation for this is to use the underutilized invocation of --non-matching without --verbose to create a shorthand for what could be achieved by:

    find . |
      git check-ignore --verbose --non-matching --stdin |
      grep $'^::\t' |
      sed -e 's/.*\t//'

---
check-ignore: --non-matching without --verbose

Changes since v1:

* Rewrote description for --non-matching.
* Simplified the logic.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1236%2Fcarlsmedstad%2Fcheck-ignore-non-matching-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1236/carlsmedstad/check-ignore-non-matching-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/1236

Range-diff vs v1:

1: de794d0af18 ! 1: 350a3cfea1b check-ignore: --non-matching without --verbose

     @@ Commit message
          achieved by:
      
              find . |
     -          git check-ignore --verbose --non-matching |
     +          git check-ignore --verbose --non-matching --stdin |
                grep $'^::\t' |
                sed -e 's/.*\t//'
      
     @@ Documentation/git-check-ignore.txt: linkgit:gitignore[5].
      -	makes sense when `--verbose` is enabled, otherwise it would
      -	not be possible to distinguish between paths which match a
      -	pattern and those which don't.
     -+	Only show given paths which don't match any pattern. If `--verbose` is
     -+	enabled, show both paths that match a pattern and those which don't.
     ++	If `--verbose` is enabled, list both matching and non-matching
     ++	paths (non-matching paths along with the indicator `::`). Without
     ++	`--verbose`, list only the paths that do not match any pattern.
       
       --no-index::
       	Don't look in the index when undertaking the checks. This can
     @@ builtin/check-ignore.c: static int check_ignore(struct dir_struct *dir,
       				pattern = NULL;
       		}
      -		if (!quiet && (pattern || show_non_matching))
     --			output_pattern(pathspec.items[i].original, pattern);
     -+		if (!quiet) {
     -+			if (verbose) {
     -+				if (show_non_matching || pattern)
     -+					output_pattern(pathspec.items[i].original, pattern);
     -+			} else {
     -+				if (show_non_matching && !pattern)
     -+					output_pattern(pathspec.items[i].original, pattern);
     -+				if (!show_non_matching && pattern)
     -+					output_pattern(pathspec.items[i].original, pattern);
     -+			}
     -+		}
     ++
     ++		/* If --non-matching, then show if verbose or the pattern is missing. */
     ++		if (!quiet && show_non_matching && (verbose || !pattern))
     ++			output_pattern(pathspec.items[i].original, pattern);
     ++
     ++		/* If not --non-matching, then show if the pattern exists. */
     ++		if (!quiet && !show_non_matching && pattern)
     + 			output_pattern(pathspec.items[i].original, pattern);
     ++
       		if (pattern)
       			num_ignored++;
       	}
 Documentation/git-check-ignore.txt |  7 +++----
 builtin/check-ignore.c             | 11 ++++++++---
 t/t0008-ignores.sh                 | 17 +++++++++++++++--
 3 files changed, 26 insertions(+), 9 deletions(-)
Documentation/git-check-ignore.txt +3 -4
index 2892799e32f..bb096875ca3 100644
@@ -50,10 +50,9 @@ linkgit:gitignore[5].
 	with a NUL character instead of a linefeed character.
 
 -n, --non-matching::
-	Show given paths which don't match any pattern.	 This only
-	makes sense when `--verbose` is enabled, otherwise it would
-	not be possible to distinguish between paths which match a
-	pattern and those which don't.
+	If `--verbose` is enabled, list both matching and non-matching
+	paths (non-matching paths along with the indicator `::`). Without
+	`--verbose`, list only the paths that do not match any pattern.
 
 --no-index::
 	Don't look in the index when undertaking the checks. This can
builtin/check-ignore.c +8 -3
index 21912569650..46108ac8593 100644
@@ -113,8 +113,15 @@ static int check_ignore(struct dir_struct *dir,
 			    pattern->flags & PATTERN_FLAG_NEGATIVE)
 				pattern = NULL;
 		}
-		if (!quiet && (pattern || show_non_matching))
+
+		/* If --non-matching, then show if verbose or the pattern is missing. */
+		if (!quiet && show_non_matching && (verbose || !pattern))
+			output_pattern(pathspec.items[i].original, pattern);
+
+		/* If not --non-matching, then show if the pattern exists. */
+		if (!quiet && !show_non_matching && pattern)
 			output_pattern(pathspec.items[i].original, pattern);
+
 		if (pattern)
 			num_ignored++;
 	}
@@ -175,8 +182,6 @@ int cmd_check_ignore(int argc, const char **argv, const char *prefix)
 		if (verbose)
 			die(_("cannot have both --quiet and --verbose"));
 	}
-	if (show_non_matching && !verbose)
-		die(_("--non-matching is only valid with --verbose"));
 
 	/* read_cache() is only necessary so we can watch out for submodules. */
 	if (!no_index && read_cache() < 0)
t/t0008-ignores.sh +15 -2
index 5575dade8ee..2ea178797bc 100755
@@ -90,7 +90,8 @@ test_check_ignore () {
 #   1. with -q / --quiet
 #   2. with default verbosity
 #   3. with -v / --verbose
-#   4. with -v / --verbose, *and* -n / --non-matching
+#   4. with -n / --non-matching
+#   5. with -v / --verbose, *and* -n / --non-matching
 #
 # expecting success each time.  Takes advantage of the fact that
 # check-ignore --verbose output is the same as normal output except
@@ -102,7 +103,7 @@ test_check_ignore () {
 # Arguments:
 #   - (optional) prereqs for this test, e.g. 'SYMLINKS'
 #   - test name
-#   - output to expect from the fourth verbosity mode (the output
+#   - output to expect from the fifth verbosity mode (the output
 #     from the other verbosity modes is automatically inferred
 #     from this value)
 #   - code to run (should invoke test_check_ignore)
@@ -124,6 +125,7 @@ test_expect_success_multiple () {
 
 	expect_verbose=$( echo "$expect_all" | grep -v '^::	' )
 	expect=$( echo "$expect_verbose" | sed -e 's/.*	//' )
+	expect_non_matching=$( echo "$expect_all" | grep '^::	' | sed -e 's/.*	//')
 
 	test_expect_success $prereq "$testname${no_index_opt:+ with $no_index_opt}" '
 		expect "$expect" &&
@@ -144,6 +146,17 @@ test_expect_success_multiple () {
 		quiet_opt=
 	fi
 
+	for non_matching_opt in '-n' '--non-matching'
+	do
+		test_code="
+			expect '$expect_non_matching' &&
+			$code
+		"
+		opts="${no_index_opt:+$no_index_opt }$non_matching_opt"
+		test_expect_success $prereq "$testname${opts:+ with $opts}" "$test_code"
+	done
+	non_matching_opt=
+
 	for verbose_opt in '-v' '--verbose'
 	do
 		for non_matching_opt in '' '-n' '--non-matching'

base-commit: f9b95943b68b6b8ca5a6072f50a08411c6449b55

Signature · 1 line
gitgitgadget
#8 lore raw
In reply to Carl Smedstad via GitGitGadget

Documentation/git-check-ignore.txt linkgit:gitignore[5]. · line 56

Quoted 5 lines · Carl Smedstad via GitGitGadget

-n, --non-matching::

Documentation/git-check-ignore.txt +0 -4
-	Show given paths which don't match any pattern.	 This only
-	makes sense when `--verbose` is enabled, otherwise it would
-	not be possible to distinguish between paths which match a
-	pattern and those which don't.

OK. In the current code, verbose mode output can show both the pattern that decided the fate for the path and the path itself, so we can afford to include paths that match and that do not match. There may not be any pattern that matched the path, or the last pattern that matched the path was negative, or the last pattern that matched was positive. Users can tell them apart (the first two cases are the paths that do not match, the last case is the paths that do match). But non-verbose mode only show paths, so showing both matched and non-matching paths to the output would not make sense. But we document that it is a silly thing to ask, and explicitly reject the combination of non-verbose option and --non-matching option.

So the option is misnamed; it is more like 'include-non-matching', not 'non-matching-only'.

Documentation/git-check-ignore.txt linkgit:gitignore[5]. · line 54

Quoted 2 lines
Documentation/git-check-ignore.txt +2 -0
+	Only show given paths which don't match any pattern. If `--verbose` is
+	enabled, show both paths that match a pattern and those which don't.

But this patch proposes the option to 'non-matching-only' only in non-verbose mode. Since "check-ignore --non-matching <pathspec>" without "--verbose" immediately errors out, making it do something different does not have too much backward-compatibility worries.

It may be confusing to the users that the set of paths included in the output, when the same "-n" option is given, were different between the non-verbose and the verbose mode. That is one thing I feel uncomfortable about this change.

I wonder if we are better off to invent a new "--non-matching-only" option, have it support both the non-verbose and the verbose mode, and leave the current "-n" alone, with an option to perhaps giving "--include-non-matching" alias to the latter to clarify?

Quoted 84 lines
builtin/check-ignore.c +11 -4
index 21912569650..10e8bba1057 100644
@@ -113,8 +113,17 @@ static int check_ignore(struct dir_struct *dir,
 			    pattern->flags & PATTERN_FLAG_NEGATIVE)
 				pattern = NULL;
 		}
-		if (!quiet && (pattern || show_non_matching))
-			output_pattern(pathspec.items[i].original, pattern);
+		if (!quiet) {
+			if (verbose) {
+				if (show_non_matching || pattern)
+					output_pattern(pathspec.items[i].original, pattern);
+			} else {
+				if (show_non_matching && !pattern)
+					output_pattern(pathspec.items[i].original, pattern);
+				if (!show_non_matching && pattern)
+					output_pattern(pathspec.items[i].original, pattern);
+			}
+		}
 		if (pattern)
 			num_ignored++;
 	}
@@ -175,8 +184,6 @@ int cmd_check_ignore(int argc, const char **argv, const char *prefix)
 		if (verbose)
 			die(_("cannot have both --quiet and --verbose"));
 	}
-	if (show_non_matching && !verbose)
-		die(_("--non-matching is only valid with --verbose"));
 
 	/* read_cache() is only necessary so we can watch out for submodules. */
 	if (!no_index && read_cache() < 0)
t/t0008-ignores.sh +15 -2
index 5575dade8ee..2ea178797bc 100755
@@ -90,7 +90,8 @@ test_check_ignore () {
 #   1. with -q / --quiet
 #   2. with default verbosity
 #   3. with -v / --verbose
-#   4. with -v / --verbose, *and* -n / --non-matching
+#   4. with -n / --non-matching
+#   5. with -v / --verbose, *and* -n / --non-matching
 #
 # expecting success each time.  Takes advantage of the fact that
 # check-ignore --verbose output is the same as normal output except
@@ -102,7 +103,7 @@ test_check_ignore () {
 # Arguments:
 #   - (optional) prereqs for this test, e.g. 'SYMLINKS'
 #   - test name
-#   - output to expect from the fourth verbosity mode (the output
+#   - output to expect from the fifth verbosity mode (the output
 #     from the other verbosity modes is automatically inferred
 #     from this value)
 #   - code to run (should invoke test_check_ignore)
@@ -124,6 +125,7 @@ test_expect_success_multiple () {
 
 	expect_verbose=$( echo "$expect_all" | grep -v '^::	' )
 	expect=$( echo "$expect_verbose" | sed -e 's/.*	//' )
+	expect_non_matching=$( echo "$expect_all" | grep '^::	' | sed -e 's/.*	//')
 
 	test_expect_success $prereq "$testname${no_index_opt:+ with $no_index_opt}" '
 		expect "$expect" &&
@@ -144,6 +146,17 @@ test_expect_success_multiple () {
 		quiet_opt=
 	fi
 
+	for non_matching_opt in '-n' '--non-matching'
+	do
+		test_code="
+			expect '$expect_non_matching' &&
+			$code
+		"
+		opts="${no_index_opt:+$no_index_opt }$non_matching_opt"
+		test_expect_success $prereq "$testname${opts:+ with $opts}" "$test_code"
+	done
+	non_matching_opt=
+
 	for verbose_opt in '-v' '--verbose'
 	do
 		for non_matching_opt in '' '-n' '--non-matching'

base-commit: f9b95943b68b6b8ca5a6072f50a08411c6449b55