-
-
Notifications
You must be signed in to change notification settings - Fork 282
fix: Add git worktree support for Docker environments #6456
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
philkuz
wants to merge
4
commits into
oxsecurity:main
Choose a base branch
from
philkuz:fix/worktree-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+219
−2
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a76b19a
fix: Add git worktree support for Docker environments
philkuz 1fdf74f
Merge branch 'main' into fix/worktree-support
nvuillam 4d5935a
Merge branch 'main' into fix/worktree-support
nvuillam 0a822f0
Merge branch 'main' into fix/worktree-support
echoix File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| #!/usr/bin/env python3 | ||
| """ | ||
| Unit tests for Git worktree handling in MegaLinter | ||
|
|
||
| """ | ||
| import os | ||
| import tempfile | ||
| import unittest | ||
| from unittest.mock import MagicMock, Mock, patch | ||
|
|
||
| import git | ||
|
|
||
| from megalinter import Megalinter | ||
|
|
||
|
|
||
| class worktree_test(unittest.TestCase): | ||
| """Test Git worktree detection and error handling""" | ||
|
|
||
| def test_is_git_worktree_detection_for_regular_repo(self): | ||
| """Test that a regular repository is NOT detected as a worktree""" | ||
| # Create a mock repo that represents a regular repository | ||
| mock_repo = Mock() | ||
| mock_repo.git_dir = "/path/to/repo/.git" | ||
| mock_repo.working_dir = "/path/to/repo" | ||
|
|
||
| # Create a temporary directory to simulate a regular repo | ||
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| git_dir = os.path.join(tmpdir, ".git") | ||
| os.makedirs(git_dir) | ||
|
|
||
| mock_repo.working_dir = tmpdir | ||
| mock_repo.git_dir = git_dir | ||
|
|
||
| # Create MegaLinter instance | ||
| megalinter = Megalinter({"workspace": tmpdir, "cli": False}) | ||
|
|
||
| # Test worktree detection | ||
| is_worktree = megalinter._is_git_worktree(mock_repo) | ||
|
|
||
| self.assertFalse( | ||
| is_worktree, | ||
| "Regular repository should NOT be detected as a worktree" | ||
| ) | ||
|
|
||
| def test_is_git_worktree_detection_for_worktree_file(self): | ||
| """Test that a worktree (with .git as a file) IS detected""" | ||
| # Create a mock repo that represents a worktree | ||
| mock_repo = Mock() | ||
| mock_repo.git_dir = "/path/to/repo/.git/worktrees/my-worktree" | ||
|
|
||
| # Create a temporary directory to simulate a worktree | ||
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| # Create .git as a FILE (worktree indicator) | ||
| git_file = os.path.join(tmpdir, ".git") | ||
| with open(git_file, "w") as f: | ||
| f.write("gitdir: /path/to/repo/.git/worktrees/my-worktree\n") | ||
|
|
||
| mock_repo.working_dir = tmpdir | ||
|
|
||
| # Create MegaLinter instance | ||
| megalinter = Megalinter({"workspace": tmpdir, "cli": False}) | ||
|
|
||
| # Test worktree detection | ||
| is_worktree = megalinter._is_git_worktree(mock_repo) | ||
|
|
||
| self.assertTrue( | ||
| is_worktree, | ||
| "Worktree (with .git file) should be detected as a worktree" | ||
| ) | ||
|
|
||
| def test_is_git_worktree_detection_by_path(self): | ||
| """Test that a worktree is detected by 'worktrees' in git_dir path""" | ||
| # Create a mock repo with 'worktrees' in the path | ||
| mock_repo = Mock() | ||
| mock_repo.git_dir = "/path/to/repo/.git/worktrees/my-worktree" | ||
| mock_repo.working_dir = "/path/to/worktree" | ||
|
|
||
| # Create a temporary directory | ||
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| mock_repo.working_dir = tmpdir | ||
| git_dir_path = os.path.join(tmpdir, ".git") | ||
| os.makedirs(git_dir_path) | ||
|
|
||
| # Override git_dir to have 'worktrees' in path | ||
| mock_repo.git_dir = "/main/.git/worktrees/test" | ||
|
|
||
| # Create MegaLinter instance | ||
| megalinter = Megalinter({"workspace": tmpdir, "cli": False}) | ||
|
|
||
| # Test worktree detection | ||
| is_worktree = megalinter._is_git_worktree(mock_repo) | ||
|
|
||
| self.assertTrue( | ||
| is_worktree, | ||
| "Worktree should be detected by 'worktrees' in git_dir path" | ||
| ) | ||
|
|
||
| @patch("git.Repo") | ||
| def test_git_fetch_error_handling_in_worktree(self, mock_repo_class): | ||
| """Test that git fetch errors are properly handled in worktrees""" | ||
| # Create a temporary directory | ||
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| # Create .git as a file to simulate worktree | ||
| git_file = os.path.join(tmpdir, ".git") | ||
| with open(git_file, "w") as f: | ||
| f.write("gitdir: /main/.git/worktrees/test\n") | ||
|
|
||
| # Mock the Repo object | ||
| mock_repo_instance = Mock() | ||
| mock_repo_instance.git_dir = "/main/.git/worktrees/test" | ||
| mock_repo_instance.working_dir = tmpdir | ||
| mock_repo_instance.refs = [] | ||
|
|
||
| # Mock git.fetch to raise GitCommandError | ||
| mock_repo_instance.git.fetch.side_effect = git.exc.GitCommandError( | ||
| "git fetch", | ||
| 128, | ||
| stderr="fatal: not a git repository: /host/path/.git/worktrees/test" | ||
| ) | ||
|
|
||
| mock_repo_class.return_value = mock_repo_instance | ||
|
|
||
| # Create MegaLinter instance | ||
| megalinter = Megalinter({"workspace": tmpdir, "cli": False}) | ||
|
|
||
| # Try to list files using git diff - should not raise an exception | ||
| try: | ||
| # This should handle the error gracefully | ||
| files = megalinter.list_files_git_diff() | ||
| # If we get here, the error was handled | ||
| self.assertTrue( | ||
| True, | ||
| "Git fetch error should be caught and handled" | ||
| ) | ||
| except git.exc.GitCommandError: | ||
| self.fail( | ||
| "GitCommandError should be caught and handled, not raised" | ||
| ) | ||
|
|
||
| def test_worktree_detection_handles_exceptions(self): | ||
| """Test that worktree detection handles exceptions gracefully""" | ||
| # Create a mock repo that raises an exception | ||
| mock_repo = Mock() | ||
| mock_repo.git_dir = Mock(side_effect=Exception("Test exception")) | ||
|
|
||
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| # Create MegaLinter instance | ||
| megalinter = Megalinter({"workspace": tmpdir, "cli": False}) | ||
|
|
||
| # Test worktree detection - should not raise exception | ||
| try: | ||
| is_worktree = megalinter._is_git_worktree(mock_repo) | ||
| # Should return False when exception occurs | ||
| self.assertFalse( | ||
| is_worktree, | ||
| "Should return False when exception occurs during detection" | ||
| ) | ||
| except Exception: | ||
| self.fail( | ||
| "Worktree detection should handle exceptions gracefully" | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
|
|
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Fix overly broad worktree directory detection
The worktree detection check
if 'worktrees' in git_dir:is too broad and will produce false positives. It will incorrectly identify regular git repositories as worktrees if their path happens to contain the substring 'worktrees' anywhere (e.g.,/home/user/my_worktrees_project/.gitor/path/to/worktrees/.git). The check should be more specific, such as checking if the path contains/.git/worktrees/or matches the actual worktree directory structure pattern.