Skip to content

Do not create tag if it was created on same commit as current branch#10

Closed
pionl wants to merge 1 commit into
MichaelCurrin:masterfrom
pionl:master
Closed

Do not create tag if it was created on same commit as current branch#10
pionl wants to merge 1 commit into
MichaelCurrin:masterfrom
pionl:master

Conversation

@pionl

@pionl pionl commented Jun 2, 2021

Copy link
Copy Markdown

Hi, thanks for the great script! Before I've checked the issue I wanted this feature too.

I've tried on clean repo, made changes, etc..

Resolves #3

@MichaelCurrin

Copy link
Copy Markdown
Owner

thanks for the contribution! I'll test it out

Comment thread autotag
LAST_TAG=$(git describe --abbrev=0 --tags 2>/dev/null)

# Ensure that tag is not created on same commit as current branch commit
if [[ ! -z "$LAST_TAG" ]]; then

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's more elegant to use the not empty check than to check if filled and negate

https://devhints.io/bash

Suggested change
if [[ ! -z "$LAST_TAG" ]]; then
if [[ -n "$LAST_TAG" ]]; then

Comment thread autotag

if [[ "$BRANCH_HASH" = "$LAST_TAG_HASH" ]]; then
echo "Last tag is on the same commit as current branch";
exit 0;

@MichaelCurrin MichaelCurrin Jun 6, 2021

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather make it an error.

Suggested change
exit 0;
exit 1

Comment thread autotag
# Ensure that tag is not created on same commit as current branch commit
if [[ ! -z "$LAST_TAG" ]]; then
CURRENT_BRANCH=$(git branch --show-current)
BRANCH_HASH=$(git rev-list -n 1 $CURRENT_BRANCH)

@MichaelCurrin MichaelCurrin Jun 6, 2021

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you use rev-parse it already gets one commit so you don't have to set it.

https://michaelcurrin.github.io/dev-cheatsheets/cheatsheets/version-control/git/advanced.html

And if you use HEAD, that already points to the current commit. No branch name needed.

Suggested change
BRANCH_HASH=$(git rev-list -n 1 $CURRENT_BRANCH)
BRANCH_HASH=$(git rev-parse HEAD)

Comment thread autotag
if [[ ! -z "$LAST_TAG" ]]; then
CURRENT_BRANCH=$(git branch --show-current)
BRANCH_HASH=$(git rev-list -n 1 $CURRENT_BRANCH)
LAST_TAG_HASH=$(git rev-list -n 1 $LAST_TAG)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
LAST_TAG_HASH=$(git rev-list -n 1 $LAST_TAG)
LAST_TAG_HASH=$(git rev-parse $LAST_TAG)

Comment thread autotag

# Ensure that tag is not created on same commit as current branch commit
if [[ ! -z "$LAST_TAG" ]]; then
CURRENT_BRANCH=$(git branch --show-current)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer needed. See below.

Suggested change
CURRENT_BRANCH=$(git branch --show-current)

Comment thread autotag
BRANCH_HASH=$(git rev-list -n 1 $CURRENT_BRANCH)
LAST_TAG_HASH=$(git rev-list -n 1 $LAST_TAG)

if [[ "$BRANCH_HASH" = "$LAST_TAG_HASH" ]]; then

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've used double equals elsewhere in the script, so remain consistent with that. Same behavior though.

Suggested change
if [[ "$BRANCH_HASH" = "$LAST_TAG_HASH" ]]; then
if [[ "$BRANCH_HASH" == "$LAST_TAG_HASH" ]]; then

Comment thread autotag

LAST_TAG=$(git describe --abbrev=0 --tags 2>/dev/null)

# Ensure that tag is not created on same commit as current branch commit

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Ensure that tag is not created on same commit as current branch commit
# Check if the current commit is already tagged.

Comment thread autotag
LAST_TAG_HASH=$(git rev-list -n 1 $LAST_TAG)

if [[ "$BRANCH_HASH" = "$LAST_TAG_HASH" ]]; then
echo "Last tag is on the same commit as current branch";

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's quiet a mouthful. Just say the thing that we are watching out for and alert on it.

Suggested change
echo "Last tag is on the same commit as current branch";
echo "The current commit already has a commit on it. Aborting, to avoid tagging it again."

Trailing semi-colons are superfluous here. Got JavaScript / Java on the brain?

@MichaelCurrin

MichaelCurrin commented Jun 6, 2021

Copy link
Copy Markdown
Owner

I made some refactor suggestions, so that you can learn more about the shell and git.

But closing this PR because of alternative covered below.

@MichaelCurrin

MichaelCurrin commented Jun 6, 2021

Copy link
Copy Markdown
Owner

A more elegant solution is to use git's built-in check for whether a commit is tagged or not.

Try this out in your terminal.

git describe --tags --exact-match HEAD &> /dev/null \
  && echo 'Commit is tagged' \
  || echo 'Commit is NOT tagged'

Thanks to --exact-match flag - if the commit reference is tagged, you'll get details as usual. But if no tag is found, then it will give an error status.

And then that can be adapted. I've made PR #11 as a shorter form and replacement of what you are doing here. I'd appreciate your review of that please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Skip tag creation if there is already a tag on the current commit

2 participants