Skip to content

Commit 11a1aaf

Browse files
committed
gitk: add external diff file rename detection
If a file is renamed between commits and an external diff is started through gitk on the original or the renamed file name, gitk is unable to open the renamed file in the external diff editor. It fails to fetch the renamed file from git, because it fetches it using its original path in contrast to using the renamed path of the file. Detect the rename and open the external diff with the original and the renamed file instead of no file (fetch the renamed file path and name from git) no matter if the original or the renamed file is selected in gitk. Signed-off-by: Tobias Boesch <[email protected]>
1 parent 14de3eb commit 11a1aaf

File tree

1 file changed

+57
-2
lines changed

1 file changed

+57
-2
lines changed

gitk-git/gitk

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3775,6 +3775,53 @@ proc external_diff_get_one_file {diffid filename diffdir} {
37753775
"revision $diffid"]
37763776
}
37773777
3778+
proc check_for_renames_in_diff {diffidfrom diffidto filepath} {
3779+
global nullid nullid2
3780+
3781+
if {$diffidfrom eq $nullid} {
3782+
set rev [list $diffidto]
3783+
set cmd [list diff-index -R]
3784+
} elseif {$diffidfrom eq $nullid2} {
3785+
set rev [list $diffidto]
3786+
set cmd [list diff-index --cached -R]
3787+
} elseif {$diffidto eq $nullid} {
3788+
set rev [list $diffidfrom]
3789+
set cmd [list diff-index]
3790+
} elseif {$diffidto eq $nullid2} {
3791+
set rev [list $diffidfrom]
3792+
set cmd [list diff-index --cached]
3793+
} else {
3794+
set rev [list $diffidfrom..$diffidto]
3795+
set cmd [list diff-tree]
3796+
}
3797+
3798+
set renames [list {}]
3799+
if {[catch {eval exec git $cmd --find-renames --stat --raw --diff-filter=R $rev} cmd_result]} {
3800+
error_popup "[mc "Error getting file rename info for file \"%s\" from commit %s to %s." \
3801+
$filepath $diffidfrom $diffidto] $cmd_result.\n\n"
3802+
}
3803+
set filename [file tail $filepath]
3804+
set esc_chars {\\ | ? ^ * . $ \[ \] + \( \) \{ \}}
3805+
foreach char $esc_chars {
3806+
set filename [string map [list $char \\$char] $filename]
3807+
}
3808+
set regex_base {\d+\s\d+\s\S+\s\S+\s\S+\s+}
3809+
set regex_ren_from $regex_base[subst -nobackslashes -nocommands {(\S+$filename)\s+(\S+)}]
3810+
set regex_ren_to $regex_base[subst -nobackslashes -nocommands {(\S+)\s+(\S+$filename)}]
3811+
if {[regexp -line -- $regex_ren_from $cmd_result whole_match ren_from ren_to]} {
3812+
if {$ren_from ne {} && $ren_to ne {}} {
3813+
lappend renames $ren_from
3814+
lappend renames $ren_to
3815+
}
3816+
} elseif {[regexp -line -- $regex_ren_to $cmd_result whole_match ren_from ren_to]} {
3817+
if {$ren_from ne {} && $ren_to ne {}} {
3818+
lappend renames $ren_from
3819+
lappend renames $ren_to
3820+
}
3821+
}
3822+
return $renames
3823+
}
3824+
37783825
proc external_diff {} {
37793826
global nullid nullid2
37803827
global flist_menu_file
@@ -3805,8 +3852,16 @@ proc external_diff {} {
38053852
if {$diffdir eq {}} return
38063853
38073854
# gather files to diff
3808-
set difffromfile [external_diff_get_one_file $diffidfrom $flist_menu_file $diffdir]
3809-
set difftofile [external_diff_get_one_file $diffidto $flist_menu_file $diffdir]
3855+
set renames [check_for_renames_in_diff $diffidfrom $diffidto $flist_menu_file]
3856+
set renamefrom [lindex $renames 1]
3857+
set renameto [lindex $renames 2]
3858+
if { ($renamefrom != {}) && ($renameto != {}) } {
3859+
set difffromfile [external_diff_get_one_file $diffidfrom $renamefrom $diffdir]
3860+
set difftofile [external_diff_get_one_file $diffidto $renameto $diffdir]
3861+
} else {
3862+
set difffromfile [external_diff_get_one_file $diffidfrom $flist_menu_file $diffdir]
3863+
set difftofile [external_diff_get_one_file $diffidto $flist_menu_file $diffdir]
3864+
}
38103865
38113866
if {$difffromfile ne {} && $difftofile ne {}} {
38123867
set cmd [list [shellsplit $extdifftool] $difffromfile $difftofile]

0 commit comments

Comments
 (0)