Skip to content

Commit 948b94b

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 <[email protected]>
1 parent 5b97a56 commit 948b94b

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

gitk-git/gitk

+43-2
Original file line numberDiff line numberDiff line change
@@ -3806,6 +3806,39 @@ proc external_diff_get_one_file {diffid filename diffdir} {
38063806
"revision $diffid"]
38073807
}
38083808
3809+
proc check_for_renames_in_diff {filepath} {
3810+
global ctext
3811+
3812+
set renamed_filenames [list {}]
3813+
set filename [file tail $filepath]
3814+
set rename_from_text_identifier_length 12
3815+
set rename_to_text_identifier_length 10
3816+
set reg_expr_rename_from {^rename from (.*$filename)}
3817+
set reg_expr_rename_from [subst -nobackslashes -nocommands $reg_expr_rename_from]
3818+
set rename_from_text_index [$ctext search -elide -regexp -- $reg_expr_rename_from 0.0]
3819+
if { ($rename_from_text_index != {})} {
3820+
set reg_expr_rename_to {^rename to (.*)}
3821+
set rename_to_text_index [$ctext search -elide -regexp -- $reg_expr_rename_to $rename_from_text_index]
3822+
if { ($rename_from_text_index != {}) && ($rename_to_text_index != {}) } {
3823+
lappend renamed_filenames [$ctext get "$rename_from_text_index + $rename_from_text_identifier_length chars" "$rename_from_text_index lineend"]
3824+
lappend renamed_filenames [$ctext get "$rename_to_text_index + $rename_to_text_identifier_length chars" "$rename_to_text_index lineend"]
3825+
}
3826+
return $renamed_filenames
3827+
}
3828+
set reg_expr_rename_to {^rename to (.*$filename)}
3829+
set reg_expr_rename_to [subst -nobackslashes -nocommands $reg_expr_rename_to]
3830+
set rename_to_text_index [$ctext search -elide -regexp -- $reg_expr_rename_to 0.0]
3831+
if { ($rename_to_text_index != {})} {
3832+
set reg_expr_rename_from {^rename from (.*)}
3833+
set rename_from_text_index [$ctext search -backwards -elide -regexp -- $reg_expr_rename_from $rename_to_text_index]
3834+
if { ($rename_to_text_index != {}) && ($rename_from_text_index != {}) } {
3835+
lappend renamed_filenames [$ctext get "$rename_from_text_index + $rename_from_text_identifier_length chars" "$rename_from_text_index lineend"]
3836+
lappend renamed_filenames [$ctext get "$rename_to_text_index + $rename_to_text_identifier_length chars" "$rename_to_text_index lineend"]
3837+
}
3838+
return $renamed_filenames
3839+
}
3840+
}
3841+
38093842
proc external_diff {} {
38103843
global nullid nullid2
38113844
global flist_menu_file
@@ -3836,8 +3869,16 @@ proc external_diff {} {
38363869
if {$diffdir eq {}} return
38373870
38383871
# 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]
3872+
set renamed_filenames [check_for_renames_in_diff $flist_menu_file]
3873+
set rename_from_filename [lindex $renamed_filenames 1]
3874+
set rename_to_filename [lindex $renamed_filenames 2]
3875+
if { ($rename_from_filename != {}) && ($rename_to_filename != {}) } {
3876+
set difffromfile [external_diff_get_one_file $diffidfrom $rename_from_filename $diffdir]
3877+
set difftofile [external_diff_get_one_file $diffidto $rename_to_filename $diffdir]
3878+
} else {
3879+
set difffromfile [external_diff_get_one_file $diffidfrom $flist_menu_file $diffdir]
3880+
set difftofile [external_diff_get_one_file $diffidto $flist_menu_file $diffdir]
3881+
}
38413882
38423883
if {$difffromfile ne {} && $difftofile ne {}} {
38433884
set cmd [list [shellsplit $extdifftool] $difffromfile $difftofile]

0 commit comments

Comments
 (0)