git-mergetool--lib.sh 7.38 KB
Newer Older
1
# git-mergetool--lib is a shell library for common merge tool functions
2 3

: ${MERGE_TOOLS_DIR=$(git --exec-path)/mergetools}
4

5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
mode_ok () {
	if diff_mode
	then
		can_diff
	elif merge_mode
	then
		can_merge
	else
		false
	fi
}

is_available () {
	merge_tool_path=$(translate_merge_tool_path "$1") &&
	type "$merge_tool_path" >/dev/null 2>&1
}

22 23 24 25 26 27 28 29 30 31 32 33 34 35
list_config_tools () {
	section=$1
	line_prefix=${2:-}

	git config --get-regexp $section'\..*\.cmd' |
	while read -r key value
	do
		toolname=${key#$section.}
		toolname=${toolname%.cmd}

		printf "%s%s\n" "$line_prefix" "$toolname"
	done
}

36 37 38
show_tool_names () {
	condition=${1:-true} per_line_prefix=${2:-} preamble=${3:-}
	not_found_msg=${4:-}
39
	extra_content=${5:-}
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

	shown_any=
	( cd "$MERGE_TOOLS_DIR" && ls ) | {
		while read toolname
		do
			if setup_tool "$toolname" 2>/dev/null &&
				(eval "$condition" "$toolname")
			then
				if test -n "$preamble"
				then
					printf "%s\n" "$preamble"
					preamble=
				fi
				shown_any=yes
				printf "%s%s\n" "$per_line_prefix" "$toolname"
			fi
		done

58 59 60 61 62 63 64 65 66 67 68 69 70
		if test -n "$extra_content"
		then
			if test -n "$preamble"
			then
				# Note: no '\n' here since we don't want a
				# blank line if there is no initial content.
				printf "%s" "$preamble"
				preamble=
			fi
			shown_any=yes
			printf "\n%s\n" "$extra_content"
		fi

71 72 73 74 75 76 77 78 79
		if test -n "$preamble" && test -n "$not_found_msg"
		then
			printf "%s\n" "$not_found_msg"
		fi

		test -n "$shown_any"
	}
}

80 81 82 83 84 85 86 87 88
diff_mode() {
	test "$TOOL_MODE" = diff
}

merge_mode() {
	test "$TOOL_MODE" = merge
}

translate_merge_tool_path () {
89
	echo "$1"
90 91 92
}

check_unchanged () {
93 94
	if test "$MERGED" -nt "$BACKUP"
	then
95 96
		status=0
	else
97 98
		while true
		do
99 100
			echo "$MERGED seems unchanged."
			printf "Was the merge successful? [y/n] "
101
			read answer || return 1
102 103 104 105 106 107 108 109 110
			case "$answer" in
			y*|Y*) status=0; break ;;
			n*|N*) status=1; break ;;
			esac
		done
	fi
}

valid_tool () {
111 112 113
	setup_tool "$1" && return 0
	cmd=$(get_merge_tool_cmd "$1")
	test -n "$cmd"
114 115
}

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
setup_user_tool () {
	merge_tool_cmd=$(get_merge_tool_cmd "$tool")
	test -n "$merge_tool_cmd" || return 1

	diff_cmd () {
		( eval $merge_tool_cmd )
		status=$?
		return $status
	}

	merge_cmd () {
		trust_exit_code=$(git config --bool \
			"mergetool.$1.trustExitCode" || echo false)
		if test "$trust_exit_code" = "false"
		then
			touch "$BACKUP"
			( eval $merge_tool_cmd )
			status=$?
			check_unchanged
		else
			( eval $merge_tool_cmd )
			status=$?
		fi
		return $status
	}
}

143
setup_tool () {
144 145
	tool="$1"

Ondřej Bílka's avatar
Ondřej Bílka committed
146
	# Fallback definitions, to be overridden by tools.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
	can_merge () {
		return 0
	}

	can_diff () {
		return 0
	}

	diff_cmd () {
		status=1
		return $status
	}

	merge_cmd () {
		status=1
		return $status
	}
164

165 166 167 168 169
	translate_merge_tool_path () {
		echo "$1"
	}

	if ! test -f "$MERGE_TOOLS_DIR/$tool"
170
	then
171 172
		setup_user_tool
		return $?
173 174 175
	fi

	# Load the redefined functions
176
	. "$MERGE_TOOLS_DIR/$tool"
177 178 179
	# Now let the user override the default command for the tool.  If
	# they have not done so then this will return 1 which we ignore.
	setup_user_tool
180 181 182 183

	if merge_mode && ! can_merge
	then
		echo "error: '$tool' can not be used to resolve merges" >&2
184
		return 1
185 186 187
	elif diff_mode && ! can_diff
	then
		echo "error: '$tool' can only be used to resolve merges" >&2
188
		return 1
189 190
	fi
	return 0
191 192 193
}

get_merge_tool_cmd () {
194
	merge_tool="$1"
195 196
	if diff_mode
	then
197 198
		git config "difftool.$merge_tool.cmd" ||
		git config "mergetool.$merge_tool.cmd"
199
	else
200
		git config "mergetool.$merge_tool.cmd"
201
	fi
202 203
}

204
# Entry point for running tools
205
run_merge_tool () {
206 207 208 209 210
	# If GIT_PREFIX is empty then we cannot use it in tools
	# that expect to be able to chdir() to its value.
	GIT_PREFIX=${GIT_PREFIX:-.}
	export GIT_PREFIX

211
	merge_tool_path=$(get_merge_tool_path "$1") || exit
212 213 214
	base_present="$2"
	status=0

215
	# Bring tool-specific functions into scope
216
	setup_tool "$1" || return 1
217 218 219

	if merge_mode
	then
220
		run_merge_cmd "$1"
221
	else
222
		run_diff_cmd "$1"
223
	fi
224 225 226
	return $status
}

227 228
# Run a either a configured or built-in diff tool
run_diff_cmd () {
229
	diff_cmd "$1"
230 231 232 233
}

# Run a either a configured or built-in merge tool
run_merge_cmd () {
234
	merge_cmd "$1"
235 236
}

237
list_merge_tool_candidates () {
238 239
	if merge_mode
	then
240 241 242 243
		tools="tortoisemerge"
	else
		tools="kompare"
	fi
244 245 246 247
	if test -n "$DISPLAY"
	then
		if test -n "$GNOME_DESKTOP_SESSION_ID"
		then
248 249 250 251
			tools="meld opendiff kdiff3 tkdiff xxdiff $tools"
		else
			tools="opendiff kdiff3 tkdiff xxdiff meld $tools"
		fi
252 253
		tools="$tools gvimdiff diffuse diffmerge ecmerge"
		tools="$tools p4merge araxis bc3 codecompare"
254
	fi
255 256
	case "${VISUAL:-$EDITOR}" in
	*vim*)
257
		tools="$tools vimdiff emerge"
258 259
		;;
	*)
260
		tools="$tools emerge vimdiff"
261 262
		;;
	esac
263 264
}

265
show_tool_help () {
266
	tool_opt="'git ${TOOL_MODE}tool --tool=<tool>'"
267

268 269 270 271
	tab='	'
	LF='
'
	any_shown=no
272 273

	cmd_name=${TOOL_MODE}tool
274 275 276 277 278 279 280 281 282 283
	config_tools=$({
		diff_mode && list_config_tools difftool "$tab$tab"
		list_config_tools mergetool "$tab$tab"
	} | sort)
	extra_content=
	if test -n "$config_tools"
	then
		extra_content="${tab}user-defined:${LF}$config_tools"
	fi

284 285
	show_tool_names 'mode_ok && is_available' "$tab$tab" \
		"$tool_opt may be set to one of the following:" \
286 287
		"No suitable tool for 'git $cmd_name --tool=<tool>' found." \
		"$extra_content" &&
288 289 290 291 292 293 294
		any_shown=yes

	show_tool_names 'mode_ok && ! is_available' "$tab$tab" \
		"${LF}The following tools are valid, but not currently available:" &&
		any_shown=yes

	if test "$any_shown" = yes
295 296 297 298 299 300 301 302
	then
		echo
		echo "Some of the tools listed above only work in a windowed"
		echo "environment. If run in a terminal-only session, they will fail."
	fi
	exit 0
}

303 304
guess_merge_tool () {
	list_merge_tool_candidates
305 306 307 308 309 310 311
	cat >&2 <<-EOF

	This message is displayed because '$TOOL_MODE.tool' is not configured.
	See 'git ${TOOL_MODE}tool --tool-help' or 'git help config' for more details.
	'git ${TOOL_MODE}tool' will now attempt to use one of the following tools:
	$tools
	EOF
312 313

	# Loop over each candidate and stop when a valid merge tool is found.
314
	for tool in $tools
315
	do
316
		is_available "$tool" && echo "$tool" && return 0
317 318
	done

319
	echo >&2 "No known ${TOOL_MODE} tool is available."
320
	return 1
321 322 323 324 325
}

get_configured_merge_tool () {
	# Diff mode first tries diff.tool and falls back to merge.tool.
	# Merge mode only checks merge.tool
326 327
	if diff_mode
	then
328 329 330
		merge_tool=$(git config diff.tool || git config merge.tool)
	else
		merge_tool=$(git config merge.tool)
331
	fi
332 333
	if test -n "$merge_tool" && ! valid_tool "$merge_tool"
	then
334 335 336 337
		echo >&2 "git config option $TOOL_MODE.tool set to unknown tool: $merge_tool"
		echo >&2 "Resetting to default..."
		return 1
	fi
338
	echo "$merge_tool"
339 340 341 342
}

get_merge_tool_path () {
	# A merge tool has been set, so verify that it's valid.
343
	merge_tool="$1"
344 345
	if ! valid_tool "$merge_tool"
	then
346 347 348
		echo >&2 "Unknown merge tool $merge_tool"
		exit 1
	fi
349 350
	if diff_mode
	then
351
		merge_tool_path=$(git config difftool."$merge_tool".path ||
352
				  git config mergetool."$merge_tool".path)
353 354
	else
		merge_tool_path=$(git config mergetool."$merge_tool".path)
355
	fi
356 357
	if test -z "$merge_tool_path"
	then
358
		merge_tool_path=$(translate_merge_tool_path "$merge_tool")
359
	fi
360
	if test -z "$(get_merge_tool_cmd "$merge_tool")" &&
361 362
		! type "$merge_tool_path" >/dev/null 2>&1
	then
363
		echo >&2 "The $TOOL_MODE tool $merge_tool is not available as"\
364
			 "'$merge_tool_path'"
365 366 367 368 369 370 371
		exit 1
	fi
	echo "$merge_tool_path"
}

get_merge_tool () {
	# Check if a merge tool has been configured
372
	merge_tool=$(get_configured_merge_tool)
373
	# Try to guess an appropriate merge tool if no tool has been set.
374 375
	if test -z "$merge_tool"
	then
376
		merge_tool=$(guess_merge_tool) || exit
377 378 379
	fi
	echo "$merge_tool"
}