Skip to content

Commit 7c09a28

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 57da342 commit 7c09a28

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

gitk-git/gitk

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3788,6 +3788,34 @@ proc external_diff_get_one_file {diffid filename diffdir} {
37883788
"revision $diffid"]
37893789
}
37903790
3791+
proc check_for_renames_in_diff {filepath} { # renames
3792+
global difffilestart ctext
3793+
3794+
set filename [file tail $filepath]
3795+
set renames {}
3796+
3797+
foreach loc $difffilestart {
3798+
set loclineend [string map {.0 .end} $loc]
3799+
set ctextline [$ctext get $loc $loclineend]
3800+
if {[string first $filename $ctextline] != -1} {
3801+
set fromlineloc "$loc + 2 lines"
3802+
set tolineloc "$loc + 3 lines"
3803+
set renfromline [$ctext get $fromlineloc [string map {.0 .end} $fromlineloc]]
3804+
set rentoline [$ctext get $tolineloc [string map {.0 .end} $tolineloc]]
3805+
if {[string match "rename from *" $renfromline]
3806+
&& [string match "rename to *" $rentoline]} {
3807+
set renfrom [string range $renfromline 12 end]
3808+
set rento [string range $rentoline 10 end]
3809+
lappend renames $renfrom
3810+
lappend renames $rento
3811+
break
3812+
}
3813+
}
3814+
}
3815+
3816+
return $renames
3817+
}
3818+
37913819
proc external_diff {} {
37923820
global nullid nullid2
37933821
global flist_menu_file
@@ -3818,8 +3846,16 @@ proc external_diff {} {
38183846
if {$diffdir eq {}} return
38193847
38203848
# gather files to diff
3821-
set difffromfile [external_diff_get_one_file $diffidfrom $flist_menu_file $diffdir]
3822-
set difftofile [external_diff_get_one_file $diffidto $flist_menu_file $diffdir]
3849+
set renames [check_for_renames_in_diff $flist_menu_file]
3850+
set renamefrom [lindex $renames 0]
3851+
set renameto [lindex $renames 1]
3852+
if { ($renamefrom != {}) && ($renameto != {}) } {
3853+
set difffromfile [external_diff_get_one_file $diffidfrom $renamefrom $diffdir]
3854+
set difftofile [external_diff_get_one_file $diffidto $renameto $diffdir]
3855+
} else {
3856+
set difffromfile [external_diff_get_one_file $diffidfrom $flist_menu_file $diffdir]
3857+
set difftofile [external_diff_get_one_file $diffidto $flist_menu_file $diffdir]
3858+
}
38233859
38243860
if {$difffromfile ne {} && $difftofile ne {}} {
38253861
set cmd [list [shellsplit $extdifftool] $difffromfile $difftofile]

0 commit comments

Comments
 (0)