@matheusbsbs / matheus-bagliano / commits / 0ce76ded1b
Fork 0

git-gui: Add a Tools menu for arbitrary commands.

Due to the emphasis on scriptability in the git design, it is impossible to provide 100% complete GUI. Currently unaccounted areas include git-svn and other source control system interfaces, TopGit, all custom scripts. This problem can be mitigated by providing basic customization capabilities in Git Gui. This commit adds a new Tools menu, which can be configured to contain items invoking arbitrary shell commands. The interface is powerful enough to allow calling both batch text programs like git-svn, and GUI editors. To support the latter use, the commands have access to the name of the currently selected file through the environment. Signed-off-by: Alexander Gavrilov <angavrilov@gmail.com> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Alexander Gavrilov committed Nov 16, 2008 at 21:46 UTC 0ce76ded1b207063a460f859fe7bfeb6d90b4eb6
3 files changed +359
git-gui.sh
+17
@@ -2289,6 +2289,9 @@ if {[is_enabled transport]} {
2289 .mbar add cascade -label [mc Merge] -menu .mbar.merge
2290 .mbar add cascade -label [mc Remote] -menu .mbar.remote
2291 }
2292 +if {[is_enabled multicommit] || [is_enabled singlecommit]} {
2293 + .mbar add cascade -label [mc Tools] -menu .mbar.tools
2294 +}
2295 . configure -menu .mbar
2296
2297 # -- Repository Menu
@@ -2563,6 +2566,20 @@ if {[is_MacOSX]} {
2566 -command do_options
2567 }
2568
2569 +# -- Tools Menu
2570 +#
2571 +if {[is_enabled multicommit] || [is_enabled singlecommit]} {
2572 + set tools_menubar .mbar.tools
2573 + menu $tools_menubar
2574 + $tools_menubar add separator
2575 + $tools_menubar add command -label [mc "Add..."] -command tools_add::dialog
2576 + $tools_menubar add command -label [mc "Remove..."] -command tools_remove::dialog
2577 + set tools_tailcnt 3
2578 + if {[array names repo_config guitool.*.cmd] ne {}} {
2579 + tools_populate_all
2580 + }
2581 +}
2582 +
2583 # -- Help Menu
2584 #
2585 .mbar add cascade -label [mc Help] -menu .mbar.help
lib/tools.tcl new
+108
@@ -0,0 +1,108 @@
1 +# git-gui Tools menu implementation
2 +
3 +proc tools_list {} {
4 + global repo_config
5 +
6 + set names {}
7 + foreach item [array names repo_config guitool.*.cmd] {
8 + lappend names [string range $item 8 end-4]
9 + }
10 + return [lsort $names]
11 +}
12 +
13 +proc tools_populate_all {} {
14 + global tools_menubar tools_menutbl
15 + global tools_tailcnt
16 +
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
21 + }
22 +
23 + array unset tools_menutbl
24 +
25 + foreach fullname [tools_list] {
26 + tools_populate_one $fullname
27 + }
28 +}
29 +
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
37 + }
38 +}
39 +
40 +proc tools_populate_one {fullname} {
41 + global tools_menubar tools_menutbl tools_id
42 +
43 + if {![info exists tools_id]} {
44 + set tools_id 0
45 + }
46 +
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
61 + }
62 + }
63 +
64 + tools_create_item $parent command \
65 + -label [lindex $names end] \
66 + -command [list tools_exec $fullname]
67 +}
68 +
69 +proc tools_exec {fullname} {
70 + global repo_config env current_diff_path
71 + global current_branch is_detached
72 +
73 + if {[is_config_true "guitool.$fullname.needsfile"]} {
74 + if {$current_diff_path eq {}} {
75 + error_popup [mc "Running %s requires a selected file." $fullname]
76 + return
77 + }
78 + }
79 +
80 + if {[is_config_true "guitool.$fullname.confirm"]} {
81 + if {[ask_popup [mc "Are you sure you want to run %s?" $fullname]] ne {yes}} {
82 + return
83 + }
84 + }
85 +
86 + set env(GIT_GUITOOL) $fullname
87 + set env(FILENAME) $current_diff_path
88 + if {$is_detached} {
89 + set env(CUR_BRANCH) ""
90 + } else {
91 + set env(CUR_BRANCH) $current_branch
92 + }
93 +
94 + set cmdline $repo_config(guitool.$fullname.cmd)
95 + if {[is_config_true "guitool.$fullname.noconsole"]} {
96 + exec sh -c $cmdline &
97 + } else {
98 + regsub {/} $fullname { / } title
99 + set w [console::new \
100 + [mc "Tool: %s" $title] \
101 + [mc "Running: %s" $cmdline]]
102 + console::exec $w [list sh -c $cmdline]
103 + }
104 +
105 + unset env(GIT_GUITOOL)
106 + unset env(FILENAME)
107 + unset env(CUR_BRANCH)
108 +}
lib/tools_dlg.tcl new
+234
@@ -0,0 +1,234 @@
1 +# git-gui Tools menu dialogs
2 +
3 +class tools_add {
4 +
5 +field w ; # widget path
6 +field w_name ; # new remote name widget
7 +field w_cmd ; # new remote location widget
8 +
9 +field name {}; # name of the tool
10 +field command {}; # command to execute
11 +field add_global 0; # add to the --global config
12 +field no_console 0; # disable using the console
13 +field needs_file 0; # ensure filename is set
14 +field confirm 0; # ask for confirmation
15 +
16 +constructor dialog {} {
17 + global repo_config
18 +
19 + make_toplevel top w
20 + wm title $top [append "[appname] ([reponame]): " [mc "Add Tool"]]
21 + if {$top ne {.}} {
22 + wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
23 + wm transient $top .
24 + }
25 +
26 + label $w.header -text [mc "Add New Tool Command"] -font font_uibold
27 + pack $w.header -side top -fill x
28 +
29 + frame $w.buttons
30 + checkbutton $w.buttons.global \
31 + -text [mc "Add globally"] \
32 + -variable @add_global
33 + pack $w.buttons.global -side left -padx 5
34 + button $w.buttons.create -text [mc Add] \
35 + -default active \
36 + -command [cb _add]
37 + pack $w.buttons.create -side right
38 + button $w.buttons.cancel -text [mc Cancel] \
39 + -command [list destroy $w]
40 + pack $w.buttons.cancel -side right -padx 5
41 + pack $w.buttons -side bottom -fill x -pady 10 -padx 10
42 +
43 + labelframe $w.desc -text [mc "Tool Details"]
44 +
45 + label $w.desc.name_cmnt -anchor w\
46 + -text [mc "Use '/' separators to create a submenu tree:"]
47 + grid x $w.desc.name_cmnt -sticky we -padx {0 5} -pady {0 2}
48 + label $w.desc.name_l -text [mc "Name:"]
49 + set w_name $w.desc.name_t
50 + entry $w_name \
51 + -borderwidth 1 \
52 + -relief sunken \
53 + -width 40 \
54 + -textvariable @name \
55 + -validate key \
56 + -validatecommand [cb _validate_name %d %S]
57 + grid $w.desc.name_l $w_name -sticky we -padx {0 5}
58 +
59 + label $w.desc.cmd_l -text [mc "Command:"]
60 + set w_cmd $w.desc.cmd_t
61 + entry $w_cmd \
62 + -borderwidth 1 \
63 + -relief sunken \
64 + -width 40 \
65 + -textvariable @command
66 + grid $w.desc.cmd_l $w_cmd -sticky we -padx {0 5} -pady {0 3}
67 +
68 + grid columnconfigure $w.desc 1 -weight 1
69 + pack $w.desc -anchor nw -fill x -pady 5 -padx 5
70 +
71 + checkbutton $w.confirm \
72 + -text [mc "Ask for confirmation before running"] \
73 + -variable @confirm
74 + pack $w.confirm -anchor w -pady {5 0} -padx 5
75 +
76 + checkbutton $w.noconsole \
77 + -text [mc "Don't show the command output window"] \
78 + -variable @no_console
79 + pack $w.noconsole -anchor w -padx 5
80 +
81 + checkbutton $w.needsfile \
82 + -text [mc "Run only if a diff is selected (\$FILENAME not empty)"] \
83 + -variable @needs_file
84 + pack $w.needsfile -anchor w -padx 5
85 +
86 + bind $w <Visibility> [cb _visible]
87 + bind $w <Key-Escape> [list destroy $w]
88 + bind $w <Key-Return> [cb _add]\;break
89 + tkwait window $w
90 +}
91 +
92 +method _add {} {
93 + global repo_config
94 +
95 + if {$name eq {}} {
96 + error_popup [mc "Please supply a name for the tool."]
97 + focus $w_name
98 + return
99 + }
100 +
101 + set item "guitool.$name.cmd"
102 +
103 + if {[info exists repo_config($item)]} {
104 + error_popup [mc "Tool '%s' already exists." $name]
105 + focus $w_name
106 + return
107 + }
108 +
109 + set cmd [list git config]
110 + if {$add_global} { lappend cmd --global }
111 + set items {}
112 + if {$no_console} { lappend items "guitool.$name.noconsole" }
113 + if {$confirm} { lappend items "guitool.$name.confirm" }
114 + if {$needs_file} { lappend items "guitool.$name.needsfile" }
115 +
116 + if {[catch {
117 + eval $cmd [list $item $command]
118 + foreach citem $items { eval $cmd [list $citem yes] }
119 + } err]} {
120 + error_popup [mc "Could not add tool:\n%s" $err]
121 + } else {
122 + set repo_config($item) $command
123 + foreach citem $items { set repo_config($citem) yes }
124 +
125 + tools_populate_all
126 + }
127 +
128 + destroy $w
129 +}
130 +
131 +method _validate_name {d S} {
132 + if {$d == 1} {
133 + if {[regexp {[~?*&\[\0\"\\\{]} $S]} {
134 + return 0
135 + }
136 + }
137 + return 1
138 +}
139 +
140 +method _visible {} {
141 + grab $w
142 + $w_name icursor end
143 + focus $w_name
144 +}
145 +
146 +}
147 +
148 +class tools_remove {
149 +
150 +field w ; # widget path
151 +field w_names ; # name list
152 +
153 +constructor dialog {} {
154 + global repo_config global_config system_config
155 +
156 + load_config 1
157 +
158 + make_toplevel top w
159 + wm title $top [append "[appname] ([reponame]): " [mc "Remove Tool"]]
160 + if {$top ne {.}} {
161 + wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
162 + wm transient $top .
163 + }
164 +
165 + label $w.header -text [mc "Remove Tool Commands"] -font font_uibold
166 + pack $w.header -side top -fill x
167 +
168 + frame $w.buttons
169 + button $w.buttons.create -text [mc Remove] \
170 + -default active \
171 + -command [cb _remove]
172 + pack $w.buttons.create -side right
173 + button $w.buttons.cancel -text [mc Cancel] \
174 + -command [list destroy $w]
175 + pack $w.buttons.cancel -side right -padx 5
176 + pack $w.buttons -side bottom -fill x -pady 10 -padx 10
177 +
178 + frame $w.list
179 + set w_names $w.list.l
180 + listbox $w_names \
181 + -height 10 \
182 + -width 30 \
183 + -selectmode extended \
184 + -exportselection false \
185 + -yscrollcommand [list $w.list.sby set]
186 + scrollbar $w.list.sby -command [list $w.list.l yview]
187 + pack $w.list.sby -side right -fill y
188 + pack $w.list.l -side left -fill both -expand 1
189 + pack $w.list -fill both -expand 1 -pady 5 -padx 5
190 +
191 + set local_cnt 0
192 + foreach fullname [tools_list] {
193 + # Cannot delete system tools
194 + if {[info exists system_config(guitool.$fullname.cmd)]} continue
195 +
196 + $w_names insert end $fullname
197 + if {![info exists global_config(guitool.$fullname.cmd)]} {
198 + $w_names itemconfigure end -foreground blue
199 + incr local_cnt
200 + }
201 + }
202 +
203 + if {$local_cnt > 0} {
204 + label $w.colorlbl -foreground blue \
205 + -text [mc "(Blue denotes repository-local tools)"]
206 + pack $w.colorlbl -fill x -pady 5 -padx 5
207 + }
208 +
209 + bind $w <Visibility> [cb _visible]
210 + bind $w <Key-Escape> [list destroy $w]
211 + bind $w <Key-Return> [cb _remove]\;break
212 + tkwait window $w
213 +}
214 +
215 +method _remove {} {
216 + foreach i [$w_names curselection] {
217 + set name [$w_names get $i]
218 +
219 + catch { git config --remove-section guitool.$name }
220 + catch { git config --global --remove-section guitool.$name }
221 + }
222 +
223 + load_config 0
224 + tools_populate_all
225 +
226 + destroy $w
227 +}
228 +
229 +method _visible {} {
230 + grab $w
231 + focus $w_names
232 +}
233 +
234 +}