Subject: [PATCH v0 4/4] git-gui - use mkshortcut on Cygwin

From: Mark Levedahl <[email-masked]>
Date: Sat, 24 Jun 2023 17:23:47 -0400

Prior to 2012, git-gui enabled the "Repository->Create Desktop Icon"
item on Cygwin, offering to create a shortcut that starts git-gui on a
particular repository. The original code for this in lib/win32.tcl,
shared with Git for Windows support, requires Windows pathnames, while
git-gui must use unix pathnames with the unix/X11 Tcl/Tk since 2012. The
ability to use this from Cygwin was removed in a previous patch.

Cygwin's default installation provides /bin/mkshortcut for creating
desktop shortuts, this is compatible with exec under tcl, and understands
Cygwin's unix pathnames. So, teach git-gui to use mkshortcut on Cygwin,
leaving lib/win32.tcl as Git for Windows specific support.

Notes: "CHERE_INVOKING=1" is recognized by Cygwin's /etc/profile and
prevents a "chdir $HOME", leaving the shell in the working directory
specified by the shortcut. That directory is written directly by
mkshortcut eliminating any problems with shell escapes and quoting.

The pre-2012 code includes the full pathname of the git-gui creating the
shortcut (rather than using the system git-gui), but that git-gui might
not be compatible with the git found after /etc/profile sets the path,
and might have a pathname that defies encoding using shell escapes that
can survive the multiple incompatible interpreters involved in this
chain. Instead, use "git gui", thus defaulting to the system git and
avoiding both issues.

Signed-off-by: Mark Levedahl <[email-masked]>
---
 git-gui.sh       |  4 ++++
 lib/shortcut.tcl | 38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+)

diff --git a/git-gui.sh b/git-gui.sh
index 523770a..5c13521 100755
--- a/git-gui.sh
+++ b/git-gui.sh
@@ -2836,6 +2836,10 @@ if {[is_enabled multicommit]} {
 		.mbar.repository add command \
 			-label [mc "Create Desktop Icon"] \
 			-command do_windows_shortcut
+	} elseif {[is_Cygwin]} {
+		.mbar.repository add command \
+			-label [mc "Create Desktop Icon"] \
+			-command do_cygwin_shortcut
 	} elseif {[is_MacOSX]} {
 		.mbar.repository add command \
 			-label [mc "Create Desktop Icon"] \
diff --git a/lib/shortcut.tcl b/lib/shortcut.tcl
index 1d8374b..6c2a99e 100644
--- a/lib/shortcut.tcl
+++ b/lib/shortcut.tcl
@@ -26,6 +26,44 @@ proc do_windows_shortcut {} {
 	}
 }
 
+proc do_cygwin_shortcut {} {
+	global argv0 _gitworktree oguilib
+
+	if {[catch {
+		set desktop [exec cygpath \
+			--desktop]
+		}]} {
+			set desktop .
+	}
+	set fn [tk_getSaveFile \
+		-parent . \
+		-title [mc "%s (%s): Create Desktop Icon" [appname] [reponame]] \
+		-initialdir $desktop \
+		-initialfile "Git [reponame].lnk"]
+	if {$fn != {}} {
+		if {[file extension $fn] ne {.lnk}} {
+			set fn ${fn}.lnk
+		}
+		if {[catch {
+				set repodir [file normalize $_gitworktree]
+				set shargs {-c \
+					"CHERE_INVOKING=1 \
+					source /etc/profile; \
+					git gui"}
+				exec /bin/mkshortcut.exe \
+					-a $shargs \
+					-d "git-gui on $repodir" \
+					-i $oguilib/git-gui.ico \
+					-n $fn \
+					-s min \
+					-w $repodir \
+					/bin/sh.exe
+			} err]} {
+			error_popup [strcat [mc "Cannot write shortcut:"] "\n\n$err"]
+		}
+	}
+}
+
 proc do_macosx_app {} {
 	global argv0 env
 
-- 
2.41.0.99.19