From 9712b81a765978d2770b32bb77d25bfbf05d5eb5 Mon Sep 17 00:00:00 2001 From: Paul Mackerras Date: Sat, 6 Dec 2008 21:44:05 +1100 Subject: [PATCH] gitk: Fix bugs in blaming code The "show origin of this line" function wasn't working when gitk was run in a subdirectory, since it passed the path relative to the top-level directory to git blame. This fixes it by passing the absolute path to the file instead of the relative path. The same problem occurs when running git gui blame, except that git gui blame appears not to be able to accept an absolute path to the file, so we make a relative path using a new [make_relative] function. Finally, this fixes a bug in [show_line_source] where we weren't setting id, resulting in an error when trying to find the origin of a line in the fake commit for local changes not checked in, when its parent was a real commit (i.e. there were no changes checked in). Signed-off-by: Paul Mackerras --- gitk | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/gitk b/gitk index 64a873d2e..a5e24e4a2 100755 --- a/gitk +++ b/gitk @@ -3318,8 +3318,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 @@ -3338,7 +3357,12 @@ 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 + puts "cmdline={$cmdline}" if {[catch {eval exec $cmdline &} err]} { error_popup "[mc "git gui blame: command failed:"] $err" } @@ -3382,6 +3406,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] @@ -3398,7 +3424,7 @@ 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]} { -- 2.30.2