Skip to content

Commit cf22498

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 7e61ebc commit cf22498

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
@@ -3775,6 +3775,34 @@ proc external_diff_get_one_file {diffid filename diffdir} {
37753775
"revision $diffid"]
37763776
}
37773777
3778+
proc check_for_renames_in_diff {filepath} { # renames
3779+
global difffilestart ctext
3780+
3781+
set filename [file tail $filepath]
3782+
set renames {}
3783+
3784+
foreach loc $difffilestart {
3785+
set loc_line_end [string map {.0 .end} $loc]
3786+
set ctext_line [$ctext get $loc $loc_line_end]
3787+
if {[string first $filename $ctext_line] != -1} {
3788+
set from_line_loc "$loc + 2 lines"
3789+
set to_line_loc "$loc + 3 lines"
3790+
set ren_from_line [$ctext get $from_line_loc [string map {.0 .end} $from_line_loc]]
3791+
set ren_to_line [$ctext get $to_line_loc [string map {.0 .end} $to_line_loc]]
3792+
if {[string match "rename from *" $ren_from_line]
3793+
&& [string match "rename to *" $ren_to_line]} {
3794+
set ren_from [string range $ren_from_line 12 end]
3795+
set ren_to [string range $ren_to_line 10 end]
3796+
lappend renames $ren_from
3797+
lappend renames $ren_to
3798+
break
3799+
}
3800+
}
3801+
}
3802+
3803+
return $renames
3804+
}
3805+
37783806
proc external_diff {} {
37793807
global nullid nullid2
37803808
global flist_menu_file
@@ -3805,8 +3833,16 @@ proc external_diff {} {
38053833
if {$diffdir eq {}} return
38063834
38073835
# 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]
3836+
set renames [check_for_renames_in_diff $diffidfrom $diffidto $flist_menu_file]
3837+
set renamefrom [lindex $renames 0]
3838+
set renameto [lindex $renames 1]
3839+
if { ($renamefrom != {}) && ($renameto != {}) } {
3840+
set difffromfile [external_diff_get_one_file $diffidfrom $renamefrom $diffdir]
3841+
set difftofile [external_diff_get_one_file $diffidto $renameto $diffdir]
3842+
} else {
3843+
set difffromfile [external_diff_get_one_file $diffidfrom $flist_menu_file $diffdir]
3844+
set difftofile [external_diff_get_one_file $diffidto $flist_menu_file $diffdir]
3845+
}
38103846
38113847
if {$difffromfile ne {} && $difftofile ne {}} {
38123848
set cmd [list [shellsplit $extdifftool] $difffromfile $difftofile]

0 commit comments

Comments
 (0)