Skip to content

Commit

Permalink
gitk: Show diff of commits at end of compare-commits output
Browse files Browse the repository at this point in the history
When comparing a string of commits, when we find two non-merge commits
that differ, we now write the two commits to files and diff the files.
This pulls out the logic for creating a temporary directory from
external_diff into a separate procedure so that the new diffcommits
procedure can use it.

Because the diff command returns an exit status of 1 when the files
differ, and Tcl treats that as an error, this adds catch {} around the
close statements in getblobdiffline.

At present this only removes the temporary files when gitk exits.  It
should remove them when the diff is done.

Signed-off-by: Paul Mackerras <[email protected]>
  • Loading branch information
paulusmack committed Sep 10, 2009
1 parent b53bb30 commit c21398b
Showing 1 changed file with 58 additions and 23 deletions.
81 changes: 58 additions & 23 deletions gitk
Original file line number Diff line number Diff line change
Expand Up @@ -3167,6 +3167,28 @@ proc flist_hl {only} {
set gdttype [mc "touching paths:"]
}

proc gitknewtmpdir {} {
global diffnum gitktmpdir gitdir

if {![info exists gitktmpdir]} {
set gitktmpdir [file join [file dirname $gitdir] \
[format ".gitk-tmp.%s" [pid]]]
if {[catch {file mkdir $gitktmpdir} err]} {
error_popup "[mc "Error creating temporary directory %s:" $gitktmpdir] $err"
unset gitktmpdir
return {}
}
set diffnum 0
}
incr diffnum
set diffdir [file join $gitktmpdir $diffnum]
if {[catch {file mkdir $diffdir} err]} {
error_popup "[mc "Error creating temporary directory %s:" $diffdir] $err"
return {}
}
return $diffdir
}

proc save_file_from_commit {filename output what} {
global nullfile

Expand Down Expand Up @@ -3201,11 +3223,10 @@ proc external_diff_get_one_file {diffid filename diffdir} {
}

proc external_diff {} {
global gitktmpdir nullid nullid2
global nullid nullid2
global flist_menu_file
global diffids
global diffnum
global gitdir extdifftool
global extdifftool

if {[llength $diffids] == 1} {
# no reference commit given
Expand All @@ -3227,22 +3248,8 @@ proc external_diff {} {
}

# make sure that several diffs wont collide
if {![info exists gitktmpdir]} {
set gitktmpdir [file join [file dirname $gitdir] \
[format ".gitk-tmp.%s" [pid]]]
if {[catch {file mkdir $gitktmpdir} err]} {
error_popup "[mc "Error creating temporary directory %s:" $gitktmpdir] $err"
unset gitktmpdir
return
}
set diffnum 0
}
incr diffnum
set diffdir [file join $gitktmpdir $diffnum]
if {[catch {file mkdir $diffdir} err]} {
error_popup "[mc "Error creating temporary directory %s:" $diffdir] $err"
return
}
set diffdir [gitknewtmpdir]
if {$diffdir eq {}} return

# gather files to diff
set difffromfile [external_diff_get_one_file $diffidfrom $flist_menu_file $diffdir]
Expand Down Expand Up @@ -7400,7 +7407,7 @@ proc getblobdiffline {bdf ids} {
$ctext conf -state normal
while {[incr nr] <= 1000 && [gets $bdf line] >= 0} {
if {$ids != $diffids || $bdf != $blobdifffd($ids)} {
close $bdf
catch {close $bdf}
return 0
}
if {![string compare -length 5 "diff " $line]} {
Expand Down Expand Up @@ -7552,7 +7559,7 @@ proc getblobdiffline {bdf ids} {
}
$ctext conf -state disabled
if {[eof $bdf]} {
close $bdf
catch {close $bdf}
return 0
}
return [expr {$nr >= 1000? 2: 1}]
Expand Down Expand Up @@ -8273,8 +8280,11 @@ proc do_cmp_commits {a b} {
appendshortlink $a [mc "Commit "] " $heada\n"
appendshortlink $b [mc " differs from\n "] \
" $headb\n"
$ctext insert end [mc "- stopping\n"]
break
$ctext insert end [mc "Diff of commits:\n\n"]
$ctext conf -state disabled
update
diffcommits $a $b
return
}
}
if {$skipa} {
Expand All @@ -8300,6 +8310,31 @@ proc do_cmp_commits {a b} {
$ctext conf -state disabled
}

proc diffcommits {a b} {
global diffcontext diffids blobdifffd diffinhdr

set tmpdir [gitknewtmpdir]
set fna [file join $tmpdir "commit-[string range $a 0 7]"]
set fnb [file join $tmpdir "commit-[string range $b 0 7]"]
if {[catch {
exec git diff-tree -p --pretty $a >$fna
exec git diff-tree -p --pretty $b >$fnb
} err]} {
error_popup [mc "Error writing commit to file: %s" $err]
return
}
if {[catch {
set fd [open "| diff -U$diffcontext $fna $fnb" r]
} err]} {
error_popup [mc "Error diffing commits: %s" $err]
return
}
set diffids [list commits $a $b]
set blobdifffd($diffids) $fd
set diffinhdr 0
filerun $fd [list getblobdiffline $fd $diffids]
}

proc diffvssel {dirn} {
global rowmenuid selectedline

Expand Down

0 comments on commit c21398b

Please sign in to comment.