-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.sh
executable file
·86 lines (68 loc) · 2.66 KB
/
update.sh
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/bin/bash
set -ex
# Update default branch
function update_default_branch() {
default_branch=$(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')
current_branch=$(git branch --show-current)
# if on default branch or detached HEAD
if [ "$current_branch" = "$default_branch" ] || [ -z "$current_branch" ]; then
# We are on dev branch, fast-forward it
git fetch origin "${default_branch}"
git checkout "${default_branch}"
# if pull failed and we are in detached HEAD with local changes, abort
if ! git pull --ff-only origin "${default_branch}" \
&& [ -z "$current_branch" ] \
&& [ -n "$(git status --porcelain --ignore-submodules=dirty)" ]; then
# We get into detached head with git submodule update --init
echo
pwd
echo
git status --porcelain --ignore-submodules=dirty
echo
echo "!!! ERROR !!! Detached HEAD with local changes & failed to fast-forward, aborting."
echo "Tell Odd."
echo
exit 1
fi
else
# Fast-forward dev branch without switching to it
git fetch origin "${default_branch}:${default_branch}"
fi
}
# Set up (ssh) push access for submodules
function ensure_ssh_push_submodules() {
if test -f .git-blame-ignore-revs; then
git config --local blame.ignoreRevsFile .git-blame-ignore-revs
else
git config --local blame.ignoreRevsFile ""
fi
if test -f .gitmodules; then
# Initialize git submodules
git submodule init
IN=$(sed -Ezn 's!\[submodule\ "[^"]+"\].*?(\s*(path\s*=\s*([^\n]+)|url\s*=\s*([^\n]+))){2}!\3\t\4!gmp' .gitmodules)
while IFS= read -r line; do
if test -z "${line[@]}"; then
continue
fi
IFS=$'\t' read -ra LINE <<< "${line[@]}"
dir="${LINE[0]}"
url="${LINE[1]}"
newurl=$(sed -E 's!https?://github.com/[email protected]:!g' <<< "${url}")
pushd "${dir}" > /dev/null
git remote set-url --push origin "${newurl}"
update_default_branch
ensure_ssh_push_submodules
popd > /dev/null
done <<< "${IN[@]}"
fi
}
# Fetch updates
update_default_branch
# Initialize git submodules
git submodule init
ensure_ssh_push_submodules
# TODO: Set up pre-commit
# Issue URL: https://github.com/EventAccess/Meta/issues/18
# TODO: Prune merged branches
# Issue URL: https://github.com/EventAccess/Meta/issues/22
# git branch --merged | grep -Ev "(^\*|^\+|^\s*(master|main|dev)\s*$)" | xargs --no-run-if-empty git branch -d