-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpull
More file actions
executable file
·82 lines (73 loc) · 4.15 KB
/
Copy pathpull
File metadata and controls
executable file
·82 lines (73 loc) · 4.15 KB
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
#!/usr/bin/env bash
################################################################################
#
# Pulls the shared project configuration from the `config` repository and brings
# the config-managed submodules up to date.
#
# It updates `config` itself to the tip of `origin/master`, then floats every
# *config-managed* submodule — those declared with a tracked `branch` in
# `.gitmodules` (the shared `agents` submodule at `.agents/shared`, and any shared
# submodule added in the future) — to the tip of that branch, leaving each on the
# branch rather than in a detached `HEAD`.
#
# Submodules the consumer owns (a Hugo theme, a vendored library, ...) declare no
# tracked branch and are deliberately left untouched: this script never advances
# or resets them, and a failure floating one shared submodule is warned about, not
# fatal, so it can never take down the whole pull.
#
# The code of the script assumes that:
#
# 1. The project uses this code as a Git sub-module installed in the `config`
# directory under the project root.
#
# 2. The script is called from the root of the project, i.e. `./config/pull`.
#
################################################################################
# ---------------------------------------------------------------------------
# 1. Bring `config` itself to the tip of `origin/master`, on the `master`
# branch (not a detached `HEAD`). Pulling here also refreshes `migrate`,
# which is sourced from the updated `config` below.
# ---------------------------------------------------------------------------
echo "Updating 'config' to the latest 'master'"
( cd ./config && git checkout master && git pull --ff-only ) \
|| { echo "Error: failed to update the 'config' submodule."; exit 1; }
# ---------------------------------------------------------------------------
# 2. Float the config-managed submodules to the tip of their tracked branch.
# A submodule is "config-managed" when its `.gitmodules` entry declares a
# `branch` (set up by `adopt-shared-agents`); consumer-owned submodules have
# none and are skipped. `--ff-only` keeps each a clean descendant of its
# remote; any failure is warned about, never fatal.
#
# For each such submodule we also re-assert the active update strategy from
# `.gitmodules` into `.git/config`. `.gitmodules` is only a template: Git
# copies `update` into the active config once, at first `git submodule init`,
# and never again — so a clone initialized before `update = merge` was adopted
# stays on `checkout` forever, and a bare `git submodule update` would then
# detach the float back to the pinned commit. Re-syncing here makes the policy
# converge on every pull instead of only on fresh clones.
# ---------------------------------------------------------------------------
if [ -f .gitmodules ]; then
git config -f .gitmodules --get-regexp '^submodule\..*\.branch$' 2>/dev/null \
| while read -r key branch; do
name=${key#submodule.}; name=${name%.branch}
path=$(git config -f .gitmodules --get "submodule.$name.path") || continue
echo "Floating '$path' -> origin/$branch"
git submodule sync -- "$path" >/dev/null 2>&1 || true
git submodule update --init -- "$path" >/dev/null 2>&1 || true
# Converge the active update strategy with `.gitmodules` (see note above).
strategy=$(git config -f .gitmodules --get "submodule.$name.update" 2>/dev/null) \
&& git config "submodule.$name.update" "$strategy"
( cd "$path" \
&& git fetch --quiet origin "$branch" \
&& git checkout --quiet "$branch" \
&& git merge --quiet --ff-only "origin/$branch" ) \
|| echo " WARNING: could not float '$path' to origin/$branch; left as-is." >&2
done
fi
# ---------------------------------------------------------------------------
# 3. Apply the shared files. `migrate` runs with `config` as the working dir.
# ---------------------------------------------------------------------------
cd ./config || { echo "Error: 'config' directory is missing."; exit 1; }
# Handle file operations related to the migration from the previous version(s).
source migrate
echo "Done."