threads / patch / 64920

patchwhitespace: symbolic links usually lack LF at the end

Subject: [PATCH] whitespace: symbolic links usually lack LF at the end

## tl;dr

7 messages between Feb 4, 2026 and Feb 6, 2026. Diffs are folded; open one to read it.

replies: 6people: 2as markdown or json

Junio C Hamano· Feb 4, 2026, 21:23 UTC · lore

For a patch that touches a symbolic link, it is perfectly normal that the payload ends with "\ No newline at end of file". The checks introduced recently to detect incomplete lines (i.e., a text file that lack the newline on its final line) should not trigger.

Disable the check early for symbolic links, both in "git apply" and "git diff" and test them.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 apply.c                    |  5 +++++
 diff.c                     | 20 ++++++++++++++++++--
 t/t4015-diff-whitespace.sh | 13 +++++++++++++
 t/t4124-apply-ws-rule.sh   | 31 +++++++++++++++++++++++++++++++
 4 files changed, 67 insertions(+), 2 deletions(-)
Show changes to 4 files +67 −2

apply.c, diff.c, t/t4015-diff-whitespace.sh, t/t4124-apply-ws-rule.sh

diff --git a/apply.c b/apply.c
index 3de4aa4d2e..581aafb8be 100644
--- a/apply.c
+++ b/apply.c
@@ -2193,6 +2193,11 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
 		patch->ws_rule = whitespace_rule(state->repo->index,
 						 patch->old_name);
 
+	/* being an incomplete line is the norm for a symbolic link */
+	if ((patch->old_mode && S_ISLNK(patch->old_mode)) ||
+	    (patch->new_mode && S_ISLNK(patch->new_mode)))
+		patch->ws_rule &= ~WS_INCOMPLETE_LINE;
+
 	patchsize = parse_single_patch(state,
 				       buffer + offset + hdrsize,
 				       size - offset - hdrsize,
diff --git a/diff.c b/diff.c
index a68ddd2168..2b37432eed 100644
--- a/diff.c
+++ b/diff.c
@@ -1837,6 +1837,7 @@ static void emit_rewrite_diff(const char *name_a,
 	const char *a_prefix, *b_prefix;
 	char *data_one, *data_two;
 	size_t size_one, size_two;
+	unsigned ws_rule;
 	struct emit_callback ecbdata;
 	struct strbuf out = STRBUF_INIT;
 
@@ -1859,9 +1860,14 @@ static void emit_rewrite_diff(const char *name_a,
 	size_one = fill_textconv(o->repo, textconv_one, one, &data_one);
 	size_two = fill_textconv(o->repo, textconv_two, two, &data_two);
 
+	ws_rule = whitespace_rule(o->repo->index, name_b);
+	if ((DIFF_FILE_VALID(one) && S_ISLNK(one->mode)) ||
+	    (DIFF_FILE_VALID(two) && S_ISLNK(two->mode)))
+		ws_rule &= ~WS_INCOMPLETE_LINE;
+
 	memset(&ecbdata, 0, sizeof(ecbdata));
 	ecbdata.color_diff = o->use_color;
-	ecbdata.ws_rule = whitespace_rule(o->repo->index, name_b);
+	ecbdata.ws_rule = ws_rule;
 	ecbdata.opt = o;
 	if (ecbdata.ws_rule & WS_BLANK_AT_EOF) {
 		mmfile_t mf1, mf2;
@@ -3764,6 +3770,7 @@ static void builtin_diff(const char *name_a,
 		xpparam_t xpp;
 		xdemitconf_t xecfg;
 		struct emit_callback ecbdata;
+		unsigned ws_rule;
 		const struct userdiff_funcname *pe;
 
 		if (must_show_header) {
@@ -3775,6 +3782,11 @@ static void builtin_diff(const char *name_a,
 		mf1.size = fill_textconv(o->repo, textconv_one, one, &mf1.ptr);
 		mf2.size = fill_textconv(o->repo, textconv_two, two, &mf2.ptr);
 
+		ws_rule = whitespace_rule(o->repo->index, name_b);
+		if ((DIFF_FILE_VALID(one) && S_ISLNK(one->mode)) ||
+		    (DIFF_FILE_VALID(two) && S_ISLNK(two->mode)))
+			ws_rule &= ~WS_INCOMPLETE_LINE;
+
 		pe = diff_funcname_pattern(o, one);
 		if (!pe)
 			pe = diff_funcname_pattern(o, two);
@@ -3786,7 +3798,7 @@ static void builtin_diff(const char *name_a,
 			lbl[0] = NULL;
 		ecbdata.label_path = lbl;
 		ecbdata.color_diff = o->use_color;
-		ecbdata.ws_rule = whitespace_rule(o->repo->index, name_b);
+		ecbdata.ws_rule = ws_rule;
 		if (ecbdata.ws_rule & WS_BLANK_AT_EOF)
 			check_blank_at_eof(&mf1, &mf2, &ecbdata);
 		ecbdata.opt = o;
@@ -3993,6 +4005,10 @@ static void builtin_checkdiff(const char *name_a, const char *name_b,
 	data.ws_rule = whitespace_rule(o->repo->index, attr_path);
 	data.conflict_marker_size = ll_merge_marker_size(o->repo->index, attr_path);
 
+	if ((DIFF_FILE_VALID(one) && S_ISLNK(one->mode)) ||
+	    (DIFF_FILE_VALID(two) && S_ISLNK(two->mode)))
+		data.ws_rule &= ~WS_INCOMPLETE_LINE;
+
 	if (fill_mmfile(o->repo, &mf1, one) < 0 ||
 	    fill_mmfile(o->repo, &mf2, two) < 0)
 		die("unable to read files to diff");
diff --git a/t/t4015-diff-whitespace.sh b/t/t4015-diff-whitespace.sh
index 3c8eb02e4f..903128f1d2 100755
--- a/t/t4015-diff-whitespace.sh
+++ b/t/t4015-diff-whitespace.sh
@@ -90,6 +90,19 @@ test_expect_success "new incomplete line in post-image" '
 	git -c core.whitespace=incomplete diff -R --check x
 '
 
+test_expect_success SYMLINKS "incomplete-line error is disabled for symlinks" '
+	test_when_finished "git reset --hard" &&
+	test_when_finished "rm -f mylink" &&
+	ln -s one mylink &&
+	git add mylink &&
+	ln -s -f two mylink &&
+
+	git -c core.whitespace=incomplete diff mylink &&
+	git -c core.whitespace=incomplete diff -R mylink &&
+	git -c core.whitespace=incomplete diff --check mylink &&
+	git -c core.whitespace=incomplete diff -R --check mylink
+'
+
 test_expect_success "Ray Lehtiniemi's example" '
 	cat <<-\EOF >x &&
 	do {
diff --git a/t/t4124-apply-ws-rule.sh b/t/t4124-apply-ws-rule.sh
index 115a0f8579..f48d8bbf49 100755
--- a/t/t4124-apply-ws-rule.sh
+++ b/t/t4124-apply-ws-rule.sh
@@ -743,4 +743,35 @@ test_expect_success 'incomplete line modified at the end (error)' '
 	test_cmp sample target
 '
 
+test_expect_success "incomplete-line error is disabled for symlinks" '
+	test_when_finished "git reset" &&
+	test_when_finished "rm -f patch.txt" &&
+	oneblob=$(printf "one" | git hash-object --stdin -w -t blob) &&
+	twoblob=$(printf "two" | git hash-object --stdin -w -t blob) &&
+
+	git update-index --add --cacheinfo "120000,$oneblob,mylink" &&
+
+	oneshort=$(git rev-parse --short $oneblob) &&
+	twoshort=$(git rev-parse --short $twoblob) &&
+	cat >patch.txt <<-EOF &&
+	diff --git a/mylink b/mylink
+	index $oneshort..$twoshort 120000
+	--- a/mylink
+	+++ b/mylink
+	@@ -1 +1 @@
+	-one
+	\ No newline at end of file
+	+two
+	\ No newline at end of file
+	EOF
+
+	git -c core.whitespace=incomplete apply --cached --check patch.txt &&
+
+	git -c core.whitespace=incomplete apply --cached --whitespace=error \
+		patch.txt &&
+
+	git -c core.whitespace=incomplete apply --cached -R --whitespace=error \
+		patch.txt
+'
+
 test_done
-- 
2.53.0-169-ga09cd4eb64
Patrick Steinhardt· Feb 5, 2026, 12:21 UTC · re: Junio C Hamano · lore

Re: [PATCH] whitespace: symbolic links usually lack LF at the end

On Wed, Feb 04, 2026 at 01:23:06PM -0800, Junio C Hamano wrote:
Show 16 quoted lines
> diff --git a/apply.c b/apply.c
> index 3de4aa4d2e..581aafb8be 100644
> --- a/apply.c
> +++ b/apply.c
> @@ -2193,6 +2193,11 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
>  		patch->ws_rule = whitespace_rule(state->repo->index,
>  						 patch->old_name);
>  
> +	/* being an incomplete line is the norm for a symbolic link */
> +	if ((patch->old_mode && S_ISLNK(patch->old_mode)) ||
> +	    (patch->new_mode && S_ISLNK(patch->new_mode)))
> +		patch->ws_rule &= ~WS_INCOMPLETE_LINE;
> +
>  	patchsize = parse_single_patch(state,
>  				       buffer + offset + hdrsize,
>  				       size - offset - hdrsize,

Hm. Wouldn't that mean that we disable this check for both sides of a diff if either of them is a symlink? That's typically fine, but if the diff also contains a mode change it might not be.

I'd suggest that we only disable this check in case either:
  - One side doesn't exist, the other is a symbolic link.
  - Both sides are a symbolic link.

Another question is whether we support symref targets that end in a newline. I guess the answer is going to be some form of "yes", and in that case we could of course loose some information. But honestly, this is so much of an edge case that I don't really worry about it too much.

Thanks!
Patrick
Junio C Hamano· Feb 5, 2026, 15:50 UTC · re: Patrick Steinhardt · lore

Re: [PATCH] whitespace: symbolic links usually lack LF at the end

Patrick Steinhardt <ps@pks.im> writes:
Show 5 quoted lines
> I'd suggest that we only disable this check in case either:
>
>   - One side doesn't exist, the other is a symbolic link.
>
>   - Both sides are a symbolic link.

Hmm. That is indeed a thoguht. But we do not want to complain in text-to-symlink transition that postimage lacks the terminating LF, so the above rules may be a good start but will need further tweaking, I am afraid.

> Another question is whether we support symref targets that end in a
> newline. I guess the answer is going to be some form of "yes", and in
> that case we could of course loose some information. But honestly, this
> is so much of an edge case that I don't really worry about it too much.

Do we track, apply and diff any symrefs? I thought that we do not touch anything inside .git/ and symrefs live inside .git/refs/ (except for .git/HEAD)?

Patrick Steinhardt· Feb 6, 2026, 06:31 UTC · re: Junio C Hamano · lore

Re: [PATCH] whitespace: symbolic links usually lack LF at the end

On Thu, Feb 05, 2026 at 07:50:55AM -0800, Junio C Hamano wrote:
Show 12 quoted lines
> Patrick Steinhardt <ps@pks.im> writes:
> 
> > I'd suggest that we only disable this check in case either:
> >
> >   - One side doesn't exist, the other is a symbolic link.
> >
> >   - Both sides are a symbolic link.
> 
> Hmm.  That is indeed a thoguht.  But we do not want to complain in
> text-to-symlink transition that postimage lacks the terminating LF,
> so the above rules may be a good start but will need further
> tweaking, I am afraid.
Ah, right. Only the other way around, when converting from LF to text.
Show 8 quoted lines
> > Another question is whether we support symref targets that end in a
> > newline. I guess the answer is going to be some form of "yes", and in
> > that case we could of course loose some information. But honestly, this
> > is so much of an edge case that I don't really worry about it too much.
> 
> Do we track, apply and diff any symrefs?  I thought that we do not
> touch anything inside .git/ and symrefs live inside .git/refs/
> (except for .git/HEAD)?

Eh, I didn't mean symrefs here, but symbolic links :) Tools like ln(1) seem to strip trailing newlines, but if you try hard enough you'll probably be able to create symlinks that have a target with trailing newline.

Patrick
Junio C Hamano· Feb 6, 2026, 16:25 UTC · re: Patrick Steinhardt · lore

Re: [PATCH] whitespace: symbolic links usually lack LF at the end

Patrick Steinhardt <ps@pks.im> writes:
Show 15 quoted lines
> On Thu, Feb 05, 2026 at 07:50:55AM -0800, Junio C Hamano wrote:
>> Patrick Steinhardt <ps@pks.im> writes:
>> 
>> > I'd suggest that we only disable this check in case either:
>> >
>> >   - One side doesn't exist, the other is a symbolic link.
>> >
>> >   - Both sides are a symbolic link.
>> 
>> Hmm.  That is indeed a thoguht.  But we do not want to complain in
>> text-to-symlink transition that postimage lacks the terminating LF,
>> so the above rules may be a good start but will need further
>> tweaking, I am afraid.
>
> Ah, right. Only the other way around, when converting from LF to text.

I've decided to use the "disable only when the side that appears postimage (taking --reverse option into account) is a symbolic link" rule.

Strictly speaking, "diff" (but not "apply") has wsErrorHighlight feature where it can be configured to complain about whitespace glitches in both pre- and postimage, so it is technically not sufficient, but it is not worth supporting diff.wsErrorHighlight that is set to anything but "new" (or "default" which is its synonym).

> Eh, I didn't mean symrefs here, but symbolic links :) Tools like ln(1)
> seem to strip trailing newlines, but if you try hard enough you'll
> probably be able to create symlinks that have a target with trailing
> newline.

Yes, as you can create a file whose name contains a newline, a name that ends in a newline is a valid filename that "ln -s" may want to support. I am reasonably sure that we do not want to flag such a symbolic link as whitespace damaged.

Patrick Steinhardt· Feb 6, 2026, 16:58 UTC · re: Junio C Hamano · lore

Re: [PATCH] whitespace: symbolic links usually lack LF at the end

On Fri, Feb 06, 2026 at 08:25:42AM -0800, Junio C Hamano wrote:
Show 28 quoted lines
> Patrick Steinhardt <ps@pks.im> writes:
> 
> > On Thu, Feb 05, 2026 at 07:50:55AM -0800, Junio C Hamano wrote:
> >> Patrick Steinhardt <ps@pks.im> writes:
> >> 
> >> > I'd suggest that we only disable this check in case either:
> >> >
> >> >   - One side doesn't exist, the other is a symbolic link.
> >> >
> >> >   - Both sides are a symbolic link.
> >> 
> >> Hmm.  That is indeed a thoguht.  But we do not want to complain in
> >> text-to-symlink transition that postimage lacks the terminating LF,
> >> so the above rules may be a good start but will need further
> >> tweaking, I am afraid.
> >
> > Ah, right. Only the other way around, when converting from LF to text.
> 
> I've decided to use the "disable only when the side that appears
> postimage (taking --reverse option into account) is a symbolic link"
> rule.
> 
> Strictly speaking, "diff" (but not "apply") has wsErrorHighlight
> feature where it can be configured to complain about whitespace
> glitches in both pre- and postimage, so it is technically not
> sufficient, but it is not worth supporting diff.wsErrorHighlight
> that is set to anything but "new" (or "default" which is its
> synonym).
Sounds sensible.
Show 9 quoted lines
> > Eh, I didn't mean symrefs here, but symbolic links :) Tools like ln(1)
> > seem to strip trailing newlines, but if you try hard enough you'll
> > probably be able to create symlinks that have a target with trailing
> > newline.
> 
> Yes, as you can create a file whose name contains a newline, a name
> that ends in a newline is a valid filename that "ln -s" may want to
> support.  I am reasonably sure that we do not want to flag such a
> symbolic link as whitespace damaged.

Yeah, we certainly don't want that. The remark was rather about a reader not being able to discern those two cases (does or does not end in a newline) anymore. Or would they?

Patrick
Junio C Hamano· Feb 6, 2026, 16:25 UTC · re: Junio C Hamano · lore

[PATCH v2] whitespace: symbolic links usually lack LF at the end

For a patch that touches a symbolic link, it is perfectly normal that the contents ends with "\ No newline at end of file". The checks introduced recently to detect incomplete lines (i.e., a text file that lack the newline on its final line) should not trigger.

Disable the check early for symbolic links, both in "git apply" and "git diff" and test them. For "git apply", we check only when the postimage is a symbolic link regardless of the preimage, and we only care about preimage when applying in reverse. Similarly, "git diff" would warn only when the postimage is a symbolic link, or the preimage when running "git diff -R".

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 * v1 used to disable whitespace=incomplete-line when either side of
   comparison is a symbolic link; this iteration only cares about
   the case where the postimage is a symbolic link.  If you turn
   what used to be a symlink into a text file, and if incomplete
   line detection is turned on, you would want to make sure that the
   resulting text file is without an incomplete line.
 apply.c                    | 20 +++++++++
 diff.c                     | 22 +++++++++-
 t/t4015-diff-whitespace.sh | 26 ++++++++++++
 t/t4124-apply-ws-rule.sh   | 86 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 152 insertions(+), 2 deletions(-)
Show changes to 4 files +152 −2

apply.c, diff.c, t/t4015-diff-whitespace.sh, t/t4124-apply-ws-rule.sh

diff --git a/apply.c b/apply.c
index c9fb45247d..f01204d15b 100644
--- a/apply.c
+++ b/apply.c
@@ -1725,6 +1725,26 @@ static int parse_fragment(struct apply_state *state,
 	unsigned long oldlines, newlines;
 	unsigned long leading, trailing;
 
+	/* do not complain a symbolic link being an incomplete line */
+	if (patch->ws_rule & WS_INCOMPLETE_LINE) {
+		/*
+		 * We want to figure out if the postimage is a
+		 * symbolic link when applying the patch normally, or
+		 * if the preimage is a symbolic link when applying
+		 * the patch in reverse.  A normal patch only has
+		 * old_mode without new_mode.  If it changes the
+		 * filemode, new_mode has value, which is different
+		 * from old_mode.
+		 */
+		unsigned mode = (state->apply_in_reverse
+				 ? patch->old_mode
+				 : patch->new_mode
+				 ? patch->new_mode
+				 : patch->old_mode);
+		if (mode && S_ISLNK(mode))
+			patch->ws_rule &= ~WS_INCOMPLETE_LINE;
+	}
+
 	offset = parse_fragment_header(line, len, fragment);
 	if (offset < 0)
 		return -1;
diff --git a/diff.c b/diff.c
index 7b7cd50dc2..9e4b92ed69 100644
--- a/diff.c
+++ b/diff.c
@@ -1834,6 +1834,7 @@ static void emit_rewrite_diff(const char *name_a,
 	const char *a_prefix, *b_prefix;
 	char *data_one, *data_two;
 	size_t size_one, size_two;
+	unsigned ws_rule;
 	struct emit_callback ecbdata;
 	struct strbuf out = STRBUF_INIT;
 
@@ -1856,9 +1857,15 @@ static void emit_rewrite_diff(const char *name_a,
 	size_one = fill_textconv(o->repo, textconv_one, one, &data_one);
 	size_two = fill_textconv(o->repo, textconv_two, two, &data_two);
 
+	ws_rule = whitespace_rule(o->repo->index, name_b);
+
+	/* symlink being an incomplete line is not a news */
+	if (DIFF_FILE_VALID(two) && S_ISLNK(two->mode))
+		ws_rule &= ~WS_INCOMPLETE_LINE;
+
 	memset(&ecbdata, 0, sizeof(ecbdata));
 	ecbdata.color_diff = o->use_color;
-	ecbdata.ws_rule = whitespace_rule(o->repo->index, name_b);
+	ecbdata.ws_rule = ws_rule;
 	ecbdata.opt = o;
 	if (ecbdata.ws_rule & WS_BLANK_AT_EOF) {
 		mmfile_t mf1, mf2;
@@ -3762,6 +3769,7 @@ static void builtin_diff(const char *name_a,
 		xpparam_t xpp;
 		xdemitconf_t xecfg;
 		struct emit_callback ecbdata;
+		unsigned ws_rule;
 		const struct userdiff_funcname *pe;
 
 		if (must_show_header) {
@@ -3773,6 +3781,12 @@ static void builtin_diff(const char *name_a,
 		mf1.size = fill_textconv(o->repo, textconv_one, one, &mf1.ptr);
 		mf2.size = fill_textconv(o->repo, textconv_two, two, &mf2.ptr);
 
+		ws_rule = whitespace_rule(o->repo->index, name_b);
+
+		/* symlink being an incomplete line is not a news */
+		if (DIFF_FILE_VALID(two) && S_ISLNK(two->mode))
+			ws_rule &= ~WS_INCOMPLETE_LINE;
+
 		pe = diff_funcname_pattern(o, one);
 		if (!pe)
 			pe = diff_funcname_pattern(o, two);
@@ -3784,7 +3798,7 @@ static void builtin_diff(const char *name_a,
 			lbl[0] = NULL;
 		ecbdata.label_path = lbl;
 		ecbdata.color_diff = o->use_color;
-		ecbdata.ws_rule = whitespace_rule(o->repo->index, name_b);
+		ecbdata.ws_rule = ws_rule;
 		if (ecbdata.ws_rule & WS_BLANK_AT_EOF)
 			check_blank_at_eof(&mf1, &mf2, &ecbdata);
 		ecbdata.opt = o;
@@ -3991,6 +4005,10 @@ static void builtin_checkdiff(const char *name_a, const char *name_b,
 	data.ws_rule = whitespace_rule(o->repo->index, attr_path);
 	data.conflict_marker_size = ll_merge_marker_size(o->repo->index, attr_path);
 
+	/* symlink being an incomplete line is not a news */
+	if (DIFF_FILE_VALID(two) && S_ISLNK(two->mode))
+		data.ws_rule &= ~WS_INCOMPLETE_LINE;
+
 	if (fill_mmfile(o->repo, &mf1, one) < 0 ||
 	    fill_mmfile(o->repo, &mf2, two) < 0)
 		die("unable to read files to diff");
diff --git a/t/t4015-diff-whitespace.sh b/t/t4015-diff-whitespace.sh
index 3c8eb02e4f..b691d29479 100755
--- a/t/t4015-diff-whitespace.sh
+++ b/t/t4015-diff-whitespace.sh
@@ -90,6 +90,32 @@ test_expect_success "new incomplete line in post-image" '
 	git -c core.whitespace=incomplete diff -R --check x
 '
 
+test_expect_success SYMLINKS "incomplete-line error is disabled for symlinks" '
+	test_when_finished "git reset --hard" &&
+	test_when_finished "rm -f mylink" &&
+
+	# a regular file with an incomplete line
+	printf "%s" one >mylink &&
+	git add mylink &&
+
+	# a symbolic link
+	rm mylink &&
+	ln -s two mylink &&
+
+	git -c diff.color=always -c core.whitespace=incomplete \
+		diff mylink >forward.raw &&
+	test_decode_color >forward <forward.raw &&
+	test_grep ! "<BRED>\\\\ No newline at end of file<RESET>" forward &&
+
+	git -c diff.color=always -c core.whitespace=incomplete \
+		diff -R mylink >reverse.raw &&
+	test_decode_color >reverse <reverse.raw &&
+	test_grep "<BRED>\\\\ No newline at end of file<RESET>" reverse &&
+
+	git -c core.whitespace=incomplete diff --check mylink &&
+	test_must_fail git -c core.whitespace=incomplete diff --check -R mylink
+'
+
 test_expect_success "Ray Lehtiniemi's example" '
 	cat <<-\EOF >x &&
 	do {
diff --git a/t/t4124-apply-ws-rule.sh b/t/t4124-apply-ws-rule.sh
index 115a0f8579..29ea7d4268 100755
--- a/t/t4124-apply-ws-rule.sh
+++ b/t/t4124-apply-ws-rule.sh
@@ -743,4 +743,90 @@ test_expect_success 'incomplete line modified at the end (error)' '
 	test_cmp sample target
 '
 
+test_expect_success "incomplete-line error is disabled for symlinks" '
+	test_when_finished "git reset" &&
+	test_when_finished "rm -f patch.txt" &&
+	oneblob=$(printf "one" | git hash-object --stdin -w -t blob) &&
+	twoblob=$(printf "two" | git hash-object --stdin -w -t blob) &&
+
+	oneshort=$(git rev-parse --short $oneblob) &&
+	twoshort=$(git rev-parse --short $twoblob) &&
+
+	cat >patch0.txt <<-EOF &&
+	diff --git a/mylink b/mylink
+	index $oneshort..$twoshort 120000
+	--- a/mylink
+	+++ b/mylink
+	@@ -1 +1 @@
+	-one
+	\ No newline at end of file
+	+two
+	\ No newline at end of file
+	EOF
+
+	# the index has the preimage symlink
+	git update-index --add --cacheinfo "120000,$oneblob,mylink" &&
+
+	# check the patch going forward and reverse
+	git -c core.whitespace=incomplete apply --cached --check \
+		--whitespace=error patch0.txt &&
+
+	git update-index --add --cacheinfo "120000,$twoblob,mylink" &&
+	git -c core.whitespace=incomplete apply --cached --check \
+		--whitespace=error -R patch0.txt &&
+
+	# the patch turns it into the postimage symlink
+	git update-index --add --cacheinfo "120000,$oneblob,mylink" &&
+	git -c core.whitespace=incomplete apply --cached --whitespace=error \
+		patch0.txt &&
+
+	# and then back.
+	git -c core.whitespace=incomplete apply --cached -R --whitespace=error \
+		patch0.txt &&
+
+	# a text file turns into a symlink
+	cat >patch1.txt <<-EOF &&
+	diff --git a/mylink b/mylink
+	deleted file mode 100644
+	index $oneshort..0000000
+	--- a/mylink
+	+++ /dev/null
+	@@ -1 +0,0 @@
+	-one
+	\ No newline at end of file
+	diff --git a/mylink b/mylink
+	new file mode 120000
+	index 0000000..$twoshort
+	--- /dev/null
+	+++ b/mylink
+	@@ -0,0 +1 @@
+	+two
+	\ No newline at end of file
+	EOF
+
+	# the index has the preimage text
+	git update-index --cacheinfo "100644,$oneblob,mylink" &&
+
+	# check
+	git -c core.whitespace=incomplete apply --cached \
+		--check --whitespace=error patch1.txt &&
+
+	# reverse, leaving an incomplete text file, should error
+	git update-index --cacheinfo "120000,$twoblob,mylink" &&
+	test_must_fail git -c core.whitespace=incomplete \
+		apply --cached --check --whitespace=error -R patch1.txt &&
+
+	# apply to create a symbolic link
+	git update-index --cacheinfo "100644,$oneblob,mylink" &&
+	git -c core.whitespace=incomplete apply --cached --whitespace=error \
+		patch1.txt &&
+
+	# turning it back into an incomplete text file is an error
+	test_must_fail git -c core.whitespace=incomplete \
+		apply --cached --whitespace=error -R patch1.txt
+
+
+
+'
+
 test_done

Interdiff against v1:
  diff --git a/apply.c b/apply.c
  index 81ea174637..f01204d15b 100644
  --- a/apply.c
  +++ b/apply.c
  @@ -1725,6 +1725,26 @@ static int parse_fragment(struct apply_state *state,
   	unsigned long oldlines, newlines;
   	unsigned long leading, trailing;
   
  +	/* do not complain a symbolic link being an incomplete line */
  +	if (patch->ws_rule & WS_INCOMPLETE_LINE) {
  +		/*
  +		 * We want to figure out if the postimage is a
  +		 * symbolic link when applying the patch normally, or
  +		 * if the preimage is a symbolic link when applying
  +		 * the patch in reverse.  A normal patch only has
  +		 * old_mode without new_mode.  If it changes the
  +		 * filemode, new_mode has value, which is different
  +		 * from old_mode.
  +		 */
  +		unsigned mode = (state->apply_in_reverse
  +				 ? patch->old_mode
  +				 : patch->new_mode
  +				 ? patch->new_mode
  +				 : patch->old_mode);
  +		if (mode && S_ISLNK(mode))
  +			patch->ws_rule &= ~WS_INCOMPLETE_LINE;
  +	}
  +
   	offset = parse_fragment_header(line, len, fragment);
   	if (offset < 0)
   		return -1;
  @@ -2193,11 +2213,6 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
   		patch->ws_rule = whitespace_rule(state->repo->index,
   						 patch->old_name);
   
  -	/* being an incomplete line is the norm for a symbolic link */
  -	if ((patch->old_mode && S_ISLNK(patch->old_mode)) ||
  -	    (patch->new_mode && S_ISLNK(patch->new_mode)))
  -		patch->ws_rule &= ~WS_INCOMPLETE_LINE;
  -
   	patchsize = parse_single_patch(state,
   				       buffer + offset + hdrsize,
   				       size - offset - hdrsize,
  diff --git a/diff.c b/diff.c
  index c53aebc4a4..9e4b92ed69 100644
  --- a/diff.c
  +++ b/diff.c
  @@ -1858,8 +1858,9 @@ static void emit_rewrite_diff(const char *name_a,
   	size_two = fill_textconv(o->repo, textconv_two, two, &data_two);
   
   	ws_rule = whitespace_rule(o->repo->index, name_b);
  -	if ((DIFF_FILE_VALID(one) && S_ISLNK(one->mode)) ||
  -	    (DIFF_FILE_VALID(two) && S_ISLNK(two->mode)))
  +
  +	/* symlink being an incomplete line is not a news */
  +	if (DIFF_FILE_VALID(two) && S_ISLNK(two->mode))
   		ws_rule &= ~WS_INCOMPLETE_LINE;
   
   	memset(&ecbdata, 0, sizeof(ecbdata));
  @@ -3781,8 +3782,9 @@ static void builtin_diff(const char *name_a,
   		mf2.size = fill_textconv(o->repo, textconv_two, two, &mf2.ptr);
   
   		ws_rule = whitespace_rule(o->repo->index, name_b);
  -		if ((DIFF_FILE_VALID(one) && S_ISLNK(one->mode)) ||
  -		    (DIFF_FILE_VALID(two) && S_ISLNK(two->mode)))
  +
  +		/* symlink being an incomplete line is not a news */
  +		if (DIFF_FILE_VALID(two) && S_ISLNK(two->mode))
   			ws_rule &= ~WS_INCOMPLETE_LINE;
   
   		pe = diff_funcname_pattern(o, one);
  @@ -4003,8 +4005,8 @@ static void builtin_checkdiff(const char *name_a, const char *name_b,
   	data.ws_rule = whitespace_rule(o->repo->index, attr_path);
   	data.conflict_marker_size = ll_merge_marker_size(o->repo->index, attr_path);
   
  -	if ((DIFF_FILE_VALID(one) && S_ISLNK(one->mode)) ||
  -	    (DIFF_FILE_VALID(two) && S_ISLNK(two->mode)))
  +	/* symlink being an incomplete line is not a news */
  +	if (DIFF_FILE_VALID(two) && S_ISLNK(two->mode))
   		data.ws_rule &= ~WS_INCOMPLETE_LINE;
   
   	if (fill_mmfile(o->repo, &mf1, one) < 0 ||
  diff --git a/t/t4015-diff-whitespace.sh b/t/t4015-diff-whitespace.sh
  index 903128f1d2..b691d29479 100755
  --- a/t/t4015-diff-whitespace.sh
  +++ b/t/t4015-diff-whitespace.sh
  @@ -93,14 +93,27 @@ test_expect_success "new incomplete line in post-image" '
   test_expect_success SYMLINKS "incomplete-line error is disabled for symlinks" '
   	test_when_finished "git reset --hard" &&
   	test_when_finished "rm -f mylink" &&
  -	ln -s one mylink &&
  +
  +	# a regular file with an incomplete line
  +	printf "%s" one >mylink &&
   	git add mylink &&
  -	ln -s -f two mylink &&
   
  -	git -c core.whitespace=incomplete diff mylink &&
  -	git -c core.whitespace=incomplete diff -R mylink &&
  +	# a symbolic link
  +	rm mylink &&
  +	ln -s two mylink &&
  +
  +	git -c diff.color=always -c core.whitespace=incomplete \
  +		diff mylink >forward.raw &&
  +	test_decode_color >forward <forward.raw &&
  +	test_grep ! "<BRED>\\\\ No newline at end of file<RESET>" forward &&
  +
  +	git -c diff.color=always -c core.whitespace=incomplete \
  +		diff -R mylink >reverse.raw &&
  +	test_decode_color >reverse <reverse.raw &&
  +	test_grep "<BRED>\\\\ No newline at end of file<RESET>" reverse &&
  +
   	git -c core.whitespace=incomplete diff --check mylink &&
  -	git -c core.whitespace=incomplete diff -R --check mylink
  +	test_must_fail git -c core.whitespace=incomplete diff --check -R mylink
   '
   
   test_expect_success "Ray Lehtiniemi's example" '
  diff --git a/t/t4124-apply-ws-rule.sh b/t/t4124-apply-ws-rule.sh
  index f48d8bbf49..29ea7d4268 100755
  --- a/t/t4124-apply-ws-rule.sh
  +++ b/t/t4124-apply-ws-rule.sh
  @@ -749,11 +749,10 @@ test_expect_success "incomplete-line error is disabled for symlinks" '
   	oneblob=$(printf "one" | git hash-object --stdin -w -t blob) &&
   	twoblob=$(printf "two" | git hash-object --stdin -w -t blob) &&
   
  -	git update-index --add --cacheinfo "120000,$oneblob,mylink" &&
  -
   	oneshort=$(git rev-parse --short $oneblob) &&
   	twoshort=$(git rev-parse --short $twoblob) &&
  -	cat >patch.txt <<-EOF &&
  +
  +	cat >patch0.txt <<-EOF &&
   	diff --git a/mylink b/mylink
   	index $oneshort..$twoshort 120000
   	--- a/mylink
  @@ -765,13 +764,69 @@ test_expect_success "incomplete-line error is disabled for symlinks" '
   	\ No newline at end of file
   	EOF
   
  -	git -c core.whitespace=incomplete apply --cached --check patch.txt &&
  +	# the index has the preimage symlink
  +	git update-index --add --cacheinfo "120000,$oneblob,mylink" &&
   
  +	# check the patch going forward and reverse
  +	git -c core.whitespace=incomplete apply --cached --check \
  +		--whitespace=error patch0.txt &&
  +
  +	git update-index --add --cacheinfo "120000,$twoblob,mylink" &&
  +	git -c core.whitespace=incomplete apply --cached --check \
  +		--whitespace=error -R patch0.txt &&
  +
  +	# the patch turns it into the postimage symlink
  +	git update-index --add --cacheinfo "120000,$oneblob,mylink" &&
   	git -c core.whitespace=incomplete apply --cached --whitespace=error \
  -		patch.txt &&
  +		patch0.txt &&
   
  +	# and then back.
   	git -c core.whitespace=incomplete apply --cached -R --whitespace=error \
  -		patch.txt
  +		patch0.txt &&
  +
  +	# a text file turns into a symlink
  +	cat >patch1.txt <<-EOF &&
  +	diff --git a/mylink b/mylink
  +	deleted file mode 100644
  +	index $oneshort..0000000
  +	--- a/mylink
  +	+++ /dev/null
  +	@@ -1 +0,0 @@
  +	-one
  +	\ No newline at end of file
  +	diff --git a/mylink b/mylink
  +	new file mode 120000
  +	index 0000000..$twoshort
  +	--- /dev/null
  +	+++ b/mylink
  +	@@ -0,0 +1 @@
  +	+two
  +	\ No newline at end of file
  +	EOF
  +
  +	# the index has the preimage text
  +	git update-index --cacheinfo "100644,$oneblob,mylink" &&
  +
  +	# check
  +	git -c core.whitespace=incomplete apply --cached \
  +		--check --whitespace=error patch1.txt &&
  +
  +	# reverse, leaving an incomplete text file, should error
  +	git update-index --cacheinfo "120000,$twoblob,mylink" &&
  +	test_must_fail git -c core.whitespace=incomplete \
  +		apply --cached --check --whitespace=error -R patch1.txt &&
  +
  +	# apply to create a symbolic link
  +	git update-index --cacheinfo "100644,$oneblob,mylink" &&
  +	git -c core.whitespace=incomplete apply --cached --whitespace=error \
  +		patch1.txt &&
  +
  +	# turning it back into an incomplete text file is an error
  +	test_must_fail git -c core.whitespace=incomplete \
  +		apply --cached --whitespace=error -R patch1.txt
  +
  +
  +
   '
   
   test_done
-- 
2.53.0-179-g8fac285501

← back to recent threads