Skip to content

gitk: add external diff file rename detection #1774

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ToBoMi
Copy link

@ToBoMi ToBoMi commented Aug 15, 2024

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

Changes sine v4:

  • Use a git command to gather the changed file paths rather than parsing the text from the diff window panel for efficiency and to avoid regex containing the filename as a variable.
  • Change != to ne in string comparison
  • removed extra set of parentheses around &&
  • shorter variable names

Changes sine v5:

  • Include filename in rename check. Find only the file and its renamed version that is selected in the GUI.
  • Escape special characters in the filename to prevent that they are intepreted as part of a regular expression

cc: Johannes Sixt j6t@kdbg.org
cc: "tobias.boesch@miele.com" tobias.boesch@miele.com

Copy link

gitgitgadget bot commented Aug 15, 2024

Welcome to GitGitGadget

Hi @ToBoMi, and welcome to GitGitGadget, the GitHub App to send patch series to the Git mailing list from GitHub Pull Requests.

Please make sure that either:

  • Your Pull Request has a good description, if it consists of multiple commits, as it will be used as cover letter.
  • Your Pull Request description is empty, if it consists of a single commit, as the commit message should be descriptive enough by itself.

You can CC potential reviewers by adding a footer to the PR description with the following syntax:

CC: Revi Ewer <revi.ewer@example.com>, Ill Takalook <ill.takalook@example.net>

NOTE: DO NOT copy/paste your CC list from a previous GGG PR's description,
because it will result in a malformed CC list on the mailing list. See
example.

Also, it is a good idea to review the commit messages one last time, as the Git project expects them in a quite specific form:

  • the lines should not exceed 76 columns,
  • the first line should be like a header and typically start with a prefix like "tests:" or "revisions:" to state which subsystem the change is about, and
  • the commit messages' body should be describing the "why?" of the change.
  • Finally, the commit messages should end in a Signed-off-by: line matching the commits' author.

It is in general a good idea to await the automated test ("Checks") in this Pull Request before contributing the patches, e.g. to avoid trivial issues such as unportable code.

Contributing the patches

Before you can contribute the patches, your GitHub username needs to be added to the list of permitted users. Any already-permitted user can do that, by adding a comment to your PR of the form /allow. A good way to find other contributors is to locate recent pull requests where someone has been /allowed:

Both the person who commented /allow and the PR author are able to /allow you.

An alternative is the channel #git-devel on the Libera Chat IRC network:

<newcontributor> I've just created my first PR, could someone please /allow me? https://github.com/gitgitgadget/git/pull/12345
<veteran> newcontributor: it is done
<newcontributor> thanks!

Once on the list of permitted usernames, you can contribute the patches to the Git mailing list by adding a PR comment /submit.

If you want to see what email(s) would be sent for a /submit request, add a PR comment /preview to have the email(s) sent to you. You must have a public GitHub email address for this. Note that any reviewers CC'd via the list in the PR description will not actually be sent emails.

After you submit, GitGitGadget will respond with another comment that contains the link to the cover letter mail in the Git mailing list archive. Please make sure to monitor the discussion in that thread and to address comments and suggestions (while the comments and suggestions will be mirrored into the PR by GitGitGadget, you will still want to reply via mail).

If you do not want to subscribe to the Git mailing list just to be able to respond to a mail, you can download the mbox from the Git mailing list archive (click the (raw) link), then import it into your mail program. If you use GMail, you can do this via:

curl -g --user "<EMailAddress>:<Password>" \
    --url "imaps://imap.gmail.com/INBOX" -T /path/to/raw.txt

To iterate on your change, i.e. send a revised patch or patch series, you will first want to (force-)push to the same branch. You probably also want to modify your Pull Request description (or title). It is a good idea to summarize the revision by adding something like this to the cover letter (read: by editing the first comment on the PR, i.e. the PR description):

Changes since v1:
- Fixed a typo in the commit message (found by ...)
- Added a code comment to ... as suggested by ...
...

To send a new iteration, just add another PR comment with the contents: /submit.

Need help?

New contributors who want advice are encouraged to join git-mentoring@googlegroups.com, where volunteers who regularly contribute to Git are willing to answer newbie questions, give advice, or otherwise provide mentoring to interested contributors. You must join in order to post or view messages, but anyone can join.

You may also be able to find help in real time in the developer IRC channel, #git-devel on Libera Chat. Remember that IRC does not support offline messaging, so if you send someone a private message and log out, they cannot respond to you. The scrollback of #git-devel is archived, though.

Copy link

gitgitgadget bot commented Aug 15, 2024

There are issues in commit 37f18d1:
gitk: Added external diff file rename detection
Prefixed commit message must be in lower case
Commit not signed off

@ToBoMi ToBoMi force-pushed the detect_renamed_files_when_opening_diff branch from 37f18d1 to 383d90c Compare August 15, 2024 13:29
Copy link

gitgitgadget bot commented Aug 15, 2024

There are issues in commit 383d90c:
gitk: Added external diff file rename detection
Prefixed commit message must be in lower case

@ToBoMi ToBoMi force-pushed the detect_renamed_files_when_opening_diff branch from 383d90c to 5d4b12e Compare August 15, 2024 13:46
@ToBoMi ToBoMi changed the title gitk: Added external diff file rename detection gitk: added external diff file rename detection Aug 15, 2024
@ToBoMi ToBoMi force-pushed the detect_renamed_files_when_opening_diff branch 2 times, most recently from 383d90c to 5d4b12e Compare August 16, 2024 08:57
Copy link

gitgitgadget bot commented Aug 16, 2024

Error: User ToBoMi is not yet permitted to use GitGitGadget

@dscho
Copy link
Member

dscho commented Aug 16, 2024

/allow

@dscho
Copy link
Member

dscho commented Aug 16, 2024

/allow

Just for the record (this was already accomplished in #1773).

Copy link

gitgitgadget bot commented Aug 16, 2024

User ToBoMi already allowed to use GitGitGadget.

Copy link

gitgitgadget bot commented Aug 16, 2024

Error: Could not determine full name of ToBoMi

@ToBoMi
Copy link
Author

ToBoMi commented Aug 16, 2024

/preview

Copy link

gitgitgadget bot commented Aug 16, 2024

Preview email sent as pull.1774.git.1723806010304.gitgitgadget@gmail.com

@ToBoMi ToBoMi force-pushed the detect_renamed_files_when_opening_diff branch from 5d4b12e to 829b37b Compare August 16, 2024 12:02
@ToBoMi
Copy link
Author

ToBoMi commented Aug 16, 2024

/preview

Copy link

gitgitgadget bot commented Aug 16, 2024

Preview email sent as pull.1774.git.1723816893059.gitgitgadget@gmail.com

@ToBoMi ToBoMi force-pushed the detect_renamed_files_when_opening_diff branch from 829b37b to 4ff4aec Compare August 19, 2024 08:42
@ToBoMi
Copy link
Author

ToBoMi commented Aug 22, 2024

/submit

Copy link

gitgitgadget bot commented Aug 22, 2024

Submitted as pull.1774.git.1724318874608.gitgitgadget@gmail.com

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-1774/ToBoMi/detect_renamed_files_when_opening_diff-v1

To fetch this version to local tag pr-1774/ToBoMi/detect_renamed_files_when_opening_diff-v1:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-1774/ToBoMi/detect_renamed_files_when_opening_diff-v1

@ToBoMi ToBoMi force-pushed the detect_renamed_files_when_opening_diff branch from 4ff4aec to a4b9c33 Compare August 27, 2024 14:01
@ToBoMi ToBoMi force-pushed the detect_renamed_files_when_opening_diff branch from a4b9c33 to 6209080 Compare September 6, 2024 06:47
@ToBoMi
Copy link
Author

ToBoMi commented Sep 6, 2024

/submit

Copy link

gitgitgadget bot commented Sep 6, 2024

Submitted as pull.1774.v2.git.1725607698680.gitgitgadget@gmail.com

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-1774/ToBoMi/detect_renamed_files_when_opening_diff-v2

To fetch this version to local tag pr-1774/ToBoMi/detect_renamed_files_when_opening_diff-v2:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-1774/ToBoMi/detect_renamed_files_when_opening_diff-v2

@ToBoMi ToBoMi force-pushed the detect_renamed_files_when_opening_diff branch from 6209080 to c585fd5 Compare February 11, 2025 14:50
@ToBoMi
Copy link
Author

ToBoMi commented Feb 11, 2025

/preview

Copy link

gitgitgadget bot commented Feb 11, 2025

Preview email sent as pull.1774.v3.git.1739286914838.gitgitgadget@gmail.com

@ToBoMi ToBoMi force-pushed the detect_renamed_files_when_opening_diff branch from c585fd5 to c4db1e5 Compare March 4, 2025 10:07
@ToBoMi
Copy link
Author

ToBoMi commented Mar 4, 2025

/preview

Copy link

gitgitgadget bot commented Mar 4, 2025

Preview email sent as pull.1774.v3.git.1741092841997.gitgitgadget@gmail.com

@ToBoMi
Copy link
Author

ToBoMi commented Mar 4, 2025

/submit

Copy link

gitgitgadget bot commented Mar 4, 2025

Submitted as pull.1774.v3.git.1741093275742.gitgitgadget@gmail.com

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-1774/ToBoMi/detect_renamed_files_when_opening_diff-v3

To fetch this version to local tag pr-1774/ToBoMi/detect_renamed_files_when_opening_diff-v3:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-1774/ToBoMi/detect_renamed_files_when_opening_diff-v3

Copy link

gitgitgadget bot commented Mar 16, 2025

On the Git mailing list, Johannes Sixt wrote (reply to this):

Am 04.03.25 um 14:01 schrieb ToBoMi via GitGitGadget:
> From: Tobias Boesch <tobias.boesch@miele.com>
> 
> * 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.
> 
> Signed-off-by: Tobias Boesch <tobias.boesch@miele.com>
> ---

I've picked up this patch, but haven't found time to look at it in
detail. It will take some time. Please bear with me.

The commit message would need to be rewritten to match our usual style:

- We do not use bullet points for normal text paragraphs.
- We describe the status quo in present tense,
- and then the changes in imperative mood. (Like, "code, become so!")
- The subject is not in past tense, but usually also imperative.

-- Hannes

Copy link

gitgitgadget bot commented Mar 16, 2025

User Johannes Sixt <j6t@kdbg.org> has been added to the cc: list.

@ToBoMi ToBoMi force-pushed the detect_renamed_files_when_opening_diff branch from 1a64e98 to 948b94b Compare March 31, 2025 10:07
@ToBoMi
Copy link
Author

ToBoMi commented Mar 31, 2025

/preview

Copy link

gitgitgadget bot commented Mar 31, 2025

Preview email sent as pull.1774.v4.git.1743415804106.gitgitgadget@gmail.com

@ToBoMi ToBoMi changed the title gitk: added external diff file rename detection gitk: add external diff file rename detection Apr 28, 2025
@ToBoMi
Copy link
Author

ToBoMi commented Apr 28, 2025

/submit

Copy link

gitgitgadget bot commented Apr 28, 2025

Submitted as pull.1774.v4.git.1745830037917.gitgitgadget@gmail.com

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-1774/ToBoMi/detect_renamed_files_when_opening_diff-v4

To fetch this version to local tag pr-1774/ToBoMi/detect_renamed_files_when_opening_diff-v4:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-1774/ToBoMi/detect_renamed_files_when_opening_diff-v4

Copy link

gitgitgadget bot commented Apr 28, 2025

On the Git mailing list, "tobias.boesch@miele.com" wrote (reply to this):

Thanks for the update.
Take the time you need.
Changes to the message are following.

> -----Ursprüngliche Nachricht-----
> Von: Johannes Sixt <j6t@kdbg.org>
> Gesendet: Sonntag, 16. März 2025 17:22
> An: Boesch, Tobias <tobias.boesch@miele.com>
> Cc: git@vger.kernel.org; ToBoMi via GitGitGadget <gitgitgadget@gmail.com>
> Betreff: Re: [PATCH v3] gitk: added external diff file rename detection
>
> Am 04.03.25 um 14:01 schrieb ToBoMi via GitGitGadget:
> > From: Tobias Boesch <tobias.boesch@miele.com>
> >
> > * 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.
> >
> > Signed-off-by: Tobias Boesch <tobias.boesch@miele.com>
> > ---
>
> I've picked up this patch, but haven't found time to look at it in detail. It will
> take some time. Please bear with me.
>
> The commit message would need to be rewritten to match our usual style:
>
> - We do not use bullet points for normal text paragraphs.
> - We describe the status quo in present tense,
> - and then the changes in imperative mood. (Like, "code, become so!")
> - The subject is not in past tense, but usually also imperative.
>
> -- Hannes



-------------------------------------------------------------------------------------------------
imperial-Werke oHG, Sitz Bünde, Registergericht Bad Oeynhausen - HRA 4825

Copy link

gitgitgadget bot commented Apr 28, 2025

User "tobias.boesch@miele.com" <tobias.boesch@miele.com> has been added to the cc: list.

Copy link

gitgitgadget bot commented May 6, 2025

On the Git mailing list, Johannes Sixt wrote (reply to this):

Am 28.04.25 um 10:47 schrieb ToBoMi via GitGitGadget:
> 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>

Thank you. Sorry for taking so long to respond.

In general, I like the goal of this patch.

I am not familar, yet, how renamed files are represented in Gitk.

I wonder whether it is necessary to parse diff text to find renamed file
names. When you click on a renamed file in the file list, the diff panel
jumps to the corresponding text for both the original file name and the
renamed file name. Is the information about those two names not already
available?

Would it make sense to support also copied files?

>  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)}

$filename can certainly have characters that are special for a regular
expression, such as the fullstop, right? They need to be escaped or this
will find the wrong file if one at all.

If this search wants to find one side of the rename, why does it ignore
the directories?

> +    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 != {})} {

Here and elsewhere in this patch we have a string comparison that uses
'!='. It should use 'ne'.

Please avoid the extra set of parentheses, even around && (below). Also,
in this code base, we do not have spaces around the condition inside {}.

> +        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
> +    }
> +}
> +

Can we please have shorter variable names? They are all local variables.
I have to spend so mucht time to find the end of the variable names
before I can understand what the lines do...

>  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

-- Hannes

@ToBoMi ToBoMi force-pushed the detect_renamed_files_when_opening_diff branch from 948b94b to ad4121b Compare June 6, 2025 11:56
@ToBoMi
Copy link
Author

ToBoMi commented Jun 6, 2025

/preview

Copy link

gitgitgadget bot commented Jun 6, 2025

Preview email sent as pull.1774.v5.git.1749211339281.gitgitgadget@gmail.com

@ToBoMi ToBoMi force-pushed the detect_renamed_files_when_opening_diff branch from ad4121b to 0d28f18 Compare June 10, 2025 07:40
@ToBoMi
Copy link
Author

ToBoMi commented Jun 10, 2025

/submit

Copy link

gitgitgadget bot commented Jun 10, 2025

Submitted as pull.1774.v5.git.1749544174590.gitgitgadget@gmail.com

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-1774/ToBoMi/detect_renamed_files_when_opening_diff-v5

To fetch this version to local tag pr-1774/ToBoMi/detect_renamed_files_when_opening_diff-v5:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-1774/ToBoMi/detect_renamed_files_when_opening_diff-v5

Copy link

gitgitgadget bot commented Jun 10, 2025

On the Git mailing list, "tobias.boesch@miele.com" wrote (reply to this):

> -----Ursprüngliche Nachricht-----
> Von: Johannes Sixt <j6t@kdbg.org>
> Gesendet: Dienstag, 6. Mai 2025 21:39
> An: ToBoMi via GitGitGadget <gitgitgadget@gmail.com>; git@vger.kernel.org
> Cc: Boesch, Tobias <tobias.boesch@miele.com>
> Betreff: Re: [PATCH v4] gitk: add external diff file rename detection
>
> Am 28.04.25 um 10:47 schrieb ToBoMi via GitGitGadget:
> > 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>
>
> Thank you. Sorry for taking so long to respond.
>
> In general, I like the goal of this patch.
>
> I am not familar, yet, how renamed files are represented in Gitk.
>
> I wonder whether it is necessary to parse diff text to find renamed file names.
> When you click on a renamed file in the file list, the diff panel jumps to the
> corresponding text for both the original file name and the renamed file name.
> Is the information about those two names not already available?

I believe this could go wrong, because I think one can scroll after selecting
the file and then right click on it or even another file to execute the external diff.
Then the filenames are no longer at the top top of the text field.

I got around this by finding a git command that delivers the full paths of the original
and the renamed file. Since git commands are executed in the codebase already I
hope this is okay and maybe this is even more efficient than parsing the ctext.

>
> Would it make sense to support also copied files?

I don't know how why this is a benefit and I believe that git currently does not handle
copied files the way it does with renamed or moved files. That's why I would like to skip this.

>
> >  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)}
>
> $filename can certainly have characters that are special for a regular
> expression, such as the fullstop, right? They need to be escaped or this will
> find the wrong file if one at all.
>
> If this search wants to find one side of the rename, why does it ignore the
> directories?

True. Now that a git command is used that emits the full path of the files
This regex is no longer necessary and is removed.

>
> > +    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 != {})} {
>
> Here and elsewhere in this patch we have a string comparison that uses '!='. It
> should use 'ne'.

done

>
> Please avoid the extra set of parentheses, even around && (below). Also, in
> this code base, we do not have spaces around the condition inside {}.

done

>
> > +        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
> > +    }
> > +}
> > +
>
> Can we please have shorter variable names? They are all local variables.
> I have to spend so mucht time to find the end of the variable names before I
> can understand what the lines do...

done

>
> >  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
>
> -- Hannes



-------------------------------------------------------------------------------------------------
imperial-Werke oHG, Sitz Bünde, Registergericht Bad Oeynhausen - HRA 4825

Copy link

gitgitgadget bot commented Jun 13, 2025

On the Git mailing list, "tobias.boesch@miele.com" wrote (reply to this):

Hi Hannes,

please ignore the V5 patch.
I totally missed to check for the file of interest when checking for renames.
I'll correct that and send an update.

Best wishes
Tobias

> -----Ursprüngliche Nachricht-----
> Von: ToBoMi via GitGitGadget <gitgitgadget@gmail.com>
> Gesendet: Dienstag, 10. Juni 2025 10:30
> An: git@vger.kernel.org
> Cc: Johannes Sixt <j6t@kdbg.org>; Boesch, Tobias
> <tobias.boesch@miele.com>; Boesch, Tobias <tobias.boesch@miele.com>;
> Boesch, Tobias <tobias.boesch@miele.com>
> Betreff: [PATCH v5] gitk: add external diff file rename detection
>
> 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
>
>     Changes sine v4:
>
>      * Use a git command to gather the changed file paths rather than
>        parsing the text from the diff window panel for efficiency and to
>        avoid regex containing the filename as a variable.
>      * Change != to ne in string comparison
>      * removed extra set of parentheses around &&
>      * shorter variable names
>
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-
> 1774%2FToBoMi%2Fdetect_renamed_files_when_opening_diff-v5
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-
> 1774/ToBoMi/detect_renamed_files_when_opening_diff-v5
> Pull-Request: https://github.com/gitgitgadget/git/pull/1774
>
> Range-diff vs v4:
>
>  1:  948b94bef5c ! 1:  0d28f189dc3 gitk: add external diff file rename
> detection
>      @@ gitk-git/gitk: proc external_diff_get_one_file {diffid filename diffdir} {
>                       "revision $diffid"]
>        }
>
>      -+proc check_for_renames_in_diff {filepath} {
>      -+    global ctext
>      ++proc check_for_renames_in_diff {diffidfrom diffidto filepath} {
>      ++    global nullid nullid2
>       +
>      -+    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
>      ++    if {$diffidfrom eq $nullid} {
>      ++        set rev [list $diffidto -R]
>      ++    } elseif {$diffidfrom eq $nullid2} {
>      ++        set rev [list $diffidto --cached -R]
>      ++    } elseif {$diffidto eq $nullid} {
>      ++        set rev [list $diffidfrom]
>      ++    } elseif {$diffidto eq $nullid2} {
>      ++        set rev [list $diffidfrom --cached]
>      ++    } else {
>      ++        set rev [list $diffidfrom..$diffidto]
>       +    }
>      -+    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"]
>      ++
>      ++    set renames [list {}]
>      ++    if {[catch {eval exec git diff $rev --find-renames --stat --raw --diff-
> filter=R} cmd_result]} {
>      ++        error_popup "[mc "Error getting file rename info for file \"%s\" from
> commit %s to %s." \
>      ++                            $filepath $diffidfrom $diffidto] $cmd_result.\n\n"
>      ++    }
>      ++    set filename [file tail $filepath]
>      ++    set regex_ren {\d+\s\d+\s\S+\s\S+\s\S+\s+(\S+)\s+(\S+)}
>      ++    set regex_ren [subst -nobackslashes -nocommands $regex_ren]
>      ++    if {[regexp -line -- $regex_ren $cmd_result whole_match ren_from
> ren_to]} {
>      ++        if {$ren_from ne {} && $ren_to ne {}} {
>      ++            lappend renames $ren_from
>      ++            lappend renames $ren_to
>       +        }
>      -+        return $renamed_filenames
>       +    }
>      ++    return $renames
>       +}
>       +
>        proc external_diff {} {
>      @@ gitk-git/gitk: proc external_diff {} {
>            # 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 renamed_filenames [check_for_renames_in_diff $diffidfrom
> $diffidto $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 != {}) } {
>
>
>  gitk-git/gitk | 44 ++++++++++++++++++++++++++++++++++++++++++--
>  1 file changed, 42 insertions(+), 2 deletions(-)
>
> diff --git a/gitk-git/gitk b/gitk-git/gitk index 19689765cde..f97904f5fa2
> 100755
> --- a/gitk-git/gitk
> +++ b/gitk-git/gitk
> @@ -3775,6 +3775,38 @@ proc external_diff_get_one_file {diffid filename
> diffdir} {
>                 "revision $diffid"]
>  }
>
> +proc check_for_renames_in_diff {diffidfrom diffidto filepath} {
> +    global nullid nullid2
> +
> +    if {$diffidfrom eq $nullid} {
> +        set rev [list $diffidto -R]
> +    } elseif {$diffidfrom eq $nullid2} {
> +        set rev [list $diffidto --cached -R]
> +    } elseif {$diffidto eq $nullid} {
> +        set rev [list $diffidfrom]
> +    } elseif {$diffidto eq $nullid2} {
> +        set rev [list $diffidfrom --cached]
> +    } else {
> +        set rev [list $diffidfrom..$diffidto]
> +    }
> +
> +    set renames [list {}]
> +    if {[catch {eval exec git diff $rev --find-renames --stat --raw --diff-filter=R}
> cmd_result]} {
> +        error_popup "[mc "Error getting file rename info for file \"%s\" from
> commit %s to %s." \
> +                            $filepath $diffidfrom $diffidto] $cmd_result.\n\n"
> +    }
> +    set filename [file tail $filepath]
> +    set regex_ren {\d+\s\d+\s\S+\s\S+\s\S+\s+(\S+)\s+(\S+)}
> +    set regex_ren [subst -nobackslashes -nocommands $regex_ren]
> +    if {[regexp -line -- $regex_ren $cmd_result whole_match ren_from ren_to]}
> {
> +        if {$ren_from ne {} && $ren_to ne {}} {
> +            lappend renames $ren_from
> +            lappend renames $ren_to
> +        }
> +    }
> +    return $renames
> +}
> +
>  proc external_diff {} {
>      global nullid nullid2
>      global flist_menu_file
> @@ -3805,8 +3837,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 $diffidfrom $diffidto
> $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: 14de3eb34435db79c6e7edc8082c302a26a8330a
> --
> gitgitgadget


-------------------------------------------------------------------------------------------------
imperial-Werke oHG, Sitz Bünde, Registergericht Bad Oeynhausen - HRA 4825

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>
@ToBoMi ToBoMi force-pushed the detect_renamed_files_when_opening_diff branch from 0d28f18 to f1c71e5 Compare June 24, 2025 07:46
@ToBoMi
Copy link
Author

ToBoMi commented Jun 24, 2025

/preview

Copy link

gitgitgadget bot commented Jun 24, 2025

Preview email sent as pull.1774.v6.git.1750751741977.gitgitgadget@gmail.com

@ToBoMi
Copy link
Author

ToBoMi commented Jun 24, 2025

/submit

Copy link

gitgitgadget bot commented Jun 24, 2025

Submitted as pull.1774.v6.git.1750755954011.gitgitgadget@gmail.com

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-1774/ToBoMi/detect_renamed_files_when_opening_diff-v6

To fetch this version to local tag pr-1774/ToBoMi/detect_renamed_files_when_opening_diff-v6:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-1774/ToBoMi/detect_renamed_files_when_opening_diff-v6

Copy link

gitgitgadget bot commented Jun 25, 2025

On the Git mailing list, Johannes Sixt wrote (reply to this):

Am 24.06.25 um 11:05 schrieb ToBoMi via GitGitGadget:
> 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.

In Git parlance, when we talk about "renamed" files, we always mean
files that have any part of the path name changed (and not just the last
path component). Therefore, this last sentence is redundant.

>     Changes sine v4:
>     
>      * Use a git command to gather the changed file paths rather than
>        parsing the text from the diff window panel for efficiency and to
>        avoid regex containing the filename as a variable.

An earlier round parsed the rename information from the patch text
panel. I argued that it should not be necessary, because the file list
already knows how to scroll the diff text panel to the correct section
and should already what was renamed. It turns out it's not that simple.

But I still think that this information can be leveraged for this new
purpose and that it is not necessary to invoke an external process. At a
minimum, it should be possible to parse off the rename information from
a smaller section of the patch text, because we know where the section
pertaining to the file of interest starts. Look for uses of the variable
'difffilestart'.

I think that the goal of this patch can be achieved easier by parsing
the patch text panel rather than parsing the output of another `git`
command. I'll still comment on the presented solution just in case it
turns out we must invoke `git` anyway.

> +    set renames [list {}]

This constructs a list with one element that is the empty string. I
assume you meant one of these:

    set renames [list]
    set renames {}

> +    if {[catch {eval exec git diff $rev --find-renames --stat --raw --diff-filter=R} cmd_result]} {

A few things I have to note here:

- Don't use porcelain commands, use plumbing commands, i.e., `git
diff-tree`, `git diff-index`, and `git diff-files` instead of `git diff`.
- Place non-option arguments after all options.
- Why use --stat?
- --numstat may be easier to parse than --raw, but see below.

> +        error_popup "[mc "Error getting file rename info for file \"%s\" from commit %s to %s." \
> +                            $filepath $diffidfrom $diffidto] $cmd_result.\n\n"
> +    }
> +    set filename [file tail $filepath]
> +    set esc_chars {\\ | ? ^ * . $ \[ \] + \( \) \{ \}}
> +    foreach char $esc_chars {
> +        set filename [string map [list $char \\$char] $filename]
> +    }
> +    set regex_base {\d+\s\d+\s\S+\s\S+\s\S+\s+}
> +    set regex_ren_from $regex_base[subst -nobackslashes -nocommands {(\S+$filename)\s+(\S+)}]

This regular expression wants to parse the first of the two file names
that are on the output line. But it assumes that the second of the two
names cannot contain spaces: '(\S+)'. This is not a valid assumption.

Note that the output format of --raw is very restricted. In particular,
the file names are separated from the rest not by space of any kind, but
by TAB. A much stricter regular expression can be used. But still, TAB
is a character that is permitted in file names, and the regular
expression could match still match incorrectly. You can use -z to
separate the parts with a zero byte in an unambiguous way and split the
parts without a regular expression.

> +    set regex_ren_to $regex_base[subst -nobackslashes -nocommands {(\S+)\s+(\S+$filename)}]

I don't understand the purpose of 'subst' here. Is this not just

    set regex_ren_to $regex_base{(\S+)\s+(\S+}$filename{)}

> @@ -3805,8 +3847,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 $diffidfrom $diffidto $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 != {}) } {

This expression doesn't follow the pattern that we see elsewhere, e.g.,
in the context below.

Please lose the redundant "_filename" in the variable names. In this
context, it's clear that these are names, not indices or somehthing
else. Also, when you look around, you notice that we normally don't use
the underscore in variable names.

> +        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]

-- Hannes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants