-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/gh32 implement sc tag #33
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
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,210 @@ | ||
| # Copyright 2025 RDK Management | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| """Module for `sc tag` functionality.""" | ||
|
|
||
| from dataclasses import dataclass | ||
| import logging | ||
| from pathlib import Path | ||
| import subprocess | ||
| import sys | ||
|
|
||
| from git import Repo | ||
| from sc_manifest_parser import ScManifest | ||
|
|
||
| from .command import Command | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| @dataclass | ||
| class TagShow(Command): | ||
| """Show information about a particular tag.""" | ||
| tag: str | ||
|
|
||
| def run_git_command(self): | ||
| self._git_show(self.top_dir) | ||
|
|
||
| def run_repo_command(self): | ||
| manifest = ScManifest.from_repo_root(self.top_dir / ".repo") | ||
| for proj in manifest.projects: | ||
| logger.info(f"Operating in: {self.top_dir / proj.path}") | ||
| if proj.lock_status: | ||
| logger.info(f"GIT_LOCK_STATUS: {proj.lock_status}") | ||
| self._git_show(self.top_dir / proj.path) | ||
|
|
||
| logger.info("-" * 100) | ||
|
|
||
| logger.info(f"Operating on manifest {self.top_dir / '.repo' / 'manifests'}") | ||
| self._git_show(self.top_dir / ".repo" / "manifests") | ||
|
|
||
| def _git_show(self, repo_path: Path): | ||
| try: | ||
| out = subprocess.run( | ||
| ["git", "show", self.tag, "--color=always"], | ||
| cwd=repo_path, | ||
| check=True, | ||
| capture_output=True, | ||
| text=True | ||
| ) | ||
| print(out.stdout) | ||
| except subprocess.CalledProcessError: | ||
| logger.warning(f"Tag {self.tag} not found!") | ||
BenjiMilan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @dataclass | ||
| class TagList(Command): | ||
| """List all tags.""" | ||
| def run_git_command(self): | ||
| self._list_tags(self.top_dir) | ||
|
|
||
| def run_repo_command(self): | ||
| logger.info(f"Tags in mainfest: {self.top_dir / '.repo' / 'manifests'}") | ||
BenjiMilan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self._list_tags(self.top_dir / '.repo' / 'manifests') | ||
|
|
||
| def _list_tags(self, repo_path: Path): | ||
| subprocess.run(["git", "tag", "-l"], cwd=repo_path, check=False) | ||
|
|
||
| @dataclass | ||
| class TagCreate(Command): | ||
| """Create a tag.""" | ||
| tag: str | ||
|
|
||
| def run_git_command(self): | ||
| subprocess.run(["git", "tag", self.tag], cwd=self.top_dir, check=False) | ||
|
|
||
BenjiMilan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| def run_repo_command(self): | ||
| manifest = ScManifest.from_repo_root(self.top_dir / '.repo') | ||
| self._error_if_tag_already_exists(manifest) | ||
|
|
||
| for proj in manifest.projects: | ||
| logger.info(f"Operating on: {self.top_dir / proj.path}") | ||
| if proj.lock_status == "READ_ONLY": | ||
| logger.info("READ_ONLY, skipping creating tag.") | ||
| continue | ||
|
|
||
| Repo(self.top_dir / proj.path).git.tag(self.tag) | ||
| logger.info(f"Tagged with {self.tag}") | ||
|
|
||
| logger.info(f"Operating on manifest: {self.top_dir / '.repo' / 'manifests'}") | ||
| Repo(self.top_dir / '.repo' / 'manifests').git.tag(self.tag) | ||
| logger.info(f"Tagged with {self.tag}") | ||
|
|
||
| def _error_if_tag_already_exists(self, manifest: ScManifest): | ||
| existing = [ | ||
| self.top_dir / proj.path for proj in manifest.projects | ||
| if proj.lock_status != "READ_ONLY" # We aren't tagging READ_ONLY anyway | ||
| and self._tag_exists(self.top_dir / proj.path) | ||
| ] | ||
|
|
||
| if self._tag_exists(self.top_dir / '.repo' / 'manifests'): | ||
| logger.error(f"Tag {self.tag} already exists in the manifest.") | ||
| existing.append(self.top_dir / '.repo' / 'manifests') | ||
|
|
||
| if existing: | ||
| logger.error( | ||
| "Tag already exists in the following projects:\n" + | ||
| "\n".join(str(p) for p in existing) | ||
| ) | ||
| sys.exit(1) | ||
|
|
||
| def _tag_exists(self, repo_path: Path): | ||
| return any(t.name == self.tag for t in Repo(repo_path).tags) | ||
|
|
||
| @dataclass | ||
| class TagRm(Command): | ||
| """Remove a tag.""" | ||
| tag: str | ||
| remote: bool | ||
|
|
||
| def run_git_command(self): | ||
| self._delete_tag(self.top_dir) | ||
TB-1993 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def run_repo_command(self): | ||
| manifest = ScManifest.from_repo_root(self.top_dir / '.repo') | ||
| for proj in manifest.projects: | ||
| logger.info(f"Operating on: {self.top_dir / proj.path}") | ||
| if proj.lock_status == "READ_ONLY": | ||
| logger.info("READ_ONLY, skipping removing tag") | ||
| continue | ||
|
|
||
| self._delete_tag(self.top_dir / proj.path, proj.remote) | ||
|
|
||
| logger.info(f"Operating on manifest: {self.top_dir / '.repo' / 'manifests'}") | ||
| self._delete_tag(self.top_dir / '.repo' / 'manifests') | ||
|
|
||
| def _delete_tag(self, repo_path: Path, remote: str = "origin"): | ||
| subprocess.run(["git", "tag", "--delete", self.tag], cwd=repo_path, check=False) | ||
| if self.remote: | ||
| subprocess.run( | ||
| ["git", "push", remote, f":refs/tags/{self.tag}"], | ||
| cwd=repo_path, | ||
| check=False | ||
| ) | ||
|
|
||
| @dataclass | ||
| class TagPush(Command): | ||
| """Push a tag.""" | ||
| tag: str | ||
|
|
||
| def run_git_command(self): | ||
| remote = Repo(self.top_dir).remotes[0].name | ||
| self._push_tags(self.top_dir, remote) | ||
|
|
||
| def run_repo_command(self): | ||
| manifest = ScManifest.from_repo_root(self.top_dir / '.repo') | ||
| for proj in manifest.projects: | ||
| logger.info(f"Operating on: {self.top_dir / proj.path}") | ||
| if proj.lock_status == "READ_ONLY": | ||
| logger.info("READ_ONLY, skipping pushing tags") | ||
| continue | ||
|
|
||
| self._push_tags(self.top_dir / proj.path, proj.remote) | ||
|
|
||
| logger.info(f"Operating on manifest: {self.top_dir / '.repo' / 'manifests'}") | ||
| self._push_tags(self.top_dir / '.repo' / 'manifests') | ||
|
|
||
| logger.info("Push tags complete.") | ||
|
|
||
| def _push_tags(self, repo_path: Path, remote: str = "origin"): | ||
| subprocess.run( | ||
| ["git", "push", remote, f"refs/tags/{self.tag}"], | ||
| cwd=repo_path, | ||
| check=False | ||
| ) | ||
|
|
||
| @dataclass | ||
| class TagCheck(Command): | ||
| """Check all repos for a specific tag.""" | ||
| tag: str | ||
|
|
||
| def run_git_command(self): | ||
| self._check_tag(self.top_dir) | ||
|
|
||
| def run_repo_command(self): | ||
| manifest = ScManifest.from_repo_root(self.top_dir / '.repo') | ||
| for proj in manifest.projects: | ||
| logger.info(f"Operating on: {self.top_dir / proj.path}") | ||
| if proj.lock_status == "READ_ONLY": | ||
| logger.info("READ_ONLY, skipping checking tags.") | ||
| continue | ||
|
|
||
| self._check_tag(self.top_dir / proj.path) | ||
|
|
||
| logger.info(f"Operating on manifest: {self.top_dir / '.repo' / 'manifests'}") | ||
| self._check_tag(self.top_dir / '.repo' / 'manifests') | ||
|
|
||
| def _check_tag(self, repo_path: Path): | ||
| subprocess.run( | ||
| ["git", "show-ref", "--tags", "--verify", f"refs/tags/{self.tag}"], | ||
| cwd=repo_path, | ||
| check=False | ||
| ) | ||
BenjiMilan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.