threads / patch / 62479

patchdiff: update conflict handling for whitespace to issue a warning

Subject: [PATCH] diff: update conflict handling for whitespace to issue a warning

## tl;dr

11 messages between Nov 11, 2024 and Nov 20, 2024. Diffs are folded; open one to read it.

replies: 10people: 3as markdown or json

Usman Akinyemi via GitGitGadget· Nov 11, 2024, 17:49 UTC · lore
From: Usman Akinyemi <usmanakinyemi202@gmail.com>

Modify the conflict resolution between tab-in-indent and indent-with-non-tab to issue a warning instead of terminating the operation with `die()`. Update the `git diff --check` test to capture and verify the warning message output.

Suggested-by: Phillip Wood <phillip.wood123@gmail.com>
Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com>
---
    diff: update conflict handling for whitespace to issue a warning
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1828%2FUnique-Usman%2Fmaster-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1828/Unique-Usman/master-v1
Pull-Request: https://github.com/git/git/pull/1828
 t/t4015-diff-whitespace.sh | 3 ++-
 ws.c                       | 7 +++++--
 2 files changed, 7 insertions(+), 3 deletions(-)
Show changes to 2 files +7 −3

t/t4015-diff-whitespace.sh, ws.c

diff --git a/t/t4015-diff-whitespace.sh b/t/t4015-diff-whitespace.sh
index 851cfe4f32c..ada3f90b288 100755
--- a/t/t4015-diff-whitespace.sh
+++ b/t/t4015-diff-whitespace.sh
@@ -808,7 +808,8 @@ test_expect_success 'ditto, but tabwidth=1 (must be irrelevant)' '
 test_expect_success 'check tab-in-indent and indent-with-non-tab conflict' '
 	git config core.whitespace "tab-in-indent,indent-with-non-tab" &&
 	echo "foo ();" >x &&
-	test_must_fail git diff --check
+	git diff --check 2>error &&
+	test_grep "warning: cannot enforce both tab-in-indent and indent-with-non-tab, removing tab-in-indent" error
 '
 
 test_expect_success 'check tab-in-indent excluded from wildcard whitespace attribute' '
diff --git a/ws.c b/ws.c
index 9456e2fdbe3..2c11715177e 100644
--- a/ws.c
+++ b/ws.c
@@ -6,6 +6,7 @@
 #include "git-compat-util.h"
 #include "attr.h"
 #include "strbuf.h"
+#include "gettext.h"
 #include "ws.h"
 
 unsigned whitespace_rule_cfg = WS_DEFAULT_RULE;
@@ -70,8 +71,10 @@ unsigned parse_whitespace_rule(const char *string)
 		string = ep;
 	}
 
-	if (rule & WS_TAB_IN_INDENT && rule & WS_INDENT_WITH_NON_TAB)
-		die("cannot enforce both tab-in-indent and indent-with-non-tab");
+	if (rule & WS_TAB_IN_INDENT && rule & WS_INDENT_WITH_NON_TAB) {
+		warning(_("cannot enforce both tab-in-indent and indent-with-non-tab, removing tab-in-indent"));
+		rule &= ~WS_TAB_IN_INDENT;
+	}
 	return rule;
 }
 

base-commit: facbe4f633e4ad31e641f64617bc88074c659959
-- 
gitgitgadget
Junio C Hamano· Nov 11, 2024, 23:59 UTC · re: Usman Akinyemi via GitGitGadget · lore

Re: [PATCH] diff: update conflict handling for whitespace to issue a warning

"Usman Akinyemi via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 6 quoted lines
> From: Usman Akinyemi <usmanakinyemi202@gmail.com>
>
> Modify the conflict resolution between tab-in-indent and
> indent-with-non-tab to issue a warning instead of terminating
> the operation with `die()`. Update the `git diff --check` test to
> capture and verify the warning message output.

Hmph, giving a warning against these conflicting setting (instead of dying) and continuing _may_ make sense sometimes, but it is unclear which one should survive.

I do not think of a scenario in which it makes much sense to let the program warn only on one but not the other one. Perhaps disabling both, if we were to do the "warn and keep going, instead of dying", may make some more sense than that. I dunno.

Usman Akinyemi via GitGitGadget· Nov 13, 2024, 19:01 UTC · re: Usman Akinyemi via GitGitGadget · lore

[PATCH v2] diff: update conflict handling for whitespace to issue a warning

From: Usman Akinyemi <usmanakinyemi202@gmail.com>

Modify the conflict resolution between tab-in-indent and indent-with-non-tab to issue a warning instead of terminating the operation with `die()`. Update the `git diff --check` test to capture and verify the warning message output.

Suggested-by: Phillip Wood <phillip.wood123@gmail.com>
Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com>
---
    diff: update conflict handling for whitespace to issue a warning
    
    Changes from V1
    
     * Disable both WS_TAB_IN_INDENT and WS_INDENT_WITH_NON_TAB when both
       are set.
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1828%2FUnique-Usman%2Fmaster-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1828/Unique-Usman/master-v2
Pull-Request: https://github.com/git/git/pull/1828
Range-diff vs v1:
 1:  dfb80a7ff2d ! 1:  8531e80811c diff: update conflict handling for whitespace to issue a warning
     @@ t/t4015-diff-whitespace.sh: test_expect_success 'ditto, but tabwidth=1 (must be
       	echo "foo ();" >x &&
      -	test_must_fail git diff --check
      +	git diff --check 2>error &&
     -+	test_grep "warning: cannot enforce both tab-in-indent and indent-with-non-tab, removing tab-in-indent" error
     ++	test_grep "warning: cannot enforce both tab-in-indent and indent-with-non-tab, disabling both" error
       '
       
       test_expect_success 'check tab-in-indent excluded from wildcard whitespace attribute' '
     @@ ws.c: unsigned parse_whitespace_rule(const char *string)
      -	if (rule & WS_TAB_IN_INDENT && rule & WS_INDENT_WITH_NON_TAB)
      -		die("cannot enforce both tab-in-indent and indent-with-non-tab");
      +	if (rule & WS_TAB_IN_INDENT && rule & WS_INDENT_WITH_NON_TAB) {
     -+		warning(_("cannot enforce both tab-in-indent and indent-with-non-tab, removing tab-in-indent"));
     ++		warning(_("cannot enforce both tab-in-indent and indent-with-non-tab, disabling both"));
      +		rule &= ~WS_TAB_IN_INDENT;
     ++		rule &= ~WS_INDENT_WITH_NON_TAB;
      +	}
       	return rule;
       }
 t/t4015-diff-whitespace.sh | 3 ++-
 ws.c                       | 8 ++++++--
 2 files changed, 8 insertions(+), 3 deletions(-)
Show changes to 2 files +8 −3

t/t4015-diff-whitespace.sh, ws.c

diff --git a/t/t4015-diff-whitespace.sh b/t/t4015-diff-whitespace.sh
index 851cfe4f32c..849f1854fb9 100755
--- a/t/t4015-diff-whitespace.sh
+++ b/t/t4015-diff-whitespace.sh
@@ -808,7 +808,8 @@ test_expect_success 'ditto, but tabwidth=1 (must be irrelevant)' '
 test_expect_success 'check tab-in-indent and indent-with-non-tab conflict' '
 	git config core.whitespace "tab-in-indent,indent-with-non-tab" &&
 	echo "foo ();" >x &&
-	test_must_fail git diff --check
+	git diff --check 2>error &&
+	test_grep "warning: cannot enforce both tab-in-indent and indent-with-non-tab, disabling both" error
 '
 
 test_expect_success 'check tab-in-indent excluded from wildcard whitespace attribute' '
diff --git a/ws.c b/ws.c
index 9456e2fdbe3..3e9ce55d095 100644
--- a/ws.c
+++ b/ws.c
@@ -6,6 +6,7 @@
 #include "git-compat-util.h"
 #include "attr.h"
 #include "strbuf.h"
+#include "gettext.h"
 #include "ws.h"
 
 unsigned whitespace_rule_cfg = WS_DEFAULT_RULE;
@@ -70,8 +71,11 @@ unsigned parse_whitespace_rule(const char *string)
 		string = ep;
 	}
 
-	if (rule & WS_TAB_IN_INDENT && rule & WS_INDENT_WITH_NON_TAB)
-		die("cannot enforce both tab-in-indent and indent-with-non-tab");
+	if (rule & WS_TAB_IN_INDENT && rule & WS_INDENT_WITH_NON_TAB) {
+		warning(_("cannot enforce both tab-in-indent and indent-with-non-tab, disabling both"));
+		rule &= ~WS_TAB_IN_INDENT;
+		rule &= ~WS_INDENT_WITH_NON_TAB;
+	}
 	return rule;
 }
 

base-commit: facbe4f633e4ad31e641f64617bc88074c659959
-- 
gitgitgadget
Junio C Hamano· Nov 14, 2024, 02:15 UTC · re: Usman Akinyemi via GitGitGadget · lore

Re: [PATCH v2] diff: update conflict handling for whitespace to issue a warning

"Usman Akinyemi via GitGitGadget" <gitgitgadget@gmail.com> writes:

[jc: As Phillip is blamed for suggesting this addition, I added him to the recipient of this message.]

Show 10 quoted lines
> From: Usman Akinyemi <usmanakinyemi202@gmail.com>
>
> Modify the conflict resolution between tab-in-indent and
> indent-with-non-tab to issue a warning instead of terminating
> the operation with `die()`. Update the `git diff --check` test to
> capture and verify the warning message output.
>
> Suggested-by: Phillip Wood <phillip.wood123@gmail.com>
> Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com>
> ---

If the settings requires an impossible way to use whitespaces, the settings is buggy, and it generally would be better to correct the setting before moving on.

I am curious to know in what situations this new behaviour can be seen as an improvement. It may allow you to go on _without_ fixing such a broken setting, but how would it help the end user? If the user set both of these mutually-incompatible options A and B by mistake, but what the user really wanted to check for was A, picking just one of A or B arbitrarily and disabling it would not help, and disabling both would not help, either. But wouldn't the real source of the problem be that we are trying to demote die() to force the user to correct contradictiong setting into warning()?

Thanks.
Phillip Wood· Nov 14, 2024, 10:06 UTC · re: Junio C Hamano · lore

Re: [PATCH v2] diff: update conflict handling for whitespace to issue a warning

On 14/11/2024 02:15, Junio C Hamano wrote:
> "Usman Akinyemi via GitGitGadget" <gitgitgadget@gmail.com> writes:
> 
> [jc: As Phillip is blamed for suggesting this addition, I added him
> to the recipient of this message.]
Thanks
Show 6 quoted lines
>> From: Usman Akinyemi <usmanakinyemi202@gmail.com>
>>
>> Modify the conflict resolution between tab-in-indent and
>> indent-with-non-tab to issue a warning instead of terminating
>> the operation with `die()`. Update the `git diff --check` test to
>> capture and verify the warning message output.

Usman - when you're writing a commit message it is important to explain the reason for making the changes contained in the patch so others can understand why it is a good idea. In this case the idea is to avoid breaking "git diff" for everyone who clones a repository containing a .gitattributes file with bad whitespace attributes [1]. As I mentioned in [2] I think we only want to change the behavior when parsing whitespace attributes - we still want the other callers of parse_whitespace_rule() to die() so the user can fix their config or commandline. We can do that by adding a boolean parameter called "gentle" that determines whether we call warning() or die().

Best Wishes
Phillip

[1] https://lore.kernel.org/git/e4a70501-af2d-450a-a232-4c7952196a74@gmail.com [2] https://lore.kernel.org/git/3c081d3c-3f6f-45ff-b254-09f1cd6b7de5@gmail.com

Show 19 quoted lines
>> Suggested-by: Phillip Wood <phillip.wood123@gmail.com>
>> Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com>
>> ---
> 
> If the settings requires an impossible way to use whitespaces, the
> settings is buggy, and it generally would be better to correct the
> setting before moving on.
> 
> I am curious to know in what situations this new behaviour can be
> seen as an improvement.  It may allow you to go on _without_ fixing
> such a broken setting, but how would it help the end user?  If the
> user set both of these mutually-incompatible options A and B by
> mistake, but what the user really wanted to check for was A, picking
> just one of A or B arbitrarily and disabling it would not help, and
> disabling both would not help, either.  But wouldn't the real source
> of the problem be that we are trying to demote die() to force the
> user to correct contradictiong setting into warning()?
> 
> Thanks.
Usman Akinyemi· Nov 14, 2024, 11:29 UTC · re: Phillip Wood · lore

Re: [PATCH v2] diff: update conflict handling for whitespace to issue a warning

On Thu, Nov 14, 2024 at 5:06 AM Phillip Wood <phillip.wood123@gmail.com> wrote:
Show 8 quoted lines
>
> On 14/11/2024 02:15, Junio C Hamano wrote:
> > "Usman Akinyemi via GitGitGadget" <gitgitgadget@gmail.com> writes:
> >
> > [jc: As Phillip is blamed for suggesting this addition, I added him
> > to the recipient of this message.]
>
> Thanks
Hi Philip and Junio,
Show 18 quoted lines
>
> >> From: Usman Akinyemi <usmanakinyemi202@gmail.com>
> >>
> >> Modify the conflict resolution between tab-in-indent and
> >> indent-with-non-tab to issue a warning instead of terminating
> >> the operation with `die()`. Update the `git diff --check` test to
> >> capture and verify the warning message output.
>
> Usman - when you're writing a commit message it is important to explain
> the reason for making the changes contained in the patch so others can
> understand why it is a good idea. In this case the idea is to avoid
> breaking "git diff" for everyone who clones a repository containing a
> .gitattributes file with bad whitespace attributes [1]. As I mentioned
> in [2] I think we only want to change the behavior when parsing
> whitespace attributes - we still want the other callers of
> parse_whitespace_rule() to die() so the user can fix their config or
> commandline. We can do that by adding a boolean parameter called
> "gentle" that determines whether we call warning() or die().

I am very sorry for the confusion. I will take this into consideration next time and always give more explanation in commit messages.

I will make the necessary changes.

Thank you very much. Usman.

Show 30 quoted lines
>
> Best Wishes
>
> Phillip
>
> [1]
> https://lore.kernel.org/git/e4a70501-af2d-450a-a232-4c7952196a74@gmail.com
> [2]
> https://lore.kernel.org/git/3c081d3c-3f6f-45ff-b254-09f1cd6b7de5@gmail.com
>
> >> Suggested-by: Phillip Wood <phillip.wood123@gmail.com>
> >> Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com>
> >> ---
> >
> > If the settings requires an impossible way to use whitespaces, the
> > settings is buggy, and it generally would be better to correct the
> > setting before moving on.
> >
> > I am curious to know in what situations this new behaviour can be
> > seen as an improvement.  It may allow you to go on _without_ fixing
> > such a broken setting, but how would it help the end user?  If the
> > user set both of these mutually-incompatible options A and B by
> > mistake, but what the user really wanted to check for was A, picking
> > just one of A or B arbitrarily and disabling it would not help, and
> > disabling both would not help, either.  But wouldn't the real source
> > of the problem be that we are trying to demote die() to force the
> > user to correct contradictiong setting into warning()?
> >
> > Thanks.
>
Junio C Hamano· Nov 15, 2024, 00:11 UTC · re: Phillip Wood · lore

Re: [PATCH v2] diff: update conflict handling for whitespace to issue a warning

Phillip Wood <phillip.wood123@gmail.com> writes:
Show 6 quoted lines
> Usman - when you're writing a commit message it is important to
> explain the reason for making the changes contained in the patch so
> others can understand why it is a good idea. In this case the idea is
> to avoid breaking "git diff" for everyone who clones a repository
> containing a .gitattributes file with bad whitespace attributes
> [1].

Hmph, it would certainly be a problem, but the right solution is not to butcher Git, but to make it easier for the participants of such a project to know what is broken *and* what needs to be updated, to let them move forward, no?

Show 5 quoted lines
> As I mentioned in [2] I think we only want to change the behavior
> when parsing whitespace attributes - we still want the other callers
> of parse_whitespace_rule() to die() so the user can fix their config
> or commandline. We can do that by adding a boolean parameter called
> "gentle" that determines whether we call warning() or die().
I doubt that such a complexity is warranted.

It depends on the size of diff you are showing, but if it is large, then giving a small warning that gets buried in the large diff is a conter-productive way to encourage users to correct such broken setting. If it is small, then the damage may not be too bad, but still, we are showing what the user did not really request.

If we were to fix anything, it is to make sure that we die() before producing a single line of output. If you have a change to a path whose "type" is without such a misconfigured attribute, that sorts lexicographically earlier than another path with a change, with a conflicting whitespace attribute, I suspect that with the way the code is structured currently, we show the diff for the first path, before realizing that the second path has an issue and then die.

If we fix it, and then make sure that the die() message shows clearly what attribute setting we did not like, that would be sufficient to help users to locate the problem, fix it, and quickly move on, no?

Thanks.
Usman Akinyemi· Nov 18, 2024, 21:03 UTC · re: Junio C Hamano · lore

Re: [PATCH v2] diff: update conflict handling for whitespace to issue a warning

On Fri, Nov 15, 2024 at 12:11 AM Junio C Hamano <gitster@pobox.com> wrote:
Show 41 quoted lines
>
> Phillip Wood <phillip.wood123@gmail.com> writes:
>
> > Usman - when you're writing a commit message it is important to
> > explain the reason for making the changes contained in the patch so
> > others can understand why it is a good idea. In this case the idea is
> > to avoid breaking "git diff" for everyone who clones a repository
> > containing a .gitattributes file with bad whitespace attributes
> > [1].
>
> Hmph, it would certainly be a problem, but the right solution is not
> to butcher Git, but to make it easier for the participants of such a
> project to know what is broken *and* what needs to be updated, to let
> them move forward, no?
>
> > As I mentioned in [2] I think we only want to change the behavior
> > when parsing whitespace attributes - we still want the other callers
> > of parse_whitespace_rule() to die() so the user can fix their config
> > or commandline. We can do that by adding a boolean parameter called
> > "gentle" that determines whether we call warning() or die().
>
> I doubt that such a complexity is warranted.
>
> It depends on the size of diff you are showing, but if it is large,
> then giving a small warning that gets buried in the large diff is a
> conter-productive way to encourage users to correct such broken
> setting.  If it is small, then the damage may not be too bad, but
> still, we are showing what the user did not really request.
>
> If we were to fix anything, it is to make sure that we die() before
> producing a single line of output.  If you have a change to a path
> whose "type" is without such a misconfigured attribute, that sorts
> lexicographically earlier than another path with a change, with a
> conflicting whitespace attribute, I suspect that with the way the
> code is structured currently, we show the diff for the first path,
> before realizing that the second path has an issue and then die.
>
> If we fix it, and then make sure that the die() message shows
> clearly what attribute setting we did not like, that would be
> sufficient to help users to locate the problem, fix it, and quickly
> move on, no?
Hi Junio,

Thanks for the review. From what I understand from your comment, we should leave it the way it was which was die right ?

Thanks. Usman.

>
> Thanks.
Junio C Hamano· Nov 19, 2024, 00:36 UTC · re: Usman Akinyemi · lore

Re: [PATCH v2] diff: update conflict handling for whitespace to issue a warning

Usman Akinyemi <usmanakinyemi202@gmail.com> writes:
Show 17 quoted lines
> On Fri, Nov 15, 2024 at 12:11 AM Junio C Hamano <gitster@pobox.com> wrote:
>> ...
>> If we were to fix anything, it is to make sure that we die() before
>> producing a single line of output.  If you have a change to a path
>> whose "type" is without such a misconfigured attribute, that sorts
>> lexicographically earlier than another path with a change, with a
>> conflicting whitespace attribute, I suspect that with the way the
>> code is structured currently, we show the diff for the first path,
>> before realizing that the second path has an issue and then die.
>>
>> If we fix it, and then make sure that the die() message shows
>> clearly what attribute setting we did not like, that would be
>> sufficient to help users to locate the problem, fix it, and quickly
>> move on, no?
>
> Thanks for the review. From what I understand from your comment,
> we should leave it the way it was which was die right ?

Correct. I do not think replacing die() with warning() without doing anything else makes sense. Making sure that we detect the breakage before going half-way while producing a patch that touches many paths may improve the end-user experience, though.

Thanks.
Phillip Wood· Nov 19, 2024, 16:49 UTC · re: Junio C Hamano · lore

Re: [PATCH v2] diff: update conflict handling for whitespace to issue a warning

On 15/11/2024 00:11, Junio C Hamano wrote:
Show 13 quoted lines
> Phillip Wood <phillip.wood123@gmail.com> writes:
> 
>> Usman - when you're writing a commit message it is important to
>> explain the reason for making the changes contained in the patch so
>> others can understand why it is a good idea. In this case the idea is
>> to avoid breaking "git diff" for everyone who clones a repository
>> containing a .gitattributes file with bad whitespace attributes
>> [1].
> 
> Hmph, it would certainly be a problem, but the right solution is not
> to butcher Git, but to make it easier for the participants of such a
> project to know what is broken *and* what needs to be updated, to let
> them move forward, no?

Arguably yes, but that's not the approach we take when the attributes file is too large, a line in the file is is too long or the file contains a negative filename pattern. For those cases we print a warning and continue. The recently merged e36f009e69b (merge: replace atoi() with strtol_i() for marker size validation, 2024-10-24) followed suit and warns rather than dies for an invalid marker size. It would be nice to be consistent in the way we treat invalid attributes. Consistently dying and telling the user how to fix the problem would be a reasonable approach on the client side but I wonder if it could cause problems for forges running "git diff" and "git merge-tree" on a server though.

> [...]
> If we were to fix anything, it is to make sure that we die() before
> producing a single line of output.
That would certainly be a good idea
Best Wishes
Phillip
Junio C Hamano· Nov 20, 2024, 01:23 UTC · re: Phillip Wood · lore

Re: [PATCH v2] diff: update conflict handling for whitespace to issue a warning

Phillip Wood <phillip.wood123@gmail.com> writes:
Show 8 quoted lines
> Arguably yes, but that's not the approach we take when the attributes
> file is too large, a line in the file is is too long or the file
> contains a negative filename pattern. For those cases we print a
> warning and continue. The recently merged e36f009e69b (merge: replace
> atoi() with strtol_i() for marker size validation, 2024-10-24)
> followed suit and warns rather than dies for an invalid marker
> size. It would be nice to be consistent in the way we treat invalid
> attributes.

Arguably yes, but being careful when adding a new check and changing established behaviour, risking to break existing users, are different.

> Consistently dying and telling the user how to fix the
> problem would be a reasonable approach on the client side but I wonder
> if it could cause problems for forges running "git diff" and "git
> merge-tree" on a server though.

That's an interesting aspect. I wonder what happens when somebody pushes a project with a .gitattributes with such a conflicting setting to GitHub or GitLab.

Would that bring the world to its end ;-)?

← back to recent threads