Fixed a typo in Documentation/git-cvsimport-script.txt.
[git.git] / gitk
diff --git a/gitk b/gitk
index 9ad7bfc..ff4d6f8 100755 (executable)
--- a/gitk
+++ b/gitk
@@ -8,10 +8,20 @@ exec wish "$0" -- "${1+$@}"
 # either version 2, or (at your option) any later version.
 
 proc getcommits {rargs} {
 # either version 2, or (at your option) any later version.
 
 proc getcommits {rargs} {
-    global commits commfd phase canv mainfont
+    global commits commfd phase canv mainfont env
     global startmsecs nextupdate
     global ctext maincursor textcursor leftover
 
     global startmsecs nextupdate
     global ctext maincursor textcursor leftover
 
+    # check that we can find a .git directory somewhere...
+    if {[info exists env(GIT_DIR)]} {
+       set gitdir $env(GIT_DIR)
+    } else {
+       set gitdir ".git"
+    }
+    if {![file isdirectory $gitdir]} {
+       error_popup "Cannot find the git directory \"$gitdir\"."
+       exit 1
+    }
     set commits {}
     set phase getcommits
     set startmsecs [clock clicks -milliseconds]
     set commits {}
     set phase getcommits
     set startmsecs [clock clicks -milliseconds]
@@ -71,16 +81,21 @@ to allow selection of commits to be displayed.)}
     while 1 {
        set i [string first "\0" $stuff $start]
        if {$i < 0} {
     while 1 {
        set i [string first "\0" $stuff $start]
        if {$i < 0} {
-           set leftover [string range $stuff $start end]
+           append leftover [string range $stuff $start end]
            return
        }
        set cmit [string range $stuff $start [expr {$i - 1}]]
        if {$start == 0} {
            set cmit "$leftover$cmit"
            return
        }
        set cmit [string range $stuff $start [expr {$i - 1}]]
        if {$start == 0} {
            set cmit "$leftover$cmit"
+           set leftover {}
        }
        set start [expr {$i + 1}]
        if {![regexp {^([0-9a-f]{40})\n} $cmit match id]} {
        }
        set start [expr {$i + 1}]
        if {![regexp {^([0-9a-f]{40})\n} $cmit match id]} {
-           error_popup "Can't parse git-rev-list output: {$cmit}"
+           set shortcmit $cmit
+           if {[string length $shortcmit] > 80} {
+               set shortcmit "[string range $shortcmit 0 80]..."
+           }
+           error_popup "Can't parse git-rev-list output: {$shortcmit}"
            exit 1
        }
        set cmit [string range $cmit 41 end]
            exit 1
        }
        set cmit [string range $cmit 41 end]
@@ -409,6 +424,8 @@ proc makewindow {} {
        -command {diffvssel 0}
     $rowctxmenu add command -label "Diff selected -> this" \
        -command {diffvssel 1}
        -command {diffvssel 0}
     $rowctxmenu add command -label "Diff selected -> this" \
        -command {diffvssel 1}
+    $rowctxmenu add command -label "Make patch" -command mkpatch
+    $rowctxmenu add command -label "Create tag" -command mktag
 }
 
 # when we make a key binding for the toplevel, make sure
 }
 
 # when we make a key binding for the toplevel, make sure
@@ -655,7 +672,7 @@ proc drawcommitline {level} {
     global oldlevel oldnlines oldtodo
     global idtags idline idheads
     global lineno lthickness mainline sidelines
     global oldlevel oldnlines oldtodo
     global idtags idline idheads
     global lineno lthickness mainline sidelines
-    global commitlisted rowtextx
+    global commitlisted rowtextx idpos
 
     incr numcommits
     incr lineno
 
     incr numcommits
     incr lineno
@@ -716,47 +733,9 @@ proc drawcommitline {level} {
        set xt [expr {$xt + ([llength $currentparents] - 2) * $linespc}]
     }
     set rowtextx($lineno) $xt
        set xt [expr {$xt + ([llength $currentparents] - 2) * $linespc}]
     }
     set rowtextx($lineno) $xt
-    set marks {}
-    set ntags 0
-    if {[info exists idtags($id)]} {
-       set marks $idtags($id)
-       set ntags [llength $marks]
-    }
-    if {[info exists idheads($id)]} {
-       set marks [concat $marks $idheads($id)]
-    }
-    if {$marks != {}} {
-       set delta [expr {int(0.5 * ($linespc - $lthickness))}]
-       set yt [expr $y1 - 0.5 * $linespc]
-       set yb [expr $yt + $linespc - 1]
-       set xvals {}
-       set wvals {}
-       foreach tag $marks {
-           set wid [font measure $mainfont $tag]
-           lappend xvals $xt
-           lappend wvals $wid
-           set xt [expr {$xt + $delta + $wid + $lthickness + $linespc}]
-       }
-       set t [$canv create line $x $y1 [lindex $xvals end] $y1 \
-                  -width $lthickness -fill black]
-       $canv lower $t
-       foreach tag $marks x $xvals wid $wvals {
-           set xl [expr $x + $delta]
-           set xr [expr $x + $delta + $wid + $lthickness]
-           if {[incr ntags -1] >= 0} {
-               # draw a tag
-               $canv create polygon $x [expr $yt + $delta] $xl $yt\
-                   $xr $yt $xr $yb $xl $yb $x [expr $yb - $delta] \
-                   -width 1 -outline black -fill yellow
-           } else {
-               # draw a head
-               set xl [expr $xl - $delta/2]
-               $canv create polygon $x $yt $xr $yt $xr $yb $x $yb \
-                   -width 1 -outline black -fill green
-           }
-           $canv create text $xl $y1 -anchor w -text $tag \
-               -font $mainfont
-       }
+    set idpos($id) [list $x $xt $y1]
+    if {[info exists idtags($id)] || [info exists idheads($id)]} {
+       set xt [drawtags $id $x $xt $y1]
     }
     set headline [lindex $commitinfo($id) 0]
     set name [lindex $commitinfo($id) 1]
     }
     set headline [lindex $commitinfo($id) 0]
     set name [lindex $commitinfo($id) 1]
@@ -770,6 +749,58 @@ proc drawcommitline {level} {
                               -text $date -font $mainfont]
 }
 
                               -text $date -font $mainfont]
 }
 
+proc drawtags {id x xt y1} {
+    global idtags idheads
+    global linespc lthickness
+    global canv mainfont
+
+    set marks {}
+    set ntags 0
+    if {[info exists idtags($id)]} {
+       set marks $idtags($id)
+       set ntags [llength $marks]
+    }
+    if {[info exists idheads($id)]} {
+       set marks [concat $marks $idheads($id)]
+    }
+    if {$marks eq {}} {
+       return $xt
+    }
+
+    set delta [expr {int(0.5 * ($linespc - $lthickness))}]
+    set yt [expr $y1 - 0.5 * $linespc]
+    set yb [expr $yt + $linespc - 1]
+    set xvals {}
+    set wvals {}
+    foreach tag $marks {
+       set wid [font measure $mainfont $tag]
+       lappend xvals $xt
+       lappend wvals $wid
+       set xt [expr {$xt + $delta + $wid + $lthickness + $linespc}]
+    }
+    set t [$canv create line $x $y1 [lindex $xvals end] $y1 \
+              -width $lthickness -fill black -tags tag.$id]
+    $canv lower $t
+    foreach tag $marks x $xvals wid $wvals {
+       set xl [expr $x + $delta]
+       set xr [expr $x + $delta + $wid + $lthickness]
+       if {[incr ntags -1] >= 0} {
+           # draw a tag
+           $canv create polygon $x [expr $yt + $delta] $xl $yt\
+               $xr $yt $xr $yb $xl $yb $x [expr $yb - $delta] \
+               -width 1 -outline black -fill yellow -tags tag.$id
+       } else {
+           # draw a head
+           set xl [expr $xl - $delta/2]
+           $canv create polygon $x $yt $xr $yt $xr $yb $x $yb \
+               -width 1 -outline black -fill green -tags tag.$id
+       }
+       $canv create text $xl $y1 -anchor w -text $tag \
+           -font $mainfont -tags tag.$id
+    }
+    return $xt
+}
+
 proc updatetodo {level noshortcut} {
     global currentparents ncleft todo
     global mainline oldlevel oldtodo oldnlines
 proc updatetodo {level noshortcut} {
     global currentparents ncleft todo
     global mainline oldlevel oldtodo oldnlines
@@ -884,11 +915,11 @@ proc drawslants {} {
     }
 }
 
     }
 }
 
-proc decidenext {} {
+proc decidenext {{noread 0}} {
     global parents children nchildren ncleft todo
     global canv canv2 canv3 mainfont namefont canvx0 canvy linespc
     global datemode cdate
     global parents children nchildren ncleft todo
     global canv canv2 canv3 mainfont namefont canvx0 canvy linespc
     global datemode cdate
-    global lineid linehtag linentag linedtag commitinfo
+    global commitinfo
     global currentparents oldlevel oldnlines oldtodo
     global lineno lthickness
 
     global currentparents oldlevel oldnlines oldtodo
     global lineno lthickness
 
@@ -906,6 +937,12 @@ proc decidenext {} {
        set p [lindex $todo $k]
        if {$ncleft($p) == 0} {
            if {$datemode} {
        set p [lindex $todo $k]
        if {$ncleft($p) == 0} {
            if {$datemode} {
+               if {![info exists commitinfo($p)]} {
+                   if {$noread} {
+                       return {}
+                   }
+                   readcommit $p
+               }
                if {$latest == {} || $cdate($p) > $latest} {
                    set level $k
                    set latest $cdate($p)
                if {$latest == {} || $cdate($p) > $latest} {
                    set level $k
                    set latest $cdate($p)
@@ -966,15 +1003,16 @@ proc drawcommit {id} {
            lappend todo $id
            lappend startcommits $id
        }
            lappend todo $id
            lappend startcommits $id
        }
-       set level [decidenext]
-       if {$id != [lindex $todo $level]} {
+       set level [decidenext 1]
+       if {$level == {} || $id != [lindex $todo $level]} {
            return
        }
        while 1 {
            drawslants
            drawcommitline $level
            if {[updatetodo $level $datemode]} {
            return
        }
        while 1 {
            drawslants
            drawcommitline $level
            if {[updatetodo $level $datemode]} {
-               set level [decidenext]
+               set level [decidenext 1]
+               if {$level == {}} break
            }
            set id [lindex $todo $level]
            if {![info exists commitlisted($id)]} {
            }
            set id [lindex $todo $level]
            if {![info exists commitlisted($id)]} {
@@ -991,18 +1029,18 @@ proc drawcommit {id} {
 proc finishcommits {} {
     global phase
     global startcommits
 proc finishcommits {} {
     global phase
     global startcommits
-    global ctext maincursor textcursor
+    global canv mainfont ctext maincursor textcursor
 
     if {$phase != "incrdraw"} {
        $canv delete all
        $canv create text 3 3 -anchor nw -text "No commits selected" \
            -font $mainfont -tags textitems
        set phase {}
 
     if {$phase != "incrdraw"} {
        $canv delete all
        $canv create text 3 3 -anchor nw -text "No commits selected" \
            -font $mainfont -tags textitems
        set phase {}
-       return
+    } else {
+       drawslants
+       set level [decidenext]
+       drawrest $level [llength $startcommits]
     }
     }
-    drawslants
-    set level [decidenext]
-    drawrest $level [llength $startcommits]
     . config -cursor $maincursor
     $ctext config -cursor $textcursor
 }
     . config -cursor $maincursor
     $ctext config -cursor $textcursor
 }
@@ -1734,6 +1772,7 @@ proc rowmenu {x y id} {
     }
     $rowctxmenu entryconfigure 0 -state $state
     $rowctxmenu entryconfigure 1 -state $state
     }
     $rowctxmenu entryconfigure 0 -state $state
     $rowctxmenu entryconfigure 1 -state $state
+    $rowctxmenu entryconfigure 2 -state $state
     set rowmenuid $id
     tk_popup $rowctxmenu $x $y
 }
     set rowmenuid $id
     tk_popup $rowctxmenu $x $y
 }
@@ -1769,6 +1808,172 @@ proc diffvssel {dirn} {
     startdiff
 }
 
     startdiff
 }
 
+proc mkpatch {} {
+    global rowmenuid currentid commitinfo patchtop patchnum
+
+    if {![info exists currentid]} return
+    set oldid $currentid
+    set oldhead [lindex $commitinfo($oldid) 0]
+    set newid $rowmenuid
+    set newhead [lindex $commitinfo($newid) 0]
+    set top .patch
+    set patchtop $top
+    catch {destroy $top}
+    toplevel $top
+    label $top.title -text "Generate patch"
+    grid $top.title -
+    label $top.from -text "From:"
+    entry $top.fromsha1 -width 40
+    $top.fromsha1 insert 0 $oldid
+    $top.fromsha1 conf -state readonly
+    grid $top.from $top.fromsha1 -sticky w
+    entry $top.fromhead -width 60
+    $top.fromhead insert 0 $oldhead
+    $top.fromhead conf -state readonly
+    grid x $top.fromhead -sticky w
+    label $top.to -text "To:"
+    entry $top.tosha1 -width 40
+    $top.tosha1 insert 0 $newid
+    $top.tosha1 conf -state readonly
+    grid $top.to $top.tosha1 -sticky w
+    entry $top.tohead -width 60
+    $top.tohead insert 0 $newhead
+    $top.tohead conf -state readonly
+    grid x $top.tohead -sticky w
+    button $top.rev -text "Reverse" -command mkpatchrev -padx 5
+    grid $top.rev x -pady 10
+    label $top.flab -text "Output file:"
+    entry $top.fname -width 60
+    $top.fname insert 0 [file normalize "patch$patchnum.patch"]
+    incr patchnum
+    grid $top.flab $top.fname -sticky w
+    frame $top.buts
+    button $top.buts.gen -text "Generate" -command mkpatchgo
+    button $top.buts.can -text "Cancel" -command mkpatchcan
+    grid $top.buts.gen $top.buts.can
+    grid columnconfigure $top.buts 0 -weight 1 -uniform a
+    grid columnconfigure $top.buts 1 -weight 1 -uniform a
+    grid $top.buts - -pady 10 -sticky ew
+    focus $top.fname
+}
+
+proc mkpatchrev {} {
+    global patchtop
+
+    set oldid [$patchtop.fromsha1 get]
+    set oldhead [$patchtop.fromhead get]
+    set newid [$patchtop.tosha1 get]
+    set newhead [$patchtop.tohead get]
+    foreach e [list fromsha1 fromhead tosha1 tohead] \
+           v [list $newid $newhead $oldid $oldhead] {
+       $patchtop.$e conf -state normal
+       $patchtop.$e delete 0 end
+       $patchtop.$e insert 0 $v
+       $patchtop.$e conf -state readonly
+    }
+}
+
+proc mkpatchgo {} {
+    global patchtop
+
+    set oldid [$patchtop.fromsha1 get]
+    set newid [$patchtop.tosha1 get]
+    set fname [$patchtop.fname get]
+    if {[catch {exec git-diff-tree -p $oldid $newid >$fname &} err]} {
+       error_popup "Error creating patch: $err"
+    }
+    catch {destroy $patchtop}
+    unset patchtop
+}
+
+proc mkpatchcan {} {
+    global patchtop
+
+    catch {destroy $patchtop}
+    unset patchtop
+}
+
+proc mktag {} {
+    global rowmenuid mktagtop commitinfo
+
+    set top .maketag
+    set mktagtop $top
+    catch {destroy $top}
+    toplevel $top
+    label $top.title -text "Create tag"
+    grid $top.title -
+    label $top.id -text "ID:"
+    entry $top.sha1 -width 40
+    $top.sha1 insert 0 $rowmenuid
+    $top.sha1 conf -state readonly
+    grid $top.id $top.sha1 -sticky w
+    entry $top.head -width 40
+    $top.head insert 0 [lindex $commitinfo($rowmenuid) 0]
+    $top.head conf -state readonly
+    grid x $top.head -sticky w
+    label $top.tlab -text "Tag name:"
+    entry $top.tag -width 40
+    grid $top.tlab $top.tag -sticky w
+    frame $top.buts
+    button $top.buts.gen -text "Create" -command mktaggo
+    button $top.buts.can -text "Cancel" -command mktagcan
+    grid $top.buts.gen $top.buts.can
+    grid columnconfigure $top.buts 0 -weight 1 -uniform a
+    grid columnconfigure $top.buts 1 -weight 1 -uniform a
+    grid $top.buts - -pady 10 -sticky ew
+    focus $top.tag
+}
+
+proc domktag {} {
+    global mktagtop env tagids idtags
+    global idpos idline linehtag canv selectedline
+
+    set id [$mktagtop.sha1 get]
+    set tag [$mktagtop.tag get]
+    if {$tag == {}} {
+       error_popup "No tag name specified"
+       return
+    }
+    if {[info exists tagids($tag)]} {
+       error_popup "Tag \"$tag\" already exists"
+       return
+    }
+    if {[catch {
+       set dir ".git"
+       if {[info exists env(GIT_DIR)]} {
+           set dir $env(GIT_DIR)
+       }
+       set fname [file join $dir "refs/tags" $tag]
+       set f [open $fname w]
+       puts $f $id
+       close $f
+    } err]} {
+       error_popup "Error creating tag: $err"
+       return
+    }
+
+    set tagids($tag) $id
+    lappend idtags($id) $tag
+    $canv delete tag.$id
+    set xt [eval drawtags $id $idpos($id)]
+    $canv coords $linehtag($idline($id)) $xt [lindex $idpos($id) 2]
+    if {[info exists selectedline] && $selectedline == $idline($id)} {
+       selectline $selectedline
+    }
+}
+
+proc mktagcan {} {
+    global mktagtop
+
+    catch {destroy $mktagtop}
+    unset mktagtop
+}
+
+proc mktaggo {} {
+    domktag
+    mktagcan
+}
+
 proc doquit {} {
     global stopped
     set stopped 100
 proc doquit {} {
     global stopped
     set stopped 100
@@ -1807,6 +2012,7 @@ foreach arg $argv {
 set stopped 0
 set redisplaying 0
 set stuffsaved 0
 set stopped 0
 set redisplaying 0
 set stuffsaved 0
+set patchnum 0
 setcoords
 makewindow
 readrefs
 setcoords
 makewindow
 readrefs