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 <tobias.boesch@miele.com> Signed-off-by: Johannes Sixt <j6t@kdbg.org>

Tobias Boesch committed Nov 6, 2025 at 14:42 UTC bdb1cf831251b16d174f742178caac181add87f4
1 file changed +38 -2
gitk
+38 -2
@@ -3806,6 +3806,34 @@ proc external_diff_get_one_file {diffid filename diffdir} {
3806 "revision $diffid"]
3807 }
3808
3809 +proc check_for_renames_in_diff {filepath} { # renames
3810 + global difffilestart ctext
3811 +
3812 + set filename [file tail $filepath]
3813 + set renames {}
3814 +
3815 + foreach loc $difffilestart {
3816 + set loclineend [string map {.0 .end} $loc]
3817 + set fromlineloc "$loc + 2 lines"
3818 + set tolineloc "$loc + 3 lines"
3819 + set renfromline [$ctext get $fromlineloc [string map {.0 .end} $fromlineloc]]
3820 + set rentoline [$ctext get $tolineloc [string map {.0 .end} $tolineloc]]
3821 + if {[string equal -length 12 "rename from " $renfromline]
3822 + && [string equal -length 10 "rename to " $rentoline]} {
3823 + set renfrom [string range $renfromline 12 end]
3824 + set rento [string range $rentoline 10 end]
3825 + if {[string first $filename $renfrom] != -1
3826 + || [string first $filename $rento] != -1} {
3827 + lappend renames $renfrom
3828 + lappend renames $rento
3829 + break
3830 + }
3831 + }
3832 + }
3833 +
3834 + return $renames
3835 +}
3836 +
3837 proc external_diff {} {
3838 global nullid nullid2
3839 global flist_menu_file
@@ -3836,8 +3864,16 @@ proc external_diff {} {
3864 if {$diffdir eq {}} return
3865
3866 # gather files to diff
3839 - set difffromfile [external_diff_get_one_file $diffidfrom $flist_menu_file $diffdir]
3840 - set difftofile [external_diff_get_one_file $diffidto $flist_menu_file $diffdir]
3867 + set renames [check_for_renames_in_diff $flist_menu_file]
3868 + set renamefrom [lindex $renames 0]
3869 + set renameto [lindex $renames 1]
3870 + if {$renamefrom ne {} && $renameto ne {}} {
3871 + set difffromfile [external_diff_get_one_file $diffidfrom $renamefrom $diffdir]
3872 + set difftofile [external_diff_get_one_file $diffidto $renameto $diffdir]
3873 + } else {
3874 + set difffromfile [external_diff_get_one_file $diffidfrom $flist_menu_file $diffdir]
3875 + set difftofile [external_diff_get_one_file $diffidto $flist_menu_file $diffdir]
3876 + }
3877
3878 if {$difffromfile ne {} && $difftofile ne {}} {
3879 set cmd [list [shellsplit $extdifftool] $difffromfile $difftofile]