git-gui: return early when patch fails to apply
[gitweb.git] / lib / tools.tcl
index 00d46ddab5fcb9b872c3083959f7f9feb06e9fc9..413f1a170079e0cec78ecdbd1adb7baeb83406f2 100644 (file)
@@ -69,6 +69,7 @@ proc tools_populate_one {fullname} {
 proc tools_exec {fullname} {
        global repo_config env current_diff_path
        global current_branch is_detached
+       global selected_paths
 
        if {[is_config_true "guitool.$fullname.needsfile"]} {
                if {$current_diff_path eq {}} {
@@ -77,14 +78,30 @@ proc tools_exec {fullname} {
                }
        }
 
-       if {[is_config_true "guitool.$fullname.confirm"]} {
-               if {[ask_popup [mc "Are you sure you want to run %s?" $fullname]] ne {yes}} {
+       catch { unset env(ARGS) }
+       catch { unset env(REVISION) }
+
+       if {[get_config "guitool.$fullname.revprompt"] ne {} ||
+           [get_config "guitool.$fullname.argprompt"] ne {}} {
+               set dlg [tools_askdlg::dialog $fullname]
+               if {![tools_askdlg::execute $dlg]} {
                        return
                }
+       } elseif {[is_config_true "guitool.$fullname.confirm"]} {
+               if {[is_config_true "guitool.$fullname.needsfile"]} {
+                       if {[ask_popup [mc "Are you sure you want to run %1\$s on file \"%2\$s\"?" $fullname $current_diff_path]] ne {yes}} {
+                               return
+                       }
+               } else {
+                       if {[ask_popup [mc "Are you sure you want to run %s?" $fullname]] ne {yes}} {
+                               return
+                       }
+               }
        }
 
        set env(GIT_GUITOOL) $fullname
        set env(FILENAME) $current_diff_path
+       set env(FILENAMES) [join [array names selected_paths] \n]
        if {$is_detached} {
                set env(CUR_BRANCH) ""
        } else {
@@ -93,16 +110,59 @@ proc tools_exec {fullname} {
 
        set cmdline $repo_config(guitool.$fullname.cmd)
        if {[is_config_true "guitool.$fullname.noconsole"]} {
-               exec sh -c $cmdline &
+               tools_run_silent [list sh -c $cmdline] \
+                                [list tools_complete $fullname {}]
        } else {
                regsub {/} $fullname { / } title
                set w [console::new \
                        [mc "Tool: %s" $title] \
                        [mc "Running: %s" $cmdline]]
-               console::exec $w [list sh -c $cmdline]
+               console::exec $w [list sh -c $cmdline] \
+                                [list tools_complete $fullname $w]
        }
 
        unset env(GIT_GUITOOL)
        unset env(FILENAME)
+       unset env(FILENAMES)
        unset env(CUR_BRANCH)
+       catch { unset env(ARGS) }
+       catch { unset env(REVISION) }
+}
+
+proc tools_run_silent {cmd after} {
+       lappend cmd 2>@1
+       set fd [_open_stdout_stderr $cmd]
+
+       fconfigure $fd -blocking 0 -translation binary
+       fileevent $fd readable [list tools_consume_input $fd $after]
+}
+
+proc tools_consume_input {fd after} {
+       read $fd
+       if {[eof $fd]} {
+               fconfigure $fd -blocking 1
+               if {[catch {close $fd}]} {
+                       uplevel #0 $after 0
+               } else {
+                       uplevel #0 $after 1
+               }
+       }
+}
+
+proc tools_complete {fullname w {ok 1}} {
+       if {$w ne {}} {
+               console::done $w $ok
+       }
+
+       if {$ok} {
+               set msg [mc "Tool completed successfully: %s" $fullname]
+       } else {
+               set msg [mc "Tool failed: %s" $fullname]
+       }
+
+       if {[is_config_true "guitool.$fullname.norescan"]} {
+               ui_status $msg
+       } else {
+               rescan [list ui_status $msg]
+       }
 }