mtkennerly / dunamai

Dynamic versioning library and CLI
https://dunamai.readthedocs.io/en/latest
MIT License
312 stars 24 forks source link

gitflow support #46

Closed povilasb closed 2 years ago

povilasb commented 2 years ago

Would you be interested in gitflow branching model support?

image

With gitflow model, we're only tagging master/main branch. We MAY want to get a version on develop branch as well, e.g. 0.1.0+3.1a2bc3 However, since dunamai is looking for the latest tag on the current branch, it can't locate the latest tag.

Something like:

latest_tag=`git describe --tags refs/remotes/origin/main`

commits_from_tag=`git rev-list --count $latest_tag..HEAD`
if [ $commits_from_tag = 0 ]
then
    version=$latest_tag
else
    last_commit=`git rev-parse --short HEAD`
    version=$latest_tag+$commits_from_tag.$last_commit
fi

echo "Detected version: $version"

export POETRY_DYNAMIC_VERSIONING_BYPASS=$version
mtkennerly commented 2 years ago

Could you give me some more info about your setup? On my end, it worked in a simple test just now, because every commit on master is a fast forward of a commit on a release branch, and the same release branch commit exists in develop's history as well. Therefore, if such a commit is tagged on master, the tag is also available on develop. However, this is a very simple example, and there may be something different about your repository that's tripping it up (e.g., squashing or rebasing).

$ mkdir flow
$ cd flow
$ git init

$ echo foo > f.txt
$ git add . && git commit --no-gpg-sign -m initial
$ git tag v0.1 -m release

$ git checkout -b develop
$ echo bar > f.txt
$ git add . && git commit --no-gpg-sign -m second
$ echo baz > f.txt
$ git add . && git commit --no-gpg-sign -m third

$ git checkout -b release/v0.2
$ echo bugfix > f.txt
$ git add . && git commit --no-gpg-sign -m bugfix

$ git checkout develop
$ echo new-feature > f.txt
$ git add . && git commit --no-gpg-sign -m new-feature
$ git merge release/v0.2
Auto-merging f.txt
CONFLICT (content): Merge conflict in f.txt
Automatic merge failed; fix conflicts and then commit the result.

# resolve conflict, then:
$ git add . && git commit --no-gpg-sign -m merged-release-into-develop

$ git checkout master
$ git merge release/v0.2
Updating 3afe534..3ebd53d
Fast-forward

$ git tag v0.2 -m t2
$ dunamai from git
0.2

$ git checkout develop
$ dunamai from git
0.2.post2.dev0+e78dc6d
povilasb commented 2 years ago

Thank you for the script :)

So --no-ff makes the change:

git checkout develop
git merge --no-ff release/v0.2
...
git checkout master
git merge --no-ff release/v0.2

Here's a full script:

mkdir flow
cd flow
git init

echo foo > f.txt
git add . && git commit --no-gpg-sign -m initial
git tag v0.1 -m release

git checkout -b develop
echo bar > f.txt
git add . && git commit --no-gpg-sign -m second
echo baz > f.txt
git add . && git commit --no-gpg-sign -m third

git checkout -b release/v0.2
echo bugfix > f.txt
git add . && git commit --no-gpg-sign -m bugfix

git checkout develop
echo new-feature > f.txt
git add . && git commit --no-gpg-sign -m new-feature
git merge --no-ff release/v0.2

# resolve conflict, then:
git add . && git commit --no-gpg-sign -m merged-release-into-develop

git checkout master
git merge --no-ff release/v0.2

git tag v0.2 -m t2
dunamai from git

git checkout develop
dunamai from git

And here's the output:

Initialized empty Git repository in /tmp/flow/.git/
[master (root-commit) d597306] initial
 1 file changed, 1 insertion(+)
 create mode 100644 f.txt
Switched to a new branch 'develop'
[develop 63c7c4e] second
 1 file changed, 1 insertion(+), 1 deletion(-)
[develop 7cd890a] third
 1 file changed, 1 insertion(+), 1 deletion(-)
Switched to a new branch 'release/v0.2'
[release/v0.2 dfe17be] bugfix
 1 file changed, 1 insertion(+), 1 deletion(-)
Switched to branch 'develop'
[develop 4ea2cd5] new-feature
 1 file changed, 1 insertion(+), 1 deletion(-)
Auto-merging f.txt
CONFLICT (content): Merge conflict in f.txt
Automatic merge failed; fix conflicts and then commit the result.
[develop afe0c1b] merged-release-into-develop
Switched to branch 'master'
Merge made by the 'recursive' strategy.
 f.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
0.2
Switched to branch 'develop'
0.1.post5.dev0+afe0c1b
mtkennerly commented 2 years ago

Thanks! That makes sense. I'd definitely be open to supporting that. I think there just needs to be an option to set a specific branch name instead of HEAD here: https://github.com/mtkennerly/dunamai/blob/cc1b8c1fc84d33695eabd37d0156a07ced2be287/dunamai/__init__.py#L736

povilasb commented 2 years ago

Nice, thank you for the pointer. I'll experiment :)

mtkennerly commented 2 years ago

The next release will provide a --tag-branch option for this.

povilasb commented 2 years ago

Oh thank you!

I would like to tip you, if you accepted GitHub sponsoring.

mtkennerly commented 2 years ago

That's very nice of you to offer, but it's okay! I'm just happy if you get some value from the project and pass along ideas when you have them :)

By the way, there's also a new release of poetry-dynamic-versioning(-plugin) now with a corresponding tag-branch option.