-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworktree
More file actions
executable file
·77 lines (65 loc) · 2.32 KB
/
Copy pathworktree
File metadata and controls
executable file
·77 lines (65 loc) · 2.32 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
#!/usr/bin/env bash
set -euo pipefail
usage() {
cat <<'EOF'
Usage: ./worktree --check-main | --update-main
Checks whether this checkout contains the latest origin/main, or safely
fast-forwards a checkout that is directly behind it.
Options:
--check-main Fetch origin/main and warn when HEAD does not contain it.
Never changes HEAD or the working tree; a fetch failure warns
without blocking worktree setup.
--update-main Fetch origin/main and fast-forward only when HEAD is its
ancestor. Refuses divergent history.
-h, --help Show this help.
EOF
}
if [ "$#" -ne 1 ]; then
usage >&2
exit 1
fi
mode="$1"
main_ref="refs/remotes/origin/main"
fetch_main() {
git fetch --quiet origin '+refs/heads/main:refs/remotes/origin/main'
}
short_commit() {
git rev-parse --short=8 "$1"
}
case "$mode" in
--check-main)
if ! fetch_main; then
echo "warning: could not fetch origin/main; main freshness is unknown" >&2
exit 0
fi
if git merge-base --is-ancestor "$main_ref" HEAD; then
echo "==> Checkout contains latest origin/main ($(short_commit "$main_ref"))"
else
echo "warning: checkout $(short_commit HEAD) does not contain latest origin/main ($(short_commit "$main_ref"))" >&2
echo "warning: run the 'Update to latest main' action if this checkout should be a current main checkout" >&2
fi
;;
--update-main)
echo "==> Fetching origin/main"
fetch_main
if git merge-base --is-ancestor "$main_ref" HEAD; then
echo "==> Checkout already contains latest origin/main ($(short_commit "$main_ref"))"
exit 0
fi
if ! git merge-base --is-ancestor HEAD "$main_ref"; then
echo "error: checkout $(short_commit HEAD) has diverged from origin/main ($(short_commit "$main_ref")); refusing to rewrite or merge feature history" >&2
echo "error: merge or rebase origin/main explicitly instead" >&2
exit 1
fi
git merge --ff-only "$main_ref"
echo "==> Fast-forwarded checkout to latest origin/main ($(short_commit HEAD))"
;;
-h|--help)
usage
;;
*)
echo "error: unknown option '$mode'" >&2
usage >&2
exit 1
;;
esac