@matheusbsbs / matheus-bagliano / commits / 9712b81a76
Fork 0

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 <paulus@samba.org>

Paul Mackerras committed Dec 6, 2008 at 21:44 UTC 9712b81a765978d2770b32bb77d25bfbf05d5eb5
1 file changed +29 -3
gitk
+29 -3
@@ -3318,8 +3318,27 @@ proc index_sha1 {fname} {
3318 return {}
3319 }
3320
3321 +# Turn an absolute path into one relative to the current directory
3322 +proc make_relative {f} {
3323 + set elts [file split $f]
3324 + set here [file split [pwd]]
3325 + set ei 0
3326 + set hi 0
3327 + set res {}
3328 + foreach d $here {
3329 + if {$ei < $hi || $ei >= [llength $elts] || [lindex $elts $ei] ne $d} {
3330 + lappend res ".."
3331 + } else {
3332 + incr ei
3333 + }
3334 + incr hi
3335 + }
3336 + set elts [concat $res [lrange $elts $ei end]]
3337 + return [eval file join $elts]
3338 +}
3339 +
3340 proc external_blame {parent_idx {line {}}} {
3322 - global flist_menu_file
3341 + global flist_menu_file gitdir
3342 global nullid nullid2
3343 global parentlist selectedline currentid
3344
@@ -3338,7 +3357,12 @@ proc external_blame {parent_idx {line {}}} {
3357 if {$line ne {} && $line > 1} {
3358 lappend cmdline "--line=$line"
3359 }
3341 - lappend cmdline $base_commit $flist_menu_file
3360 + set f [file join [file dirname $gitdir] $flist_menu_file]
3361 + # Unfortunately it seems git gui blame doesn't like
3362 + # being given an absolute path...
3363 + set f [make_relative $f]
3364 + lappend cmdline $base_commit $f
3365 + puts "cmdline={$cmdline}"
3366 if {[catch {eval exec $cmdline &} err]} {
3367 error_popup "[mc "git gui blame: command failed:"] $err"
3368 }
@@ -3382,6 +3406,8 @@ proc show_line_source {} {
3406 error_popup [mc "Error reading index: %s" $err]
3407 return
3408 }
3409 + } else {
3410 + set id $parents($curview,$currentid)
3411 }
3412 } else {
3413 set id [lindex $parents($curview,$currentid) $pi]
@@ -3398,7 +3424,7 @@ proc show_line_source {} {
3424 } else {
3425 lappend blameargs $id
3426 }
3401 - lappend blameargs -- $flist_menu_file
3427 + lappend blameargs -- [file join [file dirname $gitdir] $flist_menu_file]
3428 if {[catch {
3429 set f [open $blameargs r]
3430 } err]} {