11#! /bin/bash
22set -ex
33
4- # Fetch updates
5- git fetch origin dev
6- git checkout dev
7- git pull --ff-only
4+ # Update default branch
5+ function update_default_branch() {
6+ default_branch=$( git symbolic-ref refs/remotes/origin/HEAD | sed ' s@^refs/remotes/origin/@@' )
7+ current_branch=$( git branch --show-current)
8+
9+ # if on default branch or detached HEAD
10+ if [ " $current_branch " = " $default_branch " ] || [ -z " $current_branch " ]; then
11+ # We are on dev branch, fast-forward it
12+ git fetch origin " ${default_branch} "
13+ git checkout " ${default_branch} "
14+
15+ # if pull failed and we are in detached HEAD with local changes, abort
16+ if ! git pull --ff-only origin " ${default_branch} " \
17+ && [ -z " $current_branch " ] \
18+ && [ -n " $( git status --porcelain --ignore-submodules=dirty) " ]; then
19+ # Not sure exactly how this happened,
20+ # possibly due to failed dev:dev fetch in an earlier iteration.
21+ echo
22+ pwd
23+ echo
24+ git status --porcelain --ignore-submodules=dirty
25+ echo
26+ echo " !!! ERROR !!! Detached HEAD with local changes & failed to fast-forward, aborting."
27+ echo " Tell Odd."
28+ echo
29+ exit 1
30+ fi
31+ else
32+ # Fast-forward dev branch without switching to it
33+ git fetch origin " ${default_branch} :${default_branch} "
34+ fi
35+ }
836
9- # Initialize git submodules
10- git submodule update --init --recursive
1137
1238
1339# Set up (ssh) push access for submodules
@@ -23,17 +49,26 @@ function ensure_ssh_push_submodules() {
2349 newurl=$( sed -E ' s!https?://github.com/!git@github.com:!g' <<< " ${url}" )
2450 pushd " ${dir} " > /dev/null
2551 git remote set-url --push origin " ${newurl} "
26- default_branch=$( git symbolic-ref refs/remotes/origin/HEAD | sed ' s@^refs/remotes/origin/@@' )
27- git fetch origin " ${default_branch} "
28- git checkout " ${default_branch} "
29- git pull --ff-only
52+
53+ update_default_branch
54+
3055 ensure_ssh_push_submodules
3156 popd > /dev/null
3257 done <<< " ${IN[@]}"
3358 fi
3459}
3560
61+
62+ # Fetch updates
63+ update_default_branch
64+
65+ # Initialize git submodules
66+ git submodule update --init --recursive
67+
3668ensure_ssh_push_submodules
3769
3870# TODO: Set up pre-commit
3971# Issue URL: https://github.com/EventAccess/Meta/issues/18
72+
73+ # TODO: Prune merged branches
74+ # git branch --merged | grep -Ev "(^\*|^\+|^\s*(master|main|dev)\s*$)" | xargs --no-run-if-empty git branch -d
0 commit comments