Repository navigation
Expand file tree
/
Copy pathworktree
More file actions
executable file
·136 lines (121 loc) · 4.42 KB
/
Copy pathworktree
File metadata and controls
executable file
·136 lines (121 loc) · 4.42 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env bash
set -euo pipefail
usage() {
cat <<'EOF'
Usage: ./worktree <mode> [options]
Checks whether this checkout contains the latest origin/main, or safely
fast-forwards a checkout that is directly behind it. Also removes build
caches when a worktree is deleted.
Modes:
--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.
--cleanup Remove this checkout's Xcode DerivedData and simulator.
--prune-derived-data
Remove DerivedData for deleted Codex worktrees of this repo,
including the older ~/.codex-workspaces/worktrees location.
Cleanup options:
--dry-run Report the cleanup actions without changing anything.
--derived-data-root PATH
Inspect this directory instead of Xcode's default DerivedData.
Other options:
-h, --help Show this help.
Ownership comes from info.plist, never from the cache directory name alone.
Caches without ownership metadata and shared Xcode caches are retained.
EOF
}
if [ "$#" -eq 0 ]; then
usage >&2
exit 1
fi
mode="$1"
shift
dry_run=false
cleanup_args=("$mode")
case "$mode" in
--cleanup|--prune-derived-data)
while [ "$#" -gt 0 ]; do
case "$1" in
--dry-run) dry_run=true; cleanup_args+=(--dry-run); shift ;;
--derived-data-root)
if [ "$#" -lt 2 ] || [ -z "$2" ] || [[ "$2" == --* ]]; then
echo "error: --derived-data-root requires a path" >&2
exit 1
fi
case "$2" in
/*|\~*) cleanup_args+=(--derived-data-root "$2") ;;
*) cleanup_args+=(--derived-data-root "$PWD/$2") ;;
esac
shift 2
;;
*) echo "error: unknown cleanup option '$1'" >&2; exit 1 ;;
esac
done
;;
*)
if [ "$#" -ne 0 ]; then
usage >&2
exit 1
fi
;;
esac
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
--cleanup|--prune-derived-data)
# Resolve resources from the wrapper, even when invoked outside the checkout.
cd "$(dirname "$0")"
status=0
python3 -B -m Tools.worktree_cleanup "${cleanup_args[@]}" || status=$?
if [ "$mode" = --cleanup ]; then
if [ "$dry_run" = true ]; then
echo "Would run ./simulator --delete in $(pwd -P)" >&2
else
./simulator --delete || status=$?
fi
fi
exit "$status"
;;
--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