Skip to content

Commit a9786bb

Browse files
angavrilovspearce
authored andcommitted
git-gui: Fix Blame Parent & Context for working copy lines.
Make Blame Parent Commit and Show History Context work properly for lines blamed on the working copy. Signed-off-by: Alexander Gavrilov <angavrilov@gmail.com> Signed-off-by: Shawn O. Pearce <sop@google.com>
1 parent 95b6a2d commit a9786bb

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

git-gui.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,9 @@ set current_diff_path {}
10251025
set is_3way_diff 0
10261026
set selected_commit_type new
10271027
1028+
set nullid "0000000000000000000000000000000000000000"
1029+
set nullid2 "0000000000000000000000000000000000000001"
1030+
10281031
######################################################################
10291032
##
10301033
## task management

lib/blame.tcl

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -942,9 +942,20 @@ method _format_offset_date {base offset} {
942942
}
943943

944944
method _gitkcommit {} {
945+
global nullid
946+
945947
set dat [_get_click_amov_info $this]
946948
if {$dat ne {}} {
947949
set cmit [lindex $dat 0]
950+
951+
# If the line belongs to the working copy, use HEAD instead
952+
if {$cmit eq $nullid} {
953+
if {[catch {set cmit [git rev-parse --verify HEAD]} err]} {
954+
error_popup [strcat [mc "Cannot find HEAD commit:"] "\n\n$err"]
955+
return;
956+
}
957+
}
958+
948959
set radius [get_config gui.blamehistoryctx]
949960
set cmdline [list --select-commit=$cmit]
950961

@@ -981,12 +992,20 @@ method _gitkcommit {} {
981992
}
982993

983994
method _blameparent {} {
995+
global nullid
996+
984997
set dat [_get_click_amov_info $this]
985998
if {$dat ne {}} {
986999
set cmit [lindex $dat 0]
9871000
set new_path [lindex $dat 1]
9881001

989-
if {[catch {set cparent [git rev-parse --verify "$cmit^"]}]} {
1002+
# Allow using Blame Parent on lines modified in the working copy
1003+
if {$cmit eq $nullid} {
1004+
set parent_ref "HEAD"
1005+
} else {
1006+
set parent_ref "$cmit^"
1007+
}
1008+
if {[catch {set cparent [git rev-parse --verify $parent_ref]} err]} {
9901009
error_popup [strcat [mc "Cannot find parent commit:"] "\n\n$err"]
9911010
return;
9921011
}
@@ -996,8 +1015,12 @@ method _blameparent {} {
9961015
# Generate a diff between the commit and its parent,
9971016
# and use the hunks to update the line number.
9981017
# Request zero context to simplify calculations.
999-
if {[catch {set fd [eval git_read diff-tree \
1000-
--unified=0 $cparent $cmit $new_path]} err]} {
1018+
if {$cmit eq $nullid} {
1019+
set diffcmd [list diff-index --unified=0 $cparent -- $new_path]
1020+
} else {
1021+
set diffcmd [list diff-tree --unified=0 $cparent $cmit -- $new_path]
1022+
}
1023+
if {[catch {set fd [eval git_read $diffcmd]} err]} {
10011024
$status stop [mc "Unable to display parent"]
10021025
error_popup [strcat [mc "Error loading diff:"] "\n\n$err"]
10031026
return

0 commit comments

Comments
 (0)