Skip to contentSkip to conversation

fixup! mergetool: add automerge configuration

PATCH Superseded David Aguilar 5 participants 24 messages Jan 9, 2021 – Jan 29, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fixup! mergetool: add automerge configuration

The use of "\r\?" in sed is not portable to macOS and possibly other unix flavors.

Replace "\r" with a substituted variable that contains "\r". Replace "\?" with "\{0,1\}".

---
This is based on top of fc/mergetool-automerge and can be squashed in (with the addition of my sign-off) if desired.

Let me know if you'd prefer a separate patch. I figured we'd want a squash to preserve bisectability.

 git-mergetool.sh | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
git-mergetool.sh +10 -3
index a44afd3822..12c3e83aa7 100755
@@ -243,9 +243,16 @@ auto_merge () {
 	git merge-file --diff3 --marker-size=7 -q -p "$LOCAL" "$BASE" "$REMOTE" >"$DIFF3"
 	if test -s "$DIFF3"
 	then
-		sed -e '/^<<<<<<< /,/^||||||| /d' -e '/^=======\r\?$/,/^>>>>>>> /d' "$DIFF3" >"$BASE"
-		sed -e '/^||||||| /,/^>>>>>>> /d' -e '/^<<<<<<< /d' "$DIFF3" >"$LOCAL"
-		sed -e '/^<<<<<<< /,/^=======\r\?$/d' -e '/^>>>>>>> /d' "$DIFF3" >"$REMOTE"
+		cr=$(printf '\x0d')
+		sed -e '/^<<<<<<< /,/^||||||| /d' \
+			-e "/^=======$cr\{0,1\}$/,/^>>>>>>> /d" \
+			"$DIFF3" >"$BASE"
+		sed -e '/^||||||| /,/^>>>>>>> /d' \
+			-e '/^<<<<<<< /d' \
+			"$DIFF3" >"$LOCAL"
+		sed -e "/^<<<<<<< /,/^=======$cr\{0,1\}$/d" \
+			-e '/^>>>>>>> /d' \
+			"$DIFF3" >"$REMOTE"
 	fi
 	rm -- "$DIFF3"
 }
Signature · 1 line
2.30.0.rc1.6.g2bc636d1d6
#1 lore raw
Quoted 5 lines · David Aguilar

The use of "\r\?" in sed is not portable to macOS and possibly other unix flavors.

Replace "\r" with a substituted variable that contains "\r". Replace "\?" with "\{0,1\}".

Ah, yes, this is true. The statement about "\r" is also true for Linux with POSIXLY_CORRECT, IIRC.

Quoted 16 lines

---
This is based on top of fc/mergetool-automerge and can be squashed in (with the addition of my sign-off) if desired.

Let me know if you'd prefer a separate patch. I figured we'd want a squash to preserve bisectability.

 git-mergetool.sh | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
git-mergetool.sh +0 -0
index a44afd3822..12c3e83aa7 100755

git-mergetool.sh auto_merge () { · line 246

Quoted 7 lines
git-mergetool.sh +1 -3
@@ -243,9 +243,16 @@ auto_merge () {
 	git merge-file --diff3 --marker-size=7 -q -p "$LOCAL" "$BASE" "$REMOTE" >"$DIFF3"
 	if test -s "$DIFF3"
 	then
-		sed -e '/^<<<<<<< /,/^||||||| /d' -e '/^=======\r\?$/,/^>>>>>>> /d' "$DIFF3" >"$BASE"
-		sed -e '/^||||||| /,/^>>>>>>> /d' -e '/^<<<<<<< /d' "$DIFF3" >"$LOCAL"
-		sed -e '/^<<<<<<< /,/^=======\r\?$/d' -e '/^>>>>>>> /d' "$DIFF3" >"$REMOTE"
+		cr=$(printf '\x0d')

Unfortunately, printf is not specified by POSIX to take hex escapes, so this, too is nonportable. We are unfortunately allowed to use only octal escapes (yuck). However, we can write this:

  cr=$(printf '\r')

or

  cr=$(printf '\015')

I think the former is clearer, since that's what we were writing before.

Signature · 2 lines
brian m. carlson (he/him or they/them)
Houston, Texas, US
#2 lore raw

PATCH v2 1/1 fixup! mergetool: add automerge configuration

"\r\?" in sed is not portable to macOS and possibly others. "\r" is not valid on Linux with POSIXLY_CORRECT.

Replace "\r" with a substituted variable that contains "\r". Replace "\?" with "\{0,1\}".

---
This is based on top of fc/mergetool-automerge and can be squashed in (with the addition of my sign-off) if desired.

Changes since last time:
- printf '\r' instead of printf '\x0d'
- The commit message now mentions POSIXLY_CORRECT.

 git-mergetool.sh | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
git-mergetool.sh +10 -3
index a44afd3822..9029a79431 100755
@@ -243,9 +243,16 @@ auto_merge () {
 	git merge-file --diff3 --marker-size=7 -q -p "$LOCAL" "$BASE" "$REMOTE" >"$DIFF3"
 	if test -s "$DIFF3"
 	then
-		sed -e '/^<<<<<<< /,/^||||||| /d' -e '/^=======\r\?$/,/^>>>>>>> /d' "$DIFF3" >"$BASE"
-		sed -e '/^||||||| /,/^>>>>>>> /d' -e '/^<<<<<<< /d' "$DIFF3" >"$LOCAL"
-		sed -e '/^<<<<<<< /,/^=======\r\?$/d' -e '/^>>>>>>> /d' "$DIFF3" >"$REMOTE"
+		cr=$(printf '\r')
+		sed -e '/^<<<<<<< /,/^||||||| /d' \
+			-e "/^=======$cr\{0,1\}$/,/^>>>>>>> /d" \
+			"$DIFF3" >"$BASE"
+		sed -e '/^||||||| /,/^>>>>>>> /d' \
+			-e '/^<<<<<<< /d' \
+			"$DIFF3" >"$LOCAL"
+		sed -e "/^<<<<<<< /,/^=======$cr\{0,1\}$/d" \
+			-e '/^>>>>>>> /d' \
+			"$DIFF3" >"$REMOTE"
 	fi
 	rm -- "$DIFF3"
 }
Signature · 1 line
2.30.0.rc1.6.g3f22546b2b.dirty
#3 lore raw
Quoted 2 lines · David Aguilar

Replace "\r" with a substituted variable that contains "\r". Replace "\?" with "\{0,1\}".

Nice. I was (very slowly) converging on that as the culprit. Thanks for the elegant fix! I'm running the test suite on Windows and OSX (now that they're set up locally) with this included and I'll send a v10 once complete.

#4 lore raw
Quoted 8 lines · Seth House
Quoted 2 lines · David Aguilar

Replace "\r" with a substituted variable that contains "\r". Replace "\?" with "\{0,1\}".

Nice. I was (very slowly) converging on that as the culprit. Thanks for the elegant fix! I'm running the test suite on Windows and OSX (now that they're set up locally) with this included and I'll send a v10 once complete.

Note that the topic fails t7800.5 even with the fix-up (and without these fix-up on a platform with sed that do not need the portability fix-up).

#5 lore raw

Re* [PATCH v2] fixup! mergetool: add automerge configuration

Quoted 6 lines · Junio C Hamano
Quoted 4 lines · Seth House
Quoted 2 lines · David Aguilar

Replace "\r" with a substituted variable that contains "\r". Replace "\?" with "\{0,1\}".

Quoted 8 lines
Quoted 4 lines

Nice. I was (very slowly) converging on that as the culprit. Thanks for the elegant fix! I'm running the test suite on Windows and OSX (now that they're set up locally) with this included and I'll send a v10 once complete.

Note that the topic fails t7800.5 even with the fix-up (and without these fix-up on a platform with sed that do not need the portability fix-up).

It seems that 51b27370 (mergetool: break setup_tool out into separate initialization function, 2020-12-28) is the culprit.

git-difftool--helper is taught to do this:

    diff --git a/git-difftool--helper.sh b/git-difftool--helper.sh
    index 46af3e60b7..234dd6944e 100755
    --- a/git-difftool--helper.sh
    +++ b/git-difftool--helper.sh
     ...
    @@ -79,6 +80,7 @@ if test -n "$GIT_DIFFTOOL_DIRDIFF"
     then
            LOCAL="$1"
            REMOTE="$2"
    +	initialize_merge_tool "$merge_tool" &&
            run_merge_tool "$merge_tool" false
     else
            # Launch the merge tool on each path provided by 'git diff'

But the thing is, t7800.5 gives a name of merge-tool that does not exist, and expects difftool to notice it and error out. The callchain starting from the above "run_merge_tool" should look like

    run_merge_tool () {
        merge_tool_path=$(get_merge_tool_path ...) || exit
	...

which in turn calls

get_merge_tool_path () {
# A merge tool has been set, so verify that it's valid. merge_tool="$1"
if ! valid_tool "$merge_tool"
then
echo >&2 "Unknown merge tool $merge_tool" exit 1
fi

and "valid_tool bad-tool" would return false, which lets us safely exit with the error message.

But the call to initialize_merge_tool inserted above, that makes us to SKIP the call to run_merge_tool, would defeat the error detection.

An ugly workaround patch that caters only to difftool breakage is attached at the end; I did not look if a similar treatment is necessary for the mergetool side.

By the way, debugging this was somewhat painful as difftool is partly rewritten in C. If it were still pure script, it would have been a lot easier to diagnose with a single "set -x" somewhere X-<.

----- >8 ---------- >8 ---------- >8 ---------- >8 -----
From: Junio C Hamano <[email protected]>
Date: Sat, 9 Jan 2021 22:35:18 -0800
Subject: [PATCH] fixup! mergetool: break setup_tool out into
separate initialization function

At least difftool wants to see even a broken tool name in its call to run_merge_tool and have it diagnose an error. &&-cascading the call to initialize_merge_tool and run_merge_tool means that a bogus tool name that does not please initialize_merge_tool is not even seen by run_merge_tool and the error goes unnoticed.

I didn't audit the original commit for its use of initialize_merge_tool on the merge-tool side. It may share the same issue, or it may not.

This should fix the errors we've been seeing in t7800.5 (and possibly others) with this topic.

---
 git-difftool--helper.sh | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
git-difftool--helper.sh +6 -2
index 234dd6944e..992124cc67 100755
@@ -61,7 +61,9 @@ launch_merge_tool () {
 		export BASE
 		eval $GIT_DIFFTOOL_EXTCMD '"$LOCAL"' '"$REMOTE"'
 	else
-		initialize_merge_tool "$merge_tool" &&
+		initialize_merge_tool "$merge_tool"
+		# ignore the error from the above --- run_merge_tool
+		# will diagnose unusable tool by itself
 		run_merge_tool "$merge_tool"
 	fi
 }
@@ -80,7 +82,9 @@ if test -n "$GIT_DIFFTOOL_DIRDIFF"
 then
 	LOCAL="$1"
 	REMOTE="$2"
-	initialize_merge_tool "$merge_tool" &&
+	initialize_merge_tool "$merge_tool"
+	# ignore the error from the above --- run_merge_tool
+	# will diagnose unusable tool by itself
 	run_merge_tool "$merge_tool" false
 else
 	# Launch the merge tool on each path provided by 'git diff'
Signature · 1 line
2.30.0-311-g8a3956654a
#6 lore raw

Re: Re* [PATCH v2] fixup! mergetool: add automerge configuration

Quoted 3 lines · Junio C Hamano

An ugly workaround patch that caters only to difftool breakage is attached at the end; I did not look if a similar treatment is necessary for the mergetool side.

That fixup does the trick on my machine too. Thank you.

How wary are you of continuing with initialize_merge_tool? Do you see a better approach to get automerge_enabled into scope? While it is a nice feature to have, is it worth the risk vs. reward?

#7 lore raw

Re: Re* [PATCH v2] fixup! mergetool: add automerge configuration

Quoted 6 lines · Seth House
Quoted 3 lines · Junio C Hamano

An ugly workaround patch that caters only to difftool breakage is attached at the end; I did not look if a similar treatment is necessary for the mergetool side.

That fixup does the trick on my machine too. Thank you.

Note that with t7800 fixed with the patch, non Windows jobs all seem to pass, but t7610 seems to have problem(s) on Windows.

https://github.com/git/git/runs/1675932107?check_suite_focus=true#step:7:10373

Quoted 3 lines

How wary are you of continuing with initialize_merge_tool? Do you see a better approach to get automerge_enabled into scope? While it is a nice feature to have, is it worth the risk vs. reward?

Hmph. We need to have a way to define helper routines customized for the tool, and in the codebase without this series, it is the job for setup_tool. It defines fallback implementations, allows tool specific customizations.

Your initialize_merge_tool is just a thin wrapper around setup_tool. It calls setup_tool, and if the function exits with non-zero status, returns with status==1 (and otherwise returns with status==0). As I expect all the callers of setup_tool or initialize_merge_tool would either ignore the status or check if it succeeded (i.e. compare $? against 0 and any non-zero values are treated equally), it does not seem to do anything useful.

I think we may be able to get rid of initialize_merge_tool, but you would need to call setup_tool in places initialize_merge_tool was called in your patch, as you must have needed to make sure that the tool specific customizations have been carried out before going forward in these places.

So, no, I do not see a reason to be wary of initialize/setup.

With or without the seemingly needless initialize wrapper, I think calling setup before starting to do certain operations that need tool specific customization is just necessary. The same machanism has been in use to give can_merge/can_diff to each tool and the way it works ought to be fairly well understood.

It is a different story if it makes sense not to exit when you see failure from initialize/setup, and instead _skip_ running helpers like run_merge_tool. It was just a mistake we all make every once in a while (i.e. a bug), and I am reasonably sure that we will introduce more of them but we will be capable of fixing all.

Thanks.

#8 lore raw

Re: Re* [PATCH v2] fixup! mergetool: add automerge configuration

Quoted 2 lines · Junio C Hamano

Note that with t7800 fixed with the patch, non Windows jobs all seem to pass, but t7610 seems to have problem(s) on Windows.

The autocrlf test is breaking because the sed that ships with some mingw versions (and also some minsys and cygwin versions) will *automatically* remove carriage returns:

$ printf 'foo\r\nbar\r\n' | sed -e '/bar/d' | cat -A
foo$
$ printf 'foo\r\nbar\r\n' | sed -b -e '/bar/d' | cat -A
foo^M$

(Note: the -b flag above is just for comparison. We can't use it here. It's not in POSIX and is not present in sed for busybox or OSX.)

I haven't found official docs that describe this behavior yet but I found a few discussions about it across a few lists. E.g.:
https://cygwin.com/pipermail/cygwin/2017-June/233121.html

Suggestions welcome.

Obviously we could try to detect crlf's before the sed and then try to re-add them back in after sed but that strikes me as error-prone.

I recall someone mentioning calling merge-file twice, once with --ours and once with --theirs, to independently generate each "side" of the conflict instead of using sed to split MERGED. Is that a viable approach?

#9 lore raw

automerge implementation ideas for Windows

Quoted 3 lines · Seth House

The autocrlf test is breaking because the sed that ships with some mingw versions (and also some minsys and cygwin versions) will *automatically* remove carriage returns:

So the mingw builds of both sed and awk change carriage returns. So far I haven't found documentation on it so I'm not aware of a portable way to disable the behavior. Instead I've been playing with alternate approaches. The two patches below work and pass the autocrlf test on Windows, however they are first-draft implementations. Feedback welcome.

One other point of discussion: I would like to change the name of this feature. "Automerge" is a bit of an overloaded term and, IMO, doesn't describe this feature very well. Several of the GUI diff programs have a feature that they call "automerge" or "auto merge", and there's a flag for Meld already in Git called "mergetool.meld.useAutoMerge" which could cause confusion.

Instead, I'd like to propose "mergetool.hideResolved" or the more verbose "mergetool.hideResolvedConflicts" as the name. We're not really merging anything (Git aleady did that before the mergetool is invoked), but rather we're just not showing any conflicts that Git was already able to resolve.

---------->8--------->8--------->8--------->8-------

#1: Use POSIX read and a while loop to emulate an awk-like approach:

git-mergetool.sh +39 -9
index 246d6b76fc..94728dd518 100755
@@ -240,19 +240,46 @@ checkout_staged_file () {
 }
 
 auto_merge () {
+	C0="<<<<<<< "
+	C1="||||||| "
+	C2="======="
+	C3=">>>>>>> "
+	inl=0
+	inb=0
+	inr=0
+
 	git merge-file --diff3 --marker-size=7 -q -p "$LOCAL" "$BASE" "$REMOTE" >"$DIFF3"
+
 	if test -s "$DIFF3"
 	then
-		C0="^<<<<<<< "
-		C1="^||||||| "
-		C2="^=======$(printf '\015')\{0,1\}$"
-		C3="^>>>>>>> "
-
-		sed -e "/$C0/,/$C1/d" -e "/$C2/,/$C3/d" "$DIFF3" >"$BASE"
-		sed -e "/$C1/,/$C3/d" -e "/$C0/d" "$DIFF3" >"$LOCAL"
-		sed -e "/$C0/,/$C2/d" -e "/$C3/d" "$DIFF3" >"$REMOTE"
+		touch "$LCONFL" "$BCONFL" "$RCONFL"
+
+		while read -r line
+		do
+			case $line in
+				$C0*) inl=1; continue ;;
+				$C1*) inl=0; inb=1; continue ;;
+				$C2*) inb=0; inr=1; continue ;;
+				$C3*) inr=0; continue ;;
+			esac
+
+			case 1 in
+				$inl) printf '%s\n' "$line" >>"$LCONFL" ;;
+				$inb) printf '%s\n' "$line" >>"$BCONFL" ;;
+				$inr) printf '%s\n' "$line" >>"$RCONFL" ;;
+				*)
+					printf '%s\n' "$line" >>"$LCONFL"
+					printf '%s\n' "$line" >>"$BCONFL"
+					printf '%s\n' "$line" >>"$RCONFL"
+				;;
+			esac
+		done < "$DIFF3"
+
+		mv -- "$LCONFL" "$LOCAL"
+		mv -- "$BCONFL" "$BASE"
+		mv -- "$RCONFL" "$REMOTE"
+		rm -- "$DIFF3"
 	fi
-	rm -- "$DIFF3"
 }
 
 merge_file () {
@@ -295,8 +322,11 @@ merge_file () {
 	DIFF3="$MERGETOOL_TMPDIR/${BASE}_DIFF3_$$$ext"
 	BACKUP="$MERGETOOL_TMPDIR/${BASE}_BACKUP_$$$ext"
 	LOCAL="$MERGETOOL_TMPDIR/${BASE}_LOCAL_$$$ext"
+	LCONFL="$MERGETOOL_TMPDIR/${BASE}_LOCAL_LCONFL_$$$ext"
 	REMOTE="$MERGETOOL_TMPDIR/${BASE}_REMOTE_$$$ext"
+	RCONFL="$MERGETOOL_TMPDIR/${BASE}_REMOTE_RCONFL_$$$ext"
 	BASE="$MERGETOOL_TMPDIR/${BASE}_BASE_$$$ext"
+	BCONFL="$MERGETOOL_TMPDIR/${BASE}_BASE_BCONFL_$$$ext"
 
 	base_mode= local_mode= remote_mode=
 

---------->8--------->8--------->8--------->8-------

#2: Call merge-file twice:

A much simpler implementation but probably less performant. Is it enough to matter?

git-mergetool.sh +6 -13
index 246d6b76fc..1cb45a7437 100755
@@ -240,19 +240,10 @@ checkout_staged_file () {
 }
 
 auto_merge () {
-	git merge-file --diff3 --marker-size=7 -q -p "$LOCAL" "$BASE" "$REMOTE" >"$DIFF3"
-	if test -s "$DIFF3"
-	then
-		C0="^<<<<<<< "
-		C1="^||||||| "
-		C2="^=======$(printf '\015')\{0,1\}$"
-		C3="^>>>>>>> "
-
-		sed -e "/$C0/,/$C1/d" -e "/$C2/,/$C3/d" "$DIFF3" >"$BASE"
-		sed -e "/$C1/,/$C3/d" -e "/$C0/d" "$DIFF3" >"$LOCAL"
-		sed -e "/$C0/,/$C2/d" -e "/$C3/d" "$DIFF3" >"$REMOTE"
-	fi
-	rm -- "$DIFF3"
+	git merge-file --ours -q -p "$LOCAL" "$BASE" "$REMOTE" >"$LCONFL"
+	git merge-file --theirs -q -p "$LOCAL" "$BASE" "$REMOTE" >"$RCONFL"
+	mv -- "$LCONFL" "$LOCAL"
+	mv -- "$RCONFL" "$REMOTE"
 }
 
 merge_file () {
@@ -295,7 +286,9 @@ merge_file () {
 	DIFF3="$MERGETOOL_TMPDIR/${BASE}_DIFF3_$$$ext"
 	BACKUP="$MERGETOOL_TMPDIR/${BASE}_BACKUP_$$$ext"
 	LOCAL="$MERGETOOL_TMPDIR/${BASE}_LOCAL_$$$ext"
+	LCONFL="$MERGETOOL_TMPDIR/${BASE}_LOCAL_LCONFL_$$$ext"
 	REMOTE="$MERGETOOL_TMPDIR/${BASE}_REMOTE_$$$ext"
+	RCONFL="$MERGETOOL_TMPDIR/${BASE}_REMOTE_RCONFL_$$$ext"
 	BASE="$MERGETOOL_TMPDIR/${BASE}_BASE_$$$ext"
 
 	base_mode= local_mode= remote_mode=
#10 lore raw

Re: automerge implementation ideas for Windows

Quoted 7 lines · Seth House

One other point of discussion: I would like to change the name of this feature. "Automerge" is a bit of an overloaded term and, IMO, doesn't describe this feature very well. Several of the GUI diff programs have a feature that they call "automerge" or "auto merge", and there's a flag for Meld already in Git called "mergetool.meld.useAutoMerge" which could cause confusion.

Quoted 5 lines

Instead, I'd like to propose "mergetool.hideResolved" or the more verbose "mergetool.hideResolvedConflicts" as the name. We're not really merging anything (Git aleady did that before the mergetool is invoked), but rather we're just not showing any conflicts that Git was already able to resolve.

I have no objetion. I didn't think 'automerge' was bad, but it probably is too broad a word as you discuss in the above.

"hide resolved" sounds like the name that describes what it does quite well.

Quoted 1 line

#1: Use POSIX read and a while loop to emulate an awk-like approach:

I'd rather not to see us do "text processing" in shell, especially with "read -r". I just do not trust it (even with the "-r" option).

Having said that, I am not familiar enough to the Windows environment to know what is trustworthy and what is not (apparently, things like "sed" that I would intuitively place as much trust as anything else is giving us so much trouble out of box), so I'll shut up and listen to others.

#11 lore raw

Re: automerge implementation ideas for Windows

Quoted 1 line · Junio C Hamano

I'd rather not to see us do "text processing" in shell

Agreed. What are your thoughts on the #2 approach?

I noticed the comment in git/xdiff-interface.h about xdiff's gigabyte limit so I created a 973 MB text file with a conflict and ran #2 through a few mergetools to see how it went. I put /usr/bin/time in front of the two git merge-file invocations. I know one person's machine is not a benchmark but perhaps it's a discussion point?

Each git merge-file call took ~11 seconds on my middle-tier laptop and did not use enough RAM to hit swap.

Writing the near-gigabyte LOCAL, BASE, REMOTE, & BACKUP files went pretty quick. The mergetools themselves had mixed results:

- vimdiff took several minutes (and a lot of swap) to open all four files but did eventually work.
- tkdiff crashed.
- Meld spun for ~10 minutes and never opened.

My takeaway: when trying to use a mergetool on a very large file, the two git merge-file invocations are not likely to be where the performance concern is. #2 is my preferred approach so far.

#12 lore raw

Re: automerge implementation ideas for Windows

Quoted 17 lines · Seth House
Quoted 1 line · Junio C Hamano

I'd rather not to see us do "text processing" in shell

Agreed. What are your thoughts on the #2 approach?

I noticed the comment in git/xdiff-interface.h about xdiff's gigabyte limit so I created a 973 MB text file with a conflict and ran #2 through a few mergetools to see how it went. I put /usr/bin/time in front of the two git merge-file invocations. I know one person's machine is not a benchmark but perhaps it's a discussion point?

Each git merge-file call took ~11 seconds on my middle-tier laptop and did not use enough RAM to hit swap.

Writing the near-gigabyte LOCAL, BASE, REMOTE, & BACKUP files went pretty quick. The mergetools themselves had mixed results:

Quoted 8 lines

- vimdiff took several minutes (and a lot of swap) to open all four files but did eventually work.
- tkdiff crashed.
- Meld spun for ~10 minutes and never opened.

My takeaway: when trying to use a mergetool on a very large file, the two git merge-file invocations are not likely to be where the performance concern is. #2 is my preferred approach so far.

Yeah, I am no expert about Windows, but at least I know how well "git merge-file" should work _anywhere_ (as opposed to "read -r" plus shell loop that I would not trust on a platform where even basic things like "sed" behaves differently from what we expect X-<), so from that point of view, it is vastly more preferrable, if the choices were only between #1 and #2.

#13 lore raw

Re: Re* [PATCH v2] fixup! mergetool: add automerge configuration

In reply to Seth House
Quoted 8 lines · Seth House
Quoted 2 lines · Junio C Hamano

Note that with t7800 fixed with the patch, non Windows jobs all seem to pass, but t7610 seems to have problem(s) on Windows.

The autocrlf test is breaking because the sed that ships with some mingw versions (and also some minsys and cygwin versions) will *automatically* remove carriage returns:

Quoted 8 lines
$ printf 'foo\r\nbar\r\n' | sed -e '/bar/d' | cat -A
foo$
$ printf 'foo\r\nbar\r\n' | sed -b -e '/bar/d' | cat -A
foo^M$

(Note: the -b flag above is just for comparison. We can't use it here. It's not in POSIX and is not present in sed for busybox or OSX.)

Can you report this as a bug? This behavior isn't compliant with POSIX and it makes it really hard for folks to write portable code if these versions implement POSIX utilities in a nonstandard way. As a non-Windows user, I have no hope of writing code that works on Windows if we can't rely on our standard utilities working properly.

Signature · 2 lines
brian m. carlson (he/him or they/them)
Houston, Texas, US
#14 lore raw

Re: Re* [PATCH v2] fixup! mergetool: add automerge configuration

Hi brian,

Quoted 15 lines · brian m. carlson
Quoted 14 lines · Seth House
Quoted 2 lines · Junio C Hamano

Note that with t7800 fixed with the patch, non Windows jobs all seem to pass, but t7610 seems to have problem(s) on Windows.

The autocrlf test is breaking because the sed that ships with some mingw versions (and also some minsys and cygwin versions) will *automatically* remove carriage returns:

$ printf 'foo\r\nbar\r\n' | sed -e '/bar/d' | cat -A
foo$
$ printf 'foo\r\nbar\r\n' | sed -b -e '/bar/d' | cat -A
foo^M$
Quoted 8 lines
Quoted 2 lines

(Note: the -b flag above is just for comparison. We can't use it here. It's not in POSIX and is not present in sed for busybox or OSX.)

Can you report this as a bug? This behavior isn't compliant with POSIX and it makes it really hard for folks to write portable code if these versions implement POSIX utilities in a nonstandard way. As a non-Windows user, I have no hope of writing code that works on Windows if we can't rely on our standard utilities working properly.

I fear that the Windows-based tools do the correct thing, though: they are meant to process _text_, and newlines are supposed to be platform-dependent in text.

From that perspective, it sounds to me as if we're trying to ask sed to do something it was not designed to do: binary editing.

Ciao,
Dscho

#15 lore raw

Re: Re* [PATCH v2] fixup! mergetool: add automerge configuration

Quoted 24 lines · Johannes Schindelin

Hi brian,

Quoted 11 lines · brian m. carlson
Quoted 10 lines · Seth House

The autocrlf test is breaking because the sed that ships with some mingw versions (and also some minsys and cygwin versions) will *automatically* remove carriage returns:

$ printf 'foo\r\nbar\r\n' | sed -e '/bar/d' | cat -A
foo$
$ printf 'foo\r\nbar\r\n' | sed -b -e '/bar/d' | cat -A
foo^M$
Quoted 8 lines
Quoted 2 lines

(Note: the -b flag above is just for comparison. We can't use it here. It's not in POSIX and is not present in sed for busybox or OSX.)

Can you report this as a bug? This behavior isn't compliant with POSIX and it makes it really hard for folks to write portable code if these versions implement POSIX utilities in a nonstandard way. As a non-Windows user, I have no hope of writing code that works on Windows if we can't rely on our standard utilities working properly.

Quoted 3 lines

I fear that the Windows-based tools do the correct thing, though: they are meant to process _text_, and newlines are supposed to be platform-dependent in text.

Ah, but POSIX gives a very specific meaning to "newline", and it refers to a single byte. If you want tools that process CRLF line endings like that, then that should be opt-in as either different tools or additional options, not the default behavior of a POSIX tool. This behavior is not conforming to POSIX and it is therefore a defect.

Quoted 2 lines

From that perspective, it sounds to me as if we're trying to ask sed to do something it was not designed to do: binary editing.

Most Windows tools are perfectly capable of handling LF line endings. Even the famously incapable Notepad can now handle LF without CR. With the advent of WSL, handling LF line endings is now pretty much required.

Signature · 2 lines
brian m. carlson (he/him or they/them)
Houston, Texas, US
#16 lore raw

Re: Re* [PATCH v2] fixup! mergetool: add automerge configuration

Hi brian,

Quoted 28 lines · brian m. carlson
Quoted 23 lines · Johannes Schindelin
Quoted 11 lines · brian m. carlson
Quoted 10 lines · Seth House

The autocrlf test is breaking because the sed that ships with some mingw versions (and also some minsys and cygwin versions) will *automatically* remove carriage returns:

$ printf 'foo\r\nbar\r\n' | sed -e '/bar/d' | cat -A
foo$
$ printf 'foo\r\nbar\r\n' | sed -b -e '/bar/d' | cat -A
foo^M$
Quoted 8 lines
Quoted 2 lines

(Note: the -b flag above is just for comparison. We can't use it here. It's not in POSIX and is not present in sed for busybox or OSX.)

Can you report this as a bug? This behavior isn't compliant with POSIX and it makes it really hard for folks to write portable code if these versions implement POSIX utilities in a nonstandard way. As a non-Windows user, I have no hope of writing code that works on Windows if we can't rely on our standard utilities working properly.

Quoted 3 lines

I fear that the Windows-based tools do the correct thing, though: they are meant to process _text_, and newlines are supposed to be platform-dependent in text.

Quoted 5 lines

Ah, but POSIX gives a very specific meaning to "newline", and it refers to a single byte. If you want tools that process CRLF line endings like that, then that should be opt-in as either different tools or additional options, not the default behavior of a POSIX tool. This behavior is not conforming to POSIX and it is therefore a defect.

If we needed another reminder that

we never say "It's in POSIX; we'll happily ignore your needs should your system not conform to it."

(https://github.com/git/git/blob/v2.30.0/Documentation/CodingGuidelines),
then we have it right here ;-)

Quoted 6 lines
Quoted 2 lines

From that perspective, it sounds to me as if we're trying to ask sed to do something it was not designed to do: binary editing.

Most Windows tools are perfectly capable of handling LF line endings. Even the famously incapable Notepad can now handle LF without CR. With the advent of WSL, handling LF line endings is now pretty much required.

I have two comments on that:

1. We could spend a splendid time questioning MSYS2's (and before that, MSys') choices regarding newlines, but I think we can spend that time a lot better.

2. Newer software _seems_ to handle LF line endings just fine. And the sed invocation you mentioned above does so: it groks input delimited by Line Feed as newline characters. That does not mean that its output is LF-only by default. I seem to remember that recent Visual Studio versions did something similar: happily read an LF-only .xml file, but then write out a modified version using CR/LF.

Would I wish that Windows used LF-only instead of CR/LF, just like macOS switched away from CR-only to LF-only after MacOS 9? Sure I do. It would remove quite a few obstacles in my daily work. Can I do anything about it? No.

So I'd rather see git mergetool be turned into a portable C program, or alternatively using a built-in helper that _is_ written in C, to perform that desired text munging instead of losing the battle to the challenge of cross-platform, advanced text processing in pure shell script.

Ciao,
Dscho

#17 lore raw

Re: Re* [PATCH v2] fixup! mergetool: add automerge configuration

Quoted 3 lines · Johannes Schindelin

So I'd rather see git mergetool be turned into a portable C program, or alternatively using a built-in helper that _is_ written in C, to perform that desired text munging

I tend to agree. Though my personal preference is Cygwin's (eventual) approach, I can appreciate the arguments made by the MSYS2 folk. But setting that aside, IMO, the ideal place to handle this would be the same place where the conflict markers are written in the first place, xmerge.c if my limited C literacy is correct.

I don't see a big distinction between writing a single file with conflict markers and writing two, diff-able files with each "side" of the conflict -- they're ultimately two different formats for expressing the same information. That would give us the portability you described and the (pretty amazing) performance that merge-file already enjoys. :)

I'm more than happy with calling merge-file twice for now. A future C optimisation, perhaps exposed via merge-file as a new (e.g.) --write-conflict-files flag, would be even more awesome.

#18 lore raw

Re: Re* [PATCH v2] fixup! mergetool: add automerge configuration

Quoted 17 lines · Seth House
Quoted 3 lines · Johannes Schindelin

So I'd rather see git mergetool be turned into a portable C program, or alternatively using a built-in helper that _is_ written in C, to perform that desired text munging

I tend to agree. Though my personal preference is Cygwin's (eventual) approach, I can appreciate the arguments made by the MSYS2 folk. But setting that aside, IMO, the ideal place to handle this would be the same place where the conflict markers are written in the first place, xmerge.c if my limited C literacy is correct.

I don't see a big distinction between writing a single file with conflict markers and writing two, diff-able files with each "side" of the conflict -- they're ultimately two different formats for expressing the same information. That would give us the portability you described and the (pretty amazing) performance that merge-file already enjoys. :)

Quoted 3 lines

I'm more than happy with calling merge-file twice for now. A future C optimisation, perhaps exposed via merge-file as a new (e.g.) --write-conflict-files flag, would be even more awesome.

I am OK with that "two merge-file invocations, one with --ours and then another with --theirs" approach, as I already said in
https://lore.kernel.org/git/[email protected]/

#19 lore raw

Re: Re* [PATCH v2] fixup! mergetool: add automerge configuration

Quoted 3 lines · Junio C Hamano

I am OK with that "two merge-file invocations, one with --ours and then another with --theirs" approach, as I already said in
https://lore.kernel.org/git/[email protected]/

Sorry if it seemed like I left that thread hanging (busy week). Thanks for your reply(s). I'm finishing a v10 patchset with that change which I'll submit to the list for review this week.

#20 lore raw

Re: Re* [PATCH v2] fixup! mergetool: add automerge configuration

Quoted 3 lines · Seth House

Sorry if it seemed like I left that thread hanging (busy week). Thanks for your reply(s). I'm finishing a v10 patchset with that change which I'll submit to the list for review this week.

Thanks.

#21 lore raw
In reply to David Aguilar
Quoted 33 lines · David Aguilar

"\r\?" in sed is not portable to macOS and possibly others. "\r" is not valid on Linux with POSIXLY_CORRECT.

Replace "\r" with a substituted variable that contains "\r". Replace "\?" with "\{0,1\}".

---
This is based on top of fc/mergetool-automerge and can be squashed in (with the addition of my sign-off) if desired.

Changes since last time:
- printf '\r' instead of printf '\x0d'
- The commit message now mentions POSIXLY_CORRECT.

 git-mergetool.sh | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
git-mergetool.sh +3 -3
index a44afd3822..9029a79431 100755
@@ -243,9 +243,16 @@ auto_merge () {
 	git merge-file --diff3 --marker-size=7 -q -p "$LOCAL" "$BASE" "$REMOTE" >"$DIFF3"
 	if test -s "$DIFF3"
 	then
-		sed -e '/^<<<<<<< /,/^||||||| /d' -e '/^=======\r\?$/,/^>>>>>>> /d' "$DIFF3" >"$BASE"
-		sed -e '/^||||||| /,/^>>>>>>> /d' -e '/^<<<<<<< /d' "$DIFF3" >"$LOCAL"
-		sed -e '/^<<<<<<< /,/^=======\r\?$/d' -e '/^>>>>>>> /d' "$DIFF3" >"$REMOTE"
+		cr=$(printf '\r')
+		sed -e '/^<<<<<<< /,/^||||||| /d' \
+			-e "/^=======$cr\{0,1\}$/,/^>>>>>>> /d" \

git-mergetool.sh auto_merge () { · line 258

Quoted 10 lines
git-mergetool.sh +7 -0
+			"$DIFF3" >"$BASE"
+		sed -e '/^||||||| /,/^>>>>>>> /d' \
+			-e '/^<<<<<<< /d' \
+			"$DIFF3" >"$LOCAL"
+		sed -e "/^<<<<<<< /,/^=======$cr\{0,1\}$/d" \
+			-e '/^>>>>>>> /d' \
+			"$DIFF3" >"$REMOTE"
 	fi
 	rm -- "$DIFF3"
 }

I was hoping that we can avoid repetition that can cause bugs with something like

git-mergetool.sh +7 -3
index a44afd3822..e07dabbf72 100755
@@ -243,9 +243,13 @@ auto_merge () {
 	git merge-file --diff3 --marker-size=7 -q -p "$LOCAL" "$BASE" "$REMOTE" >"$DIFF3"
 	if test -s "$DIFF3"
 	then
-		sed -e '/^<<<<<<< /,/^||||||| /d' -e '/^=======\r\?$/,/^>>>>>>> /d' "$DIFF3" >"$BASE"
-		sed -e '/^||||||| /,/^>>>>>>> /d' -e '/^<<<<<<< /d' "$DIFF3" >"$LOCAL"
-		sed -e '/^<<<<<<< /,/^=======\r\?$/d' -e '/^>>>>>>> /d' "$DIFF3" >"$REMOTE"
+		C0="^<<<<<<< "
+		C1="^||||||| "
+		C2="^=======$(printf '\015')*\$"
+		C3="^>>>>>>> "
+		sed -e "/$C0/,/$C1/d" -e "/$C2/,/$C3/d" "$DIFF3" >"$BASE"
+		sed -e "/$C1/,/$C3/d" -e "/$C0/d" "$DIFF3" >"$LOCAL"
+		sed -e "/$C0/,/$C2/d" -e "/$C3 /d" "$DIFF3" >"$REMOTE"
 	fi
 	rm -- "$DIFF3"
 }
#22 lore raw
Quoted 3 lines · Junio C Hamano

I was hoping that we can avoid repetition that can cause bugs with something like
...

Here is a cleaned-up version that would apply cleanly on top of yours.

I suspect that it would make it even easier to follow the logic if the two sed "-e" expressions are swapped for the $LOCAL one.

It would clarify that we remove $C0 (separator before ours) and $C1..$C3 (ancestor's and theirs, including the separators) to get the local version.

The other two are already described in the correct order.

Thanks.

 git-mergetool.sh | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)
git-mergetool.sh +8 -10
index 9029a79431..ed152a4187 100755
@@ -243,16 +243,14 @@ auto_merge () {
 	git merge-file --diff3 --marker-size=7 -q -p "$LOCAL" "$BASE" "$REMOTE" >"$DIFF3"
 	if test -s "$DIFF3"
 	then
-		cr=$(printf '\r')
-		sed -e '/^<<<<<<< /,/^||||||| /d' \
-			-e "/^=======$cr\{0,1\}$/,/^>>>>>>> /d" \
-			"$DIFF3" >"$BASE"
-		sed -e '/^||||||| /,/^>>>>>>> /d' \
-			-e '/^<<<<<<< /d' \
-			"$DIFF3" >"$LOCAL"
-		sed -e "/^<<<<<<< /,/^=======$cr\{0,1\}$/d" \
-			-e '/^>>>>>>> /d' \
-			"$DIFF3" >"$REMOTE"
+		C0="^<<<<<<< "
+		C1="^||||||| "
+		C2="^=======$(printf '\015')\{0,1\}$"
+		C3="^>>>>>>> "
+
+		sed -e "/$C0/,/$C1/d" -e "/$C2/,/$C3/d" "$DIFF3" >"$BASE"
+		sed -e "/$C1/,/$C3/d" -e "/$C0/d" "$DIFF3" >"$LOCAL"
+		sed -e "/$C0/,/$C2/d" -e "/$C3/d" "$DIFF3" >"$REMOTE"
 	fi
 	rm -- "$DIFF3"
 }
Signature · 1 line
2.30.0-307-g37795e20d9
#23 lore raw
In reply to brian m. carlson
Quoted 2 lines · brian m. carlson

Ah, yes, this is true. The statement about "\r" is also true for Linux with POSIXLY_CORRECT, IIRC.

It's nice to have a way to reproduce without having a separate toolchain. Thanks for the suggestion.

Quoted 11 lines

Unfortunately, printf is not specified by POSIX to take hex escapes, so this, too is nonportable. We are unfortunately allowed to use only octal escapes (yuck). However, we can write this:

  cr=$(printf '\r')

or

  cr=$(printf '\015')

I think the former is clearer, since that's what we were writing before.

The latter however appears more portable at least to traditionalists ;-)