-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Add Git validations for publishing tools #1381
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,80 @@ | ||
| import subprocess | ||
|
|
||
|
|
||
| class Repository: | ||
| def __init__(self, path="."): | ||
| self.path = path | ||
|
|
||
| if not self.is_git_installed(): | ||
| raise ValueError("Git is not installed or not found in your PATH.") | ||
|
|
||
| if not self.is_git_repo(): | ||
| raise ValueError(f"{self.path} is not a Git repository.") | ||
|
|
||
| self.fetch() | ||
|
|
||
| def is_git_installed(self) -> bool: | ||
| """Check if Git is installed and available in the system.""" | ||
| try: | ||
| subprocess.run( | ||
| ["git", "--version"], capture_output=True, check=True, text=True | ||
| ) | ||
| return True | ||
| except (subprocess.CalledProcessError, FileNotFoundError): | ||
| return False | ||
|
|
||
| def fetch(self) -> None: | ||
| """Fetch latest updates from the remote.""" | ||
| subprocess.run(["git", "fetch"], cwd=self.path, check=True) | ||
|
|
||
| def status(self) -> str: | ||
| """Get the git status in porcelain format.""" | ||
| return subprocess.check_output( | ||
| ["git", "status", "--branch", "--porcelain"], | ||
| cwd=self.path, | ||
| encoding="utf-8", | ||
| ).strip() | ||
|
|
||
| def is_git_repo(self) -> bool: | ||
| """Check if the current directory is a git repository.""" | ||
| try: | ||
| subprocess.check_output( | ||
| ["git", "rev-parse", "--is-inside-work-tree"], | ||
| cwd=self.path, | ||
| encoding="utf-8", | ||
| ) | ||
| return True | ||
| except subprocess.CalledProcessError: | ||
| return False | ||
|
|
||
| def has_uncommitted_changes(self) -> bool: | ||
| """Check if the repository has uncommitted changes.""" | ||
| return len(self.status().splitlines()) > 1 | ||
|
|
||
| def is_ahead_or_behind(self) -> bool: | ||
| """Check if the repository is ahead or behind the remote.""" | ||
| for line in self.status().splitlines(): | ||
| if line.startswith("##") and ("ahead" in line or "behind" in line): | ||
| return True | ||
| return False | ||
|
|
||
| def is_synced(self) -> bool: | ||
| """Return True if the Git repository is fully synced with the remote, False otherwise.""" | ||
| if self.has_uncommitted_changes() or self.is_ahead_or_behind(): | ||
| return False | ||
| else: | ||
| return True | ||
|
|
||
| def origin_url(self) -> str | None: | ||
| """Get the Git repository's remote URL.""" | ||
| try: | ||
| result = subprocess.run( | ||
| ["git", "remote", "get-url", "origin"], | ||
| cwd=self.path, | ||
| capture_output=True, | ||
| text=True, | ||
| check=True, | ||
| ) | ||
| return result.stdout.strip() | ||
| except subprocess.CalledProcessError: | ||
| return None | ||
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
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,101 @@ | ||
| from crewai.cli.git import Repository | ||
| import pytest | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def repository(fp): | ||
| fp.register(["git", "--version"], stdout="git version 2.30.0\n") | ||
| fp.register(["git", "rev-parse", "--is-inside-work-tree"], stdout="true\n") | ||
| fp.register(["git", "fetch"], stdout="") | ||
| return Repository(path=".") | ||
|
|
||
|
|
||
| def test_init_with_invalid_git_repo(fp): | ||
| fp.register(["git", "--version"], stdout="git version 2.30.0\n") | ||
| fp.register( | ||
| ["git", "rev-parse", "--is-inside-work-tree"], | ||
| returncode=1, | ||
| stderr="fatal: not a git repository\n", | ||
| ) | ||
|
|
||
| with pytest.raises(ValueError): | ||
| Repository(path="invalid/path") | ||
|
|
||
|
|
||
| def test_is_git_not_installed(fp): | ||
| fp.register(["git", "--version"], returncode=1) | ||
|
|
||
| with pytest.raises( | ||
| ValueError, match="Git is not installed or not found in your PATH." | ||
| ): | ||
| Repository(path=".") | ||
|
|
||
|
|
||
| def test_status(fp, repository): | ||
| fp.register( | ||
| ["git", "status", "--branch", "--porcelain"], | ||
| stdout="## main...origin/main [ahead 1]\n", | ||
| ) | ||
| assert repository.status() == "## main...origin/main [ahead 1]" | ||
|
|
||
|
|
||
| def test_has_uncommitted_changes(fp, repository): | ||
| fp.register( | ||
| ["git", "status", "--branch", "--porcelain"], | ||
| stdout="## main...origin/main\n M somefile.txt\n", | ||
| ) | ||
| assert repository.has_uncommitted_changes() is True | ||
|
|
||
|
|
||
| def test_is_ahead_or_behind(fp, repository): | ||
| fp.register( | ||
| ["git", "status", "--branch", "--porcelain"], | ||
| stdout="## main...origin/main [ahead 1]\n", | ||
| ) | ||
| assert repository.is_ahead_or_behind() is True | ||
|
|
||
|
|
||
| def test_is_synced_when_synced(fp, repository): | ||
| fp.register( | ||
| ["git", "status", "--branch", "--porcelain"], stdout="## main...origin/main\n" | ||
| ) | ||
| fp.register( | ||
| ["git", "status", "--branch", "--porcelain"], stdout="## main...origin/main\n" | ||
| ) | ||
| assert repository.is_synced() is True | ||
|
|
||
|
|
||
| def test_is_synced_with_uncommitted_changes(fp, repository): | ||
| fp.register( | ||
| ["git", "status", "--branch", "--porcelain"], | ||
| stdout="## main...origin/main\n M somefile.txt\n", | ||
| ) | ||
| assert repository.is_synced() is False | ||
|
|
||
|
|
||
| def test_is_synced_when_ahead_or_behind(fp, repository): | ||
| fp.register( | ||
| ["git", "status", "--branch", "--porcelain"], | ||
| stdout="## main...origin/main [ahead 1]\n", | ||
| ) | ||
| fp.register( | ||
| ["git", "status", "--branch", "--porcelain"], | ||
| stdout="## main...origin/main [ahead 1]\n", | ||
| ) | ||
| assert repository.is_synced() is False | ||
|
|
||
|
|
||
| def test_is_synced_with_uncommitted_changes_and_ahead(fp, repository): | ||
| fp.register( | ||
| ["git", "status", "--branch", "--porcelain"], | ||
| stdout="## main...origin/main [ahead 1]\n M somefile.txt\n", | ||
| ) | ||
| assert repository.is_synced() is False | ||
|
|
||
|
|
||
| def test_origin_url(fp, repository): | ||
| fp.register( | ||
| ["git", "remote", "get-url", "origin"], | ||
| stdout="https://github.com/user/repo.git\n", | ||
| ) | ||
| assert repository.origin_url() == "https://github.com/user/repo.git" |
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
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.
I like the expressiveness of a if...else.
it can be short tho. Your call.
to avoid the negation, perhaps invert the logic like
is_out_of_sync. Pure juice of nitpicking on my side :)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.
Thanks! I was wrapping my head around this, and I decided to go with the verbose way.
notcombined withorcrashes my inner agent 😆