]> git.ipfire.org Git - thirdparty/git.git/commitdiff
git-gui: Windows tk_getSaveFile is not useful for shortcuts
authorMark Levedahl <mlevedahl@gmail.com>
Sun, 13 Apr 2025 18:31:52 +0000 (14:31 -0400)
committerMark Levedahl <mlevedahl@gmail.com>
Sat, 19 Jul 2025 13:12:11 +0000 (09:12 -0400)
git-gui invokes the tk_getSaveFile dialog to determine the full
path-name of the shortcut file to create. But, on Windows, this dialog
always dereferences a shortcut (.lnk) file, as this is essentially a
soft-link to its target. If the shortcut file already exists, the dialog
returns the path-name of the target (i.e., GIT/cmd/git-gui.exe), and not
the desired shortcut file selected by the user.

There is no Windows file chooser available in Tcl/Tk that does not
dereference .lnk files, so this patch avoids using a dialog: the
shortcut to be created is on the desktop and named as "Git + Repository
Name". If this .lnk file already exists, the user must give permission
to overwrite it or the process terminates.

Signed-off-by: Mark Levedahl <mlevedahl@gmail.com>
lib/shortcut.tcl

index 1d01d9cbfab0badef3fe0091a69a9a70c636a702..feaccbdd1d58a4a92287b67301067fc09e2ac02d 100644 (file)
@@ -3,27 +3,38 @@
 
 proc do_windows_shortcut {} {
        global _gitworktree
-       set fn [tk_getSaveFile \
-               -parent . \
-               -title [mc "%s (%s): Create Desktop Icon" [appname] [reponame]] \
-               -initialfile "Git [reponame].lnk"]
-       if {$fn != {}} {
-               if {[file extension $fn] ne {.lnk}} {
-                       set fn ${fn}.lnk
-               }
-               # Use git-gui.exe if available (ie: git-for-windows)
-               set cmdLine [list [_which git-gui]]
-               if {$cmdLine eq {}} {
-                       set cmdLine [list [info nameofexecutable] \
-                                                        [file normalize $::argv0]]
-               }
-               if {[catch {
-                               win32_create_lnk $fn $cmdLine \
-                                       [file normalize $_gitworktree]
-                       } err]} {
-                       error_popup [strcat [mc "Cannot write shortcut:"] "\n\n$err"]
+
+       set desktop [safe_exec [list cygpath -mD]]
+       set link_file "Git [reponame].lnk"
+       set link_path [file normalize [file join $desktop $link_file]]
+
+       # on Windows, tk_getSaveFile dereferences .lnk files, so no simple
+       # filename chooser is available. Use the default or quit.
+       if {[file exists $link_path]} {
+               set answer [tk_messageBox \
+                       -type yesno \
+                       -title [mc "%s (%s): Create Desktop Icon" [appname] [reponame]] \
+                       -default yes \
+                       -message [mc "Replace existing shortcut: %s?" $link_file]]
+               if {$answer == no} {
+                       return
                }
        }
+
+       # Use git-gui.exe if found, fall back to wish + launcher
+       set link_arguments {}
+       set link_target [_which git-gui]
+       if {![file executable $link_target]} {
+               set link_target [file normalize [info nameofexecutable]]
+               set link_arguments [file normalize $::argv0]
+       }
+       set cmdLine [list $link_target $link_arguments]
+       if {[catch {
+               win32_create_lnk $link_path $cmdLine \
+                       [file normalize $_gitworktree]
+       } err]} {
+               error_popup [strcat [mc "Cannot write shortcut:"] "\n\n$err"]
+       }
 }
 
 proc do_cygwin_shortcut {} {