Skip to content
Merged
Changes from 8 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
333 changes: 333 additions & 0 deletions sbin/tagman
Original file line number Diff line number Diff line change
@@ -0,0 +1,333 @@
#!/usr/bin/env bash
# -----------------------------------------------------------------------------
# (C) Crown copyright Met Office. All rights reserved.
# The file LICENCE, distributed with this code, contains details of the terms
# under which the code may be used.
# -----------------------------------------------------------------------------
#
# Script to manage Git tags (add/delete/list).
# Requires:
# GitHub CLI (gh): https://cli.github.com/
# Git command-line tool: https://git-scm.com/
# jq for JSON parsing: https://stedolan.github.io/jq/
# Warnings:
# - This script modifies Git tags. Use with caution.
# - Always verify the current tags before making changes.

set -euo pipefail
# Colour codes for output
GRN='\033[0;32m' # Green
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
GRN='\033[0;32m' # Green
GREEN='\033[0;32m' # Green

RED='\033[0;31m' # Red
YLW='\033[0;33m' # Yellow
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
YLW='\033[0;33m' # Yellow
YELLOW='\033[0;33m' # Yellow

NC='\033[0m' # No Color
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
NC='\033[0m' # No Color
NO_COLOUR='\033[0m' # No Color


# Default values
DEFAULT_REPO="MetOffice/git_playground"
REPO="${REPO:-$DEFAULT_REPO}"
# Variables set by parse_args()
REF=""
TAG=""
MESSAGE=""
DRY_RUN=false

usage() {
cat <<EOF
Usage:
$(basename "$0") add <tag_name> <commit_ref> [options]
$(basename "$0") delete|del <tag_name> [options]
$(basename "$0") list|ls [options]

Actions:
add Create and push a new tag
delete Delete a tag from the repository (alias: del)
list List all tags in the repository (alias: ls)

Arguments:
<tag_name> Name of the tag to create or delete
<commit_ref> Commit SHA, tag name, release name, or branch name

Options:
--repo, -R REPO Repository in format owner/repo (default: $DEFAULT_REPO)
--message MSG Tag annotation message (for add action)
--dry-run, -n Show what would be done without making changes

Examples:
# Create tag from commit SHA or existing tag or release or branch
$(basename "$0") add Test abc123def|vn1.5|main --repo MetOffice/git_playground

# Delete tag
$(basename "$0") del 2025.12.0 --repo MetOffice/SimSys_Scripts

Notes:
- REPO can be set via environment variable (default: $DEFAULT_REPO)
- All other parameters must be provided via command-line arguments
- Use --dry-run to preview changes before executing
EOF
exit 1
}

cleanup() {
if [[ -n "${WORK_TMP:-}" ]]; then
rm -rf "$WORK_TMP"
fi
}

confirm() {
local message="$1"
local response
echo -en "${YLW}"
Comment thread
r-sharp marked this conversation as resolved.
Outdated
read -rp "$message (y/n): " response
echo -en "${NC}"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

NC = No Clue ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

As above.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

as above

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -en "${NC}"
echo -en "${NO_COLOUR}"


case "${response^^}" in # Note: requires bash 4+ for ^^ operator
YES | Y)
return 0 ;;
*)
echo "Aborted..."
return 1 ;;
esac
}

run() {
local msg="$1"
shift
local timestamp
timestamp=$(date "+%F %T")

if "$@"; then
echo -e "[$timestamp] ${GRN}✓${NC} $msg succeeded."
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "[$timestamp] ${GRN}✓${NC} $msg succeeded."
echo -e "[$timestamp] ${GREEN}✓${SOMETHING_MEANINGFUL} $msg succeeded."

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

As explained earlier. Plus, the suggested change cannot be accepted as that will break the code.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't see how changing variable names to something clear will break the code. Thery're just references to a memory location and are interpreted by the underlying parser. calling something Dave and then changing it to Fred makes absolutely no difference to operation.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I may not have explained this clearly. You suggested several variable names for NC, but didn't recommend how to define it initially, unlike what you did for GRN and YLW. If I rely on your comments and commit these suggestions, it could cause serious issues.

My point is: suggest functional code changes only. Other ideas can be posted as comments.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "[$timestamp] ${GRN}✓${NC} $msg succeeded."
echo -e "[$timestamp] ${GREEN}✓${NO_COLOUR} $msg succeeded."

return 0
else
echo -e "[$timestamp] ${RED}✗${NC} $msg failed."
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "[$timestamp] ${RED}✗${NC} $msg failed."
echo -e "[$timestamp] ${RED}✗${ANYTHING} $msg failed."

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

As above. Suggested change cannot be accepted.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

as above - I don't see why not.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "[$timestamp] ${RED}✗${NC} $msg failed."
echo -e "[$timestamp] ${RED}✗${NO_COLOUR} $msg failed."

return 1
fi
}

trap cleanup EXIT ERR SIGINT

verify_tag() {
if gh api "repos/${REPO}/git/refs/tags/${TAG}" >/dev/null 2>&1; then
echo -e "${YLW}Tag '$TAG' exists in repository '$REPO'.${NC}"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${YLW}Tag '$TAG' exists in repository '$REPO'.${NC}"
echo -e "${YELLOW}Tag '$TAG' exists in repository '$REPO'.${GRANNYS_CHIN_HAIR}"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

As above. Suggested change cannot be accepted.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

as above - I don't see why not.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${YLW}Tag '$TAG' exists in repository '$REPO'.${NC}"
echo -e "${YELLOW}Tag '$TAG' exists in repository '$REPO'.${NO_COLOUR}"

return 0
fi
return 1
}

verify_ref() {
local resolved_sha=""

# First, try to resolve as a commit SHA (handles both short and full)
if resolved_sha=$(gh api "repos/${REPO}/commits/${REF}" --jq '.sha' 2>/dev/null); then
REF="$resolved_sha"
echo -e "${GRN}Using commit SHA: $REF${NC}"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${GRN}Using commit SHA: $REF${NC}"
echo -e "${GREEN}Using commit SHA: $REF${NEOPOLITAN_CHOCOLATE}"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

As above. Suggested change cannot be accepted.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

as above - I don't see why not.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${GRN}Using commit SHA: $REF${NC}"
echo -e "${GREEN}Using commit SHA: $REF${NO_COLOUR}"

return 0
fi

# Try to resolve as a tag
if gh api "repos/${REPO}/git/refs/tags/${REF}" >/dev/null 2>&1; then
echo -e "${YLW}Resolving tag '$REF' to commit SHA...${NC}"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${YLW}Resolving tag '$REF' to commit SHA...${NC}"
echo -e "${YELLOW}Resolving tag '$REF' to commit SHA...${NORMAL_COLOSTOMY}"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

As above. Suggested change cannot be accepted.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

as above - I don't see why not.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${YLW}Resolving tag '$REF' to commit SHA...${NC}"
echo -e "${YELLOW}Resolving tag '$REF' to commit SHA...${NO_COLOUR}"

local tag_sha
tag_sha=$(gh api "repos/${REPO}/git/refs/tags/${REF}" --jq '.object.sha')

# Try to get tag object to determine if it's annotated
local tag_info
if tag_info=$(gh api "repos/${REPO}/git/tags/${tag_sha}" 2>/dev/null); then
# It's an annotated tag - get the commit SHA it points to
local tag_type
tag_type=$(echo "$tag_info" | jq -r '.object.type')

if [[ "$tag_type" == "commit" ]]; then
resolved_sha=$(echo "$tag_info" | jq -r '.object.sha')
else
echo -e "${RED}** Tag points to unexpected object type: $tag_type${NC}"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

"unexpected object type" would be appropriate IFF thewre was more than one type accepted.
However, only "commit" is accepted so the error message should be
"Tag does not point to a commit type object"

Suggested change
echo -e "${RED}** Tag points to unexpected object type: $tag_type${NC}"
echo -e "${RED}** Tag does not point to a commit type object: $tag_type${NEW_COMBATANT}"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Thank you for the suggestion. However, the current message is actually more accurate for future-proofing. While we currently only handle commit types, the Git object model supports multiple types (blob, tree, commit, tag), and the message correctly indicates that something unexpected was encountered. If we hardcode the message as suggested, it becomes less accurate if the code ever needs to handle other types.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

When the code changes, maker the comment more general purpose.
Until then make the comment reflect the code.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The comment, as it stands, does not reflect teh truth opf the current code.
When the code is made more general, then make the comment the comment general, but do not generalise a comment before the code has caught up.

Suggested change
echo -e "${RED}** Tag points to unexpected object type: $tag_type${NC}"
echo -e "${RED}** Tag does not point to a commit type objec: $tag_type${NO_COLOUR}"

return 1
fi
else
# It's a lightweight tag - the SHA is the commit SHA
resolved_sha="$tag_sha"
fi

# Verify it's a full SHA and a valid commit
if resolved_sha=$(gh api "repos/${REPO}/commits/${resolved_sha}" --jq '.sha' 2>/dev/null); then
REF="$resolved_sha"
echo -e "${GRN}Resolved to commit: $REF${NC}"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${GRN}Resolved to commit: $REF${NC}"
echo -e "${GREEN}Resolved to commit: $REF${NATIVITY_CAROLS}"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

As above. Suggested change cannot be accepted.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

as above - I don't see why not.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${GRN}Resolved to commit: $REF${NC}"
echo -e "${GREEN}Resolved to commit: $REF${NO_COLOUR}"

return 0
else
echo -e "${RED}** Failed to verify commit SHA from tag${NC}"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${RED}** Failed to verify commit SHA from tag${NC}"
echo -e "${RED}** Failed to verify commit SHA from tag${NAUGHTY_CHORISTERS}"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

As above. Suggested change cannot be accepted.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

as above - I don't see why not.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${RED}** Failed to verify commit SHA from tag${NC}"
echo -e "${RED}** Failed to verify commit SHA from tag${NO_COLOUR}"

return 1
fi
fi

# Try to resolve as a release
if gh api "repos/${REPO}/releases/tags/${REF}" >/dev/null 2>&1; then
echo -e "${YLW}Resolving release '$REF' to commit SHA...${NC}"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${YLW}Resolving release '$REF' to commit SHA...${NC}"
echo -e "${YELLOW}Resolving release '$REF' to commit SHA...${NAMED_COMMIT}"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

As above. Suggested change cannot be accepted.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

as above - I don't see why not.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${YLW}Resolving release '$REF' to commit SHA...${NC}"
echo -e "${YELLOW}Resolving release '$REF' to commit SHA...${NO_COLOUR}"

local target_ref
target_ref=$(gh api "repos/${REPO}/releases/tags/${REF}" --jq '.target_commitish')

# Resolve the target to full SHA
if resolved_sha=$(gh api "repos/${REPO}/commits/${target_ref}" --jq '.sha' 2>/dev/null); then
REF="$resolved_sha"
echo -e "${GRN}Resolved to commit: $REF${NC}"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${GRN}Resolved to commit: $REF${NC}"
echo -e "${GREEN}Resolved to commit: $REF${NAVAL_CUTTER}"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

As above. Suggested change cannot be accepted.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

as above - I don't see why not.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${GRN}Resolved to commit: $REF${NC}"
echo -e "${GREEN}Resolved to commit: $REF${NO_COLOUR}"

return 0
else
echo -e "${RED}** Failed to resolve release target to commit SHA${NC}"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${RED}** Failed to resolve release target to commit SHA${NC}"
echo -e "${RED}** Failed to resolve release target to commit SHA${NONSENSICAL_COMMENT}"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

As above. Suggested change cannot be accepted.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

as above - I don't see why not.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${RED}** Failed to resolve release target to commit SHA${NC}"
echo -e "${RED}** Failed to resolve release target to commit SHA${NO_COLOUR}"

return 1
fi
fi

# Try as a branch name
if gh api "repos/${REPO}/git/refs/heads/${REF}" >/dev/null 2>&1; then
echo -e "${YLW}Resolving branch '$REF' to commit SHA...${NC}"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${YLW}Resolving branch '$REF' to commit SHA...${NC}"
echo -e "${YELLOW}Resolving branch '$REF' to commit SHA...${NOCTILUCENT_CLOUDS}"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

As above. Suggested change cannot be accepted.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

as above - I don't see why not.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${YLW}Resolving branch '$REF' to commit SHA...${NC}"
echo -e "${YELLOW}Resolving branch '$REF' to commit SHA...${NO_COLOUR}"

local branch_sha
branch_sha=$(gh api "repos/${REPO}/git/refs/heads/${REF}" --jq '.object.sha')

# Verify it's a full SHA
if resolved_sha=$(gh api "repos/${REPO}/commits/${branch_sha}" --jq '.sha' 2>/dev/null); then
REF="$resolved_sha"
echo -e "${GRN}Resolved to commit: $REF${NC}"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${GRN}Resolved to commit: $REF${NC}"
echo -e "${GREEN}Resolved to commit: $REF${NAKED_COMMANDO}"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

As above. Suggested change cannot be accepted.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

as above - I don't see why not.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${GRN}Resolved to commit: $REF${NC}"
echo -e "${GREEN}Resolved to commit: $REF${NO_COLOUR}"

return 0
else
echo -e "${RED}** Failed to verify commit SHA from branch${NC}"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${RED}** Failed to verify commit SHA from branch${NC}"
echo -e "${RED}** Failed to verify commit SHA from branch${NAVIGATIONAL_COMPUTER}"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

As above. Suggested change cannot be accepted.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

as above - I don't see why not.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${RED}** Failed to verify commit SHA from branch${NC}"
echo -e "${RED}** Failed to verify commit SHA from branch${NO_COLOUR}"

return 1
fi
fi

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A comment to remind us how we got here might be appropriate.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The function name verify_ref() and the preceding attempts make it self-documenting how we got there. The error messages around lines 204-206 explicitly state "Reference not found" and "Tried: commit SHA, tag, release, and branch name" which fully explains the situation. An additional comment wouldn't add value here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I asked for a acomment because I did not find the code to be self documenting and had to work back through several poages of nested if's to estable what had to have NOT been the answer for a standard "return 1" to be applicable.
Therefore a comment to improve clarity is needed.

echo -e "${RED}** Reference '$REF' not found in repository '$REPO'.${NC}"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${RED}** Reference '$REF' not found in repository '$REPO'.${NC}"
echo -e "${RED}** Reference '$REF' not found in repository '$REPO'.${NORTHERLY_CONNECTION}"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

As above. Suggested change cannot be accepted.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

as above - I don't see why not.

echo -e "${RED}** Tried: commit SHA, tag, release, and branch name.${NC}"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${RED}** Tried: commit SHA, tag, release, and branch name.${NC}"
echo -e "${RED}** Tried: commit SHA, tag, release, and branch name.${NOT_CONTAGIOUS}"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

As above. Suggested change cannot be accepted.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

as above - I don't see why not.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${RED}** Tried: commit SHA, tag, release, and branch name.${NC}"
echo -e "${RED}** Tried: commit SHA, tag, release, and branch name.${NO_COLOUR}"

return 1
}

add_tag() {
if [[ -z "$TAG" || -z "$REF" ]]; then
echo -e "${RED}** TAG and REF are required for add action.${NC}"
usage
fi

verify_tag && exit 1 # Tag already exists, exit with error
verify_ref || exit 1 # Reference resolution failed, exit with error
Comment thread
yaswant marked this conversation as resolved.
Outdated

local url="https://github.com/${REPO}.git"
local msg="${MESSAGE:-"Tagging $TAG @ $REF"}"

if [[ "$DRY_RUN" == true ]]; then
echo -e "${YLW}[DRY RUN] Would create tag with the following details:${NC}"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${YLW}[DRY RUN] Would create tag with the following details:${NC}"
echo -e "${YELLOW}[DRY RUN] Would create tag with the following details:${NOTHING_COUNTS}"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Suggested change cannot be accepted.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

as above - I don't see why not.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${YLW}[DRY RUN] Would create tag with the following details:${NC}"
echo -e "${YELLOW}[DRY RUN] Would create tag with the following details:${NO_COLOUR}"

echo -e " Repository: $REPO"
echo -e " Tag name: $TAG"
echo -e " Commit SHA: $REF"
echo -e " Message: $msg"
echo -e "${YLW}[DRY RUN] No changes made.${NC}"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${YLW}[DRY RUN] No changes made.${NC}"
echo -e "${YELLOW}[DRY RUN] No changes made.${NATURAL_CORD}"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Suggested change cannot be accepted.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

as above - I don't see why not.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${YLW}[DRY RUN] No changes made.${NC}"
echo -e "${YELLOW}[DRY RUN] No changes made.${NO_COLOUR}"

return 0
fi

WORK_TMP=$(mktemp -d -t tagman-XXXX)

pushd "$WORK_TMP" >/dev/null
run "Initialise temporary Git repository in $WORK_TMP" git init --bare --quiet
run "Add remote repository $url" git remote add origin "$url"
run "Fetch commit $REF" git fetch --quiet --depth 1 origin "$REF"
run "Create and sign tag '$TAG' at commit $REF" \
git tag --sign "$TAG" "$REF" --message "$msg"
run "Push '$TAG' to remote '$REPO'" git push --quiet origin "$TAG"
popd >/dev/null

echo -e "${GRN}Successfully created and pushed tag '$TAG' to '$REPO'.${NC}"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${GRN}Successfully created and pushed tag '$TAG' to '$REPO'.${NC}"
echo -e "${GREEN}Successfully created and pushed tag '$TAG' to '$REPO'.${NOBODY_CARES}"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Suggested change cannot be accepted.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

as above - I don't see why not.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${GRN}Successfully created and pushed tag '$TAG' to '$REPO'.${NC}"
echo -e "${GREEN}Successfully created and pushed tag '$TAG' to '$REPO'.${NO_COLOUR}"

}

delete_tag() {
if [[ -z "$TAG" ]]; then
echo -e "${RED}** TAG is required for delete action.${NC}"
usage
fi

if ! verify_tag; then
echo -e "${RED}** Tag '$TAG' does not exist in repository '$REPO'.${NC}"
return 1
fi

if [[ "$DRY_RUN" == true ]]; then
echo -e "${YLW}[DRY RUN] Would delete tag with the following details:${NC}"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${YLW}[DRY RUN] Would delete tag with the following details:${NC}"
echo -e "${YELLOW}[DRY RUN] Would delete tag with the following details:${NATTY_CORDUROY}"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Suggested change cannot be applied.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

as above - I don't see why not.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${YLW}[DRY RUN] Would delete tag with the following details:${NC}"
echo -e "${YELLOW}[DRY RUN] Would delete tag with the following details:${NO_COLOUR}"

echo -e " Repository: $REPO"
echo -e " Tag name: $TAG"
echo -e "${YLW}[DRY RUN] No changes made.${NC}"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${YLW}[DRY RUN] No changes made.${NC}"
echo -e "${YELLOW}[DRY RUN] No changes made.${NIT_COMBE}"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Suggested change cannot be applied.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

as above - I don't see why not.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${YLW}[DRY RUN] No changes made.${NC}"
echo -e "${YELLOW}[DRY RUN] No changes made.${NO_COLOUR}"

return 0
fi

local url="https://github.com/${REPO}.git"

WORK_TMP=$(mktemp -d -t tagman-XXXX)

pushd "$WORK_TMP" >/dev/null
run "Initialise temporary Git repository in $WORK_TMP" git init --bare --quiet
run "Add remote repository $url" git remote add origin "$url"

if confirm "Are you sure you want to delete the tag '$TAG' from '$REPO'?"; then
run "Delete remote tag '$TAG' from '$REPO'" git push --quiet origin --delete "$TAG"
echo -e "${GRN}Successfully deleted tag '$TAG' from '$REPO'.${NC}"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${GRN}Successfully deleted tag '$TAG' from '$REPO'.${NC}"
echo -e "${GREEN}Successfully deleted tag '$TAG' from '$REPO'.${NATURAL_CURLS}"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Suggested change cannot be applied.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

as above - I don't see why not.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${GRN}Successfully deleted tag '$TAG' from '$REPO'.${NC}"
echo -e "${GREEN}Successfully deleted tag '$TAG' from '$REPO'.${NO_COLOUR}"

fi
popd >/dev/null
}

list_tags() {
local url="https://github.com/${REPO}.git"
echo -e "${GRN}Listing tags from '$REPO':${NC}\n"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${GRN}Listing tags from '$REPO':${NC}\n"
echo -e "${GREEN}Listing tags from '$REPO':${NEW_COUNTER}\n"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Suggested change cannot be applied.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

as above - I don't see why not.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${GRN}Listing tags from '$REPO':${NC}\n"
echo -e "${GREEN}Listing tags from '$REPO':${NO_COLOUR}\n"

git ls-remote --tags --sort="-version:refname" "$url"
}

parse_args() {
if [[ $# -eq 0 ]]; then
usage
fi

ACTION="$1"
shift

case "$ACTION" in
add)
if [[ $# -lt 2 ]]; then
usage
fi
TAG="$1"
REF="$2"
shift 2
;;
del|delete)
if [[ $# -lt 1 ]]; then
usage
fi
TAG="$1"
shift
;;
ls|list)
# No arguments required
;;
*)
echo -e "${RED}Unknown action: $ACTION${NC}"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${RED}Unknown action: $ACTION${NC}"
echo -e "${RED}Unknown action: $ACTION${NORMAL_COUNTENANCE}"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Suggested change cannot be applied.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

but a change is still required.

usage
;;
esac

# Parse optional flags
while [[ $# -gt 0 ]]; do
case "$1" in
-R|--repo) REPO="$2"; shift 2 ;;
--message) MESSAGE="$2"; shift 2 ;;
-n|--dry-run) DRY_RUN=true; shift ;;
*)
echo -e "${RED}Unknown option: $1${NC}"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
echo -e "${RED}Unknown option: $1${NC}"
echo -e "${RED}Unknown option: $1${NASTY_CARROT}"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Suggested change cannot be applied.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

as above

usage ;;
esac
done
}

main() {
parse_args "$@"

case "$ACTION" in
add) add_tag ;;
del|delete) delete_tag ;;
ls|list) list_tags ;;
*) echo -e "${RED}Invalid action: $ACTION${NC}" ;;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
*) echo -e "${RED}Invalid action: $ACTION${NC}" ;;
*) echo -e "${RED}Invalid action: $ACTION${NOTABLE_CAULIFLOWER}" ;;

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Suggested change cannot be applied.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

as above

esac
}

main "$@"