Skip to content

Commit bdb1cf8

Browse files
ToBoMij6t
authored andcommitted
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>
1 parent 4a6cc6a commit bdb1cf8

1 file changed

Lines changed: 38 additions & 2 deletions

File tree

gitk

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3806,6 +3806,34 @@ proc external_diff_get_one_file {diffid filename diffdir} {
38063806
"revision $diffid"]
38073807
}
38083808
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+
38093837
proc external_diff {} {
38103838
global nullid nullid2
38113839
global flist_menu_file
@@ -3836,8 +3864,16 @@ proc external_diff {} {
38363864
if {$diffdir eq {}} return
38373865
38383866
# 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+
}
38413877
38423878
if {$difffromfile ne {} && $difftofile ne {}} {
38433879
set cmd [list [shellsplit $extdifftool] $difffromfile $difftofile]

0 commit comments

Comments
 (0)