Skip to content

Commit 97babc1

Browse files
committed
Merge branch 'tb/external-diff-renamed' into j6t-testing
* tb/external-diff-renamed: gitk: add external diff file rename detection
2 parents ef31b13 + 33a3d9a commit 97babc1

1 file changed

Lines changed: 52 additions & 2 deletions

File tree

gitk

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3795,6 +3795,48 @@ proc external_diff_get_one_file {diffid filename diffdir} {
37953795
"revision $diffid"]
37963796
}
37973797
3798+
proc check_for_renames_in_diff {diffidfrom diffidto filepath} {
3799+
global nullid nullid2
3800+
3801+
if {$diffidfrom eq $nullid} {
3802+
set rev [list $diffidto -R]
3803+
} elseif {$diffidfrom eq $nullid2} {
3804+
set rev [list $diffidto --cached -R]
3805+
} elseif {$diffidto eq $nullid} {
3806+
set rev [list $diffidfrom]
3807+
} elseif {$diffidto eq $nullid2} {
3808+
set rev [list $diffidfrom --cached]
3809+
} else {
3810+
set rev [list $diffidfrom..$diffidto]
3811+
}
3812+
3813+
set renames [list {}]
3814+
if {[catch {eval exec git diff $rev --find-renames --stat --raw --diff-filter=R} cmd_result]} {
3815+
error_popup "[mc "Error getting file rename info for file \"%s\" from commit %s to %s." \
3816+
$filepath $diffidfrom $diffidto] $cmd_result.\n\n"
3817+
}
3818+
set filename [file tail $filepath]
3819+
set esc_chars {\\ | ? ^ * . $ \[ \] + \( \) \{ \}}
3820+
foreach char $esc_chars {
3821+
set filename [string map [list $char \\$char] $filename]
3822+
}
3823+
set regex_base {\d+\s\d+\s\S+\s\S+\s\S+\s+}
3824+
set regex_ren_from $regex_base[subst -nobackslashes -nocommands {(\S+$filename)\s+(\S+)}]
3825+
set regex_ren_to $regex_base[subst -nobackslashes -nocommands {(\S+)\s+(\S+$filename)}]
3826+
if {[regexp -line -- $regex_ren_from $cmd_result whole_match ren_from ren_to]} {
3827+
if {$ren_from ne {} && $ren_to ne {}} {
3828+
lappend renames $ren_from
3829+
lappend renames $ren_to
3830+
}
3831+
} elseif {[regexp -line -- $regex_ren_to $cmd_result whole_match ren_from ren_to]} {
3832+
if {$ren_from ne {} && $ren_to ne {}} {
3833+
lappend renames $ren_from
3834+
lappend renames $ren_to
3835+
}
3836+
}
3837+
return $renames
3838+
}
3839+
37983840
proc external_diff {} {
37993841
global nullid nullid2
38003842
global flist_menu_file
@@ -3825,8 +3867,16 @@ proc external_diff {} {
38253867
if {$diffdir eq {}} return
38263868
38273869
# gather files to diff
3828-
set difffromfile [external_diff_get_one_file $diffidfrom $flist_menu_file $diffdir]
3829-
set difftofile [external_diff_get_one_file $diffidto $flist_menu_file $diffdir]
3870+
set renamed_filenames [check_for_renames_in_diff $diffidfrom $diffidto $flist_menu_file]
3871+
set rename_from_filename [lindex $renamed_filenames 1]
3872+
set rename_to_filename [lindex $renamed_filenames 2]
3873+
if { ($rename_from_filename != {}) && ($rename_to_filename != {}) } {
3874+
set difffromfile [external_diff_get_one_file $diffidfrom $rename_from_filename $diffdir]
3875+
set difftofile [external_diff_get_one_file $diffidto $rename_to_filename $diffdir]
3876+
} else {
3877+
set difffromfile [external_diff_get_one_file $diffidfrom $flist_menu_file $diffdir]
3878+
set difftofile [external_diff_get_one_file $diffidto $flist_menu_file $diffdir]
3879+
}
38303880
38313881
if {$difffromfile ne {} && $difftofile ne {}} {
38323882
set cmd [list [shellsplit $extdifftool] $difffromfile $difftofile]

0 commit comments

Comments
 (0)