Skip to content
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

Support lexicographical sorting #17

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ It would be more useful to use this with other GitHub Actions' outputs.

## Inputs

| NAME | DESCRIPTION | TYPE | REQUIRED | DEFAULT |
|------------------------|---------------------------------------------------------------------------------------------------------------|----------|----------|----------|
| `semver_only` | Whether gets only a tag in the shape of semver. `v` prefix is accepted for tag names. | `bool` | `false` | `false` |
| `initial_version` | The initial version. Works only when `inputs.with_initial_version` == `true`. | `string` | `false` | `v0.0.0` |
| `with_initial_version` | Whether returns `inputs.initial_version` as `outputs.tag` if no tag exists. `true` and `false` are available. | `bool` | `false` | `true` |
NAME | DESCRIPTION | TYPE | REQUIRED | DEFAULT
---|---|---|---|---
`semver_only` | Whether to return only semvar format tags. `v` prefix is accepted for tag names. | `bool` | `false` | `false`
`initial_version` | The initial version. Works only when `inputs.with_initial_version` is set to `true`. | `string` | `false` | `v0.0.0`
`with_initial_version` | Whether to return `inputs.initial_version` as `outputs.tag` if no tag exists | `bool` | `false` | `true`
`sort_by_date` | Whether to sort the tags in chronological order (default) or in lexicographical order | `bool` | `false` | `true`

If `inputs.semver_only` is `true`, the latest tag among tags with semver will be set for `outputs.tag`.

Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ inputs:
description: Whether returns `inputs.initial_version` as `outputs.tag` if no tag exists. `true` and `false` are available.
required: false
default: "true"
sort_by_date:
description: If set to `false`, the tags are sorted in lexicographical order by `refname` (which can return the largest semver tag). Defaults to `true` and returns the tag which was pushed most recently (even though it might not be the largest by semver comparison).
required: false
default: "true"
outputs:
tag:
description: The latest tag. If no tag exists and `inputs.with_initial_version` == `false`, this value is `''`.
Expand Down
24 changes: 24 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ git fetch --tags
git fetch --prune --unshallow || true

latest_tag=''
sort_flag='--sort=-refname'

if [ "${INPUT_SORT_BY_DATE}" = 'true' ]; then
# Set the 'sort' flag
sort_flag='--sort=-creatordate'
fi

if [ "${INPUT_SEMVER_ONLY}" = 'false' ]; then
# Get a actual latest tag.
Expand All @@ -25,6 +31,24 @@ else
done
fi

# Get a latest tag
for ref in $(git for-each-ref ${sort_flag} --format '%(refname)' refs/tags); do
tag="${ref#refs/tags/}"

# Return if it is in semver format
if echo "${tag}" | grep -Eq '^v?([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?$'; then
latest_tag="${tag}"
break
fi

# Return if the user didn't ask for semver format
if [ "${INPUT_SEMVER_ONLY}" = 'false' ]; then
# Get a actual latest tag.
latest_tag="${tag}"
break
fi
done

if [ "${latest_tag}" = '' ] && [ "${INPUT_WITH_INITIAL_VERSION}" = 'true' ]; then
latest_tag="${INPUT_INITIAL_VERSION}"
fi
Expand Down