git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v4] gitk: add external diff file rename detection

From
TGToBoMi via GitGitGadget <gitgitgadget@gmail.com>
Date
Apr 28, 2025, 08:47 UTC
Message-ID
<pull.1774.v4.git.1745830037917.gitgitgadget@gmail.com>
In-Reply-To
<pull.1774.v3.git.1741093275742.gitgitgadget@gmail.com>
From: Tobias Boesch <tobias.boesch@miele.com>

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 <tobias.boesch@miele.com>
---
    gitk: add external diff file rename detection
    
    Changes since v1:
    
     * Commit message ident
     * Commit message line length
    
    Changes since v2:
    
     * Removed option for rename detection (Adding GUI options seems to be
       not desired - which is understandable)
     * Rebased on current master of git-for-windows
     * Renamed variables for a better understanding
     * Made rename detection also work when the renamed file is selected in
       gitk
    
    Changes since v3:
    
     * Changed message to use present tense, removed bullet points and
       described changes in imperative mood
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1774%2FToBoMi%2Fdetect_renamed_files_when_opening_diff-v4
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1774/ToBoMi/detect_renamed_files_when_opening_diff-v4
Pull-Request: https://github.com/gitgitgadget/git/pull/1774
Range-diff vs v3:
 1:  1a64e989713 ! 1:  948b94bef5c gitk: added external diff file rename detection
     @@ Metadata
      Author: Tobias Boesch <tobias.boesch@miele.com>
      
       ## Commit message ##
     -    gitk: added external diff file rename detection
     +    gitk: add external diff file rename detection
      
     -    * If a file was renamed between commits and an external diff is started
     -      through gitk on the original or the renamed file name,
     -      gitk was unable to open the renamed file in the external diff editor.
     -      It failed to fetch the renamed file from git, because it fetched it
     -      using its original path in contrast to using the renamed path of the
     -      file.
     -    * With this change gitk detects the rename and opens the external diff
     -      with the original and the renamed file instead of no file (it is able
     -      to fetch the renamed file path and name now from git).
     -    * Since git doesn't destinguish between move or rename this also works
     -      for moved files.
     -    * Showing the external diff with the original and the renamed file
     -      works when either of the files is selected in gitk.
     +    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 <tobias.boesch@miele.com>
      
 gitk-git/gitk | 45 +++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 43 insertions(+), 2 deletions(-)
diff --git a/gitk-git/gitk b/gitk-git/gitk
index bc9efa18566..ddbe60398f2 100755
--- a/gitk-git/gitk
+++ b/gitk-git/gitk
@@ -3806,6 +3806,39 @@ proc external_diff_get_one_file {diffid filename diffdir} {
                "revision $diffid"]
 }
 
+proc check_for_renames_in_diff {filepath} {
+    global ctext
+
+    set renamed_filenames [list {}]
+    set filename [file tail $filepath]
+    set rename_from_text_identifier_length 12
+    set rename_to_text_identifier_length 10
+    set reg_expr_rename_from {^rename from (.*$filename)}
+    set reg_expr_rename_from [subst -nobackslashes -nocommands $reg_expr_rename_from]
+    set rename_from_text_index [$ctext search -elide -regexp -- $reg_expr_rename_from 0.0]
+    if { ($rename_from_text_index != {})} {
+        set reg_expr_rename_to {^rename to (.*)}
+        set rename_to_text_index [$ctext search -elide -regexp -- $reg_expr_rename_to $rename_from_text_index]
+        if { ($rename_from_text_index != {}) && ($rename_to_text_index != {}) } {
+            lappend renamed_filenames [$ctext get "$rename_from_text_index + $rename_from_text_identifier_length chars" "$rename_from_text_index lineend"]
+            lappend renamed_filenames [$ctext get "$rename_to_text_index + $rename_to_text_identifier_length chars" "$rename_to_text_index lineend"]
+        }
+        return $renamed_filenames
+    }
+    set reg_expr_rename_to {^rename to (.*$filename)}
+    set reg_expr_rename_to [subst -nobackslashes -nocommands $reg_expr_rename_to]
+    set rename_to_text_index [$ctext search -elide -regexp -- $reg_expr_rename_to 0.0]
+    if { ($rename_to_text_index != {})} {
+        set reg_expr_rename_from {^rename from (.*)}
+        set rename_from_text_index [$ctext search -backwards -elide -regexp -- $reg_expr_rename_from $rename_to_text_index]
+        if { ($rename_to_text_index != {}) && ($rename_from_text_index != {}) } {
+            lappend renamed_filenames [$ctext get "$rename_from_text_index + $rename_from_text_identifier_length chars" "$rename_from_text_index lineend"]
+            lappend renamed_filenames [$ctext get "$rename_to_text_index + $rename_to_text_identifier_length chars" "$rename_to_text_index lineend"]
+        }
+        return $renamed_filenames
+    }
+}
+
 proc external_diff {} {
     global nullid nullid2
     global flist_menu_file
@@ -3836,8 +3869,16 @@ proc external_diff {} {
     if {$diffdir eq {}} return
 
     # gather files to diff
-    set difffromfile [external_diff_get_one_file $diffidfrom $flist_menu_file $diffdir]
-    set difftofile [external_diff_get_one_file $diffidto $flist_menu_file $diffdir]
+    set renamed_filenames [check_for_renames_in_diff $flist_menu_file]
+    set rename_from_filename [lindex $renamed_filenames 1]
+    set rename_to_filename [lindex $renamed_filenames 2]
+    if { ($rename_from_filename != {}) && ($rename_to_filename != {}) } {
+        set difffromfile [external_diff_get_one_file $diffidfrom $rename_from_filename $diffdir]
+        set difftofile [external_diff_get_one_file $diffidto $rename_to_filename $diffdir]
+    } else {
+        set difffromfile [external_diff_get_one_file $diffidfrom $flist_menu_file $diffdir]
+        set difftofile [external_diff_get_one_file $diffidto $flist_menu_file $diffdir]
+    }
 
     if {$difffromfile ne {} && $difftofile ne {}} {
         set cmd [list [shellsplit $extdifftool] $difffromfile $difftofile]

base-commit: 5b97a56fa0e7d580dc8865b73107407c9b3f0eff
-- 
gitgitgadget
Previous: tobias.boesch@miele.comNext: Johannes Sixt
Message 7 of 17 in “gitk: added external diff file rename detection”
  1. gitk: added external diff file rename detectionToBoMi via GitGitGadget, Aug 22, 2024
  2. gitk: added external diff file rename detectionToBoMi via GitGitGadget, Sep 6, 2024
  3. AW: [PATCH v2] gitk: added external diff file rename detectiontobias.boesch@miele.com, Oct 2, 2024
  4. gitk: added external diff file rename detectionToBoMi via GitGitGadget, Mar 4, 2025
  5. Johannes SixtMar 16, 2025
  6. AW: [PATCH v3] gitk: added external diff file rename detectiontobias.boesch@miele.com, Apr 28, 2025
  7. gitk: add external diff file rename detectionToBoMi via GitGitGadget, Apr 28, 2025
  8. Johannes SixtMay 6, 2025
  9. AW: [PATCH v4] gitk: add external diff file rename detectiontobias.boesch@miele.com, Jun 10, 2025
  10. gitk: add external diff file rename detectionToBoMi via GitGitGadget, Jun 10, 2025
  11. AW: [PATCH v5] gitk: add external diff file rename detectiontobias.boesch@miele.com, Jun 13, 2025
  12. gitk: add external diff file rename detectionToBoMi via GitGitGadget, Jun 24, 2025
  13. Johannes SixtJun 25, 2025
  14. gitk: add external diff file rename detectionToBoMi via GitGitGadget, Oct 31, 2025
  15. Johannes SixtNov 4, 2025
  16. gitk: add external diff file rename detectionToBoMi via GitGitGadget, Nov 6, 2025
  17. Johannes SixtNov 6, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.