Merge remote-tracking branch 'philoakley/dup-gui'
[git-gui.git] / lib / tools.tcl
blob413f1a170079e0cec78ecdbd1adb7baeb83406f2
1 # git-gui Tools menu implementation
3 proc tools_list {} {
4 global repo_config
6 set names {}
7 foreach item [array names repo_config guitool.*.cmd] {
8 lappend names [string range $item 8 end-4]
10 return [lsort $names]
13 proc tools_populate_all {} {
14 global tools_menubar tools_menutbl
15 global tools_tailcnt
17 set mbar_end [$tools_menubar index end]
18 set mbar_base [expr {$mbar_end - $tools_tailcnt}]
19 if {$mbar_base >= 0} {
20 $tools_menubar delete 0 $mbar_base
23 array unset tools_menutbl
25 foreach fullname [tools_list] {
26 tools_populate_one $fullname
30 proc tools_create_item {parent args} {
31 global tools_menubar tools_tailcnt
32 if {$parent eq $tools_menubar} {
33 set pos [expr {[$parent index end]-$tools_tailcnt+1}]
34 eval [list $parent insert $pos] $args
35 } else {
36 eval [list $parent add] $args
40 proc tools_populate_one {fullname} {
41 global tools_menubar tools_menutbl tools_id
43 if {![info exists tools_id]} {
44 set tools_id 0
47 set names [split $fullname '/']
48 set parent $tools_menubar
49 for {set i 0} {$i < [llength $names]-1} {incr i} {
50 set subname [join [lrange $names 0 $i] '/']
51 if {[info exists tools_menutbl($subname)]} {
52 set parent $tools_menutbl($subname)
53 } else {
54 set subid $parent.t$tools_id
55 tools_create_item $parent cascade \
56 -label [lindex $names $i] -menu $subid
57 menu $subid
58 set tools_menutbl($subname) $subid
59 set parent $subid
60 incr tools_id
64 tools_create_item $parent command \
65 -label [lindex $names end] \
66 -command [list tools_exec $fullname]
69 proc tools_exec {fullname} {
70 global repo_config env current_diff_path
71 global current_branch is_detached
72 global selected_paths
74 if {[is_config_true "guitool.$fullname.needsfile"]} {
75 if {$current_diff_path eq {}} {
76 error_popup [mc "Running %s requires a selected file." $fullname]
77 return
81 catch { unset env(ARGS) }
82 catch { unset env(REVISION) }
84 if {[get_config "guitool.$fullname.revprompt"] ne {} ||
85 [get_config "guitool.$fullname.argprompt"] ne {}} {
86 set dlg [tools_askdlg::dialog $fullname]
87 if {![tools_askdlg::execute $dlg]} {
88 return
90 } elseif {[is_config_true "guitool.$fullname.confirm"]} {
91 if {[is_config_true "guitool.$fullname.needsfile"]} {
92 if {[ask_popup [mc "Are you sure you want to run %1\$s on file \"%2\$s\"?" $fullname $current_diff_path]] ne {yes}} {
93 return
95 } else {
96 if {[ask_popup [mc "Are you sure you want to run %s?" $fullname]] ne {yes}} {
97 return
102 set env(GIT_GUITOOL) $fullname
103 set env(FILENAME) $current_diff_path
104 set env(FILENAMES) [join [array names selected_paths] \n]
105 if {$is_detached} {
106 set env(CUR_BRANCH) ""
107 } else {
108 set env(CUR_BRANCH) $current_branch
111 set cmdline $repo_config(guitool.$fullname.cmd)
112 if {[is_config_true "guitool.$fullname.noconsole"]} {
113 tools_run_silent [list sh -c $cmdline] \
114 [list tools_complete $fullname {}]
115 } else {
116 regsub {/} $fullname { / } title
117 set w [console::new \
118 [mc "Tool: %s" $title] \
119 [mc "Running: %s" $cmdline]]
120 console::exec $w [list sh -c $cmdline] \
121 [list tools_complete $fullname $w]
124 unset env(GIT_GUITOOL)
125 unset env(FILENAME)
126 unset env(FILENAMES)
127 unset env(CUR_BRANCH)
128 catch { unset env(ARGS) }
129 catch { unset env(REVISION) }
132 proc tools_run_silent {cmd after} {
133 lappend cmd 2>@1
134 set fd [_open_stdout_stderr $cmd]
136 fconfigure $fd -blocking 0 -translation binary
137 fileevent $fd readable [list tools_consume_input $fd $after]
140 proc tools_consume_input {fd after} {
141 read $fd
142 if {[eof $fd]} {
143 fconfigure $fd -blocking 1
144 if {[catch {close $fd}]} {
145 uplevel #0 $after 0
146 } else {
147 uplevel #0 $after 1
152 proc tools_complete {fullname w {ok 1}} {
153 if {$w ne {}} {
154 console::done $w $ok
157 if {$ok} {
158 set msg [mc "Tool completed successfully: %s" $fullname]
159 } else {
160 set msg [mc "Tool failed: %s" $fullname]
163 if {[is_config_true "guitool.$fullname.norescan"]} {
164 ui_status $msg
165 } else {
166 rescan [list ui_status $msg]