-
Notifications
You must be signed in to change notification settings - Fork 2
refactor git-autograder to generalize bebavior and reduce indirections #21
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
SAN-MUYUN
wants to merge
7
commits into
git-mastery:main
Choose a base branch
from
SAN-MUYUN:refactor/generalise-behavior
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.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
feb0f97
refactor git-autograder to generalize bebavior and reduce indirections
SAN-MUYUN 80d729e
make generalisation in FileHelper
SAN-MUYUN 87c80ec
further refactor gitautograder
SAN-MUYUN 77efbc4
update file comparison logic
SAN-MUYUN 4cf6b90
ensure file is opened using encoding=utf-8
SAN-MUYUN d57e8f4
implement generalization for commit count
SAN-MUYUN b701279
Apply suggestion from @desmondwong1215
desmondwong1215 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
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 |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| __all__ = ["BranchHelper", "CommitHelper", "RemoteHelper", "FileHelper"] | ||
| __all__ = ["BranchHelper", "CommitHelper", "RemoteHelper", "FileHelper", "TagHelper"] | ||
|
|
||
| from .branch_helper import BranchHelper | ||
| from .commit_helper import CommitHelper | ||
| from .remote_helper import RemoteHelper | ||
| from .file_helper import FileHelper | ||
| from .tag_helper import TagHelper |
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,79 @@ | ||
| from typing import List, Optional | ||
|
|
||
| from git import Repo | ||
| from git.exc import GitCommandError | ||
|
|
||
| from git_autograder.exception import GitAutograderInvalidStateException | ||
| from git_autograder.tag import GitAutograderTag | ||
|
|
||
|
|
||
| class TagHelper: | ||
| MISSING_TAG = "Tag {tag} is missing." | ||
| MISSING_REMOTE = "Remote {remote} is missing." | ||
| CANNOT_QUERY_REMOTE_TAGS = "Unable to query remote tags for '{remote}'." | ||
|
|
||
| @staticmethod | ||
| def _parse_remote_tag_names(raw: str) -> list[str]: | ||
| names: list[str] = [] | ||
|
|
||
| for line in raw.splitlines(): | ||
| parts = line.split() | ||
| if len(parts) != 2: | ||
| continue | ||
|
|
||
| ref = parts[1] | ||
| if not ref.startswith("refs/tags/"): | ||
| continue | ||
|
|
||
| name = ref[len("refs/tags/") :] | ||
| if name.endswith("^{}"): | ||
| name = name[:-3] | ||
|
|
||
| if name not in names: | ||
| names.append(name) | ||
|
|
||
| return names | ||
|
|
||
| def __init__(self, repo: Repo) -> None: | ||
| self.repo = repo | ||
|
|
||
| def tag_or_none(self, tag_name: str) -> Optional[GitAutograderTag]: | ||
| for tag_ref in self.repo.tags: | ||
| if str(tag_ref) == tag_name: | ||
| return GitAutograderTag(tag_ref) | ||
| return None | ||
|
|
||
| def tag(self, tag_name: str) -> GitAutograderTag: | ||
| t = self.tag_or_none(tag_name) | ||
| if t is None: | ||
| raise GitAutograderInvalidStateException( | ||
| self.MISSING_TAG.format(tag=tag_name) | ||
| ) | ||
| return t | ||
|
|
||
| def has_tag(self, tag_name: str) -> bool: | ||
| return self.tag_or_none(tag_name) is not None | ||
|
|
||
| def remote_tag_names_or_none(self, remote: str = "origin") -> Optional[List[str]]: | ||
| if not any(r.name == remote for r in self.repo.remotes): | ||
| return None | ||
|
|
||
| try: | ||
| raw = self.repo.git.ls_remote("--tags", remote) | ||
| except GitCommandError: | ||
| return None | ||
|
|
||
| return self._parse_remote_tag_names(raw) | ||
|
|
||
| def remote_tag_names(self, remote: str = "origin") -> List[str]: | ||
| if not any(r.name == remote for r in self.repo.remotes): | ||
| raise GitAutograderInvalidStateException( | ||
| self.MISSING_REMOTE.format(remote=remote) | ||
| ) | ||
|
|
||
| names = self.remote_tag_names_or_none(remote) | ||
| if names is None: | ||
| raise GitAutograderInvalidStateException( | ||
| self.CANNOT_QUERY_REMOTE_TAGS.format(remote=remote) | ||
| ) | ||
| return names | ||
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,45 @@ | ||
| from typing import Any, Optional | ||
|
|
||
| from git.refs.tag import TagReference | ||
|
|
||
| from git_autograder.commit import GitAutograderCommit | ||
|
|
||
|
|
||
| class GitAutograderTag: | ||
| def __init__(self, tag_ref: TagReference) -> None: | ||
| self.tag_ref = tag_ref | ||
|
|
||
| def __eq__(self, value: Any) -> bool: | ||
| return isinstance(value, GitAutograderTag) and value.tag_ref == self.tag_ref | ||
|
|
||
| @property | ||
| def name(self) -> str: | ||
| return str(self.tag_ref) | ||
|
|
||
| @property | ||
| def commit(self) -> GitAutograderCommit: | ||
| return GitAutograderCommit(self.tag_ref.commit) | ||
|
|
||
| @property | ||
| def is_annotated(self) -> bool: | ||
| return self.tag_ref.tag is not None | ||
|
|
||
| @property | ||
| def is_lightweight(self) -> bool: | ||
| return self.tag_ref.tag is None | ||
|
|
||
| def message_or_none( | ||
| self, *, strip: bool = True, lower: bool = False | ||
| ) -> Optional[str]: | ||
| if self.tag_ref.tag is None: | ||
| return None | ||
|
|
||
| message = self.tag_ref.tag.message | ||
| if strip: | ||
| message = message.strip() | ||
| if lower: | ||
| message = message.lower() | ||
| return message | ||
|
|
||
| def points_to(self, commit: GitAutograderCommit) -> bool: | ||
| return self.commit.hexsha == commit.hexsha |
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.
Is the ordering of the tag important in this case, as using
setwill be more efficient?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.
Personally I find using of set is unnecessary, just like you pointed out in the previous review. For a single exercise, it is unlikely that we have large number of tags such that we will need to use a
setto keep track of the tags. It would be good to keep the ordering in some cases.