git-gui: Always use lsearch -exact, to prevent globbing.
[gitweb.git] / git-gui.sh
index 5d418b99c8a3ef0873aad34d0b567ee48f6a064e..b203c56a98a49c7344a9146e2c76af4e7f2adc27 100755 (executable)
@@ -568,7 +568,7 @@ proc reshow_diff {} {
        if {$p eq {}
                || $current_diff_side eq {}
                || [catch {set s $file_states($p)}]
-               || [lsearch -sorted $file_lists($current_diff_side) $p] == -1} {
+               || [lsearch -sorted -exact $file_lists($current_diff_side) $p] == -1} {
                clear_diff
        } else {
                show_diff $p $current_diff_side
@@ -607,16 +607,13 @@ proc show_diff {path w {lno {}}} {
        if {$diff_active || ![lock_index read]} return
 
        clear_diff
-       if {$w eq {} || $lno == {}} {
-               foreach w [array names file_lists] {
-                       set lno [lsearch -sorted $file_lists($w) $path]
-                       if {$lno >= 0} {
-                               incr lno
-                               break
-                       }
+       if {$lno == {}} {
+               set lno [lsearch -sorted -exact $file_lists($w) $path]
+               if {$lno >= 0} {
+                       incr lno
                }
        }
-       if {$w ne {} && $lno >= 1} {
+       if {$lno >= 1} {
                $w tag add in_diff $lno.0 [expr {$lno + 1}].0
        }
 
@@ -1430,7 +1427,7 @@ proc display_file_helper {w path icon_name old_m new_m} {
        global file_lists
 
        if {$new_m eq {_}} {
-               set lno [lsearch -sorted $file_lists($w) $path]
+               set lno [lsearch -sorted -exact $file_lists($w) $path]
                if {$lno >= 0} {
                        set file_lists($w) [lreplace $file_lists($w) $lno $lno]
                        incr lno
@@ -1441,7 +1438,7 @@ proc display_file_helper {w path icon_name old_m new_m} {
        } elseif {$old_m eq {_} && $new_m ne {_}} {
                lappend file_lists($w) $path
                set file_lists($w) [lsort -unique $file_lists($w)]
-               set lno [lsearch -sorted $file_lists($w) $path]
+               set lno [lsearch -sorted -exact $file_lists($w) $path]
                incr lno
                $w conf -state normal
                $w image create $lno.0 \
@@ -1822,7 +1819,9 @@ proc populate_branch_menu {} {
                }
        }
 
-       $m add separator
+       if {$all_heads ne {}} {
+               $m add separator
+       }
        foreach b $all_heads {
                $m add radiobutton \
                        -label $b \
@@ -1865,8 +1864,9 @@ proc do_create_branch_action {w} {
        global all_heads null_sha1 repo_config
        global create_branch_checkout create_branch_revtype
        global create_branch_head create_branch_trackinghead
+       global create_branch_name create_branch_revexp
 
-       set newbranch [string trim [$w.desc.name_t get 0.0 end]]
+       set newbranch $create_branch_name
        if {$newbranch eq {}
                || $newbranch eq $repo_config(gui.newbranchtemplate)} {
                tk_messageBox \
@@ -1903,7 +1903,7 @@ proc do_create_branch_action {w} {
        switch -- $create_branch_revtype {
        head {set rev $create_branch_head}
        tracking {set rev $create_branch_trackinghead}
-       expression {set rev [string trim [$w.from.exp_t get 0.0 end]]}
+       expression {set rev $create_branch_revexp}
        }
        if {[catch {set cmt [exec git rev-parse --verify "${rev}^0"]}]} {
                tk_messageBox \
@@ -1958,6 +1958,7 @@ proc do_create_branch {} {
        global all_heads current_branch repo_config
        global create_branch_checkout create_branch_revtype
        global create_branch_head create_branch_trackinghead
+       global create_branch_name create_branch_revexp
 
        set w .branch_editor
        toplevel $w
@@ -1983,26 +1984,18 @@ proc do_create_branch {} {
                -text {Branch Description} \
                -font font_ui
        label $w.desc.name_l -text {Name:} -font font_ui
-       text $w.desc.name_t \
+       entry $w.desc.name_t \
                -borderwidth 1 \
                -relief sunken \
-               -height 1 \
                -width 40 \
-               -font font_ui
-       $w.desc.name_t insert 0.0 $repo_config(gui.newbranchtemplate)
-       grid $w.desc.name_l $w.desc.name_t -sticky we -padx {0 5}
-       bind $w.desc.name_t <Shift-Key-Tab> {focus [tk_focusPrev %W];break}
-       bind $w.desc.name_t <Key-Tab> {focus [tk_focusNext %W];break}
-       bind $w.desc.name_t <Key-Return> "do_create_branch_action $w;break"
-       bind $w.desc.name_t <Key> {
-               if {{%K} ne {BackSpace}
-                       && {%K} ne {Tab}
-                       && {%K} ne {Escape}
-                       && {%K} ne {Return}} {
-                       if {%k <= 32} break
-                       if {[string first %A {~^:?*[}] >= 0} break
+               -textvariable create_branch_name \
+               -font font_ui \
+               -validate key \
+               -validatecommand {
+                       if {%d == 1 && [regexp {[~^:?*\[\0- ]} %S]} {return 0}
+                       return 1
                }
-       }
+       grid $w.desc.name_l $w.desc.name_t -sticky we -padx {0 5}
        grid columnconfigure $w.desc 1 -weight 1
        pack $w.desc -anchor nw -fill x -pady 5 -padx 5
 
@@ -2034,18 +2027,21 @@ proc do_create_branch {} {
                -value expression \
                -variable create_branch_revtype \
                -font font_ui
-       text $w.from.exp_t \
+       entry $w.from.exp_t \
                -borderwidth 1 \
                -relief sunken \
-               -height 1 \
                -width 50 \
-               -font font_ui
+               -textvariable create_branch_revexp \
+               -font font_ui \
+               -validate key \
+               -validatecommand {
+                       if {%d == 1 && [regexp {\s} %S]} {return 0}
+                       if {%d == 1 && [string length %S] > 0} {
+                               set create_branch_revtype expression
+                       }
+                       return 1
+               }
        grid $w.from.exp_r $w.from.exp_t -sticky we -padx {0 5}
-       bind $w.from.exp_t <Shift-Key-Tab> {focus [tk_focusPrev %W];break}
-       bind $w.from.exp_t <Key-Tab> {focus [tk_focusNext %W];break}
-       bind $w.from.exp_t <Key-Return> "do_create_branch_action $w;break"
-       bind $w.from.exp_t <Key-space> break
-       bind $w.from.exp_t <Key> {set create_branch_revtype expression}
        grid columnconfigure $w.from 1 -weight 1
        pack $w.from -anchor nw -fill x -pady 5 -padx 5
 
@@ -2062,8 +2058,14 @@ proc do_create_branch {} {
        set create_branch_checkout 1
        set create_branch_head $current_branch
        set create_branch_revtype head
+       set create_branch_name $repo_config(gui.newbranchtemplate)
+       set create_branch_revexp {}
 
-       bind $w <Visibility> "grab $w; focus $w.desc.name_t"
+       bind $w <Visibility> "
+               grab $w
+               $w.desc.name_t icursor end
+               focus $w.desc.name_t
+       "
        bind $w <Key-Escape> "destroy $w"
        bind $w <Key-Return> "do_create_branch_action $w;break"
        wm title $w "[appname] ([reponame]): Create Branch"
@@ -2140,7 +2142,7 @@ Delete the selected branches?}
                if {[catch {exec git update-ref -d "refs/heads/$b" $o} err]} {
                        append failed " - $b: $err\n"
                } else {
-                       set x [lsearch -sorted $all_heads $b]
+                       set x [lsearch -sorted -exact $all_heads $b]
                        if {$x >= 0} {
                                set all_heads [lreplace $all_heads $x $x]
                        }
@@ -2266,6 +2268,13 @@ The rescan will be automatically started now.
                return
        }
 
+       # -- Don't do a pointless switch.
+       #
+       if {$current_branch eq $new_branch} {
+               unlock_index
+               return
+       }
+
        if {$repo_config(gui.trustmtime) eq {true}} {
                switch_branch_stage2 {} $new_branch
        } else {
@@ -2426,9 +2435,10 @@ proc load_all_remotes {} {
        set all_remotes [lsort -unique $all_remotes]
 }
 
-proc populate_fetch_menu {m} {
+proc populate_fetch_menu {} {
        global all_remotes repo_config
 
+       set m .mbar.fetch
        foreach r $all_remotes {
                set enable 0
                if {![catch {set a $repo_config(remote.$r.url)}]} {
@@ -2457,9 +2467,10 @@ proc populate_fetch_menu {m} {
        }
 }
 
-proc populate_push_menu {m} {
+proc populate_push_menu {} {
        global all_remotes repo_config
 
+       set m .mbar.push
        foreach r $all_remotes {
                set enable 0
                if {![catch {set a $repo_config(remote.$r.url)}]} {
@@ -2488,43 +2499,6 @@ proc populate_push_menu {m} {
        }
 }
 
-proc populate_pull_menu {m} {
-       global repo_config all_remotes disable_on_lock
-
-       foreach remote $all_remotes {
-               set rb_list [list]
-               if {[array get repo_config remote.$remote.url] ne {}} {
-                       if {[array get repo_config remote.$remote.fetch] ne {}} {
-                               foreach line $repo_config(remote.$remote.fetch) {
-                                       if {[regexp {^([^:]+):} $line line rb]} {
-                                               lappend rb_list $rb
-                                       }
-                               }
-                       }
-               } else {
-                       catch {
-                               set fd [open [gitdir remotes $remote] r]
-                               while {[gets $fd line] >= 0} {
-                                       if {[regexp {^Pull:[ \t]*([^:]+):} $line line rb]} {
-                                               lappend rb_list $rb
-                                       }
-                               }
-                               close $fd
-                       }
-               }
-
-               foreach rb $rb_list {
-                       regsub ^refs/heads/ $rb {} rb_short
-                       $m add command \
-                               -label "Branch $rb_short from $remote..." \
-                               -command [list pull_remote $remote $rb] \
-                               -font font_ui
-                       lappend disable_on_lock \
-                               [list $m entryconf [$m index last] -state]
-               }
-       }
-}
-
 ######################################################################
 ##
 ## icons
@@ -2950,7 +2924,7 @@ proc do_stats {} {
                label $w.stat.v_$name -text $value -anchor w -font font_ui
                grid $w.stat.l_$name $w.stat.v_$name -sticky we -padx {0 5}
        }
-       pack $w.stat
+       pack $w.stat -pady 10 -padx 10
 
        bind $w <Visibility> "grab $w; focus $w"
        bind $w <Key-Escape> [list destroy $w]
@@ -3329,10 +3303,7 @@ proc do_options {} {
        pack $w.buttons.restore -side left
        button $w.buttons.save -text Save \
                -font font_ui \
-               -command "
-                       catch {eval \[bind \[focus -displayof $w\] <FocusOut>\]}
-                       do_save_config $w
-               "
+               -command [list do_save_config $w]
        pack $w.buttons.save -side right
        button $w.buttons.cancel -text {Cancel} \
                -font font_ui \
@@ -3382,21 +3353,12 @@ proc do_options {} {
                        t {
                                frame $w.$f.$name
                                label $w.$f.$name.l -text "$text:" -font font_ui
-                               text $w.$f.$name.v \
+                               entry $w.$f.$name.v \
                                        -borderwidth 1 \
                                        -relief sunken \
-                                       -height 1 \
                                        -width 20 \
+                                       -textvariable ${f}_config_new(gui.$name) \
                                        -font font_ui
-                               $w.$f.$name.v insert 0.0 [set ${f}_config_new(gui.$name)]
-                               bind $w.$f.$name.v <Shift-Key-Tab> {focus [tk_focusPrev %W];break}
-                               bind $w.$f.$name.v <Key-Tab> {focus [tk_focusNext %W];break}
-                               bind $w.$f.$name.v <Key-Return> break
-                               bind $w.$f.$name.v <FocusIn> "$w.$f.$name.v tag add sel 0.0 end"
-                               bind $w.$f.$name.v <FocusOut> "
-                                       set ${f}_config_new(gui.$name) \
-                                       \[string trim \[$w.$f.$name.v get 0.0 end\]\]
-                               "
                                pack $w.$f.$name.l -side left -anchor w
                                pack $w.$f.$name.v -side left -anchor w \
                                        -fill x -expand 1 \
@@ -3760,7 +3722,6 @@ if {!$single_commit} {
 .mbar add cascade -label Commit -menu .mbar.commit
 if {!$single_commit} {
        .mbar add cascade -label Fetch -menu .mbar.fetch
-       .mbar add cascade -label Pull -menu .mbar.pull
        .mbar add cascade -label Push -menu .mbar.push
 }
 . configure -menu .mbar
@@ -3772,12 +3733,10 @@ menu .mbar.repository
        -label {Visualize Current Branch} \
        -command {do_gitk {}} \
        -font font_ui
-if {![is_MacOSX]} {
-       .mbar.repository add command \
-               -label {Visualize All Branches} \
-               -command {do_gitk {--all}} \
-               -font font_ui
-}
+.mbar.repository add command \
+       -label {Visualize All Branches} \
+       -command {do_gitk {--all}} \
+       -font font_ui
 .mbar.repository add separator
 
 if {!$single_commit} {
@@ -3940,7 +3899,6 @@ lappend disable_on_lock \
 #
 if {!$single_commit} {
        menu .mbar.fetch
-       menu .mbar.pull
        menu .mbar.push
 }
 
@@ -4419,6 +4377,7 @@ bind_button3 $ui_diff "
        }
        tk_popup $ctxm %X %Y
 "
+unset ui_diff_applyhunk
 
 # -- Status Bar
 #
@@ -4471,6 +4430,7 @@ bind $ui_diff <Key-Up>     {catch {%W yview scroll -1 units};break}
 bind $ui_diff <Key-Down>   {catch {%W yview scroll  1 units};break}
 bind $ui_diff <Key-Left>   {catch {%W xview scroll -1 units};break}
 bind $ui_diff <Key-Right>  {catch {%W xview scroll  1 units};break}
+bind $ui_diff <Button-1>   {focus %W}
 
 if {!$single_commit} {
        bind . <$M1B-Key-n> do_create_branch
@@ -4574,9 +4534,8 @@ if {!$single_commit} {
        load_all_heads
 
        populate_branch_menu
-       populate_fetch_menu .mbar.fetch
-       populate_pull_menu .mbar.pull
-       populate_push_menu .mbar.push
+       populate_fetch_menu
+       populate_push_menu
 }
 
 # -- Only suggest a gc run if we are going to stay running.