forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-refresh-main.sh
More file actions
executable file
·88 lines (69 loc) · 2.56 KB
/
Copy pathsetup-refresh-main.sh
File metadata and controls
executable file
·88 lines (69 loc) · 2.56 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
83
84
85
86
87
88
#!/usr/bin/env bash
set -euo pipefail
bash "$(dirname "${BASH_SOURCE[0]}")/repair-git-primary-worktree.sh"
REMOTE="${SETUP_REMOTE:-origin}"
BRANCH="${SETUP_MAIN_BRANCH:-main}"
REMOTE_BRANCH="${REMOTE}/${BRANCH}"
LOCAL_REF="refs/heads/${BRANCH}"
REMOTE_REF="refs/remotes/${REMOTE}/${BRANCH}"
echo "Fetching ${REMOTE_BRANCH}..."
git fetch "$REMOTE" "$BRANCH"
if ! git rev-parse --verify "$REMOTE_REF" >/dev/null 2>&1; then
echo "FAIL: fetched ${REMOTE_BRANCH}, but ${REMOTE_REF} is unavailable." >&2
exit 1
fi
current_branch="$(git symbolic-ref --short -q HEAD || true)"
remote_oid="$(git rev-parse "$REMOTE_REF")"
sync_current_worktree_branch() {
if [ -z "$current_branch" ] || [ "$current_branch" = "$BRANCH" ]; then
return 0
fi
local head_oid
head_oid="$(git rev-parse HEAD)"
if [ "$head_oid" = "$remote_oid" ]; then
echo "Current branch ${current_branch} already matches ${REMOTE_BRANCH}."
return 0
fi
if git merge-base --is-ancestor "$head_oid" "$remote_oid"; then
echo "Fast-forwarding current branch ${current_branch} to ${REMOTE_BRANCH}..."
git merge --ff-only "$REMOTE_REF"
return 0
fi
echo "Current branch ${current_branch} is not a fast-forward behind ${REMOTE_BRANCH}; left unchanged." >&2
}
if ! git show-ref --verify --quiet "$LOCAL_REF"; then
git branch "$BRANCH" "$REMOTE_REF"
echo "Created local ${BRANCH} from ${REMOTE_BRANCH}."
sync_current_worktree_branch
exit 0
fi
local_oid="$(git rev-parse "$LOCAL_REF")"
if [ "$local_oid" = "$remote_oid" ]; then
echo "Local ${BRANCH} already matches ${REMOTE_BRANCH}."
sync_current_worktree_branch
exit 0
fi
if ! git merge-base --is-ancestor "$local_oid" "$remote_oid"; then
echo "Fetched ${REMOTE_BRANCH}; local ${BRANCH} is not a fast-forward, so it was left unchanged." >&2
sync_current_worktree_branch
exit 0
fi
if [ "$current_branch" = "$BRANCH" ]; then
echo "Fast-forwarding current ${BRANCH} branch..."
git pull --ff-only "$REMOTE" "$BRANCH"
exit 0
fi
branch_error_file="$(mktemp "${TMPDIR:-/tmp}/omi-setup-refresh-main.XXXXXX")"
trap 'rm -f "$branch_error_file"' EXIT
if git branch --force "$BRANCH" "$REMOTE_REF" >/dev/null 2>"$branch_error_file"; then
echo "Fast-forwarded local ${BRANCH} to ${REMOTE_BRANCH}."
else
branch_error="$(cat "$branch_error_file")"
if [[ "$branch_error" == *"checked out"* || "$branch_error" == *"used by worktree"* ]]; then
echo "Fetched ${REMOTE_BRANCH}; local ${BRANCH} is checked out in another worktree, so it was left unchanged." >&2
else
echo "$branch_error" >&2
exit 1
fi
fi
sync_current_worktree_branch