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 2acd008..0c18f4d 100644 --- a/conda_build_prepare/git_helpers.py +++ b/conda_build_prepare/git_helpers.py @@ -145,10 +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_first_reachable_tag(git_repo): try: - return _call_custom_git_cmd(git_repo, 'describe --tags --abbrev=0', quiet=True) + # 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 @@ -232,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_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..ef2d57e --- /dev/null +++ b/test/test_git_helpers.py @@ -0,0 +1,43 @@ +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') + 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') + 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'