Rename detection fails on symlinked files
commit 5d51b10d8b5206ef5eeb9d214237b2ec2e0b789e (HEAD -> master)
Author: Michal Suchanek <[email protected]> Date: Tue Jun 17 13:08:51 2025 +0200
rename file
somefile-renamed renamed from somefile +0 -0
ln -s somefile-renamed somefile
git add somefile
git commit --amend
commit 377d9bd045aed61c7be55482f3c98f8f9d04a33d (HEAD -> master)
Author: Michal Suchanek <[email protected]> Date: Tue Jun 17 13:08:51 2025 +0200
rename file
somefile deleted binary +0 -0
somefile new file +1 -0
@@ -0,0 +1 @@ +somefile-renamed
somefile-renamed new file binary +0 -0
Can the rename detection be fixed to detect symlinked files as well?
Thanks
Michal
Hi,
On Tue, Jun 17, 2025 at 4:16 AM Michal Suchánek <[email protected]> wrote:
I think your subject might be slightly misleading, and that a more accurate subject might be: Rename detection is not performed for files still present in the target version. Let me explain why and you can check if I'm understanding your problem setup correctly.
somefile-renamed
Quoted 10 lines
commit
5d51b10d8b5206ef5eeb9d214237b2ec2e0b789e(HEAD -> master)Author: Michal Suchanek <[email protected]> Date: Tue Jun 17 13:08:51 2025 +0200rename file
somefile-renamed renamed from somefile +0
-0
So you've renamed a file, detected at the time you run git log -p.
Quoted 3 lines
ln -s somefile-renamed somefile
git add somefile
git commit --amend
Here, you reintroduce the original file, as a symlink, and amend the commit.
somefile-renamed
Quoted 22 lines
commit
377d9bd045aed61c7be55482f3c98f8f9d04a33d(HEAD -> master)Author: Michal Suchanek <[email protected]> Date: Tue Jun 17 13:08:51 2025 +0200rename file
somefile deleted binary +0
-0somefile new file +1
-0@@ -0,0 +1 @@ +somefile-renamedsomefile-renamed new file binary +0
-0
If I'm understanding the behavior that bothers you, it doesn't seem to be related to symlinks. You could create any regular text file unrelated to the original somefile (or even introduce a submodule) and place it in somefile and amend the commit, and you'd see that the rename wasn't detected. For example, replace your ln -s/git add/git commit --amend sequence with
$ echo content >somefile $ git add somefile $ git commit --amend
and you'd see that there was no rename detected from the original somefile to the new somefile-renamed. By default, if the file is present in both the source and the destination, it is not involved in rename detection.
Quoted 1 line
Can the rename detection be fixed to detect symlinked files as well?
Symlink renames can be and are detected, by default. For example:
$ ln -s somefile old-symlink $ git add old-symlink $ git commit -m old $ git mv old-symlink new-symlink $ git commit -m new $ git diff HEAD~1
new-symlink renamed from old-symlink +0 -0
But from your example, you're not renaming a symlink, so instead of "Can rename detection handle symlinked files?", your question really is more of "Can a renamed file be detected even when some other file/link/submodule is immediately placed where the renamed file used to be?"
git assumes, by default, that a file which exists in both the source and destination are "related" and will only look for renames in deleted or added files. Allowing git to mark a file as both a delete and an add even when it's present in both the source and the target is the job of break detection, which is not turned on by default. Further, break detection and rename detection have a bug or two when used together (brought up on the mailing list by Junio some years ago), which might need to be fixed for your example to work as you expect if you try to turn on break detection.
Also, not sure how deep your interest in break detection goes, but merge-ort was written with some implicit assumptions that break detection is _not_ active. Trying to retrofit it to support break detection might take a significant chunk of work; and even if someone is motivated to make it work, it'd defeat the safety of every optimization added to it (making it orders of magnitude slower), and also tack on a significant performance penalty on top of all that (break detection is not cheap when at least one side of the merge has a significant number of files modified).
Anyway...does that help explain what's going on?
Quoted 88 lines · Elijah Newren
Hi,
On Tue, Jun 17, 2025 at 4:16 AM Michal Suchánek <[email protected]> wrote:
I think your subject might be slightly misleading, and that a more accurate subject might be: Rename detection is not performed for files still present in the target version. Let me explain why and you can check if I'm understanding your problem setup correctly.
somefile-renamed
Quoted 10 lines
commit
5d51b10d8b5206ef5eeb9d214237b2ec2e0b789e(HEAD -> master)Author: Michal Suchanek <[email protected]> Date: Tue Jun 17 13:08:51 2025 +0200rename file
somefile-renamed renamed from somefile +0
-0So you've renamed a file, detected at the time you run git log -p.
Quoted 3 lines
ln -s somefile-renamed somefile
git add somefile
git commit --amendHere, you reintroduce the original file, as a symlink, and amend the commit.
somefile-renamed
Quoted 22 lines
commit
377d9bd045aed61c7be55482f3c98f8f9d04a33d(HEAD -> master)Author: Michal Suchanek <[email protected]> Date: Tue Jun 17 13:08:51 2025 +0200rename file
somefile deleted binary +0
-0somefile new file +1
-0@@ -0,0 +1 @@ +somefile-renamedsomefile-renamed new file binary +0
-0If I'm understanding the behavior that bothers you, it doesn't seem to be related to symlinks. You could create any regular text file unrelated to the original somefile (or even introduce a submodule) and place it in somefile and amend the commit, and you'd see that the rename wasn't detected. For example, replace your
ln -s/git add/git commit --amendsequence with$ echo content >somefile $ git add somefile $ git commit --amendand you'd see that there was no rename detected from the original somefile to the new somefile-renamed. By default, if the file is present in both the source and the destination, it is not involved in rename detection.
Quoted 1 line
Can the rename detection be fixed to detect symlinked files as well?
Symlink renames can be and are detected, by default. For example:
$ ln -s somefile old-symlink $ git add old-symlink $ git commit -m old $ git mv old-symlink new-symlink $ git commit -m new $ git diff HEAD~1new-symlink renamed from old-symlink +0
-0But from your example, you're not renaming a symlink, so instead of "Can rename detection handle symlinked files?", your question really is more of "Can a renamed file be detected even when some other file/link/submodule is immediately placed where the renamed file used to be?"
Quoted 9 lines
git assumes, by default, that a file which exists in both the source and destination are "related" and will only look for renames in deleted or added files. Allowing git to mark a file as both a delete and an add even when it's present in both the source and the target is the job of break detection, which is not turned on by default. Further, break detection and rename detection have a bug or two when used together (brought up on the mailing list by Junio some years ago), which might need to be fixed for your example to work as you expect if you try to turn on break detection.
There is indeed something fishy going on:
git show -B100 (or any value of B, really)
commit 377d9bd045aed61c7be55482f3c98f8f9d04a33d (HEAD -> master)
Author: Michal Suchanek <[email protected]> Date: Tue Jun 17 13:08:51 2025 +0200
rename file
somefile deleted binary +0 -0
somefile new file +1 -0
@@ -0,0 +1 @@ +somefile-renamed
somefile-renamed renamed from somefile +0 -0
Quoted 9 lines
Also, not sure how deep your interest in break detection goes, but merge-ort was written with some implicit assumptions that break detection is _not_ active. Trying to retrofit it to support break detection might take a significant chunk of work; and even if someone is motivated to make it work, it'd defeat the safety of every optimization added to it (making it orders of magnitude slower), and also tack on a significant performance penalty on top of all that (break detection is not cheap when at least one side of the merge has a significant number of files modified).
Does merge-ort at least refuse to run with break detection enabled?
This is mainly for examining changes, following a file trough history, and such. When it comes to that there are other merge strategies besides ort so it may not be completely hopeless trying to merge across renames, too.
Thanks
Michal
Quoted 22 lines · Elijah Newren
Hi,
On Tue, Jun 17, 2025 at 4:16 AM Michal Suchánek <[email protected]> wrote:
I think your subject might be slightly misleading, and that a more accurate subject might be: Rename detection is not performed for files still present in the target version. Let me explain why and you can check if I'm understanding your problem setup correctly.
somefile-renamed
Quoted 10 lines
commit
5d51b10d8b5206ef5eeb9d214237b2ec2e0b789e(HEAD -> master)Author: Michal Suchanek <[email protected]> Date: Tue Jun 17 13:08:51 2025 +0200rename file
somefile-renamed renamed from somefile +0
-0So you've renamed a file, detected at the time you run git log -p.
Quoted 5 lines
Quoted 3 lines
ln -s somefile-renamed somefile
git add somefile
git commit --amendHere, you reintroduce the original file, as a symlink, and amend the commit.
No, there is no original file:
some file deleted +0 -1
@@ -1 +0,0 @@ -some text
some file new file +1 -0
@@ -0,0 +1 @@ +some text
See, the plain file and symlink is so different that changing the mode to symlink is represented as removing a file, and adding a symlink. Not same file mode change, not even a rename. The symlink is so different from the file that it's completely unrelated, even with exactly same content. Mode change to/from symlink always breaks, regardless of content and the break rewrites setting.
some file deleted +0 -1
@@ -1 +0,0 @@ -some text
some file new file +1 -0
@@ -0,0 +1 @@ +some text
some other file new file +1 -0
@@ -0,0 +1 @@ +some text
And here we now have one removal and two additions of the same content, no rename detected.
So git cannot agree with itself if symlink and plain file is actually the same file or not. They are presented as comletely unrelated to the user yet rename detection fails to detect the rename of the plain file that is completely unrelated to the added symlink. That is the discrepancy, and the bug.
Thanks
Michal