Alexander Gavrilov | 0ce76de | 2008-11-16 21:46:49 +0300 | [diff] [blame] | 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 | |
Alexander Gavrilov | 67df911 | 2008-11-16 21:46:50 +0300 | [diff] [blame] | 80 | catch { unset env(ARGS) } |
| 81 | catch { unset env(REVISION) } |
| 82 | |
| 83 | if {[get_config "guitool.$fullname.revprompt"] ne {} || |
| 84 | [get_config "guitool.$fullname.argprompt"] ne {}} { |
| 85 | set dlg [tools_askdlg::dialog $fullname] |
| 86 | if {![tools_askdlg::execute $dlg]} { |
| 87 | return |
| 88 | } |
| 89 | } elseif {[is_config_true "guitool.$fullname.confirm"]} { |
Alexander Gavrilov | 0ce76de | 2008-11-16 21:46:49 +0300 | [diff] [blame] | 90 | if {[ask_popup [mc "Are you sure you want to run %s?" $fullname]] ne {yes}} { |
| 91 | return |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | set env(GIT_GUITOOL) $fullname |
| 96 | set env(FILENAME) $current_diff_path |
| 97 | if {$is_detached} { |
| 98 | set env(CUR_BRANCH) "" |
| 99 | } else { |
| 100 | set env(CUR_BRANCH) $current_branch |
| 101 | } |
| 102 | |
| 103 | set cmdline $repo_config(guitool.$fullname.cmd) |
| 104 | if {[is_config_true "guitool.$fullname.noconsole"]} { |
Alexander Gavrilov | b8dfb16 | 2008-11-16 21:46:51 +0300 | [diff] [blame] | 105 | tools_run_silent [list sh -c $cmdline] \ |
| 106 | [list tools_complete $fullname {}] |
Alexander Gavrilov | 0ce76de | 2008-11-16 21:46:49 +0300 | [diff] [blame] | 107 | } else { |
| 108 | regsub {/} $fullname { / } title |
| 109 | set w [console::new \ |
| 110 | [mc "Tool: %s" $title] \ |
| 111 | [mc "Running: %s" $cmdline]] |
Alexander Gavrilov | b8dfb16 | 2008-11-16 21:46:51 +0300 | [diff] [blame] | 112 | console::exec $w [list sh -c $cmdline] \ |
| 113 | [list tools_complete $fullname $w] |
Alexander Gavrilov | 0ce76de | 2008-11-16 21:46:49 +0300 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | unset env(GIT_GUITOOL) |
| 117 | unset env(FILENAME) |
| 118 | unset env(CUR_BRANCH) |
Alexander Gavrilov | 67df911 | 2008-11-16 21:46:50 +0300 | [diff] [blame] | 119 | catch { unset env(ARGS) } |
| 120 | catch { unset env(REVISION) } |
Alexander Gavrilov | 0ce76de | 2008-11-16 21:46:49 +0300 | [diff] [blame] | 121 | } |
Alexander Gavrilov | b8dfb16 | 2008-11-16 21:46:51 +0300 | [diff] [blame] | 122 | |
| 123 | proc tools_run_silent {cmd after} { |
| 124 | lappend cmd 2>@1 |
| 125 | set fd [_open_stdout_stderr $cmd] |
| 126 | |
| 127 | fconfigure $fd -blocking 0 -translation binary |
| 128 | fileevent $fd readable [list tools_consume_input $fd $after] |
| 129 | } |
| 130 | |
| 131 | proc tools_consume_input {fd after} { |
| 132 | read $fd |
| 133 | if {[eof $fd]} { |
| 134 | fconfigure $fd -blocking 1 |
| 135 | if {[catch {close $fd}]} { |
| 136 | uplevel #0 $after 0 |
| 137 | } else { |
| 138 | uplevel #0 $after 1 |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | proc tools_complete {fullname w {ok 1}} { |
| 144 | if {$w ne {}} { |
| 145 | console::done $w $ok |
| 146 | } |
| 147 | |
| 148 | if {$ok} { |
Sam Hocevar | 966d077 | 2009-03-24 00:42:24 +0100 | [diff] [blame] | 149 | set msg [mc "Tool completed successfully: %s" $fullname] |
Alexander Gavrilov | b8dfb16 | 2008-11-16 21:46:51 +0300 | [diff] [blame] | 150 | } else { |
| 151 | set msg [mc "Tool failed: %s" $fullname] |
| 152 | } |
| 153 | |
| 154 | if {[is_config_true "guitool.$fullname.norescan"]} { |
| 155 | ui_status $msg |
| 156 | } else { |
| 157 | rescan [list ui_status $msg] |
| 158 | } |
| 159 | } |