Skip to content

Commit 9712b81

Browse files
committed
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 <[email protected]>
1 parent 7fb0abb commit 9712b81

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

gitk

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3318,8 +3318,27 @@ proc index_sha1 {fname} {
33183318
return {}
33193319
}
33203320

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+
33213340
proc external_blame {parent_idx {line {}}} {
3322-
global flist_menu_file
3341+
global flist_menu_file gitdir
33233342
global nullid nullid2
33243343
global parentlist selectedline currentid
33253344

@@ -3338,7 +3357,12 @@ proc external_blame {parent_idx {line {}}} {
33383357
if {$line ne {} && $line > 1} {
33393358
lappend cmdline "--line=$line"
33403359
}
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}"
33423366
if {[catch {eval exec $cmdline &} err]} {
33433367
error_popup "[mc "git gui blame: command failed:"] $err"
33443368
}
@@ -3382,6 +3406,8 @@ proc show_line_source {} {
33823406
error_popup [mc "Error reading index: %s" $err]
33833407
return
33843408
}
3409+
} else {
3410+
set id $parents($curview,$currentid)
33853411
}
33863412
} else {
33873413
set id [lindex $parents($curview,$currentid) $pi]
@@ -3398,7 +3424,7 @@ proc show_line_source {} {
33983424
} else {
33993425
lappend blameargs $id
34003426
}
3401-
lappend blameargs -- $flist_menu_file
3427+
lappend blameargs -- [file join [file dirname $gitdir] $flist_menu_file]
34023428
if {[catch {
34033429
set f [open $blameargs r]
34043430
} err]} {

0 commit comments

Comments
 (0)