aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/is-branch.sh
blob: 7bba709c3b16a9244eb8f52af593d8b28c10851a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env bash
set -eou pipefail

source "$(dirname "${BASH_SOURCE[0]}")/lib/log.sh"

TARGET_BRANCH=""

main() {
  if [[ $# -ne 1 ]]; then
    echo "Usage: $0 <target-branch>" >&2
    exit 1
  fi
  TARGET_BRANCH="$1"
  local -r current_branch=$(get_current_branch)

  if [[ "$current_branch" != "$TARGET_BRANCH" ]]; then
    log_warn "Not on $TARGET_BRANCH branch (current branch is $current_branch)"
    exit 2
  fi
  log_info "On $TARGET_BRANCH branch"
}

get_current_branch() {
  # build.sr.ht automatically injects the GIT_REF environment variable
  # - GIT_REF: Git reference which triggered the build (e.g. refs/heads/master)
  #
  # See https://man.sr.ht/builds.sr.ht/
  local current_branch="${GIT_REF:-}"
  if [[ -z "$current_branch" ]]; then
    current_branch=$(git rev-parse --abbrev-ref HEAD)
  else
    # Strip refs/heads/ prefix if present
    # GIT_REF, if set, should be in the format: refs/heads/<branch-name>
    current_branch="${current_branch#refs/heads/}"
  fi
  echo "$current_branch"
}

main "$@"