git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/2] mergetools: fail if display needed but not present

From
Tim Henigan <tim.henigan@gmail.com>
Date
Mar 28, 2012, 19:58 UTC
Message-ID
<1332964693-4058-2-git-send-email-tim.henigan@gmail.com>
In-Reply-To
<1332964693-4058-1-git-send-email-tim.henigan@gmail.com>

Prior to this commit, if 'git mergetool' or 'git difftool' were run in a terminal-only session, they might still try to open a tool that required a windowed environment.

This commit teaches 'git-mergetool--lib.sh' to test for the presence of a display prior to opening tools that require one.

NOTE: The DISPLAY variable is not set in msysgit or cygwin Git.  In the
absence of that variable, the script assumes that a window environment
is always available when running msysgit or cygwin.
Signed-off-by: Tim Henigan <tim.henigan@gmail.com>
---

Only 2 of the current diff/merge tools (vim and emerge) can operate without a display.

The assumption that a display is always available on msys or cygwin is probably not optimal. However, I was not able to find a better work- around. It also matches the previous behavior that did not test for the DISPLAY prior to opening the tool.

 git-mergetool--lib.sh |   23 +++++++++++++++++++++++
 mergetools/defaults   |    4 ++++
 mergetools/emerge     |    4 ++++
 mergetools/vim        |    4 ++++
 4 files changed, 35 insertions(+)
diff --git a/git-mergetool--lib.sh b/git-mergetool--lib.sh
index 89b16dc..75c8b11 100644
--- a/git-mergetool--lib.sh
+++ b/git-mergetool--lib.sh
@@ -12,6 +12,19 @@ translate_merge_tool_path () {
 	echo "$1"
 }
 
+display_available() {
+	if test -n "$DISPLAY"
+	then
+		return 0
+	fi
+
+	case "$(uname -s)" in
+	MINGW*|CYGWIN*) return 0; break;;
+	esac
+
+	return 1
+}
+
 check_unchanged () {
 	if test "$MERGED" -nt "$BACKUP"
 	then
@@ -190,6 +203,16 @@ get_merge_tool_path () {
 			 "'$merge_tool_path'"
 		exit 1
 	fi
+	if ! display_available
+	then
+		if tool_requires_display
+		then
+			echo >&2 "The $TOOL_MODE tool $merge_tool cannot"\
+			"be run from a terminal-only session.  A window"\
+			"environment is required."
+			exit 1
+		fi
+	fi
 	echo "$merge_tool_path"
 }
 
diff --git a/mergetools/defaults b/mergetools/defaults
index 1d8f2a3..f150227 100644
--- a/mergetools/defaults
+++ b/mergetools/defaults
@@ -7,6 +7,10 @@ can_diff () {
 	return 0
 }
 
+tool_requires_display() {
+	return 0
+}
+
 diff_cmd () {
 	merge_tool_cmd="$(get_merge_tool_cmd "$1")"
 	if test -z "$merge_tool_cmd"
diff --git a/mergetools/emerge b/mergetools/emerge
index f96d9e5..c7eb80b 100644
--- a/mergetools/emerge
+++ b/mergetools/emerge
@@ -1,3 +1,7 @@
+tool_requires_display () {
+	return 1
+}
+
 diff_cmd () {
 	"$merge_tool_path" -f emerge-files-command "$LOCAL" "$REMOTE"
 }
diff --git a/mergetools/vim b/mergetools/vim
index 6817708..ffd9fd6 100644
--- a/mergetools/vim
+++ b/mergetools/vim
@@ -1,3 +1,7 @@
+tool_requires_display () {
+	return 1
+}
+
 diff_cmd () {
 	"$merge_tool_path" -R -f -d \
 		-c 'wincmd l' -c 'cd $GIT_PREFIX' "$LOCAL" "$REMOTE"
-- 
1.7.10.rc2.21.g8cb1a
Previous: Tim HeniganNext: Junio C Hamano
Message 2 of 4 in “mergetools: split config files for vim and gvim”
  1. 1/2 mergetools: split config files for vim and gvimTim Henigan, Mar 28, 2012
  2. 2/2 mergetools: fail if display needed but not presentTim Henigan, Mar 28, 2012
  3. Junio C HamanoMar 28, 2012
  4. Junio C HamanoMar 28, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.