git-semver-compute/README.md

59 lines
1.5 KiB
Markdown
Raw Permalink Normal View History

2023-06-05 08:52:39 -04:00
# Git SemVer Compute
2023-06-05 09:10:52 -04:00
A simple script to use in your projects to calculcate a
[SemVer](https://semver.org/) compliant verion identifier based on Git Tags
and Commits.
2023-06-05 08:52:39 -04:00
2023-06-05 09:10:52 -04:00
## Dependencies
2023-06-05 08:52:39 -04:00
2023-06-05 09:10:52 -04:00
- `git`
- `bash`
- `perl`
2023-06-05 08:52:39 -04:00
2023-06-05 09:10:52 -04:00
## How it Works
2023-06-05 08:52:39 -04:00
2023-06-05 09:10:52 -04:00
Rewinds the commits in the git tree to find one with a tag that is a valid
Semantic Version. If none is found, the version `0.0.0` is used. If the tag
is not found on the current commit then metadata will be added to the version
indicating how many additional commits have been added since the tag along
with a short commit hash. Additionally, if there are any uncommited changes in
tracked files, the diff will be hashed and added to the metadata.
2023-06-05 08:52:39 -04:00
2023-06-05 09:10:52 -04:00
Tags which begin with a 'v' will have it removed when evaluating the tag as a
version.
2023-10-31 02:36:52 -04:00
## Usage
Add it to your project as a submodule:
```bash
2024-06-25 10:49:59 -04:00
git submodule add https://git.sdf.org/CRThaze/git-semver-compute.git .version
2023-10-31 02:36:52 -04:00
```
### Makefile
Easily make the current version available in your Makefile like so:
```make
# Use the ?= to allow the version to be overriden easily by passing it in as
# an environment or build variable.
VERSION ?= $(shell ./.version/calculate-version.sh)
2023-10-31 02:40:13 -04:00
$(EXECUTABLE):
2023-10-31 02:40:55 -04:00
go build -ldflags="-X main.Version=$(VERSION)" -o $(EXECUTABLE) .
2023-10-31 02:36:52 -04:00
```
### Github Actions
```yaml
on: [push]
jobs:
build:
steps:
- name: Compute SemVer from Tags and Commits
id: version
run: echo "VERSION=$(${PWD}/.version/calculate-version.sh)" >> $GITHUB_OUTPUT
- name: Create Archive
run: tar -czf mycode-${{ steps.version.outputs.VERSION }}.tar.gz .
```