Code

gitk: Avoid crash if closed while reading references
[git.git] / gitk
diff --git a/gitk b/gitk
index 18d153e569bdadb92ce67d161596779a8b25dc6a..dca1741c71254175d2161c1fd58091077f6087fd 100755 (executable)
--- a/gitk
+++ b/gitk
@@ -155,18 +155,16 @@ proc parseviewargs {n arglist} {
                set origargs [lreplace $origargs $i $i]
                incr i -1
            }
-           # These request or affect diff output, which we don't want.
-           # Some could be used to set our defaults for diff display.
            "-[puabwcrRBMC]" -
            "--no-renames" - "--full-index" - "--binary" - "--abbrev=*" -
            "--find-copies-harder" - "-l*" - "--ext-diff" - "--no-ext-diff" -
            "--src-prefix=*" - "--dst-prefix=*" - "--no-prefix" -
            "-O*" - "--text" - "--full-diff" - "--ignore-space-at-eol" -
            "--ignore-space-change" - "-U*" - "--unified=*" {
+               # These request or affect diff output, which we don't want.
+               # Some could be used to set our defaults for diff display.
                lappend diffargs $arg
            }
-           # These cause our parsing of git log's output to fail, or else
-           # they're options we want to set ourselves, so ignore them.
            "--raw" - "--patch-with-raw" - "--patch-with-stat" -
            "--name-only" - "--name-status" - "--color" - "--color-words" -
            "--log-size" - "--pretty=*" - "--decorate" - "--abbrev-commit" -
@@ -174,36 +172,34 @@ proc parseviewargs {n arglist} {
            "--no-color" - "-g" - "--walk-reflogs" - "--no-walk" -
            "--timestamp" - "relative-date" - "--date=*" - "--stdin" -
            "--objects" - "--objects-edge" - "--reverse" {
+               # These cause our parsing of git log's output to fail, or else
+               # they're options we want to set ourselves, so ignore them.
            }
-           # These are harmless, and some are even useful
            "--stat=*" - "--numstat" - "--shortstat" - "--summary" -
            "--check" - "--exit-code" - "--quiet" - "--topo-order" -
            "--full-history" - "--dense" - "--sparse" -
            "--follow" - "--left-right" - "--encoding=*" {
+               # These are harmless, and some are even useful
                lappend glflags $arg
            }
-           # These mean that we get a subset of the commits
            "--diff-filter=*" - "--no-merges" - "--unpacked" -
            "--max-count=*" - "--skip=*" - "--since=*" - "--after=*" -
            "--until=*" - "--before=*" - "--max-age=*" - "--min-age=*" -
            "--author=*" - "--committer=*" - "--grep=*" - "-[iE]" -
            "--remove-empty" - "--first-parent" - "--cherry-pick" -
-           "-S*" - "--pickaxe-all" - "--pickaxe-regex" - {
+           "-S*" - "--pickaxe-all" - "--pickaxe-regex" {
+               # These mean that we get a subset of the commits
                set filtered 1
                lappend glflags $arg
            }
-           # This appears to be the only one that has a value as a
-           # separate word following it
            "-n" {
+               # This appears to be the only one that has a value as a
+               # separate word following it
                set filtered 1
                set nextisval 1
                lappend glflags $arg
            }
-           "--not" {
-               set notflag [expr {!$notflag}]
-               lappend revargs $arg
-           }
-           "--all" {
+           "--not" - "--all" {
                lappend revargs $arg
            }
            "--merge" {
@@ -211,8 +207,8 @@ proc parseviewargs {n arglist} {
                # git rev-parse doesn't understand --merge
                lappend revargs --gitk-symmetric-diff-marker MERGE_HEAD...HEAD
            }
-           # Other flag arguments including -<n>
            "-*" {
+               # Other flag arguments including -<n>
                if {[string is digit -strict [string range $arg 1 end]]} {
                    set filtered 1
                } else {
@@ -222,8 +218,8 @@ proc parseviewargs {n arglist} {
                }
                lappend glflags $arg
            }
-           # Non-flag arguments specify commits or ranges of commits
            default {
+               # Non-flag arguments specify commits or ranges of commits
                if {[string match "*...*" $arg]} {
                    lappend revargs --gitk-symmetric-diff-marker
                }
@@ -309,7 +305,7 @@ proc start_rev_list {view} {
     global viewargs viewargscmd viewfiles vfilelimit
     global showlocalchanges
     global viewactive viewinstances vmergeonly
-    global mainheadid
+    global mainheadid viewmainheadid viewmainheadid_orig
     global vcanopt vflags vrevs vorigargs
 
     set startmsecs [clock clicks -milliseconds]
@@ -367,8 +363,13 @@ proc start_rev_list {view} {
     }
     set i [reg_instance $fd]
     set viewinstances($view) [list $i]
-    if {$showlocalchanges && $mainheadid ne {}} {
-       interestedin $mainheadid dodiffindex
+    set viewmainheadid($view) $mainheadid
+    set viewmainheadid_orig($view) $mainheadid
+    if {$files ne {} && $mainheadid ne {}} {
+       get_viewmainhead $view
+    }
+    if {$showlocalchanges && $viewmainheadid($view) ne {}} {
+       interestedin $viewmainheadid($view) dodiffindex
     }
     fconfigure $fd -blocking 0 -translation lf -eofchar {}
     if {$tclencoding != {}} {
@@ -446,22 +447,26 @@ proc updatecommits {} {
     global curview vcanopt vorigargs vfilelimit viewinstances
     global viewactive viewcomplete tclencoding
     global startmsecs showneartags showlocalchanges
-    global mainheadid pending_select
+    global mainheadid viewmainheadid viewmainheadid_orig pending_select
     global isworktree
     global varcid vposids vnegids vflags vrevs
 
     set isworktree [expr {[exec git rev-parse --is-inside-work-tree] == "true"}]
-    set oldmainid $mainheadid
     rereadrefs
-    if {$showlocalchanges} {
-       if {$mainheadid ne $oldmainid} {
+    set view $curview
+    if {$mainheadid ne $viewmainheadid_orig($view)} {
+       if {$showlocalchanges} {
            dohidelocalchanges
        }
-       if {[commitinview $mainheadid $curview]} {
-           dodiffindex
+       set viewmainheadid($view) $mainheadid
+       set viewmainheadid_orig($view) $mainheadid
+       if {$vfilelimit($view) ne {}} {
+           get_viewmainhead $view
        }
     }
-    set view $curview
+    if {$showlocalchanges} {
+       doshowlocalchanges
+    }
     if {$vcanopt($view)} {
        set oldpos $vposids($view)
        set oldneg $vnegids($view)
@@ -516,7 +521,7 @@ proc updatecommits {} {
     incr viewactive($view)
     set viewcomplete($view) 0
     reset_pending_select {}
-    nowbusy $view "Reading"
+    nowbusy $view [mc "Reading"]
     if {$showneartags} {
        getallcommits
     }
@@ -696,16 +701,17 @@ proc newvarc {view id} {
 }
 
 proc splitvarc {p v} {
-    global varcid varcstart varccommits varctok
+    global varcid varcstart varccommits varctok vtokmod
     global vupptr vdownptr vleftptr vbackptr varcix varcrow vlastins
 
     set oa $varcid($v,$p)
+    set otok [lindex $varctok($v) $oa]
     set ac $varccommits($v,$oa)
     set i [lsearch -exact $varccommits($v,$oa) $p]
     if {$i <= 0} return
     set na [llength $varctok($v)]
     # "%" sorts before "0"...
-    set tok "[lindex $varctok($v) $oa]%[strrep $i]"
+    set tok "$otok%[strrep $i]"
     lappend varctok($v) $tok
     lappend varcrow($v) {}
     lappend varcix($v) {}
@@ -725,6 +731,9 @@ proc splitvarc {p v} {
     for {set b [lindex $vdownptr($v) $na]} {$b != 0} {set b [lindex $vleftptr($v) $b]} {
        lset vupptr($v) $b $na
     }
+    if {[string compare $otok $vtokmod($v)] <= 0} {
+       modify_arc $v $oa
+    }
 }
 
 proc renumbervarc {a v} {
@@ -1555,9 +1564,27 @@ proc chewcommits {} {
     return 0
 }
 
+proc do_readcommit {id} {
+    global tclencoding
+
+    # Invoke git-log to handle automatic encoding conversion
+    set fd [open [concat | git log --no-color --pretty=raw -1 $id] r]
+    # Read the results using i18n.logoutputencoding
+    fconfigure $fd -translation lf -eofchar {}
+    if {$tclencoding != {}} {
+       fconfigure $fd -encoding $tclencoding
+    }
+    set contents [read $fd]
+    close $fd
+    # Remove the heading line
+    regsub {^commit [0-9a-f]+\n} $contents {} contents
+
+    return $contents
+}
+
 proc readcommit {id} {
-    if {[catch {set contents [exec git cat-file commit $id]}]} return
-    parsecommit $id $contents 0
+    if {[catch {set contents [do_readcommit $id]}]} return
+    parsecommit $id $contents 1
 }
 
 proc parsecommit {id contents listed} {
@@ -1578,13 +1605,14 @@ proc parsecommit {id contents listed} {
     set header [string range $contents 0 [expr {$hdrend - 1}]]
     set comment [string range $contents [expr {$hdrend + 2}] end]
     foreach line [split $header "\n"] {
+       set line [split $line " "]
        set tag [lindex $line 0]
        if {$tag == "author"} {
            set audate [lindex $line end-1]
-           set auname [lrange $line 1 end-2]
+           set auname [join [lrange $line 1 end-2] " "]
        } elseif {$tag == "committer"} {
            set comdate [lindex $line end-1]
-           set comname [lrange $line 1 end-2]
+           set comname [join [lrange $line 1 end-2] " "]
        }
     }
     set headline {}
@@ -1739,6 +1767,24 @@ proc removehead {id name} {
     unset headids($name)
 }
 
+proc make_transient {window origin} {
+    global have_tk85
+
+    # In MacOS Tk 8.4 transient appears to work by setting
+    # overrideredirect, which is utterly useless, since the
+    # windows get no border, and are not even kept above
+    # the parent.
+    if {!$have_tk85 && [tk windowingsystem] eq {aqua}} return
+
+    wm transient $window $origin
+
+    # Windows fails to place transient windows normally, so
+    # schedule a callback to center them on the parent.
+    if {[tk windowingsystem] eq {win32}} {
+       after idle [list tk::PlaceWindow $window widget $origin]
+    }
+}
+
 proc show_error {w top msg} {
     message $w.m -text $msg -justify center -aspect 400
     pack $w.m -side top -fill x -padx 20 -pady 20
@@ -1754,7 +1800,7 @@ proc show_error {w top msg} {
 proc error_popup {msg {owner .}} {
     set w .error
     toplevel $w
-    wm transient $w $owner
+    make_transient $w $owner
     show_error $w $w $msg
 }
 
@@ -1763,7 +1809,7 @@ proc confirm_popup {msg {owner .}} {
     set confirm_ok 0
     set w .confirm
     toplevel $w
-    wm transient $w $owner
+    make_transient $w $owner
     message $w.m -text $msg -justify center -aspect 400
     pack $w.m -side top -fill x -padx 20 -pady 20
     button $w.ok -text [mc OK] -command "set confirm_ok 1; destroy $w"
@@ -1784,7 +1830,9 @@ proc setoptions {} {
     option add *Button.font uifont startupFile
     option add *Checkbutton.font uifont startupFile
     option add *Radiobutton.font uifont startupFile
-    option add *Menu.font uifont startupFile
+    if {[tk windowingsystem] ne "aqua"} {
+       option add *Menu.font uifont startupFile
+    }
     option add *Menubutton.font uifont startupFile
     option add *Label.font uifont startupFile
     option add *Message.font uifont startupFile
@@ -1801,6 +1849,11 @@ proc setoptions {} {
 # command to invoke for command, or {variable value} for radiobutton
 proc makemenu {m items} {
     menu $m
+    if {[tk windowingsystem] eq {aqua}} {
+       set Meta1 Cmd
+    } else {
+       set Meta1 Ctrl
+    }
     foreach i $items {
        set name [mc [lindex $i 1]]
        set type [lindex $i 2]
@@ -1826,7 +1879,9 @@ proc makemenu {m items} {
                    -value [lindex $thing 1]
            }
        }
-       eval $m add $params [lrange $i 4 end]
+       set tail [lrange $i 4 end]
+       regsub -all {\yMeta1\y} $tail $Meta1 tail
+       eval $m add $params $tail
        if {$type eq "cascade"} {
            makemenu $m.$submenu $thing
        }
@@ -1857,29 +1912,52 @@ proc makewindow {} {
 
     # The "mc" arguments here are purely so that xgettext
     # sees the following string as needing to be translated
-    makemenu .bar {
-       {mc "File" cascade {
+    set file {
+       mc "File" cascade {
            {mc "Update" command updatecommits -accelerator F5}
-           {mc "Reload" command reloadcommits}
+           {mc "Reload" command reloadcommits -accelerator Meta1-F5}
            {mc "Reread references" command rereadrefs}
-           {mc "List references" command showrefs}
-           {mc "Quit" command doquit}
+           {mc "List references" command showrefs -accelerator F2}
+           {xx "" separator}
+           {mc "Start git gui" command {exec git gui &}}
+           {xx "" separator}
+           {mc "Quit" command doquit -accelerator Meta1-Q}
        }}
-       {mc "Edit" cascade {
+    set edit {
+       mc "Edit" cascade {
            {mc "Preferences" command doprefs}
        }}
-       {mc "View" cascade {
-           {mc "New view..." command {newview 0}}
-           {mc "Edit view..." command editview -state disabled}
+    set view {
+       mc "View" cascade {
+           {mc "New view..." command {newview 0} -accelerator Shift-F4}
+           {mc "Edit view..." command editview -state disabled -accelerator F4}
            {mc "Delete view" command delview -state disabled}
            {xx "" separator}
            {mc "All files" radiobutton {selectedview 0} -command {showview 0}}
        }}
-       {mc "Help" cascade {
+    if {[tk windowingsystem] ne "aqua"} {
+       set help {
+       mc "Help" cascade {
+           {mc "About gitk" command about}
+           {mc "Key bindings" command keys}
+       }}
+       set bar [list $file $edit $view $help]
+    } else {
+       proc ::tk::mac::ShowPreferences {} {doprefs}
+       proc ::tk::mac::Quit {} {doquit}
+       lset file end [lreplace [lindex $file end] end-1 end]
+       set apple {
+       xx "Apple" cascade {
            {mc "About gitk" command about}
+           {xx "" separator}
+       }}
+       set help {
+       mc "Help" cascade {
            {mc "Key bindings" command keys}
        }}
+       set bar [list $apple $file $view $help]
     }
+    makemenu .bar $bar
     . configure -menu .bar
 
     # the gui has upper and lower half, parts of a paned window.
@@ -2173,10 +2251,16 @@ proc makewindow {} {
        }
     }
 
+    if {[info exists geometry(state)] && $geometry(state) eq "zoomed"} {
+        wm state . $geometry(state)
+    }
+
     if {[tk windowingsystem] eq {aqua}} {
         set M1B M1
+        set ::BM "3"
     } else {
         set M1B Control
+        set ::BM "2"
     }
 
     bind .pwbottom <Configure> {resizecdetpanes %W %w}
@@ -2194,10 +2278,14 @@ proc makewindow {} {
                 set delta [expr {- (%D)}]
                 allcanvs yview scroll $delta units
             }
+            bindall <Shift-MouseWheel> {
+                set delta [expr {- (%D)}]
+                $canv xview scroll $delta units
+            }
         }
     }
-    bindall <2> "canvscan mark %W %x %y"
-    bindall <B2-Motion> "canvscan dragto %W %x %y"
+    bindall <$::BM> "canvscan mark %W %x %y"
+    bindall <B$::BM-Motion> "canvscan dragto %W %x %y"
     bindkey <Home> selfirstline
     bindkey <End> sellastline
     bind . <Key-Up> "selnextline -1"
@@ -2228,11 +2316,17 @@ proc makewindow {} {
     bindkey b prevfile
     bindkey d "$ctext yview scroll 18 units"
     bindkey u "$ctext yview scroll -18 units"
-    bindkey / {dofind 1 1}
+    bindkey / {focus $fstring}
+    bindkey <Key-KP_Divide> {focus $fstring}
     bindkey <Key-Return> {dofind 1 1}
     bindkey ? {dofind -1 1}
     bindkey f nextfile
-    bindkey <F5> updatecommits
+    bind . <F5> updatecommits
+    bind . <$M1B-F5> reloadcommits
+    bind . <F2> showrefs
+    bind . <Shift-F4> {newview 0}
+    catch { bind . <Shift-Key-XF86_Switch_VT_4> {newview 0} }
+    bind . <F4> edit_or_newview
     bind . <$M1B-q> doquit
     bind . <$M1B-f> {dofind 1 1}
     bind . <$M1B-g> {dofind 1 0}
@@ -2270,6 +2364,10 @@ proc makewindow {} {
        {mc "Create new branch" command mkbranch}
        {mc "Cherry-pick this commit" command cherrypick}
        {mc "Reset HEAD branch to here" command resethead}
+       {mc "Mark this commit" command markhere}
+       {mc "Return to mark" command gotomark}
+       {mc "Find descendant of this and mark" command find_common_desc}
+       {mc "Compare with marked commit" command compare_commits}
     }
     $rowctxmenu configure -tearoff 0
 
@@ -2426,6 +2524,9 @@ proc savestuff {w} {
     if {![winfo viewable .]} return
     catch {
        set f [open "~/.gitk-new" w]
+       if {$::tcl_platform(platform) eq {windows}} {
+           file attributes "~/.gitk-new" -hidden true
+       }
        puts $f [list set mainfont $mainfont]
        puts $f [list set textfont $textfont]
        puts $f [list set uifont $uifont]
@@ -2451,6 +2552,7 @@ proc savestuff {w} {
        puts $f [list set perfile_attrs $perfile_attrs]
 
        puts $f "set geometry(main) [wm geometry .]"
+       puts $f "set geometry(state) [wm state .]"
        puts $f "set geometry(topwidth) [winfo width .tf]"
        puts $f "set geometry(topheight) [winfo height .tf]"
         puts $f "set geometry(pwsash0) \"[.tf.histframe.pwclist sash coord 0]\""
@@ -2546,7 +2648,7 @@ proc about {} {
     }
     toplevel $w
     wm title $w [mc "About gitk"]
-    wm transient $w .
+    make_transient $w .
     message $w.m -text [mc "
 Gitk - a commit viewer for git
 
@@ -2575,7 +2677,7 @@ proc keys {} {
     }
     toplevel $w
     wm title $w [mc "Gitk key bindings"]
-    wm transient $w .
+    make_transient $w .
     message $w.m -text "
 [mc "Gitk key bindings:"]
 
@@ -2604,7 +2706,7 @@ proc keys {} {
 [mc "<%s-F>            Find" $M1T]
 [mc "<%s-G>            Move to next find hit" $M1T]
 [mc "<Return>  Move to next find hit"]
-[mc "/         Move to next find hit, or redo find"]
+[mc "/         Focus the search box"]
 [mc "?         Move to previous find hit"]
 [mc "f         Scroll diff view to next file"]
 [mc "<%s-S>            Search for next hit in diff view" $M1T]
@@ -3143,9 +3245,8 @@ proc external_diff {} {
     set difftofile [external_diff_get_one_file $diffidto $flist_menu_file $diffdir]
 
     if {$difffromfile ne {} && $difftofile ne {}} {
-        set cmd [concat | [shellsplit $extdifftool] \
-                    [list $difffromfile $difftofile]]
-        if {[catch {set fl [open $cmd r]} err]} {
+        set cmd [list [shellsplit $extdifftool] $difffromfile $difftofile]
+        if {[catch {set fl [open |$cmd r]} err]} {
             file delete -force $diffdir
             error_popup "$extdifftool: [mc "command failed:"] $err"
         } else {
@@ -3262,8 +3363,27 @@ proc index_sha1 {fname} {
     return {}
 }
 
+# Turn an absolute path into one relative to the current directory
+proc make_relative {f} {
+    set elts [file split $f]
+    set here [file split [pwd]]
+    set ei 0
+    set hi 0
+    set res {}
+    foreach d $here {
+       if {$ei < $hi || $ei >= [llength $elts] || [lindex $elts $ei] ne $d} {
+           lappend res ".."
+       } else {
+           incr ei
+       }
+       incr hi
+    }
+    set elts [concat $res [lrange $elts $ei end]]
+    return [eval file join $elts]
+}
+
 proc external_blame {parent_idx {line {}}} {
-    global flist_menu_file
+    global flist_menu_file gitdir
     global nullid nullid2
     global parentlist selectedline currentid
 
@@ -3282,7 +3402,11 @@ proc external_blame {parent_idx {line {}}} {
     if {$line ne {} && $line > 1} {
        lappend cmdline "--line=$line"
     }
-    lappend cmdline $base_commit $flist_menu_file
+    set f [file join [file dirname $gitdir] $flist_menu_file]
+    # Unfortunately it seems git gui blame doesn't like
+    # being given an absolute path...
+    set f [make_relative $f]
+    lappend cmdline $base_commit $f
     if {[catch {eval exec $cmdline &} err]} {
        error_popup "[mc "git gui blame: command failed:"] $err"
     }
@@ -3326,6 +3450,8 @@ proc show_line_source {} {
                    error_popup [mc "Error reading index: %s" $err]
                    return
                }
+           } else {
+               set id $parents($curview,$currentid)
            }
        } else {
            set id [lindex $parents($curview,$currentid) $pi]
@@ -3342,13 +3468,14 @@ proc show_line_source {} {
     } else {
        lappend blameargs $id
     }
-    lappend blameargs -- $flist_menu_file
+    lappend blameargs -- [file join [file dirname $gitdir] $flist_menu_file]
     if {[catch {
        set f [open $blameargs r]
     } err]} {
        error_popup [mc "Couldn't start git blame: %s" $err]
        return
     }
+    nowbusy blaming [mc "Searching"]
     fconfigure $f -blocking 0
     set i [reg_instance $f]
     set blamestuff($i) {}
@@ -3362,6 +3489,7 @@ proc stopblaming {} {
     if {[info exists blameinst]} {
        stop_instance $blameinst
        unset blameinst
+       notbusy blaming
     }
 }
 
@@ -3376,6 +3504,7 @@ proc read_line_source {fd inst} {
     }
     unset commfd($inst)
     unset blameinst
+    notbusy blaming
     fconfigure $fd -blocking 1
     if {[catch {close $fd} err]} {
        error_popup [mc "Error running git blame: %s" $err]
@@ -3624,6 +3753,16 @@ proc decode_view_opts {n view_args} {
     set newviewopts($n,args) [shellarglist $oargs]
 }
 
+proc edit_or_newview {} {
+    global curview
+
+    if {$curview > 0} {
+       editview
+    } else {
+       newview 0
+    }
+}
+
 proc editview {} {
     global curview
     global viewname viewperm newviewname newviewopts
@@ -3638,7 +3777,7 @@ proc editview {} {
     set newviewopts($curview,perm) $viewperm($curview)
     set newviewopts($curview,cmd)  $viewargscmd($curview)
     decode_view_opts $curview $viewargs($curview)
-    vieweditor $top $curview "Gitk: edit view $viewname($curview)"
+    vieweditor $top $curview "[mc "Gitk: edit view"] $viewname($curview)"
 }
 
 proc vieweditor {top n title} {
@@ -3647,7 +3786,7 @@ proc vieweditor {top n title} {
 
     toplevel $top
     wm title $top $title
-    wm transient $top .
+    make_transient $top .
 
     # View name
     frame $top.nfr
@@ -3946,28 +4085,34 @@ proc ishighlighted {id} {
     return 0
 }
 
-proc bolden {row font} {
-    global canv linehtag selectedline boldrows
+proc bolden {id font} {
+    global canv linehtag currentid boldids need_redisplay markedid
 
-    lappend boldrows $row
-    $canv itemconf $linehtag($row) -font $font
-    if {$row == $selectedline} {
+    # need_redisplay = 1 means the display is stale and about to be redrawn
+    if {$need_redisplay} return
+    lappend boldids $id
+    $canv itemconf $linehtag($id) -font $font
+    if {[info exists currentid] && $id eq $currentid} {
        $canv delete secsel
-       set t [eval $canv create rect [$canv bbox $linehtag($row)] \
+       set t [eval $canv create rect [$canv bbox $linehtag($id)] \
                   -outline {{}} -tags secsel \
                   -fill [$canv cget -selectbackground]]
        $canv lower $t
     }
+    if {[info exists markedid] && $id eq $markedid} {
+       make_idmark $id
+    }
 }
 
-proc bolden_name {row font} {
-    global canv2 linentag selectedline boldnamerows
+proc bolden_name {id font} {
+    global canv2 linentag currentid boldnameids need_redisplay
 
-    lappend boldnamerows $row
-    $canv2 itemconf $linentag($row) -font $font
-    if {$row == $selectedline} {
+    if {$need_redisplay} return
+    lappend boldnameids $id
+    $canv2 itemconf $linentag($id) -font $font
+    if {[info exists currentid] && $id eq $currentid} {
        $canv2 delete secsel
-       set t [eval $canv2 create rect [$canv2 bbox $linentag($row)] \
+       set t [eval $canv2 create rect [$canv2 bbox $linentag($id)] \
                   -outline {{}} -tags secsel \
                   -fill [$canv2 cget -selectbackground]]
        $canv2 lower $t
@@ -3975,17 +4120,17 @@ proc bolden_name {row font} {
 }
 
 proc unbolden {} {
-    global boldrows
+    global boldids
 
     set stillbold {}
-    foreach row $boldrows {
-       if {![ishighlighted [commitonrow $row]]} {
-           bolden $row mainfont
+    foreach id $boldids {
+       if {![ishighlighted $id]} {
+           bolden $id mainfont
        } else {
-           lappend stillbold $row
+           lappend stillbold $id
        }
     }
-    set boldrows $stillbold
+    set boldids $stillbold
 }
 
 proc addvhighlight {n} {
@@ -4026,7 +4171,7 @@ proc vhighlightmore {} {
            set row [rowofcommit $id]
            if {$r0 <= $row && $row <= $r1} {
                if {![highlighted $row]} {
-                   bolden $row mainfontbold
+                   bolden $id mainfontbold
                }
                set vhighlights($id) 1
            }
@@ -4041,7 +4186,7 @@ proc askvhighlight {row id} {
 
     if {[commitinview $id $hlview]} {
        if {[info exists iddrawn($id)] && ![ishighlighted $id]} {
-           bolden $row mainfontbold
+           bolden $id mainfontbold
        }
        set vhighlights($id) 1
     } else {
@@ -4051,7 +4196,7 @@ proc askvhighlight {row id} {
 
 proc hfiles_change {} {
     global highlight_files filehighlight fhighlights fh_serial
-    global highlight_paths gdttype
+    global highlight_paths
 
     if {[info exists filehighlight]} {
        # delete previous highlights
@@ -4109,15 +4254,15 @@ proc find_change {name ix op} {
 }
 
 proc findcom_change args {
-    global nhighlights boldnamerows
+    global nhighlights boldnameids
     global findpattern findtype findstring gdttype
 
     stopfinding
     # delete previous highlights, if any
-    foreach row $boldnamerows {
-       bolden_name $row mainfont
+    foreach id $boldnameids {
+       bolden_name $id mainfont
     }
-    set boldnamerows {}
+    set boldnameids {}
     catch {unset nhighlights}
     unbolden
     unmarkmatches
@@ -4206,9 +4351,8 @@ proc readfhighlight {} {
        set fhl_list [lrange $fhl_list [expr {$i+1}] end]
        if {$line eq {}} continue
        if {![commitinview $line $curview]} continue
-       set row [rowofcommit $line]
        if {[info exists iddrawn($line)] && ![ishighlighted $line]} {
-           bolden $row mainfontbold
+           bolden $line mainfontbold
        }
        set fhighlights($line) 1
     }
@@ -4260,9 +4404,9 @@ proc askfindhighlight {row id} {
     }
     if {$isbold && [info exists iddrawn($id)]} {
        if {![ishighlighted $id]} {
-           bolden $row mainfontbold
+           bolden $id mainfontbold
            if {$isbold > 1} {
-               bolden_name $row mainfontbold
+               bolden_name $id mainfontbold
            }
        }
        if {$markingmatches} {
@@ -4282,15 +4426,15 @@ proc markrowmatches {row id} {
     if {$findloc eq [mc "All fields"] || $findloc eq [mc "Headline"]} {
        set m [findmatches $headline]
        if {$m ne {}} {
-           markmatches $canv $row $headline $linehtag($row) $m \
-               [$canv itemcget $linehtag($row) -font] $row
+           markmatches $canv $row $headline $linehtag($id) $m \
+               [$canv itemcget $linehtag($id) -font] $row
        }
     }
     if {$findloc eq [mc "All fields"] || $findloc eq [mc "Author"]} {
        set m [findmatches $author]
        if {$m ne {}} {
-           markmatches $canv2 $row $author $linentag($row) $m \
-               [$canv2 itemcget $linentag($row) -font] $row
+           markmatches $canv2 $row $author $linentag($id) $m \
+               [$canv2 itemcget $linentag($id) -font] $row
        }
     }
 }
@@ -4415,7 +4559,7 @@ proc askrelhighlight {row id} {
     }
     if {[info exists iddrawn($id)]} {
        if {$isbold && ![ishighlighted $id]} {
-           bolden $row mainfontbold
+           bolden $id mainfontbold
        }
     }
     set rhighlights($id) $isbold
@@ -4583,14 +4727,56 @@ proc layoutmore {} {
     drawvisible
 }
 
+# With path limiting, we mightn't get the actual HEAD commit,
+# so ask git rev-list what is the first ancestor of HEAD that
+# touches a file in the path limit.
+proc get_viewmainhead {view} {
+    global viewmainheadid vfilelimit viewinstances mainheadid
+
+    catch {
+       set rfd [open [concat | git rev-list -1 $mainheadid \
+                          -- $vfilelimit($view)] r]
+       set j [reg_instance $rfd]
+       lappend viewinstances($view) $j
+       fconfigure $rfd -blocking 0
+       filerun $rfd [list getviewhead $rfd $j $view]
+       set viewmainheadid($curview) {}
+    }
+}
+
+# git rev-list should give us just 1 line to use as viewmainheadid($view)
+proc getviewhead {fd inst view} {
+    global viewmainheadid commfd curview viewinstances showlocalchanges
+
+    set id {}
+    if {[gets $fd line] < 0} {
+       if {![eof $fd]} {
+           return 1
+       }
+    } elseif {[string length $line] == 40 && [string is xdigit $line]} {
+       set id $line
+    }
+    set viewmainheadid($view) $id
+    close $fd
+    unset commfd($inst)
+    set i [lsearch -exact $viewinstances($view) $inst]
+    if {$i >= 0} {
+       set viewinstances($view) [lreplace $viewinstances($view) $i $i]
+    }
+    if {$showlocalchanges && $id ne {} && $view == $curview} {
+       doshowlocalchanges
+    }
+    return 0
+}
+
 proc doshowlocalchanges {} {
-    global curview mainheadid
+    global curview viewmainheadid
 
-    if {$mainheadid eq {}} return
-    if {[commitinview $mainheadid $curview]} {
+    if {$viewmainheadid($curview) eq {}} return
+    if {[commitinview $viewmainheadid($curview) $curview]} {
        dodiffindex
     } else {
-       interestedin $mainheadid dodiffindex
+       interestedin $viewmainheadid($curview) dodiffindex
     }
 }
 
@@ -4608,19 +4794,24 @@ proc dohidelocalchanges {} {
 
 # spawn off a process to do git diff-index --cached HEAD
 proc dodiffindex {} {
-    global lserial showlocalchanges
+    global lserial showlocalchanges vfilelimit curview
     global isworktree
 
     if {!$showlocalchanges || !$isworktree} return
     incr lserial
-    set fd [open "|git diff-index --cached HEAD" r]
+    set cmd "|git diff-index --cached HEAD"
+    if {$vfilelimit($curview) ne {}} {
+       set cmd [concat $cmd -- $vfilelimit($curview)]
+    }
+    set fd [open $cmd r]
     fconfigure $fd -blocking 0
     set i [reg_instance $fd]
     filerun $fd [list readdiffindex $fd $lserial $i]
 }
 
 proc readdiffindex {fd serial inst} {
-    global mainheadid nullid nullid2 curview commitinfo commitdata lserial
+    global viewmainheadid nullid nullid2 curview commitinfo commitdata lserial
+    global vfilelimit
 
     set isdiff 1
     if {[gets $fd line] < 0} {
@@ -4637,7 +4828,11 @@ proc readdiffindex {fd serial inst} {
     }
 
     # now see if there are any local changes not checked in to the index
-    set fd [open "|git diff-files" r]
+    set cmd "|git diff-files"
+    if {$vfilelimit($curview) ne {}} {
+       set cmd [concat $cmd -- $vfilelimit($curview)]
+    }
+    set fd [open $cmd r]
     fconfigure $fd -blocking 0
     set i [reg_instance $fd]
     filerun $fd [list readdifffiles $fd $serial $i]
@@ -4650,15 +4845,18 @@ proc readdiffindex {fd serial inst} {
        if {[commitinview $nullid $curview]} {
            removefakerow $nullid
        }
-       insertfakerow $nullid2 $mainheadid
+       insertfakerow $nullid2 $viewmainheadid($curview)
     } elseif {!$isdiff && [commitinview $nullid2 $curview]} {
+       if {[commitinview $nullid $curview]} {
+           removefakerow $nullid
+       }
        removefakerow $nullid2
     }
     return 0
 }
 
 proc readdifffiles {fd serial inst} {
-    global mainheadid nullid nullid2 curview
+    global viewmainheadid nullid nullid2 curview
     global commitinfo commitdata lserial
 
     set isdiff 1
@@ -4683,7 +4881,7 @@ proc readdifffiles {fd serial inst} {
        if {[commitinview $nullid2 $curview]} {
            set p $nullid2
        } else {
-           set p $mainheadid
+           set p $viewmainheadid($curview)
        }
        insertfakerow $nullid $p
     } elseif {!$isdiff && [commitinview $nullid $curview]} {
@@ -5408,7 +5606,7 @@ proc drawcmittext {id row col} {
     global cmitlisted commitinfo rowidlist parentlist
     global rowtextx idpos idtags idheads idotherrefs
     global linehtag linentag linedtag selectedline
-    global canvxmax boldrows boldnamerows fgcolor
+    global canvxmax boldids boldnameids fgcolor markedid
     global mainheadid nullid nullid2 circleitem circlecolors ctxbut
 
     # listed is 0 for boundary, 1 for normal, 2 for negative, 3 for left, 4 for right
@@ -5473,22 +5671,25 @@ proc drawcmittext {id row col} {
     set nfont mainfont
     set isbold [ishighlighted $id]
     if {$isbold > 0} {
-       lappend boldrows $row
+       lappend boldids $id
        set font mainfontbold
        if {$isbold > 1} {
-           lappend boldnamerows $row
+           lappend boldnameids $id
            set nfont mainfontbold
        }
     }
-    set linehtag($row) [$canv create text $xt $y -anchor w -fill $fgcolor \
-                           -text $headline -font $font -tags text]
-    $canv bind $linehtag($row) $ctxbut "rowmenu %X %Y $id"
-    set linentag($row) [$canv2 create text 3 $y -anchor w -fill $fgcolor \
-                           -text $name -font $nfont -tags text]
-    set linedtag($row) [$canv3 create text 3 $y -anchor w -fill $fgcolor \
-                           -text $date -font mainfont -tags text]
+    set linehtag($id) [$canv create text $xt $y -anchor w -fill $fgcolor \
+                          -text $headline -font $font -tags text]
+    $canv bind $linehtag($id) $ctxbut "rowmenu %X %Y $id"
+    set linentag($id) [$canv2 create text 3 $y -anchor w -fill $fgcolor \
+                          -text $name -font $nfont -tags text]
+    set linedtag($id) [$canv3 create text 3 $y -anchor w -fill $fgcolor \
+                          -text $date -font mainfont -tags text]
     if {$selectedline == $row} {
-       make_secsel $row
+       make_secsel $id
+    }
+    if {[info exists markedid] && $markedid eq $id} {
+       make_idmark $id
     }
     set xr [expr {$xt + [font measure $font $headline]}]
     if {$xr > $canvxmax} {
@@ -5579,7 +5780,6 @@ proc drawcommits {row {endrow {}}} {
     optimize_rows $ro1 0 $r2
     if {$need_redisplay || $nrows_drawn > 2000} {
        clear_display
-       drawvisible
     }
 
     # make the lines join to already-drawn rows either side
@@ -5696,7 +5896,7 @@ proc drawvisible {} {
 proc clear_display {} {
     global iddrawn linesegs need_redisplay nrows_drawn
     global vhighlights fhighlights nhighlights rhighlights
-    global linehtag linentag linedtag boldrows boldnamerows
+    global linehtag linentag linedtag boldids boldnameids
 
     allcanvs delete all
     catch {unset iddrawn}
@@ -5704,8 +5904,8 @@ proc clear_display {} {
     catch {unset linehtag}
     catch {unset linentag}
     catch {unset linedtag}
-    set boldrows {}
-    set boldnamerows {}
+    set boldids {}
+    set boldnameids {}
     catch {unset vhighlights}
     catch {unset fhighlights}
     catch {unset nhighlights}
@@ -6163,10 +6363,11 @@ proc findmore {} {
 proc findselectline {l} {
     global findloc commentend ctext findcurline markingmatches gdttype
 
-    set markingmatches 1
+    set markingmatches [expr {$gdttype eq [mc "containing:"]}]
     set findcurline $l
     selectline $l 1
-    if {$findloc == [mc "All fields"] || $findloc == [mc "Comments"]} {
+    if {$markingmatches &&
+       ($findloc eq [mc "All fields"] || $findloc eq [mc "Comments"])} {
        # highlight the matches in the comments
        set f [$ctext get 1.0 $commentend]
        set matches [findmatches $f]
@@ -6344,7 +6545,7 @@ proc appendrefs {pos ids var} {
        }
     }
     if {[llength $tags] > $maxrefs} {
-       $ctext insert $pos "many ([llength $tags])"
+       $ctext insert $pos "[mc "many"] ([llength $tags])"
     } else {
        set tags [lsort -index 0 -decreasing $tags]
        set sep {}
@@ -6413,24 +6614,34 @@ proc dispnexttag {} {
     }
 }
 
-proc make_secsel {l} {
+proc make_secsel {id} {
     global linehtag linentag linedtag canv canv2 canv3
 
-    if {![info exists linehtag($l)]} return
+    if {![info exists linehtag($id)]} return
     $canv delete secsel
-    set t [eval $canv create rect [$canv bbox $linehtag($l)] -outline {{}} \
+    set t [eval $canv create rect [$canv bbox $linehtag($id)] -outline {{}} \
               -tags secsel -fill [$canv cget -selectbackground]]
     $canv lower $t
     $canv2 delete secsel
-    set t [eval $canv2 create rect [$canv2 bbox $linentag($l)] -outline {{}} \
+    set t [eval $canv2 create rect [$canv2 bbox $linentag($id)] -outline {{}} \
               -tags secsel -fill [$canv2 cget -selectbackground]]
     $canv2 lower $t
     $canv3 delete secsel
-    set t [eval $canv3 create rect [$canv3 bbox $linedtag($l)] -outline {{}} \
+    set t [eval $canv3 create rect [$canv3 bbox $linedtag($id)] -outline {{}} \
               -tags secsel -fill [$canv3 cget -selectbackground]]
     $canv3 lower $t
 }
 
+proc make_idmark {id} {
+    global linehtag canv fgcolor
+
+    if {![info exists linehtag($id)]} return
+    $canv delete markid
+    set t [eval $canv create rect [$canv bbox $linehtag($id)] \
+              -tags markid -outline $fgcolor]
+    $canv raise $t
+}
+
 proc selectline {l isnew {desired_loc {}}} {
     global canv ctext commitinfo selectedline
     global canvy0 linespc parents children curview
@@ -6492,7 +6703,7 @@ proc selectline {l isnew {desired_loc {}}} {
        drawvisible
     }
 
-    make_secsel $l
+    make_secsel $id
 
     if {$isnew} {
        addtohistory [list selbyid $id]
@@ -7006,7 +7217,7 @@ proc gettreediffline {gdtf ids} {
        set treediffs($ids) $treediff
     }
     unset treepending
-    if {$cmitmode eq "tree"} {
+    if {$cmitmode eq "tree" && [llength $diffids] == 1} {
        gettree $diffids
     } elseif {$ids != $diffids} {
        if {![info exists diffmergeid]} {
@@ -7061,7 +7272,7 @@ proc getblobdiffs {ids} {
     set diffnparents 0
     set diffinhdr 0
     set diffencoding [get_path_encoding {}]
-    fconfigure $bdf -blocking 0 -encoding binary
+    fconfigure $bdf -blocking 0 -encoding binary -eofchar {}
     set blobdifffd($ids) $bdf
     filerun $bdf [list getblobdiffline $bdf $diffids]
 }
@@ -7212,7 +7423,8 @@ proc getblobdiffline {bdf ids} {
            $ctext insert end "$line\n" filesep
 
        } else {
-           set line [encoding convertfrom $diffencoding $line]
+           set line [string map {\x1A ^Z} \
+                          [encoding convertfrom $diffencoding $line]]
            # parse the prefix - one ' ', '-' or '+' for each parent
            set prefix [string range $line 0 [expr {$diffnparents - 1}]]
            set tag [expr {$diffnparents > 1? "m": "d"}]
@@ -7815,7 +8027,7 @@ proc mstime {} {
 
 proc rowmenu {x y id} {
     global rowctxmenu selectedline rowmenuid curview
-    global nullid nullid2 fakerowmenu mainhead
+    global nullid nullid2 fakerowmenu mainhead markedid
 
     stopfinding
     set rowmenuid $id
@@ -7827,10 +8039,19 @@ proc rowmenu {x y id} {
     if {$id ne $nullid && $id ne $nullid2} {
        set menu $rowctxmenu
        if {$mainhead ne {}} {
-           $menu entryconfigure 7 -label [mc "Reset %s branch to here" $mainhead]
+           $menu entryconfigure 7 -label [mc "Reset %s branch to here" $mainhead] -state normal
        } else {
            $menu entryconfigure 7 -label [mc "Detached head: can't reset" $mainhead] -state disabled
        }
+       if {[info exists markedid] && $markedid ne $id} {
+           $menu entryconfigure 9 -state normal
+           $menu entryconfigure 10 -state normal
+           $menu entryconfigure 11 -state normal
+       } else {
+           $menu entryconfigure 9 -state disabled
+           $menu entryconfigure 10 -state disabled
+           $menu entryconfigure 11 -state disabled
+       }
     } else {
        set menu $fakerowmenu
     }
@@ -7840,6 +8061,145 @@ proc rowmenu {x y id} {
     tk_popup $menu $x $y
 }
 
+proc markhere {} {
+    global rowmenuid markedid canv
+
+    set markedid $rowmenuid
+    make_idmark $markedid
+}
+
+proc gotomark {} {
+    global markedid
+
+    if {[info exists markedid]} {
+       selbyid $markedid
+    }
+}
+
+proc replace_by_kids {l r} {
+    global curview children
+
+    set id [commitonrow $r]
+    set l [lreplace $l 0 0]
+    foreach kid $children($curview,$id) {
+       lappend l [rowofcommit $kid]
+    }
+    return [lsort -integer -decreasing -unique $l]
+}
+
+proc find_common_desc {} {
+    global markedid rowmenuid curview children
+
+    if {![info exists markedid]} return
+    if {![commitinview $markedid $curview] ||
+       ![commitinview $rowmenuid $curview]} return
+    #set t1 [clock clicks -milliseconds]
+    set l1 [list [rowofcommit $markedid]]
+    set l2 [list [rowofcommit $rowmenuid]]
+    while 1 {
+       set r1 [lindex $l1 0]
+       set r2 [lindex $l2 0]
+       if {$r1 eq {} || $r2 eq {}} break
+       if {$r1 == $r2} {
+           selectline $r1 1
+           break
+       }
+       if {$r1 > $r2} {
+           set l1 [replace_by_kids $l1 $r1]
+       } else {
+           set l2 [replace_by_kids $l2 $r2]
+       }
+    }
+    #set t2 [clock clicks -milliseconds]
+    #puts "took [expr {$t2-$t1}]ms"
+}
+
+proc compare_commits {} {
+    global markedid rowmenuid curview children
+
+    if {![info exists markedid]} return
+    if {![commitinview $markedid $curview]} return
+    addtohistory [list do_cmp_commits $markedid $rowmenuid]
+    do_cmp_commits $markedid $rowmenuid
+}
+
+proc getpatchid {id} {
+    global patchids
+
+    if {![info exists patchids($id)]} {
+       set x [exec git diff-tree -p --root $id | git patch-id]
+       set patchids($id) [lindex $x 0]
+    }
+    return $patchids($id)
+}
+
+proc do_cmp_commits {a b} {
+    global ctext curview parents children patchids commitinfo
+
+    $ctext conf -state normal
+    clear_ctext
+    init_flist {}
+    for {set i 0} {$i < 100} {incr i} {
+       set shorta [string range $a 0 7]
+       set shortb [string range $b 0 7]
+       set skipa 0
+       set skipb 0
+       if {[llength $parents($curview,$a)] > 1} {
+           appendwithlinks [mc "Skipping merge commit %s\n" $shorta] {}
+           set skipa 1
+       } else {
+           set patcha [getpatchid $a]
+       }
+       if {[llength $parents($curview,$b)] > 1} {
+           appendwithlinks [mc "Skipping merge commit %s\n" $shortb] {}
+           set skipb 1
+       } else {
+           set patchb [getpatchid $b]
+       }
+       if {!$skipa && !$skipb} {
+           set heada [lindex $commitinfo($a) 0]
+           set headb [lindex $commitinfo($b) 0]
+           if {$patcha eq $patchb} {
+               if {$heada eq $headb} {
+                   appendwithlinks [mc "Commit %s == %s  %s\n" \
+                                        $shorta $shortb $heada] {}
+               } else {
+                   appendwithlinks [mc "Commit %s  %s\n" $shorta $heada] {}
+                   appendwithlinks [mc " is the same patch as\n"] {}
+                   appendwithlinks [mc "       %s  %s\n" $shortb $headb] {}
+               }
+               set skipa 1
+               set skipb 1
+           } else {
+               $ctext insert end "\n"
+               appendwithlinks [mc "Commit %s  %s\n" $shorta $heada] {}
+               appendwithlinks [mc " differs from\n"] {}
+               appendwithlinks [mc "       %s  %s\n" $shortb $headb] {}
+               appendwithlinks [mc "- stopping\n"]
+               break
+           }
+       }
+       if {$skipa} {
+           if {[llength $children($curview,$a)] != 1} {
+               $ctext insert end "\n"
+               appendwithlinks [mc "Commit %s has %s children - stopping\n" \
+                                   $shorta [llength $children($curview,$a)]] {}
+               break
+           }
+           set a [lindex $children($curview,$a) 0]
+       }
+       if {$skipb} {
+           if {[llength $children($curview,$b)] != 1} {
+               appendwithlinks [mc "Commit %s has %s children - stopping\n" \
+                                   $shortb [llength $children($curview,$b)]] {}
+               break
+           }
+           set b [lindex $children($curview,$b) 0]
+       }
+    }
+    $ctext conf -state disabled
+}
+
 proc diffvssel {dirn} {
     global rowmenuid selectedline
 
@@ -7890,7 +8250,7 @@ proc mkpatch {} {
     set patchtop $top
     catch {destroy $top}
     toplevel $top
-    wm transient $top .
+    make_transient $top .
     label $top.title -text [mc "Generate patch"]
     grid $top.title - -pady 10
     label $top.from -text [mc "From:"]
@@ -7977,7 +8337,7 @@ proc mktag {} {
     set mktagtop $top
     catch {destroy $top}
     toplevel $top
-    wm transient $top .
+    make_transient $top .
     label $top.title -text [mc "Create tag"]
     grid $top.title - -pady 10
     label $top.id -text [mc "ID:"]
@@ -8034,7 +8394,7 @@ proc domktag {} {
 }
 
 proc redrawtags {id} {
-    global canv linehtag idpos currentid curview cmitlisted
+    global canv linehtag idpos currentid curview cmitlisted markedid
     global canvxmax iddrawn circleitem mainheadid circlecolors
 
     if {![commitinview $id $curview]} return
@@ -8048,16 +8408,19 @@ proc redrawtags {id} {
     $canv itemconf $circleitem($row) -fill $ofill
     $canv delete tag.$id
     set xt [eval drawtags $id $idpos($id)]
-    $canv coords $linehtag($row) $xt [lindex $idpos($id) 2]
-    set text [$canv itemcget $linehtag($row) -text]
-    set font [$canv itemcget $linehtag($row) -font]
+    $canv coords $linehtag($id) $xt [lindex $idpos($id) 2]
+    set text [$canv itemcget $linehtag($id) -text]
+    set font [$canv itemcget $linehtag($id) -font]
     set xr [expr {$xt + [font measure $font $text]}]
     if {$xr > $canvxmax} {
        set canvxmax $xr
        setcanvscroll
     }
     if {[info exists currentid] && $currentid == $id} {
-       make_secsel $row
+       make_secsel $id
+    }
+    if {[info exists markedid] && $markedid eq $id} {
+       make_idmark $id
     }
 }
 
@@ -8080,7 +8443,7 @@ proc writecommit {} {
     set wrcomtop $top
     catch {destroy $top}
     toplevel $top
-    wm transient $top .
+    make_transient $top .
     label $top.title -text [mc "Write commit to file"]
     grid $top.title - -pady 10
     label $top.id -text [mc "ID:"]
@@ -8137,7 +8500,7 @@ proc mkbranch {} {
     set top .makebranch
     catch {destroy $top}
     toplevel $top
-    wm transient $top .
+    make_transient $top .
     label $top.title -text [mc "Create new branch"]
     grid $top.title - -pady 10
     label $top.id -text [mc "ID:"]
@@ -8147,7 +8510,6 @@ proc mkbranch {} {
     grid $top.id $top.sha1 -sticky w
     label $top.nlab -text [mc "Name:"]
     entry $top.name -width 40
-    bind $top.name <Key-Return> "[list mkbrgo $top]"
     grid $top.nlab $top.name -sticky w
     frame $top.buts
     button $top.buts.go -text [mc "Create"] -command [list mkbrgo $top]
@@ -8281,6 +8643,7 @@ proc cherrypick {} {
     }
     addnewchild $newhead $oldhead
     if {[commitinview $oldhead $curview]} {
+       # XXX this isn't right if we have a path limit...
        insertrow $newhead $oldhead $curview
        if {$mainhead ne {}} {
            movehead $newhead $mainhead
@@ -8300,7 +8663,7 @@ proc resethead {} {
     set confirm_ok 0
     set w ".confirmreset"
     toplevel $w
-    wm transient $w .
+    make_transient $w .
     wm title $w [mc "Confirm reset"]
     message $w.m -text \
        [mc "Reset branch %s to %s?" $mainhead [string range $rowmenuid 0 7]] \
@@ -8388,7 +8751,7 @@ proc headmenu {x y id head} {
 
 proc cobranch {} {
     global headmenuid headmenuhead headids
-    global showlocalchanges mainheadid
+    global showlocalchanges
 
     # check the tree is clean first??
     nowbusy checkout [mc "Checking out"]
@@ -8409,6 +8772,7 @@ proc cobranch {} {
 
 proc readcheckoutstat {fd newhead newheadid} {
     global mainhead mainheadid headids showlocalchanges progresscoords
+    global viewmainheadid curview
 
     if {[gets $fd line] >= 0} {
        if {[regexp {([0-9]+)% \(([0-9]+)/([0-9]+)\)} $line match p m n]} {
@@ -8426,6 +8790,7 @@ proc readcheckoutstat {fd newhead newheadid} {
     set oldmainid $mainheadid
     set mainhead $newhead
     set mainheadid $newheadid
+    set viewmainheadid($curview) $newheadid
     redrawtags $oldmainid
     redrawtags $newheadid
     selbyid $newheadid
@@ -8480,7 +8845,7 @@ proc showrefs {} {
     }
     toplevel $top
     wm title $top [mc "Tags and heads: %s" [file tail [pwd]]]
-    wm transient $top .
+    make_transient $top .
     text $top.list -background $bgcolor -foreground $fgcolor \
        -selectbackground $selectbgcolor -font mainfont \
        -xscrollcommand "$top.xsb set" -yscrollcommand "$top.ysb set" \
@@ -9822,7 +10187,7 @@ proc choosefont {font which} {
        font create sample
        eval font config sample [font actual $font]
        toplevel $top
-       wm transient $top $prefstop
+       make_transient $top $prefstop
        wm title $top [mc "Gitk font chooser"]
        label $top.l -textvariable fontparam(which)
        pack $top.l -side top
@@ -9939,7 +10304,7 @@ proc doprefs {} {
     }
     toplevel $top
     wm title $top [mc "Gitk preferences"]
-    wm transient $top .
+    make_transient $top .
     label $top.ldisp -text [mc "Commit list display options"]
     grid $top.ldisp - -sticky w -pady 10
     label $top.spacer -text " "
@@ -9951,15 +10316,11 @@ proc doprefs {} {
        -font optionfont
     spinbox $top.maxpct -from 1 -to 100 -width 4 -textvariable maxgraphpct
     grid x $top.maxpctl $top.maxpct -sticky w
-    frame $top.showlocal
-    label $top.showlocal.l -text [mc "Show local changes"] -font optionfont
-    checkbutton $top.showlocal.b -variable showlocalchanges
-    pack $top.showlocal.b $top.showlocal.l -side left
+    checkbutton $top.showlocal -text [mc "Show local changes"] \
+       -font optionfont -variable showlocalchanges
     grid x $top.showlocal -sticky w
-    frame $top.autoselect
-    label $top.autoselect.l -text [mc "Auto-select SHA1"] -font optionfont
-    checkbutton $top.autoselect.b -variable autoselect
-    pack $top.autoselect.b $top.autoselect.l -side left
+    checkbutton $top.autoselect -text [mc "Auto-select SHA1"] \
+       -font optionfont -variable autoselect
     grid x $top.autoselect -sticky w
 
     label $top.ddisp -text [mc "Diff display options"]
@@ -9967,20 +10328,14 @@ proc doprefs {} {
     label $top.tabstopl -text [mc "Tab spacing"] -font optionfont
     spinbox $top.tabstop -from 1 -to 20 -width 4 -textvariable tabstop
     grid x $top.tabstopl $top.tabstop -sticky w
-    frame $top.ntag
-    label $top.ntag.l -text [mc "Display nearby tags"] -font optionfont
-    checkbutton $top.ntag.b -variable showneartags
-    pack $top.ntag.b $top.ntag.l -side left
+    checkbutton $top.ntag -text [mc "Display nearby tags"] \
+       -font optionfont -variable showneartags
     grid x $top.ntag -sticky w
-    frame $top.ldiff
-    label $top.ldiff.l -text [mc "Limit diffs to listed paths"] -font optionfont
-    checkbutton $top.ldiff.b -variable limitdiffs
-    pack $top.ldiff.b $top.ldiff.l -side left
+    checkbutton $top.ldiff -text [mc "Limit diffs to listed paths"] \
+       -font optionfont -variable limitdiffs
     grid x $top.ldiff -sticky w
-    frame $top.lattr
-    label $top.lattr.l -text [mc "Support per-file encodings"] -font optionfont
-    checkbutton $top.lattr.b -variable perfile_attrs
-    pack $top.lattr.b $top.lattr.l -side left
+    checkbutton $top.lattr -text [mc "Support per-file encodings"] \
+       -font optionfont -variable perfile_attrs
     grid x $top.lattr -sticky w
 
     entry $top.extdifft -textvariable extdifftool
@@ -9996,26 +10351,26 @@ proc doprefs {} {
     grid $top.cdisp - -sticky w -pady 10
     label $top.bg -padx 40 -relief sunk -background $bgcolor
     button $top.bgbut -text [mc "Background"] -font optionfont \
-       -command [list choosecolor bgcolor {} $top.bg background setbg]
+       -command [list choosecolor bgcolor {} $top.bg [mc "background"] setbg]
     grid x $top.bgbut $top.bg -sticky w
     label $top.fg -padx 40 -relief sunk -background $fgcolor
     button $top.fgbut -text [mc "Foreground"] -font optionfont \
-       -command [list choosecolor fgcolor {} $top.fg foreground setfg]
+       -command [list choosecolor fgcolor {} $top.fg [mc "foreground"] setfg]
     grid x $top.fgbut $top.fg -sticky w
     label $top.diffold -padx 40 -relief sunk -background [lindex $diffcolors 0]
     button $top.diffoldbut -text [mc "Diff: old lines"] -font optionfont \
-       -command [list choosecolor diffcolors 0 $top.diffold "diff old lines" \
+       -command [list choosecolor diffcolors 0 $top.diffold [mc "diff old lines"] \
                      [list $ctext tag conf d0 -foreground]]
     grid x $top.diffoldbut $top.diffold -sticky w
     label $top.diffnew -padx 40 -relief sunk -background [lindex $diffcolors 1]
     button $top.diffnewbut -text [mc "Diff: new lines"] -font optionfont \
-       -command [list choosecolor diffcolors 1 $top.diffnew "diff new lines" \
+       -command [list choosecolor diffcolors 1 $top.diffnew [mc "diff new lines"] \
                      [list $ctext tag conf dresult -foreground]]
     grid x $top.diffnewbut $top.diffnew -sticky w
     label $top.hunksep -padx 40 -relief sunk -background [lindex $diffcolors 2]
     button $top.hunksepbut -text [mc "Diff: hunk header"] -font optionfont \
        -command [list choosecolor diffcolors 2 $top.hunksep \
-                     "diff hunk header" \
+                     [mc "diff hunk header"] \
                      [list $ctext tag conf hunksep -foreground]]
     grid x $top.hunksepbut $top.hunksep -sticky w
     label $top.markbgsep -padx 40 -relief sunk -background $markbgcolor
@@ -10026,7 +10381,7 @@ proc doprefs {} {
     grid x $top.markbgbut $top.markbgsep -sticky w
     label $top.selbgsep -padx 40 -relief sunk -background $selectbgcolor
     button $top.selbgbut -text [mc "Select bg"] -font optionfont \
-       -command [list choosecolor selectbgcolor {} $top.selbgsep background setselbg]
+       -command [list choosecolor selectbgcolor {} $top.selbgsep [mc "background"] setselbg]
     grid x $top.selbgbut $top.selbgsep -sticky w
 
     label $top.cfont -text [mc "Fonts: press to choose"]
@@ -10050,7 +10405,7 @@ proc doprefs {} {
 proc choose_extdiff {} {
     global extdifftool
 
-    set prog [tk_getOpenFile -title "External diff tool" -multiple false]
+    set prog [tk_getOpenFile -title [mc "External diff tool"] -multiple false]
     if {$prog ne {}} {
        set extdifftool $prog
     }
@@ -10093,6 +10448,7 @@ proc setfg {c} {
     }
     allcanvs itemconf text -fill $c
     $canv itemconf circle -outline $c
+    $canv itemconf markid -outline $c
 }
 
 proc prefscan {} {
@@ -10518,6 +10874,9 @@ set gitencoding {}
 catch {
     set gitencoding [exec git config --get i18n.commitencoding]
 }
+catch {
+    set gitencoding [exec git config --get i18n.logoutputencoding]
+}
 if {$gitencoding == ""} {
     set gitencoding "utf-8"
 }
@@ -10539,9 +10898,15 @@ catch {
     }
 }
 
-set mainfont {Helvetica 9}
-set textfont {Courier 9}
-set uifont {Helvetica 9 bold}
+if {[tk windowingsystem] eq "aqua"} {
+    set mainfont {{Lucida Grande} 9}
+    set textfont {Monaco 9}
+    set uifont {{Lucida Grande} 9 bold}
+} else {
+    set mainfont {Helvetica 9}
+    set textfont {Courier 9}
+    set uifont {Helvetica 9 bold}
+}
 set tabstop 8
 set findmergefiles 0
 set maxgraphpct 50
@@ -10562,7 +10927,11 @@ set datetimeformat "%Y-%m-%d %H:%M:%S"
 set autoselect 1
 set perfile_attrs 0
 
-set extdifftool "meld"
+if {[tk windowingsystem] eq "aqua"} {
+    set extdifftool "opendiff"
+} else {
+    set extdifftool "meld"
+}
 
 set colors {green red blue magenta darkgrey brown orange}
 set bgcolor white
@@ -10702,8 +11071,8 @@ set nhl_names {}
 set highlight_paths {}
 set findpattern {}
 set searchdirn -forwards
-set boldrows {}
-set boldnamerows {}
+set boldids {}
+set boldnameids {}
 set diffelide {0 0}
 set markingmatches 0
 set linkentercount 0
@@ -10733,9 +11102,33 @@ set lserial 0
 set isworktree [expr {[exec git rev-parse --is-inside-work-tree] == "true"}]
 setcoords
 makewindow
+catch {
+    image create photo gitlogo      -width 16 -height 16
+
+    image create photo gitlogominus -width  4 -height  2
+    gitlogominus put #C00000 -to 0 0 4 2
+    gitlogo copy gitlogominus -to  1 5
+    gitlogo copy gitlogominus -to  6 5
+    gitlogo copy gitlogominus -to 11 5
+    image delete gitlogominus
+
+    image create photo gitlogoplus  -width  4 -height  4
+    gitlogoplus  put #008000 -to 1 0 3 4
+    gitlogoplus  put #008000 -to 0 1 4 3
+    gitlogo copy gitlogoplus  -to  1 9
+    gitlogo copy gitlogoplus  -to  6 9
+    gitlogo copy gitlogoplus  -to 11 9
+    image delete gitlogoplus
+
+    image create photo gitlogo32    -width 32 -height 32
+    gitlogo32 copy gitlogo -zoom 2 2
+
+    wm iconphoto . -default gitlogo gitlogo32
+}
 # wait for the window to become visible
 tkwait visibility .
 wm title . "[file tail $argv0]: [file tail [pwd]]"
+update
 readrefs
 
 if {$cmdline_files ne {} || $revtreeargs ne {} || $revtreeargscmd ne {}} {
@@ -10766,4 +11159,9 @@ if {[info exists permviews]} {
        addviewmenu $n
     }
 }
+
+if {[tk windowingsystem] eq "win32"} {
+    focus -force .
+}
+
 getcommits {}