Code

Allow frontends to bidirectionally communicate with fast-import
[git.git] / gitk
diff --git a/gitk b/gitk
index d6f62b2a0f72504dc63e537bd24931737b9647f8..57617d58b070892a730e956e82a2f382c28659e2 100755 (executable)
--- a/gitk
+++ b/gitk
@@ -139,6 +139,10 @@ proc getcommitlines {fd view}  {
     global vparentlist vdisporder vcmitlisted
 
     set stuff [read $fd 500000]
+    # git log doesn't terminate the last commit with a null...
+    if {$stuff == {} && $leftover($view) ne {} && [eof $fd]} {
+       set stuff "\0"
+    }
     if {$stuff == {}} {
        if {![eof $fd]} {
            return 1
@@ -292,7 +296,7 @@ proc readcommit {id} {
 
 proc updatecommits {} {
     global viewdata curview phase displayorder
-    global children commitrow selectedline thickerline
+    global children commitrow selectedline thickerline showneartags
 
     if {$phase ne {}} {
        stop_rev_list
@@ -309,7 +313,9 @@ proc updatecommits {} {
     catch {unset viewdata($n)}
     readrefs
     changedrefs
-    regetallcommits
+    if {$showneartags} {
+       getallcommits
+    }
     showview $n
 }
 
@@ -423,7 +429,7 @@ proc readrefs {} {
            lappend idotherrefs($id) $name
        }
     }
-    close $refd
+    catch {close $refd}
     set mainhead {}
     set mainheadid {}
     catch {
@@ -809,12 +815,23 @@ proc makewindow {} {
         wm geometry . "$geometry(main)"
     }
 
+    if {[tk windowingsystem] eq {aqua}} {
+        set M1B M1
+    } else {
+        set M1B Control
+    }
+
     bind .pwbottom <Configure> {resizecdetpanes %W %w}
     pack .ctop -fill both -expand 1
     bindall <1> {selcanvline %W %x %y}
     #bindall <B1-Motion> {selcanvline %W %x %y}
-    bindall <ButtonRelease-4> "allcanvs yview scroll -5 units"
-    bindall <ButtonRelease-5> "allcanvs yview scroll 5 units"
+    if {[tk windowingsystem] == "win32"} {
+       bind . <MouseWheel> { windows_mousewheel_redirector %W %X %Y %D }
+       bind $ctext <MouseWheel> { windows_mousewheel_redirector %W %X %Y %D ; break }
+    } else {
+       bindall <ButtonRelease-4> "allcanvs yview scroll -5 units"
+       bindall <ButtonRelease-5> "allcanvs yview scroll 5 units"
+    }
     bindall <2> "canvscan mark %W %x %y"
     bindall <B2-Motion> "canvscan dragto %W %x %y"
     bindkey <Home> selfirstline
@@ -827,12 +844,12 @@ proc makewindow {} {
     bindkey <Key-Left> "goback"
     bind . <Key-Prior> "selnextpage -1"
     bind . <Key-Next> "selnextpage 1"
-    bind . <Control-Home> "allcanvs yview moveto 0.0"
-    bind . <Control-End> "allcanvs yview moveto 1.0"
-    bind . <Control-Key-Up> "allcanvs yview scroll -1 units"
-    bind . <Control-Key-Down> "allcanvs yview scroll 1 units"
-    bind . <Control-Key-Prior> "allcanvs yview scroll -1 pages"
-    bind . <Control-Key-Next> "allcanvs yview scroll 1 pages"
+    bind . <$M1B-Home> "allcanvs yview moveto 0.0"
+    bind . <$M1B-End> "allcanvs yview moveto 1.0"
+    bind . <$M1B-Key-Up> "allcanvs yview scroll -1 units"
+    bind . <$M1B-Key-Down> "allcanvs yview scroll 1 units"
+    bind . <$M1B-Key-Prior> "allcanvs yview scroll -1 pages"
+    bind . <$M1B-Key-Next> "allcanvs yview scroll 1 pages"
     bindkey <Key-Delete> "$ctext yview scroll -1 pages"
     bindkey <Key-BackSpace> "$ctext yview scroll -1 pages"
     bindkey <Key-space> "$ctext yview scroll 1 pages"
@@ -852,15 +869,15 @@ proc makewindow {} {
     bindkey ? findprev
     bindkey f nextfile
     bindkey <F5> updatecommits
-    bind . <Control-q> doquit
-    bind . <Control-f> dofind
-    bind . <Control-g> {findnext 0}
-    bind . <Control-r> dosearchback
-    bind . <Control-s> dosearch
-    bind . <Control-equal> {incrfont 1}
-    bind . <Control-KP_Add> {incrfont 1}
-    bind . <Control-minus> {incrfont -1}
-    bind . <Control-KP_Subtract> {incrfont -1}
+    bind . <$M1B-q> doquit
+    bind . <$M1B-f> dofind
+    bind . <$M1B-g> {findnext 0}
+    bind . <$M1B-r> dosearchback
+    bind . <$M1B-s> dosearch
+    bind . <$M1B-equal> {incrfont 1}
+    bind . <$M1B-KP_Add> {incrfont 1}
+    bind . <$M1B-minus> {incrfont -1}
+    bind . <$M1B-KP_Subtract> {incrfont -1}
     wm protocol . WM_DELETE_WINDOW doquit
     bind . <Button-1> "click %W"
     bind $fstring <Key-Return> dofind
@@ -869,6 +886,7 @@ proc makewindow {} {
     bind $cflist <1> {sel_flist %W %x %y; break}
     bind $cflist <B1-Motion> {sel_flist %W %x %y; break}
     bind $cflist <ButtonRelease-1> {treeclick %W %x %y}
+    bind $cflist <Button-3> {pop_flist_menu %W %X %Y %x %y}
 
     set maincursor [. cget -cursor]
     set textcursor [$ctext cget -cursor]
@@ -906,6 +924,32 @@ proc makewindow {} {
        -command cobranch
     $headctxmenu add command -label "Remove this branch" \
        -command rmbranch
+
+    global flist_menu
+    set flist_menu .flistctxmenu
+    menu $flist_menu -tearoff 0
+    $flist_menu add command -label "Highlight this too" \
+       -command {flist_hl 0}
+    $flist_menu add command -label "Highlight this only" \
+       -command {flist_hl 1}
+}
+
+# Windows sends all mouse wheel events to the current focused window, not
+# the one where the mouse hovers, so bind those events here and redirect
+# to the correct window
+proc windows_mousewheel_redirector {W X Y D} {
+    global canv canv2 canv3
+    set w [winfo containing -displayof $W $X $Y]
+    if {$w ne ""} {
+       set u [expr {$D < 0 ? 5 : -5}]
+       if {$w == $canv || $w == $canv2 || $w == $canv3} {
+           allcanvs yview scroll $u units
+       } else {
+           catch {
+               $w yview scroll $u units
+           }
+       }
+    }
 }
 
 # mouse-2 makes all windows scan vertically, but only the one
@@ -945,8 +989,8 @@ proc bindkey {ev script} {
 # set the focus back to the toplevel for any click outside
 # the entry widgets
 proc click {w} {
-    global entries
-    foreach e $entries {
+    global ctext entries
+    foreach e [concat $entries $ctext] {
        if {$w == $e} return
     }
     focus .
@@ -1101,12 +1145,17 @@ proc keys {} {
        raise $w
        return
     }
+    if {[tk windowingsystem] eq {aqua}} {
+       set M1T Cmd
+    } else {
+       set M1T Ctrl
+    }
     toplevel $w
     wm title $w "Gitk key bindings"
-    message $w.m -text {
+    message $w.m -text "
 Gitk key bindings:
 
-<Ctrl-Q>               Quit
+<$M1T-Q>               Quit
 <Home>         Move to first commit
 <End>          Move to last commit
 <Up>, p, i     Move up one commit
@@ -1115,12 +1164,12 @@ Gitk key bindings:
 <Right>, x, l  Go forward in history list
 <PageUp>       Move up one page in commit list
 <PageDown>     Move down one page in commit list
-<Ctrl-Home>    Scroll to top of commit list
-<Ctrl-End>     Scroll to bottom of commit list
-<Ctrl-Up>      Scroll commit list up one line
-<Ctrl-Down>    Scroll commit list down one line
-<Ctrl-PageUp>  Scroll commit list up one page
-<Ctrl-PageDown>        Scroll commit list down one page
+<$M1T-Home>    Scroll to top of commit list
+<$M1T-End>     Scroll to bottom of commit list
+<$M1T-Up>      Scroll commit list up one line
+<$M1T-Down>    Scroll commit list down one line
+<$M1T-PageUp>  Scroll commit list up one page
+<$M1T-PageDown>        Scroll commit list down one page
 <Shift-Up>     Move to previous highlighted line
 <Shift-Down>   Move to next highlighted line
 <Delete>, b    Scroll diff view up one page
@@ -1128,20 +1177,20 @@ Gitk key bindings:
 <Space>                Scroll diff view down one page
 u              Scroll diff view up 18 lines
 d              Scroll diff view down 18 lines
-<Ctrl-F>               Find
-<Ctrl-G>               Move to next find hit
+<$M1T-F>               Find
+<$M1T-G>               Move to next find hit
 <Return>       Move to next find hit
 /              Move to next find hit, or redo find
 ?              Move to previous find hit
 f              Scroll diff view to next file
-<Ctrl-S>               Search for next hit in diff view
-<Ctrl-R>               Search for previous hit in diff view
-<Ctrl-KP+>     Increase font size
-<Ctrl-plus>    Increase font size
-<Ctrl-KP->     Decrease font size
-<Ctrl-minus>   Decrease font size
+<$M1T-S>               Search for next hit in diff view
+<$M1T-R>               Search for previous hit in diff view
+<$M1T-KP+>     Increase font size
+<$M1T-plus>    Increase font size
+<$M1T-KP->     Decrease font size
+<$M1T-minus>   Decrease font size
 <F5>           Update
-} \
+" \
            -justify left -bg white -border 2 -relief groove
     pack $w.m -side top -fill both -padx 2 -pady 2
     $w.m configure -font $uifont
@@ -1484,6 +1533,33 @@ proc sel_flist {w x y} {
     }
 }
 
+proc pop_flist_menu {w X Y x y} {
+    global ctext cflist cmitmode flist_menu flist_menu_file
+    global treediffs diffids
+
+    set l [lindex [split [$w index "@$x,$y"] "."] 0]
+    if {$l <= 1} return
+    if {$cmitmode eq "tree"} {
+       set e [linetoelt $l]
+       if {[string index $e end] eq "/"} return
+    } else {
+       set e [lindex $treediffs($diffids) [expr {$l-2}]]
+    }
+    set flist_menu_file $e
+    tk_popup $flist_menu $X $Y
+}
+
+proc flist_hl {only} {
+    global flist_menu_file highlight_files
+
+    set x [shellquote $flist_menu_file]
+    if {$only || $highlight_files eq {}} {
+       set highlight_files $x
+    } else {
+       append highlight_files " " $x
+    }
+}
+
 # Functions for adding and removing shell-type quoting
 
 proc shellquote {str} {
@@ -2146,7 +2222,7 @@ proc readfhighlight {} {
 
 proc find_change {name ix op} {
     global nhighlights mainfont boldnamerows
-    global findstring findpattern findtype markingmatches
+    global findstring findpattern findtype
 
     # delete previous highlights, if any
     foreach row $boldnamerows {
@@ -2161,7 +2237,6 @@ proc find_change {name ix op} {
                   $findstring]
        set findpattern "*$e*"
     }
-    set markingmatches [expr {$findstring ne {}}]
     drawvisible
 }
 
@@ -2207,26 +2282,32 @@ proc askfindhighlight {row id} {
            }
        }
        if {$markingmatches} {
-           markrowmatches $row [lindex $info 0] [lindex $info 1]
+           markrowmatches $row $id
        }
     }
     set nhighlights($row) $isbold
 }
 
-proc markrowmatches {row headline author} {
-    global canv canv2 linehtag linentag
+proc markrowmatches {row id} {
+    global canv canv2 linehtag linentag commitinfo findloc
 
+    set headline [lindex $commitinfo($id) 0]
+    set author [lindex $commitinfo($id) 1]
     $canv delete match$row
     $canv2 delete match$row
-    set m [findmatches $headline]
-    if {$m ne {}} {
-       markmatches $canv $row $headline $linehtag($row) $m \
-           [$canv itemcget $linehtag($row) -font]
+    if {$findloc eq "All fields" || $findloc eq "Headline"} {
+       set m [findmatches $headline]
+       if {$m ne {}} {
+           markmatches $canv $row $headline $linehtag($row) $m \
+               [$canv itemcget $linehtag($row) -font] $row
+       }
     }
-    set m [findmatches $author]
-    if {$m ne {}} {
-       markmatches $canv2 $row $author $linentag($row) $m \
-           [$canv2 itemcget $linentag($row) -font]
+    if {$findloc eq "All fields" || $findloc eq "Author"} {
+       set m [findmatches $author]
+       if {$m ne {}} {
+           markmatches $canv2 $row $author $linentag($row) $m \
+               [$canv2 itemcget $linentag($row) -font] $row
+       }
     }
 }
 
@@ -2816,17 +2897,12 @@ proc layoutrows {row endrow last} {
     set offs [lindex $rowoffsets $row]
     while {$row < $endrow} {
        set id [lindex $displayorder $row]
-       set oldolds {}
-       set newolds {}
+       set nev [expr {[llength $idlist] - $maxwidth + 1}]
        foreach p [lindex $parentlist $row] {
-           if {![info exists idinlist($p)]} {
-               lappend newolds $p
-           } elseif {!$idinlist($p)} {
-               lappend oldolds $p
+           if {![info exists idinlist($p)] || !$idinlist($p)} {
+               incr nev
            }
        }
-       set nev [expr {[llength $idlist] + [llength $newolds]
-                      + [llength $oldolds] - $maxwidth + 1}]
        if {$nev > 0} {
            if {!$last &&
                $row + $uparrowlen + $mingaplen >= $commitidx($curview)} break
@@ -2845,12 +2921,22 @@ proc layoutrows {row endrow last} {
                        if {[incr nev -1] <= 0} break
                        continue
                    }
-                   set rowchk($id) [expr {$row + $r}]
+                   set rowchk($i) [expr {$row + $r}]
                }
            }
            lset rowidlist $row $idlist
            lset rowoffsets $row $offs
        }
+       set oldolds {}
+       set newolds {}
+       foreach p [lindex $parentlist $row] {
+           if {![info exists idinlist($p)]} {
+               lappend newolds $p
+           } elseif {!$idinlist($p)} {
+               lappend oldolds $p
+           }
+           set idinlist($p) 1
+       }
        set col [lsearch -exact $idlist $id]
        if {$col < 0} {
            set col [llength $idlist]
@@ -2896,12 +2982,10 @@ proc layoutrows {row endrow last} {
            lset offs $col {}
        }
        foreach i $newolds {
-           set idinlist($i) 1
            set idrowranges($i) $id
        }
        incr col $l
        foreach oid $oldolds {
-           set idinlist($oid) 1
            set idlist [linsert $idlist $col $oid]
            set offs [linsert $offs $col $o]
            makeuparrow $oid $col $row $o
@@ -2942,7 +3026,7 @@ proc layouttail {} {
        set col [expr {[llength $idlist] - 1}]
        set id [lindex $idlist $col]
        addextraid $id $row
-       unset idinlist($id)
+       catch {unset idinlist($id)}
        lappend idrowranges($id) $id
        lappend rowrangelist $idrowranges($id)
        unset idrowranges($id)
@@ -3395,7 +3479,7 @@ proc drawcmittext {id row col} {
     global linespc canv canv2 canv3 canvy0 fgcolor curview
     global commitlisted commitinfo rowidlist parentlist
     global rowtextx idpos idtags idheads idotherrefs
-    global linehtag linentag linedtag markingmatches
+    global linehtag linentag linedtag
     global mainfont canvxmax boldrows boldnamerows fgcolor nullid nullid2
 
     # listed is 0 for boundary, 1 for normal, 2 for left, 3 for right
@@ -3472,9 +3556,6 @@ proc drawcmittext {id row col} {
     set linedtag($row) [$canv3 create text 3 $y -anchor w -fill $fgcolor \
                            -text $date -font $mainfont -tags text]
     set xr [expr {$xt + [font measure $mainfont $headline]}]
-    if {$markingmatches} {
-       markrowmatches $row $headline $name
-    }
     if {$xr > $canvxmax} {
        set canvxmax $xr
        setcanvscroll
@@ -3483,7 +3564,7 @@ proc drawcmittext {id row col} {
 
 proc drawcmitrow {row} {
     global displayorder rowidlist
-    global iddrawn
+    global iddrawn markingmatches
     global commitinfo parentlist numcommits
     global filehighlight fhighlights findstring nhighlights
     global hlview vhighlights
@@ -3504,18 +3585,22 @@ proc drawcmitrow {row} {
     if {$highlight_related ne "None" && ![info exists rhighlights($row)]} {
        askrelhighlight $row $id
     }
-    if {[info exists iddrawn($id)]} return
-    set col [lsearch -exact [lindex $rowidlist $row] $id]
-    if {$col < 0} {
-       puts "oops, row $row id $id not in list"
-       return
+    if {![info exists iddrawn($id)]} {
+       set col [lsearch -exact [lindex $rowidlist $row] $id]
+       if {$col < 0} {
+           puts "oops, row $row id $id not in list"
+           return
+       }
+       if {![info exists commitinfo($id)]} {
+           getcommit $id
+       }
+       assigncolor $id
+       drawcmittext $id $row $col
+       set iddrawn($id) 1
     }
-    if {![info exists commitinfo($id)]} {
-       getcommit $id
+    if {$markingmatches} {
+       markrowmatches $row $id
     }
-    assigncolor $id
-    drawcmittext $id $row $col
-    set iddrawn($id) 1
 }
 
 proc drawcommits {row {endrow {}}} {
@@ -4033,7 +4118,6 @@ proc dofind {{rev 0}} {
     if {!$rev} {
        run findmore
     } else {
-       set findcurline $findstartline
        if {$findcurline == 0} {
            set findcurline $numcommits
        }
@@ -4068,7 +4152,7 @@ proc findprev {} {
 
 proc findmore {} {
     global commitdata commitinfo numcommits findstring findpattern findloc
-    global findstartline findcurline markingmatches displayorder
+    global findstartline findcurline displayorder
 
     set fldtypes {Headline Author Date Committer CDate Comments}
     set l [expr {$findcurline + 1}]
@@ -4086,6 +4170,8 @@ proc findmore {} {
     set last 0
     for {} {$l < $lim} {incr l} {
        set id [lindex $displayorder $l]
+       # shouldn't happen unless git log doesn't give all the commits...
+       if {![info exists commitdata($id)]} continue
        if {![doesmatch $commitdata($id)]} continue
        if {![info exists commitinfo($id)]} {
            getcommit $id
@@ -4094,7 +4180,6 @@ proc findmore {} {
        foreach f $info ty $fldtypes {
            if {($findloc eq "All fields" || $findloc eq $ty) &&
                [doesmatch $f]} {
-               set markingmatches 1
                findselectline $l
                notbusy finding
                return 0
@@ -4113,7 +4198,7 @@ proc findmore {} {
 
 proc findmorerev {} {
     global commitdata commitinfo numcommits findstring findpattern findloc
-    global findstartline findcurline markingmatches displayorder
+    global findstartline findcurline displayorder
 
     set fldtypes {Headline Author Date Committer CDate Comments}
     set l $findcurline
@@ -4140,7 +4225,6 @@ proc findmorerev {} {
        foreach f $info ty $fldtypes {
            if {($findloc eq "All fields" || $findloc eq $ty) &&
                [doesmatch $f]} {
-               set markingmatches 1
                findselectline $l
                notbusy finding
                return 0
@@ -4158,7 +4242,10 @@ proc findmorerev {} {
 }
 
 proc findselectline {l} {
-    global findloc commentend ctext
+    global findloc commentend ctext findcurline markingmatches
+
+    set markingmatches 1
+    set findcurline $l
     selectline $l 1
     if {$findloc == "All fields" || $findloc == "Comments"} {
        # highlight the matches in the comments
@@ -4170,10 +4257,13 @@ proc findselectline {l} {
            $ctext tag add found "1.0 + $start c" "1.0 + $end c"
        }
     }
+    drawvisible
 }
 
 # mark the bits of a headline or author that match a find string
-proc markmatches {canv l str tag matches font} {
+proc markmatches {canv l str tag matches font row} {
+    global selectedline
+
     set bbox [$canv bbox $tag]
     set x0 [lindex $bbox 0]
     set y0 [lindex $bbox 1]
@@ -4188,6 +4278,9 @@ proc markmatches {canv l str tag matches font} {
                   [expr {$x0+$xlen+2}] $y1 \
                   -outline {} -tags [list match$l matches] -fill yellow]
        $canv lower $t
+       if {[info exists selectedline] && $row == $selectedline} {
+           $canv raise $t secsel
+       }
     }
 }
 
@@ -4536,6 +4629,7 @@ proc sellastline {} {
 
 proc selnextline {dir} {
     global selectedline
+    focus .
     if {![info exists selectedline]} return
     set l [expr {$selectedline + $dir}]
     unmarkmatches
@@ -4616,6 +4710,7 @@ proc godo {elt} {
 
 proc goback {} {
     global history historyindex
+    focus .
 
     if {$historyindex > 1} {
        incr historyindex -1
@@ -4629,6 +4724,7 @@ proc goback {} {
 
 proc goforw {} {
     global history historyindex
+    focus .
 
     if {$historyindex < [llength $history]} {
        set cmd [lindex $history $historyindex]
@@ -6105,17 +6201,13 @@ proc rmbranch {} {
 proc getallcommits {} {
     global allcommits allids nbmp nextarc seeds
 
-    set allids {}
-    set nbmp 0
-    set nextarc 0
-    set allcommits 0
-    set seeds {}
-    regetallcommits
-}
-
-# Called when the graph might have changed
-proc regetallcommits {} {
-    global allcommits seeds
+    if {![info exists allcommits]} {
+       set allids {}
+       set nbmp 0
+       set nextarc 0
+       set allcommits 0
+       set seeds {}
+    }
 
     set cmd [concat | git rev-list --all --parents]
     foreach id $seeds {
@@ -7546,7 +7638,10 @@ catch {source ~/.gitk}
 font create optionfont -family sans-serif -size -12
 
 # check that we can find a .git directory somewhere...
-set gitdir [gitdir]
+if {[catch {set gitdir [gitdir]}]} {
+    show_error {} . "Cannot find a git repository here."
+    exit 1
+}
 if {![file isdirectory $gitdir]} {
     show_error {} . "Cannot find the git directory \"$gitdir\"."
     exit 1
@@ -7635,6 +7730,8 @@ set localfrow -1
 set lserial 0
 setcoords
 makewindow
+# wait for the window to become visible
+tkwait visibility .
 wm title . "[file tail $argv0]: [file tail [pwd]]"
 readrefs