Skip to content
Merged
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,19 @@ if hasRemoteTag "v0.1.0"; then
elif hasRemoteTag "v0.1.0" "anotherRemote"; then
echo "do whatever you want to do..."
fi

echo "all existing tags on remote origin, starting from smallest to biggest version number"
remoteTagsSorted

# if you specify the name of the remote, then all additional arguments are passed to `sort` which is used internally
echo "all existing tags on remote upstream, starting from smallest to biggest version number"
remoteTagsSorted upstream -r

declare latestTag
latestTag=$(latestRemoteTag)
echo "latest tag on origin: $latestTag"
latestTag=$(latestRemoteTag upstream)
echo "latest tag on upstream: $latestTag"
```

</utility-git-utils>
Expand Down
13 changes: 13 additions & 0 deletions src/utility/git-utils.doc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,16 @@ if hasRemoteTag "v0.1.0"; then
elif hasRemoteTag "v0.1.0" "anotherRemote"; then
echo "do whatever you want to do..."
fi

echo "all existing tags on remote origin, starting from smallest to biggest version number"
remoteTagsSorted

# if you specify the name of the remote, then all additional arguments are passed to `sort` which is used internally
echo "all existing tags on remote upstream, starting from smallest to biggest version number"
remoteTagsSorted upstream -r

declare latestTag
latestTag=$(latestRemoteTag)
echo "latest tag on origin: $latestTag"
latestTag=$(latestRemoteTag upstream)
echo "latest tag on upstream: $latestTag"
52 changes: 42 additions & 10 deletions src/utility/git-utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@
# echo "do whatever you want to do..."
# fi
#
# echo "all existing tags on remote origin, starting from smallest to biggest version number"
# remoteTagsSorted
#
# # if you specify the name of the remote, then all additional arguments are passed to `sort` which is used internally
# echo "all existing tags on remote upstream, starting from smallest to biggest version number"
# remoteTagsSorted upstream -r
#
# declare latestTag
# latestTag=$(latestRemoteTag)
# echo "latest tag on origin: $latestTag"
# latestTag=$(latestRemoteTag upstream)
# echo "latest tag on upstream: $latestTag"
#
###################################
set -euo pipefail
shopt -s inherit_errexit
Expand Down Expand Up @@ -89,11 +102,11 @@ function countCommits() {
}

function localGitIsAhead() {
if ! (($# == 0)) && ! (($# == 1)); then
traceAndDie "you need to pass at least the branch name to localGitIsAhead and optionally the name of the remote (defaults to origin)"
if ! (($# == 1)) && ! (($# == 2)); then
traceAndDie "you need to pass at least the branch name to localGitIsAhead and optionally the name of the remote (defaults to origin) but not more, given: %s" "$#"
fi
local -r branch=$1
local -r remote=${2-"origin"}
local -r remote=${2:-"origin"}
local -i count
# we know that set -e is disabled for countCommits, that OK
# shellcheck disable=SC2310
Expand All @@ -102,11 +115,11 @@ function localGitIsAhead() {
}

function localGitIsBehind() {
if ! (($# == 0)) && ! (($# == 1)); then
traceAndDie "you need to pass at least the branch name to localGitIsBehind and optionally the name of the remote (defaults to origin)"
if ! (($# == 1)) && ! (($# == 2)); then
traceAndDie "you need to pass at least the branch name to localGitIsBehind and optionally the name of the remote (defaults to origin) but not more, given: %s" "$#"
fi
local -r branch=$1
local -r remote=${2-"origin"}
local -r remote=${2:-"origin"}
local -i count
# we know that set -e is disabled for countCommits, that OK
# shellcheck disable=SC2310
Expand All @@ -115,13 +128,32 @@ function localGitIsBehind() {
}

function hasRemoteTag() {
if ! (($# == 0)) && ! (($# == 1)); then
traceAndDie "you need to pass at least the tag to hasRemoteTag and optionally the name of the remote (defaults to origin)"
if ! (($# == 1)) && ! (($# == 2)); then
traceAndDie "you need to pass at least the tag to hasRemoteTag and optionally the name of the remote (defaults to origin) but not more, given: %s" "$#"
fi
local -r tag=$1
local -r remote=${2-"origin"}
local -r remote=${2:-"origin"}
shift 1 || die "could not shift by 1"
local output
output=$(git ls-remote -t "$remote") || die "the following command failed (see above): git ls-remote -t \"$remote\""
grep "$tag" >/dev/null <<<"$output" || false
grep "$tag" >/dev/null <<<"$output"
}

function remoteTagsSorted() {
local repo="origin"
if (($# > 1)); then
repo=$1
shift || die "could not shift by 1"
fi
git ls-remote --refs --tags "$repo" |
cut --delimiter='/' --fields=3 |
sort --version-sort "$@"
}

function latestRemoteTag() {
if (($# > 1)); then
traceAndDie "you can optionally pass the name of the remote (defaults to origin) to latestRemoteTag but not more, given: %s" "$#"
fi
local repo=${1:-"origin"}
remoteTagsSorted "$repo" | tail --lines=1
}