Subject: Re: [PATCH v4] Enable auto-merge for meld to follow the vim-diff beharior

From: Đoàn Trần Công Danh <[email-masked]>
Date: Tue, 30 Jun 2020 23:23:33 +0700

On 2020-06-30 11:26:37+0000, sunlin via GitGitGadget <[email-masked]> wrote:
> From: "lin.sun" <[email-masked]>

This part needs to be fixed, aka, use your real name here :).

> 
> Make the mergetool used with "meld" backend behave similarly to
> how "vimdiff" beheaves by telling it to auto-merge parts without
> conflicts and highlight the parts with conflicts.
> 
> Signed-off-by: lin.sun <[email-masked]>
> ---
>     Enable auto-merge for meld to follow the vimdiff beharior
>     
>     Hi, the mergetool "meld" does NOT merge the no-conflict changes, while
>     the mergetool "vimdiff" will merge the no-conflict changes and highlight
>     the conflict parts. This patch will make the mergetool "meld" similar to
>     "vimdiff", auto-merge the no-conflict changes, highlight conflict parts.
> 
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-781%2Fsunlin7%2Fmaster-v4
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-781/sunlin7/master-v4
> Pull-Request: https://github.com/git/git/pull/781
> 
> Range-diff vs v3:
> 
>  1:  3b70fd0bfc ! 1:  93ae3ec011 Enable auto-merge for meld to follow the vim-diff beharior
>      @@ Metadata
>        ## Commit message ##
>           Enable auto-merge for meld to follow the vim-diff beharior
>       
>      -    The mergetool "meld" does NOT merge the no-conflict changes, while the
>      -    mergetool "vimdiff" will merge the no-conflict parts and highlight the
>      -    conflict parts.
>      -    This patch will make the mergetool "meld" similar to "vimdiff",
>      -    auto-merge the no-conflict parts, highlight conflict parts.
>      +    Make the mergetool used with "meld" backend behave similarly to
>      +    how "vimdiff" beheaves by telling it to auto-merge parts without
>      +    conflicts and highlight the parts with conflicts.
>       
>      -    Signed-off-by: Lin Sun <[email-masked]>
>      +    Signed-off-by: lin.sun <[email-masked]>
>       
>        ## mergetools/meld ##
>      -@@ mergetools/meld: merge_cmd () {
>      - 	then
>      - 		check_meld_for_output_version
>      - 	fi
>      -+	if test -z "${meld_has_auto_merge_option:+set}"
>      -+	then
>      -+		check_meld_for_auto_merge_version
>      -+	fi
>      +@@ mergetools/meld: diff_cmd () {
>      + }
>      + 
>      + merge_cmd () {
>      +-	if test -z "${meld_has_output_option:+set}"
>      ++	check_meld_for_features
>       +
>       +	option_auto_merge=
>       +	if test "$meld_has_auto_merge_option" = true
>      -+	then
>      + 	then
>      +-		check_meld_for_output_version
>       +		option_auto_merge="--auto-merge"
>      -+	fi
>      + 	fi
>        
>        	if test "$meld_has_output_option" = true
>        	then
>      @@ mergetools/meld: merge_cmd () {
>        	fi
>        }
>        
>      -@@ mergetools/meld: check_meld_for_output_version () {
>      - 		meld_has_output_option=false
>      - 	fi
>      - }
>      -+
>      -+# Check whether we should use 'meld --auto-merge ...'
>      -+check_meld_for_auto_merge_version () {
>      -+	meld_path="$(git config mergetool.meld.path)"
>      -+	meld_path="${meld_path:-meld}"
>      +-# Check whether we should use 'meld --output <file>'
>      +-check_meld_for_output_version () {
>      +-	meld_path="$(git config mergetool.meld.path)"
>      +-	meld_path="${meld_path:-meld}"
>      ++# Get meld help message
>      ++get_meld_help_msg () {
>      ++	meld_path="$(git config mergetool.meld.path || echo meld)"
>      ++  $meld_path --help 2>&1
>      ++}
>      + 
>      +-	if meld_has_output_option=$(git config --bool mergetool.meld.hasOutput)
>      ++# Check the features and set flags
>      ++check_meld_for_features () {
>      ++	# Check whether we should use 'meld --output <file>'
>      ++	if test -z "${meld_has_output_option:+set}"
>      + 	then
>      +-		: use configured value
>      +-	elif "$meld_path" --help 2>&1 |
>      +-		grep -e '--output=' -e '\[OPTION\.\.\.\]' >/dev/null
>      ++		meld_has_output_option=$(git config --bool mergetool.meld.hasOutput)
>      ++		if test "$meld_has_output_option" = true -o \
>      ++						"$meld_has_output_option" = false
>      ++		then
>      ++			: use configured value
>      ++		else												# treat meld_has_output_option as "auto"
>      ++			if test -z "$meld_help_msg"
>      ++			then
>      ++				meld_help_msg="$(get_meld_help_msg)"
>      ++			fi
>       +
>      -+	if meld_has_auto_merge_option=$(git config --bool mergetool.meld.hasAutoMerge)
>      -+	then
>      -+		: use configured value
>      -+	elif "$meld_path" --help 2>&1 |
>      -+		grep -e '--auto-merge' -e '\[OPTION\.\.\.\]' >/dev/null
>      -+	then
>      -+		: old ones mention --auto-merge and new ones just say OPTION...
>      -+		meld_has_auto_merge_option=true
>      -+	else
>      -+		meld_has_auto_merge_option=false
>      ++			if echo "$meld_help_msg" |
>      ++							grep -e '--output=' -e '\[OPTION\.\.\.\]' >/dev/null
>      ++			then
>      ++				: old ones mention --output and new ones just say OPTION...
>      ++				meld_has_output_option=true
>      ++			else
>      ++				meld_has_output_option=false
>      ++			fi
>      ++		fi
>       +	fi
>      -+}
>      ++	# Check whether we should use 'meld --auto-merge ...'
>      ++	if test -z "${meld_has_auto_merge_option:+set}"
>      + 	then
>      +-		: old ones mention --output and new ones just say OPTION...
>      +-		meld_has_output_option=true
>      +-	else
>      +-		meld_has_output_option=false
>      ++		meld_has_auto_merge_option=$(git config --bool mergetool.meld.hasAutoMerge)
>      ++		if test "$meld_has_auto_merge_option" = true -o \
>      ++						"$meld_has_auto_merge_option" = false
>      ++		then
>      ++			: use configured value
>      ++		else												# treat meld_has_auto_merge_option as "auto"
>      ++			if test -z "$meld_help_msg"
>      ++			then
>      ++					meld_help_msg="$(get_meld_help_msg)"
>      ++			fi
>      ++
>      ++			if echo "$meld_help_msg" | grep -e '--auto-merge' >/dev/null
>      ++			then
>      ++					meld_has_auto_merge_option=true
>      ++			else
>      ++				meld_has_auto_merge_option=false
>      ++			fi
>      ++		fi
>      + 	fi
>      + }
> 
> 
>  mergetools/meld | 73 ++++++++++++++++++++++++++++++++++++++-----------
>  1 file changed, 57 insertions(+), 16 deletions(-)
> 
> diff --git a/mergetools/meld b/mergetools/meld
> index 7a08470f88..1b92771228 100644
> --- a/mergetools/meld
> +++ b/mergetools/meld
> @@ -3,34 +3,75 @@ diff_cmd () {
>  }
>  
>  merge_cmd () {
> -	if test -z "${meld_has_output_option:+set}"
> +	check_meld_for_features
> +
> +	option_auto_merge=
> +	if test "$meld_has_auto_merge_option" = true

We probably want "meld_want_auto_merge"?
Someone may use meld that has --auto-merge but don't want to use it.

>  	then
> -		check_meld_for_output_version
> +		option_auto_merge="--auto-merge"
>  	fi
>  
>  	if test "$meld_has_output_option" = true
>  	then
> -		"$merge_tool_path" --output="$MERGED" \
> +		"$merge_tool_path" $option_auto_merge --output="$MERGED" \
>  			"$LOCAL" "$BASE" "$REMOTE"
>  	else
> -		"$merge_tool_path" "$LOCAL" "$MERGED" "$REMOTE"
> +		"$merge_tool_path" $option_auto_merge "$LOCAL" "$MERGED" "$REMOTE"
>  	fi
>  }
>  
> -# Check whether we should use 'meld --output <file>'
> -check_meld_for_output_version () {
> -	meld_path="$(git config mergetool.meld.path)"
> -	meld_path="${meld_path:-meld}"
> +# Get meld help message
> +get_meld_help_msg () {
> +	meld_path="$(git config mergetool.meld.path || echo meld)"
> +  $meld_path --help 2>&1

We always use TAB for indentation.

> +}
>  
> -	if meld_has_output_option=$(git config --bool mergetool.meld.hasOutput)
> +# Check the features and set flags
> +check_meld_for_features () {
> +	# Check whether we should use 'meld --output <file>'
> +	if test -z "${meld_has_output_option:+set}"
>  	then
> -		: use configured value
> -	elif "$meld_path" --help 2>&1 |
> -		grep -e '--output=' -e '\[OPTION\.\.\.\]' >/dev/null
> +		meld_has_output_option=$(git config --bool mergetool.meld.hasOutput)
> +		if test "$meld_has_output_option" = true -o \
> +						"$meld_has_output_option" = false
> +		then
> +			: use configured value
> +		else												# treat meld_has_output_option as "auto"

Please don't put your comment like this,
it's too far away from code,
reader may miss that.

> +			if test -z "$meld_help_msg"
> +			then
> +				meld_help_msg="$(get_meld_help_msg)"
> +			fi
> +
> +			if echo "$meld_help_msg" |
> +							grep -e '--output=' -e '\[OPTION\.\.\.\]' >/dev/null

I probably write like this to save a pipe, a fork and an exec:

	case "$meld_help_msg" in
	*"[OPTION"???"]"*)
		meld_has_output_option=true ;;
	*)
		meld_has_output_option=false ;;
	esac

If you don't like the case ... esac,
I prefer "grep -q" in this case,
I didn't check how much portable it's.
Don't forget to hide the error with 2>/dev/null (or in you case 2>&1)

> +			then
> +				: old ones mention --output and new ones just say OPTION...
> +				meld_has_output_option=true
> +			else
> +				meld_has_output_option=false
> +			fi
> +		fi
> +	fi
> +	# Check whether we should use 'meld --auto-merge ...'
> +	if test -z "${meld_has_auto_merge_option:+set}"
>  	then
> -		: old ones mention --output and new ones just say OPTION...
> -		meld_has_output_option=true

I think something was forgotten here?

> -	else
> -		meld_has_output_option=false
> +		meld_has_auto_merge_option=$(git config --bool mergetool.meld.hasAutoMerge)
> +		if test "$meld_has_auto_merge_option" = true -o \
> +						"$meld_has_auto_merge_option" = false
> +		then
> +			: use configured value
> +		else												# treat meld_has_auto_merge_option as "auto"
> +			if test -z "$meld_help_msg"
> +			then
> +					meld_help_msg="$(get_meld_help_msg)"
> +			fi
> +
> +			if echo "$meld_help_msg" | grep -e '--auto-merge' >/dev/null

And I'll use the same "case ... esac" above for this one :)

> +			then
> +					meld_has_auto_merge_option=true
> +			else
> +				meld_has_auto_merge_option=false
> +			fi
> +		fi
>  	fi
>  }
> 
> base-commit: 07d8ea56f2ecb64b75b92264770c0a664231ce17
> -- 
> gitgitgadget

-- 
Danh