Repository navigation
Expand file tree
/
Copy pathupdate-opencode-commit.sh
More file actions
executable file
·137 lines (116 loc) · 4.79 KB
/
Copy pathupdate-opencode-commit.sh
File metadata and controls
executable file
·137 lines (116 loc) · 4.79 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
137
#!/usr/bin/env bash
set -euo pipefail
root_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
dockerfile="${root_dir}/packages/core/src/docker/Dockerfile"
submodule_dir="${root_dir}/packages/opencode"
gitmodules="${root_dir}/.gitmodules"
set_output() {
local key="$1"
local value="$2"
if [[ -n "${GITHUB_OUTPUT:-}" ]]; then
echo "${key}=${value}" >> "${GITHUB_OUTPUT}"
fi
}
normalize_repo_url() {
local input="$1"
local normalized="$input"
normalized="https://github.com/${normalized#git@github.com:}"
normalized="https://github.com/${normalized#ssh://git@github.com/}"
elif [[ "${normalized}" == git://github.com/* ]]; then
normalized="https://github.com/${normalized#git://github.com/}"
elif [[ "${normalized}" == http://github.com/* ]]; then
normalized="https://github.com/${normalized#http://github.com/}"
fi
normalized="${normalized%.git}"
normalized="${normalized%/}"
echo "${normalized}"
}
if [[ ! -f "${dockerfile}" ]]; then
echo "Dockerfile not found at ${dockerfile}" >&2
exit 1
fi
if [[ ! -d "${submodule_dir}" || (! -f "${submodule_dir}/.git" && ! -d "${submodule_dir}/.git") ]]; then
echo "Submodule is not initialized at ${submodule_dir}." >&2
echo "Run: git submodule update --init --recursive" >&2
exit 1
fi
dirty_state="$(git -C "${submodule_dir}" status --porcelain)"
if [[ -n "${dirty_state}" ]]; then
echo "Submodule ${submodule_dir} has uncommitted changes; clean or stash first." >&2
echo "${dirty_state}" >&2
exit 1
fi
current_submodule_commit="$(git -C "${submodule_dir}" rev-parse HEAD)"
submodule_branch="dev"
if [[ -f "${gitmodules}" ]]; then
configured_branch="$(git config -f "${gitmodules}" --get submodule.packages/opencode.branch 2>/dev/null || true)"
if [[ -n "${configured_branch}" ]]; then
submodule_branch="${configured_branch}"
fi
fi
set_output "submodule_branch" "${submodule_branch}"
submodule_repo_url_raw="$(git -C "${submodule_dir}" remote get-url origin 2>/dev/null || true)"
if [[ -z "${submodule_repo_url_raw}" && -f "${gitmodules}" ]]; then
submodule_repo_url_raw="$(git config -f "${gitmodules}" --get submodule.packages/opencode.url 2>/dev/null || true)"
fi
submodule_repo_url="$(normalize_repo_url "${submodule_repo_url_raw}")"
set_output "submodule_repo_url" "${submodule_repo_url}"
set_output "current_submodule_commit" "${current_submodule_commit}"
# In GitHub Actions, submodule URLs may be SSH-style from .gitmodules
# ([email protected]:...), but runners usually do not have an SSH key.
# Rewrite those URLs to HTTPS for this fetch so CI can read the repo.
git \
-C "${submodule_dir}" \
-c url."https://github.com/"[email protected]: \
-c url."https://github.com/".insteadOf=ssh://[email protected]/ \
fetch --prune origin "${submodule_branch}"
latest_commit="$(git -C "${submodule_dir}" rev-parse FETCH_HEAD)"
if [[ -z "${latest_commit}" ]]; then
echo "Failed to resolve latest commit for branch ${submodule_branch}." >&2
exit 1
fi
set_output "latest_commit" "${latest_commit}"
current_pin="$(grep -oE 'OPENCODE_COMMIT="[^\"]+"' "${dockerfile}" | head -n1 || true)"
if [[ -z "${current_pin}" ]]; then
echo "Failed to find OPENCODE_COMMIT in ${dockerfile}." >&2
exit 1
fi
current_pin_value="${current_pin#OPENCODE_COMMIT=\"}"
current_pin_value="${current_pin_value%\"}"
set_output "current_pin_value" "${current_pin_value}"
needs_update="false"
if [[ "${latest_commit}" != "${current_submodule_commit}" ]]; then
needs_update="true"
fi
if [[ "${latest_commit}" != "${current_pin_value}" ]]; then
needs_update="true"
fi
set_output "needs_update" "${needs_update}"
if [[ "${needs_update}" == "false" ]]; then
set_output "updated" "false"
set_output "updated_pin_value" "${current_pin_value}"
echo "No update needed; opencode is already at the latest commit."
echo " Branch: ${submodule_branch}"
echo " Commit: ${latest_commit}"
echo " Submodule: ${submodule_dir}"
echo " Dockerfile: ${dockerfile}"
exit 0
fi
git -C "${submodule_dir}" checkout --detach "${latest_commit}"
perl -0pi -e "s/OPENCODE_COMMIT=\"[^\"]+\"/OPENCODE_COMMIT=\"${latest_commit}\"/" "${dockerfile}"
expected_pin="OPENCODE_COMMIT=\"${latest_commit}\""
updated_pin="$(grep -oE 'OPENCODE_COMMIT="[^\"]+"' "${dockerfile}" | head -n1 || true)"
if [[ "${updated_pin}" != "${expected_pin}" ]]; then
echo "Failed to update OPENCODE_COMMIT in ${dockerfile}." >&2
exit 1
fi
set_output "updated" "true"
set_output "updated_pin_value" "${latest_commit}"
echo "Updated opencode references."
echo " Branch: ${submodule_branch}"
echo " Submodule: ${current_submodule_commit} -> ${latest_commit}"
echo " Dockerfile pin: ${current_pin_value} -> ${latest_commit}"
echo " Submodule: ${submodule_dir}"
echo " Dockerfile: ${dockerfile}"