Skip to content

Commit f1c71e5

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. Since moved or renamed file are handled the same way do this also for moved files. Signed-off-by: Tobias Boesch <tobias.boesch@miele.com>
1 parent 14de3eb commit f1c71e5

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

gitk-git/gitk

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3775,6 +3775,48 @@ 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 -R]
3783+
} elseif {$diffidfrom eq $nullid2} {
3784+
set rev [list $diffidto --cached -R]
3785+
} elseif {$diffidto eq $nullid} {
3786+
set rev [list $diffidfrom]
3787+
} elseif {$diffidto eq $nullid2} {
3788+
set rev [list $diffidfrom --cached]
3789+
} else {
3790+
set rev [list $diffidfrom..$diffidto]
3791+
}
3792+
3793+
set renames [list {}]
3794+
if {[catch {eval exec git diff $rev --find-renames --stat --raw --diff-filter=R} cmd_result]} {
3795+
error_popup "[mc "Error getting file rename info for file \"%s\" from commit %s to %s." \
3796+
$filepath $diffidfrom $diffidto] $cmd_result.\n\n"
3797+
}
3798+
set filename [file tail $filepath]
3799+
set esc_chars {\\ | ? ^ * . $ \[ \] + \( \) \{ \}}
3800+
foreach char $esc_chars {
3801+
set filename [string map [list $char \\$char] $filename]
3802+
}
3803+
set regex_base {\d+\s\d+\s\S+\s\S+\s\S+\s+}
3804+
set regex_ren_from $regex_base[subst -nobackslashes -nocommands {(\S+$filename)\s+(\S+)}]
3805+
set regex_ren_to $regex_base[subst -nobackslashes -nocommands {(\S+)\s+(\S+$filename)}]
3806+
if {[regexp -line -- $regex_ren_from $cmd_result whole_match ren_from ren_to]} {
3807+
if {$ren_from ne {} && $ren_to ne {}} {
3808+
lappend renames $ren_from
3809+
lappend renames $ren_to
3810+
}
3811+
} elseif {[regexp -line -- $regex_ren_to $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+
}
3817+
return $renames
3818+
}
3819+
37783820
proc external_diff {} {
37793821
global nullid nullid2
37803822
global flist_menu_file
@@ -3805,8 +3847,16 @@ proc external_diff {} {
38053847
if {$diffdir eq {}} return
38063848
38073849
# 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]
3850+
set renamed_filenames [check_for_renames_in_diff $diffidfrom $diffidto $flist_menu_file]
3851+
set rename_from_filename [lindex $renamed_filenames 1]
3852+
set rename_to_filename [lindex $renamed_filenames 2]
3853+
if { ($rename_from_filename != {}) && ($rename_to_filename != {}) } {
3854+
set difffromfile [external_diff_get_one_file $diffidfrom $rename_from_filename $diffdir]
3855+
set difftofile [external_diff_get_one_file $diffidto $rename_to_filename $diffdir]
3856+
} else {
3857+
set difffromfile [external_diff_get_one_file $diffidfrom $flist_menu_file $diffdir]
3858+
set difftofile [external_diff_get_one_file $diffidto $flist_menu_file $diffdir]
3859+
}
38103860
38113861
if {$difffromfile ne {} && $difftofile ne {}} {
38123862
set cmd [list [shellsplit $extdifftool] $difffromfile $difftofile]

0 commit comments

Comments
 (0)