From 02dd0229e32f35732d11db236b514ff78949b93e Mon Sep 17 00:00:00 2001 From: Johan Euphrosine Date: Thu, 22 Dec 2022 15:38:38 +0900 Subject: [PATCH 1/5] git_helpers: add fallback for inconsistent describe --- conda_build_prepare/git_helpers.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/conda_build_prepare/git_helpers.py b/conda_build_prepare/git_helpers.py index 2acd008..8b6c639 100644 --- a/conda_build_prepare/git_helpers.py +++ b/conda_build_prepare/git_helpers.py @@ -148,7 +148,11 @@ def _call_custom_git_cmd(git_repo, cmd_string, check=True, quiet=False): def get_latest_describe_tag(git_repo): # Find the `git describe` tag having any version-like part try: - return _call_custom_git_cmd(git_repo, 'describe --tags --abbrev=0', quiet=True) + describe_tag = _call_custom_git_cmd(git_repo, 'describe --tags --abbrev=0', quiet=True) + tag_commit = _call_custom_git_cmd(git_repo, f'git rev-list --tags -1') + alt_tag = _call_custom_git_cmd(git_repo, f'tag --points-at {tag_commit}') + # Prefer actual tag over `git describe' if inconsistent. + return describe_tag if describe_tag.startswith(alt_tag) else alt_tag except subprocess.CalledProcessError: return None From 917639f86b132b565557f83db8507e02adb8a357 Mon Sep 17 00:00:00 2001 From: Johan Euphrosine Date: Fri, 23 Dec 2022 12:45:54 +0900 Subject: [PATCH 2/5] conda_build_prepare/git_helpers: use git tag instead of describe --- conda_build_prepare/git_helpers.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/conda_build_prepare/git_helpers.py b/conda_build_prepare/git_helpers.py index 8b6c639..498805c 100644 --- a/conda_build_prepare/git_helpers.py +++ b/conda_build_prepare/git_helpers.py @@ -145,14 +145,11 @@ def _call_custom_git_cmd(git_repo, cmd_string, check=True, quiet=False): return stdout.strip() -def get_latest_describe_tag(git_repo): - # Find the `git describe` tag having any version-like part +def get_latest_tag(git_repo): try: - describe_tag = _call_custom_git_cmd(git_repo, 'describe --tags --abbrev=0', quiet=True) - tag_commit = _call_custom_git_cmd(git_repo, f'git rev-list --tags -1') - alt_tag = _call_custom_git_cmd(git_repo, f'tag --points-at {tag_commit}') - # Prefer actual tag over `git describe' if inconsistent. - return describe_tag if describe_tag.startswith(alt_tag) else alt_tag + tag_commit = _call_custom_git_cmd(git_repo, f'rev-list --tags -1') + tag_refname = _call_custom_git_cmd(git_repo, f'tag --points-at {tag_commit}') + return tag_refname except subprocess.CalledProcessError: return None @@ -236,7 +233,7 @@ def git_rewrite_tags(git_repo): print(f'\nRewriting tags in "{os.path.abspath(git_repo)}"...\n') while True: - tag = get_latest_describe_tag(git_repo) + tag = get_latest_tag(git_repo) if tag is None: # Add '0.0' tag on initial commit and skip rewriting. git_add_initial_tag(git_repo) From 17e71e4ce25c0b82ac0bec16557b6fe8f36d1ff2 Mon Sep 17 00:00:00 2001 From: Johan Euphrosine Date: Fri, 23 Dec 2022 21:39:48 +0900 Subject: [PATCH 3/5] test: add git_helpers unit tests --- .github/workflows/ci.yml | 11 +++++++++ conda_build_prepare/git_helpers.py | 10 ++++---- test/test_git_helpers.py | 38 ++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 test/test_git_helpers.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a6c4d10..ebb7631 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,6 +7,17 @@ on: workflow_dispatch: jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 + with: + python-version: '3.10' + - run: | + python -m pip install pytest + python -m pytest + build: strategy: fail-fast: false diff --git a/conda_build_prepare/git_helpers.py b/conda_build_prepare/git_helpers.py index 498805c..0c18f4d 100644 --- a/conda_build_prepare/git_helpers.py +++ b/conda_build_prepare/git_helpers.py @@ -145,11 +145,11 @@ def _call_custom_git_cmd(git_repo, cmd_string, check=True, quiet=False): return stdout.strip() -def get_latest_tag(git_repo): +def get_first_reachable_tag(git_repo): try: - tag_commit = _call_custom_git_cmd(git_repo, f'rev-list --tags -1') - tag_refname = _call_custom_git_cmd(git_repo, f'tag --points-at {tag_commit}') - return tag_refname + # list reachable tag sorted by commit date + tags = _call_custom_git_cmd(git_repo, 'tag --merged').splitlines() + return tags[-1] except subprocess.CalledProcessError: return None @@ -233,7 +233,7 @@ def git_rewrite_tags(git_repo): print(f'\nRewriting tags in "{os.path.abspath(git_repo)}"...\n') while True: - tag = get_latest_tag(git_repo) + tag = get_first_reachable_tag(git_repo) if tag is None: # Add '0.0' tag on initial commit and skip rewriting. git_add_initial_tag(git_repo) diff --git a/test/test_git_helpers.py b/test/test_git_helpers.py new file mode 100644 index 0000000..9b2bf35 --- /dev/null +++ b/test/test_git_helpers.py @@ -0,0 +1,38 @@ +import pathlib +import time + +import conda_build_prepare.git_helpers as gh + +COMMIT_DELAY = 1 + + +def git_commit_and_tag(filepath, msg, tag=None): + now = time.time() + with open(filepath, 'w') as f: + f.write(f'{now}') + gh._call_custom_git_cmd(filepath.parent, f'add {filepath}') + gh._call_custom_git_cmd(filepath.parent, f'commit -m {msg}') + if tag: + gh._call_custom_git_cmd(filepath.parent, f'tag {tag}') + time.sleep(COMMIT_DELAY) + + +def test_get_first_reachable_tag(tmp_path): + repo = tmp_path + gh._call_custom_git_cmd(repo, 'init -b main') + git_commit_and_tag(repo / 'foo', 'first', tag=None) + + initial_rev = gh._call_custom_git_cmd(repo, 'rev-parse HEAD') + gh._call_custom_git_cmd(repo, 'checkout -b fixup') + git_commit_and_tag(repo / 'foo', 'unreachable', tag='v0.0-unreachabe') + + gh._call_custom_git_cmd(repo, 'checkout main') + git_commit_and_tag(repo / 'foo', 'second', tag='v0.1') + + git_commit_and_tag(repo / 'foo', 'untagged', tag=None) + + gh._call_custom_git_cmd(repo, 'checkout main') + git_commit_and_tag(repo / 'foo', 'third', tag='v0.2') + + gh._call_custom_git_cmd(repo, f'tag v0.0-retroactive {initial_rev}') + assert gh.get_first_reachable_tag(repo) == 'v0.2' From 06ea4bd011ee25c8a3fcf5a9aa06e244a6cfa68b Mon Sep 17 00:00:00 2001 From: Johan Euphrosine Date: Fri, 23 Dec 2022 21:41:46 +0900 Subject: [PATCH 4/5] test/test_git_helpers: remove unused imports --- test/test_git_helpers.py | 1 - 1 file changed, 1 deletion(-) diff --git a/test/test_git_helpers.py b/test/test_git_helpers.py index 9b2bf35..7861a56 100644 --- a/test/test_git_helpers.py +++ b/test/test_git_helpers.py @@ -1,4 +1,3 @@ -import pathlib import time import conda_build_prepare.git_helpers as gh From 41ecf0614fdef1cbdb41d3a365d952ee32624dee Mon Sep 17 00:00:00 2001 From: Johan Euphrosine Date: Fri, 23 Dec 2022 21:44:38 +0900 Subject: [PATCH 5/5] test/test_git_helpers: add local git config --- test/test_git_helpers.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/test_git_helpers.py b/test/test_git_helpers.py index 7861a56..ef2d57e 100644 --- a/test/test_git_helpers.py +++ b/test/test_git_helpers.py @@ -19,6 +19,12 @@ def git_commit_and_tag(filepath, msg, tag=None): def test_get_first_reachable_tag(tmp_path): repo = tmp_path gh._call_custom_git_cmd(repo, 'init -b main') + gh._call_custom_git_cmd( + repo, 'config --local user.email "you@example.com"' + ) + gh._call_custom_git_cmd( + repo, 'config --local user.name "Your Name"' + ) git_commit_and_tag(repo / 'foo', 'first', tag=None) initial_rev = gh._call_custom_git_cmd(repo, 'rev-parse HEAD')