-
Notifications
You must be signed in to change notification settings - Fork 9
Expand release-information to include git tags #26
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import re | ||
| import os | ||
| import subprocess | ||
| from collections import OrderedDict | ||
| from cfbs.utils import ( | ||
| write_json, | ||
| ) | ||
|
|
||
| CACHE_DIR = "cache/git/github.com" | ||
| TARGET_DIR = "git/github.com" | ||
|
|
||
| TAG_REGEX = re.compile(r"v?\d+\.\d+\.\d+(-\d+)?") | ||
|
|
||
| REPOS = [ | ||
| "cfengine/core", | ||
| "cfengine/enterprise", | ||
| "cfengine/nova", | ||
| "cfengine/mission-portal", | ||
| "cfengine/buildscripts", | ||
| "cfengine/masterfiles", | ||
| ] | ||
|
|
||
|
|
||
| def clone_or_update_repo(repo): | ||
| # Clone repo if not present, else fetch latest version | ||
| repo_path = os.path.join(CACHE_DIR, repo) | ||
|
|
||
| if os.path.isdir(repo_path): | ||
| print(f"Updating {repo}...") | ||
| subprocess.run(["git", "fetch", "--tags"], cwd=repo_path, check=True) | ||
|
|
||
| else: | ||
| print(f"Cloning {repo}...") | ||
| os.makedirs(os.path.dirname(repo_path), exist_ok=True) | ||
| subprocess.run( | ||
| ["git", "clone", f"git@github.com:{repo}.git", repo_path], | ||
| check=True, | ||
| ) | ||
| return repo_path | ||
|
|
||
|
|
||
| def get_commit_shas_from_tags(repo_path): | ||
| # Returns a mapping of git tag to commit SHA for all version tags in the repo | ||
| output = ( | ||
| subprocess.check_output(["git", "show-ref", "--tags"], cwd=repo_path) | ||
| .decode() | ||
| .strip() | ||
| ) | ||
| tag_map = {} | ||
|
|
||
| for line in output.splitlines(): | ||
| ref = line.split()[1] | ||
| tag = ref.split("refs/tags/")[1] | ||
| if re.fullmatch(TAG_REGEX, tag): | ||
| sha = ( | ||
| subprocess.check_output( | ||
| ["git", "log", "-n", "1", "--format=%H", tag], cwd=repo_path | ||
| ) | ||
| .decode() | ||
| .strip() | ||
| ) | ||
| tag_map[tag] = sha | ||
|
|
||
| return tag_map | ||
|
|
||
|
|
||
| def build_tag_map(repo): | ||
| repo_path = clone_or_update_repo(repo) | ||
| tag_map = get_commit_shas_from_tags(repo_path) | ||
|
|
||
| return sort_git_tags(tag_map) | ||
|
|
||
|
|
||
| def write_tag_map(repo, tag_map): | ||
| repo_dir = os.path.join(TARGET_DIR, repo) | ||
| os.makedirs(repo_dir, exist_ok=True) | ||
| write_json(f"{repo_dir}/tags.json", tag_map) | ||
|
|
||
|
|
||
| def sort_git_tags(tag_map): | ||
| # Sorts git tags by version descending | ||
| return OrderedDict( | ||
| sorted( | ||
| tag_map.items(), | ||
| reverse=True, | ||
| key=lambda item: tuple( | ||
| int(x) for x in item[0].lstrip("v").replace("-", ".").split(".") | ||
| ), | ||
| ) | ||
| ) | ||
|
|
||
|
|
||
| def generate_git_tags_map(): | ||
| os.makedirs(TARGET_DIR, exist_ok=True) | ||
| os.makedirs(CACHE_DIR, exist_ok=True) | ||
| for repo in REPOS: | ||
| print(f"\nProcessing {repo}...") | ||
| tag_map = build_tag_map(repo) | ||
| write_tag_map(repo, tag_map) | ||
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
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.
The purpose of using
git show-ref --tagswas to eliminate these unnecessary subprocesses, I made a mistake when showing you the command, this is the correct version of it;With that command, you get all the information you need in just 1 process instead of having to call git log again and again;
The lines ending with ^{} have the correct commit SHA for that version.