Subject: [PATCH 3/8] sh-tools: add a git-sh-tools shell helper script

From: David Aguilar <[email-masked]>
Date: Sun, 29 Mar 2009 22:03:43 -0700

git-sh-tools holds functions common to the git-*tool commands.

Signed-off-by: David Aguilar <[email-masked]>
---
 .gitignore                     |    1 +
 Documentation/git-sh-tools.txt |   49 +++++++++++++++++++++++++++++++++++++++
 Makefile                       |    1 +
 command-list.txt               |    1 +
 git-sh-tools.sh                |   50 ++++++++++++++++++++++++++++++++++++++++
 5 files changed, 102 insertions(+), 0 deletions(-)
 create mode 100644 Documentation/git-sh-tools.txt
 create mode 100644 git-sh-tools.sh

diff --git a/.gitignore b/.gitignore
index 966c886..cecf77e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -114,6 +114,7 @@ git-rm
 git-send-email
 git-send-pack
 git-sh-setup
+git-sh-tools
 git-shell
 git-shortlog
 git-show
diff --git a/Documentation/git-sh-tools.txt b/Documentation/git-sh-tools.txt
new file mode 100644
index 0000000..055a10c
--- /dev/null
+++ b/Documentation/git-sh-tools.txt
@@ -0,0 +1,49 @@
+git-sh-tool(1)
+==============
+
+NAME
+----
+git-sh-tools - Common git *tool shell script functions
+
+SYNOPSIS
+--------
+'. "$(git --exec-path)/git-sh-tools"'
+
+DESCRIPTION
+-----------
+
+This is not a command the end user would want to run.  Ever.
+This documentation is meant for people who are studying the
+Porcelain-ish scripts and/or are writing new ones.
+
+The 'git-sh-tools' scriptlet is designed to be sourced (using
+`.`) by other shell scripts to set up some functions for
+working with git merge/diff tools.
+
+Before sourcing it, your script should set up a few variables;
+`TOOL_MODE` is used to define the operation mode for various
+functions.  'diff' and 'merge' are valid values.
+
+FUNCTIONS
+---------
+valid_tool::
+	verifies that the specified merge tool is properly setup.
+
+valid_custom_tool::
+	verifies that a '(diff|merge)tool.<tool>.cmd' configuration exists.
+
+init_merge_tool_path::
+	sets up `$merge_tool_path` according to '(diff|merge)tool.<tool>.path'
+	configurations.
+
+Author
+------
+Written by David Aguilar <[email-masked]>
+
+Documentation
+--------------
+Documentation by David Aguilar and the git-list <[email-masked]>.
+
+GIT
+---
+Part of the linkgit:git[1] suite
diff --git a/Makefile b/Makefile
index d77fd71..3b7c20f 100644
--- a/Makefile
+++ b/Makefile
@@ -292,6 +292,7 @@ SCRIPT_SH += git-rebase.sh
 SCRIPT_SH += git-repack.sh
 SCRIPT_SH += git-request-pull.sh
 SCRIPT_SH += git-sh-setup.sh
+SCRIPT_SH += git-sh-tools.sh
 SCRIPT_SH += git-stash.sh
 SCRIPT_SH += git-submodule.sh
 SCRIPT_SH += git-web--browse.sh
diff --git a/command-list.txt b/command-list.txt
index fb03a2e..c3b6c87 100644
--- a/command-list.txt
+++ b/command-list.txt
@@ -109,6 +109,7 @@ git-show-branch                         ancillaryinterrogators
 git-show-index                          plumbinginterrogators
 git-show-ref                            plumbinginterrogators
 git-sh-setup                            purehelpers
+git-sh-tools                            purehelpers
 git-stash                               mainporcelain
 git-status                              mainporcelain common
 git-stripspace                          purehelpers
diff --git a/git-sh-tools.sh b/git-sh-tools.sh
new file mode 100644
index 0000000..234bac7
--- /dev/null
+++ b/git-sh-tools.sh
@@ -0,0 +1,50 @@
+# Verifies that the chosen merge tool is properly setup.
+# Built-in merge tools are always valid.
+valid_tool() {
+	case "$1" in
+	kdiff3 | kompare | tkdiff | xxdiff | meld | opendiff | emerge | vimdiff | gvimdiff | ecmerge)
+		;; # happy
+	*)
+		if ! valid_custom_tool "$1"; then
+			return 1
+		fi
+		;;
+	esac
+}
+
+# Verifies that (difftool|mergetool).<tool>.cmd exists
+# Requires $TOOL_MODE to be set.
+valid_custom_tool() {
+	if test "$TOOL_MODE" = "diff"; then
+		merge_tool_cmd="$(git config difftool.$1.cmd)"
+		test -z "$merge_tool_cmd" &&
+		merge_tool_cmd="$(git config mergetool.$1.cmd)"
+		test -n "$merge_tool_cmd"
+	elif test "$TOOL_MODE" = "merge"; then
+		merge_tool_cmd="$(git config mergetool.$1.cmd)"
+		test -n "$merge_tool_cmd"
+	fi
+}
+
+
+# Set up $merge_tool_path for (diff|merge)tool.<tool>.path configurations
+init_merge_tool_path() {
+	if test "$TOOL_MODE" = "diff"; then
+		merge_tool_path=$(git config difftool."$1".path)
+		test -z "$merge_tool_path" &&
+		merge_tool_path=$(git config mergetool."$1".path)
+	elif test "$TOOL_MODE" = "merge"; then
+		merge_tool_path=$(git config mergetool."$1".path)
+	fi
+
+	if test -z "$merge_tool_path" ; then
+		case "$1" in
+			emerge)
+				merge_tool_path=emacs
+				;;
+			*)
+				merge_tool_path=$1
+				;;
+		esac
+	fi
+}
-- 
1.6.1.3