aboutsummaryrefslogtreecommitdiffstats
path: root/gitolite-cgit/auto-default-branch.sh
blob: d9c60a0572ee512263477ca998c97a7fcb1eb011 (plain)
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
#!/bin/sh

info() { printf >&2 %s\\n "$*" ; }

die()  { info "$*" ; exit 1 ; }

ensure_HEAD() {
   [ "$1" = 'POST_GIT' ] || die "unsupported trigger '$1'"

   cd "$GL_REPO_BASE/$2.git"

   # everything OK if the default in HEAD points to a real branch
   git show-ref --quiet --verify "$(git symbolic-ref HEAD)" && return 0

   # there *might* be a mismatch, so let's find out a real branch
   local head
   head="$(git show-ref --heads | head -1 | sed -e 's/^.* //')"

   # the repo might still be empty
   [ -n "$head" ] || return 0

   # we have a default branch that we can set here
   info "setting HEAD to <$head>"
   git symbolic-ref HEAD "$head" -m "Default HEAD to branch <$head>"
}

set -eu

ensure_HEAD "$@"