Fix generate_sub_moved_events corrupting paths with unbounded str.replace#1158
Merged
BoboTiG merged 2 commits intogorakhargosh:masterfrom Feb 20, 2026
Merged
Conversation
str.replace() without a count argument replaces ALL occurrences of the destination directory name in the path, not just the leading prefix. If the directory name appears in a subdirectory name within the tree, sub-paths get silently corrupted. For example, moving /tmp/bar (containing /tmp/bar/data/bar/file.txt) to /tmp/foo would produce src_path /tmp/foo/data/foo/file.txt instead of /tmp/foo/data/bar/file.txt. Fix: use string slicing to replace only the prefix.
Collaborator
|
Thanks @bysiber! Can you update the changelog and add a test? |
Add a test that exercises the case where a directory name appears multiple times in the path, verifying only the prefix is replaced. Also add a changelog entry.
Contributor
Author
|
Added a regression test and a changelog entry. The test creates a directory tree where the directory name appears twice in the path (e.g. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
generate_sub_moved_eventsusesstr.replace()to convert destination paths back to source paths when generating subevents for directory moves. Sincestr.replace()without a count argument replaces all occurrences of the substring, this corrupts paths when the directory name being moved also appears elsewhere in the subtree.Example:
Moving
/tmp/bar/(containingdata/bar/file.txt) to/tmp/foo/:Fix: replace only the leading prefix using string slicing instead of
str.replace():This ensures only the prefix is substituted, leaving the rest of the path intact.