Files
CyberStrikeAI/internal/update/apply.go
T
Sycun b645914e1f fix: a rollback keeps a recreated role, and an owed build is finished first
Three ways the installation could still end up with its source, its binary and
the operator's content describing different things.

Rollback copied aside only *tracked* protected files, so a role upstream had
deleted and the operator had recreated at the same path was untracked - and
`git reset --hard` wrote the old commit's copy over it. The stash now also
covers untracked protected paths the target commit writes to, and names them in
keptContent like any other file it saved.

The pending-build marker said only "a compile is owed". An update that found
new commits therefore merged them and built the *new* tree, leaving `.prev`
holding a binary for a commit the state file no longer named - a rollback then
paired old source with the original binary. And a marker left behind after a
successful swap rebuilt and overwrote that same `.prev`. The marker now names
its target commit and the hashes on both sides of the swap, the state records
which commit the live binary came from, an owed build is finished before the
tree moves again, and a retry that finds the swap already done clears the
marker instead of repeating it. Rollback verifies that pairing before touching
anything and refuses (binary_changed) rather than reset the source beside a
binary it cannot account for; an update whose build failed never swapped one,
so its rollback moves the source only and no longer demands a `.prev`.

The version pair is recorded before the config is written, so a crash in
between still leaves a rollback that can put the old number back.
2026-10-08 22:46:17 +08:00

913 lines
35 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package update
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"time"
)
// stateFile records what the last update moved, so a rollback has a target that is not
// guessed at. It lives in the install tree and is deliberately not in git: it describes
// this machine's installation.
const stateFile = ".update-state.json"
// State is the rollback contract of one installation.
type State struct {
PreviousCommit string `json:"previous_commit"`
UpdatedCommit string `json:"updated_commit"`
BackupDir string `json:"backup_dir"`
UpdatedAt string `json:"updated_at"`
// BinaryCommit says which source commit produced the live binary. Before a build it is
// PreviousCommit; after a successful swap it is UpdatedCommit. Old state files omit it
// and keep the legacy assumption that their .prev binary must be restored.
BinaryCommit string `json:"binary_commit,omitempty"`
BinarySHA256 string `json:"binary_sha256,omitempty"`
PreviousBinarySHA256 string `json:"previous_binary_sha256,omitempty"`
// VersionBefore/VersionAfter record a version-line change this update made, so a
// rollback can put the old number back next to the old code. Absent for updates that
// touched no version (old state files simply have no such fields).
VersionBefore string `json:"version_before,omitempty"`
VersionAfter string `json:"version_after,omitempty"`
}
func statePath(root string) string { return filepath.Join(root, stateFile) }
// buildPendingFile records "the source has moved (or was moved), but the binary has not
// followed": a failed build, or a machine with no Go toolchain. It is written the moment
// the tree moves and removed only once the new binary is in place, so a retry on an
// already-current tree still knows there is a compile to finish. A file of its own rather
// than a State field because an adoption owes a build too, and an adoption has no state
// (there is no previous commit to roll back to).
const buildPendingFile = ".update-build-pending"
type buildPendingState struct {
Commit string `json:"commit"`
PreviousCommit string `json:"previous_commit,omitempty"`
BackupDir string `json:"backup_dir,omitempty"`
KeptContent []string `json:"kept_content,omitempty"`
VersionBefore string `json:"version_before,omitempty"`
VersionAfter string `json:"version_after,omitempty"`
BinarySHA256 string `json:"binary_sha256,omitempty"`
PreviousBinarySHA256 string `json:"previous_binary_sha256,omitempty"`
Legacy bool `json:"-"`
}
func buildPendingPath(root string) string { return filepath.Join(root, buildPendingFile) }
func readBuildPending(root string) (buildPendingState, bool, error) {
var st buildPendingState
data, err := os.ReadFile(buildPendingPath(root))
if os.IsNotExist(err) {
return st, false, nil
}
if err != nil {
return st, true, err
}
if err := json.Unmarshal(data, &st); err != nil {
if strings.TrimSpace(string(data)) == "source is ahead of the binary" {
return buildPendingState{Legacy: true}, true, nil
}
return buildPendingState{}, true, fmt.Errorf("待编译状态格式损坏: %w", err)
}
return st, true, nil
}
func writeBuildPending(root string, st buildPendingState) error {
data, err := json.Marshal(st)
if err != nil {
return err
}
tmp := buildPendingPath(root) + ".tmp"
if err := os.WriteFile(tmp, append(data, '\n'), 0o644); err != nil {
return err
}
return os.Rename(tmp, buildPendingPath(root))
}
func clearBuildPending(root string) error {
if err := os.Remove(buildPendingPath(root)); err != nil && !os.IsNotExist(err) {
return err
}
return nil
}
func buildPending(root string) bool { return fileExists(buildPendingPath(root)) }
func readState(root string) (State, bool) {
var st State
data, err := os.ReadFile(statePath(root))
if err != nil {
return st, false
}
if err := json.Unmarshal(data, &st); err != nil || st.PreviousCommit == "" || st.UpdatedCommit == "" {
return st, false
}
return st, true
}
func writeState(root string, st State) error {
st.UpdatedAt = time.Now().Format(time.RFC3339)
data, err := json.MarshalIndent(st, "", " ")
if err != nil {
return err
}
tmp := statePath(root) + ".tmp"
if err := os.WriteFile(tmp, append(data, '\n'), 0o644); err != nil {
return err
}
return os.Rename(tmp, statePath(root))
}
// Step is one progress line. A build takes minutes, so Apply reports where it is rather
// than appearing to hang; the console renders these directly.
type Step struct {
Phase string `json:"phase"`
Message string `json:"message"`
At string `json:"at"`
}
// Result is what one update attempt produced. Everything the operator would otherwise
// have to go and inspect by hand is in here.
type Result struct {
FromCommit string `json:"fromCommit"`
ToCommit string `json:"toCommit"`
Commits int `json:"commits"`
FilesTouched int `json:"filesTouched"`
// KeptContent names the operator-owned files that were restored over the update.
// Upstream may well have changed them; saying which ones is the difference between
// "my roles disappeared" and "my roles are still mine, and these upstream files did
// not land".
KeptContent []string `json:"keptContent"`
// Overwritten names local files whose content the target repository's version replaced
// during an adoption; copies are kept under <BackupDir>/overwritten/ so nothing is lost
// without a way back.
Overwritten []string `json:"overwritten,omitempty"`
// Adopted marks a result that comes from connecting a plain directory to a source
// rather than from moving an existing work tree.
Adopted bool `json:"adopted,omitempty"`
BinaryPath string `json:"binaryPath"`
BinaryBuilt bool `json:"binaryBuilt"`
PrevBinary string `json:"prevBinary"`
BackupDir string `json:"backupDir"`
NeedsRestart bool `json:"needsRestart"`
Duration string `json:"duration"`
}
// Error is a refusal with the information needed to act on it. Callers render the
// Message; Details is for the page to list file names or commits.
type Error struct {
Reason string `json:"reason"`
Message string `json:"message"`
Items []string `json:"items,omitempty"`
}
func (e *Error) Error() string { return e.Message }
// Apply carries an installation to the tip of the branch it tracks, then rebuilds and
// swaps the binary. It is all-or-nothing up to the point of the working-tree move: a
// failed build leaves the tree updated (source and previous binary are both recoverable),
// while a refused precondition leaves nothing touched at all.
func Apply(ctx context.Context, opts Options, onStep func(Step)) (*Result, error) {
if onStep == nil {
onStep = func(Step) {}
}
step := func(phase, msg string) { onStep(Step{Phase: phase, Message: msg, At: time.Now().Format(time.RFC3339)}) }
snap, err := Check(ctx, opts)
if err != nil {
return nil, err
}
start := time.Now()
// A refusal is not the end of the timeline: the page renders these lines, so the first
// one says what was looked at even when the answer is "nothing to do".
step("preflight", fmt.Sprintf("检查安装目录 %s(本机分支 %s / 更新源 %s / 提交 %s)", snap.Root, snap.Branch, snap.sourceLabel(), snap.Commit))
if !snap.Installed {
return nil, &Error{Reason: "not_a_repo", Message: "这个目录不是 git 工作树,无法自动更新"}
}
if snap.CheckError != "" {
return nil, &Error{Reason: "check_failed", Message: "检查更新失败:" + snap.CheckError}
}
if len(snap.BlockingChanges) > 0 {
items := make([]string, 0, len(snap.BlockingChanges))
for _, c := range snap.BlockingChanges {
items = append(items, fmt.Sprintf("%s (%s)", c.Path, c.Status))
}
return nil, &Error{
Reason: "local_source_edits",
Message: fmt.Sprintf("有 %d 个源码文件被本地改过,自动更新不会覆盖它们。先提交或还原这些文件再更新。", len(items)),
Items: items,
}
}
if snap.Diverged {
return nil, &Error{
Reason: "diverged",
Message: fmt.Sprintf("本地领先 %d 个提交、落后 %d 个提交:这是合并而不是下载,请手工处理后重试。", snap.Ahead, snap.Behind),
}
}
res := &Result{
FromCommit: snap.Commit, ToCommit: snap.Commit,
BinaryPath: filepath.Join(snap.Root, opts.binaryName()),
}
if buildPending(snap.Root) && opts.binaryName() != "none" {
if st, ok := readState(snap.Root); ok {
res.BackupDir = st.BackupDir
}
if err := finishPendingBuild(ctx, snap, opts, res, step); err != nil {
res.Duration = time.Since(start).Round(time.Millisecond).String()
return res, err
}
}
if !snap.UpdateAvailable {
// Nothing to move - but the installation's own bookkeeping still gets aligned: a
// version left over from a tree that was updated before this behaviour existed (or
// from a hand edit) is corrected here instead of waiting for the next commit.
versionBefore, versionAfter, versionErr := syncVersionToConfig(snap.Root)
versionStep(step, versionBefore, versionAfter, versionErr)
res.Duration = time.Since(start).Round(time.Millisecond).String()
return res, nil
}
step("fetch", fmt.Sprintf("%s(默认分支 %s)有 %d 个新提交:%s → %s", snap.sourceLabel(), snap.TargetBranch, snap.Behind, snap.Commit, snap.RemoteCommit))
// Put operator-owned content aside, and clear the paths the incoming change would
// write to, so the fast-forward cannot be refused by "your local changes would be
// overwritten". Nothing here deletes a file the operator made: every path is copied
// out first and copied back after.
root := snap.Root
ref := snap.sourceRef()
backupDir, kept, err := stashProtected(ctx, root, ref)
if err != nil {
return nil, err
}
step("protect", fmt.Sprintf("已暂存 %d 个本地内容文件(roles/skills/tools/数据/配置),更新完成后放回", len(kept)))
previousCommit, err := gitCmd(ctx, root, "rev-parse", "HEAD")
if err != nil {
restoreProtected(backupDir, kept)
return nil, err
}
updatedCommit, err := gitCmd(ctx, root, "rev-parse", ref)
if err != nil {
restoreProtected(backupDir, kept)
return nil, err
}
bin := filepath.Join(root, opts.binaryName())
pending := buildPendingState{
Commit: updatedCommit,
PreviousCommit: previousCommit,
BackupDir: backupDir,
KeptContent: kept,
}
if opts.binaryName() != "none" && fileExists(bin) {
pending.PreviousBinarySHA256, err = fileSHA256(bin)
if err != nil {
restoreProtected(backupDir, kept)
return nil, err
}
}
if opts.binaryName() != "none" {
if err := writeBuildPending(root, pending); err != nil {
return nil, &Error{Reason: "state_unwritable", Message: "无法写入待编译状态:" + err.Error()}
}
}
if err := removeProtected(root, kept); err != nil {
if restoreErr := restoreProtected(backupDir, kept); restoreErr != nil {
return nil, &Error{Reason: "backup_failed", Message: fmt.Sprintf("%v;恢复本地内容又失败:%v", err, restoreErr)}
}
_ = clearBuildPending(root)
return nil, err
}
if out, err := gitCmd(ctx, root, "merge", "--ff-only", "--no-verify", ref); err != nil {
if restoreErr := restoreProtected(backupDir, kept); restoreErr != nil {
return nil, &Error{Reason: "backup_failed", Message: fmt.Sprintf("快进合并失败,恢复本地内容又失败:%v", restoreErr)}
}
_ = clearBuildPending(root)
return nil, &Error{Reason: "merge_failed", Message: fmt.Sprintf("快进合并失败:%v\n%s", err, strings.TrimSpace(out))}
}
// The merge wrote upstream's version of the content paths; the operator's copies win
// them back, which is what "update the code, keep my work" means.
if err := restoreProtected(backupDir, kept); err != nil {
return nil, err
}
files, _ := gitRaw(ctx, root, "diff", "--name-only", "-z", previousCommit+".."+updatedCommit)
res.FromCommit = snap.Commit
res.ToCommit = snap.RemoteCommit
res.Commits = snap.Behind
res.KeptContent = kept
res.BackupDir = backupDir
res.FilesTouched = len(nulList(files))
res.BinaryPath = bin
// The new code carries its own release version; the live config (which no merge ever
// touches) gets it now, so the page header and the static cache buster follow the code.
// The pair is recorded in the marker first: a crash during the write below would
// otherwise leave the new number on disk with nothing saying what it replaced.
pending.VersionBefore, pending.VersionAfter = plannedVersionChange(root)
if opts.binaryName() != "none" {
if err := writeBuildPending(root, pending); err != nil {
return res, &Error{Reason: "state_unwritable", Message: "无法更新待编译状态:" + err.Error()}
}
}
versionBefore, versionAfter, versionErr := syncVersionToConfig(root)
versionStep(step, versionBefore, versionAfter, versionErr)
state := State{
PreviousCommit: previousCommit,
UpdatedCommit: updatedCommit,
BackupDir: backupDir,
VersionBefore: versionBefore,
VersionAfter: versionAfter,
}
if opts.binaryName() != "none" {
state.BinaryCommit = previousCommit
state.BinarySHA256 = pending.PreviousBinarySHA256
state.PreviousBinarySHA256 = pending.PreviousBinarySHA256
}
if err := writeState(root, state); err != nil {
return res, &Error{Reason: "state_unwritable", Message: "无法写入更新状态文件,回滚将不可用:" + err.Error()}
}
if opts.binaryName() == "none" {
step("build", "按请求跳过重新编译;源码已更新,重启后由外部构建流程产出二进制")
res.NeedsRestart = true
res.Duration = time.Since(start).Round(time.Millisecond).String()
return res, nil
}
if !snap.CanBuild {
res.NeedsRestart = true
res.Duration = time.Since(start).Round(time.Millisecond).String()
return res, &Error{
Reason: "no_toolchain",
Message: "源码已更新,但本机没有 Go 工具链,无法自动编译。装好 go 后再点一次更新即可补上二进制。",
}
}
if err := buildAndSwap(ctx, root, opts, res, step, updatedCommit, &state, pending); err != nil {
res.Duration = time.Since(start).Round(time.Millisecond).String()
return res, err
}
step("done", fmt.Sprintf("已更新 %d 个提交并换好二进制:%s → %s", res.Commits, res.FromCommit, res.ToCommit))
res.Duration = time.Since(start).Round(time.Millisecond).String()
return res, nil
}
// finishPendingBuild compiles the binary a previous update left missing. The marker carries
// the target commit and hashes around the swap, so a retry can distinguish "still owed"
// from "the swap finished and only marker cleanup failed" without overwriting .prev.
func finishPendingBuild(ctx context.Context, snap *Snapshot, opts Options, res *Result, step func(string, string)) error {
pending, present, err := readBuildPending(snap.Root)
if err != nil {
return &Error{Reason: "state_unreadable", Message: "无法读取待编译状态:" + err.Error()}
}
if !present {
return nil
}
head, err := gitCmd(ctx, snap.Root, "rev-parse", "HEAD")
if err != nil {
return err
}
bin := filepath.Join(snap.Root, opts.binaryName())
state, hasState := readState(snap.Root)
if pending.Legacy && hasState {
updated, resolveErr := gitCmd(ctx, snap.Root, "rev-parse", state.UpdatedCommit)
if resolveErr != nil {
return &Error{Reason: "bad_state", Message: "旧待编译标记对应的更新提交不存在:" + state.UpdatedCommit}
}
if updated != head {
return &Error{Reason: "build_state_mismatch", Message: fmt.Sprintf("旧待编译标记属于 %s,但当前源码已经是 %s,拒绝重解释回滚记录", shortHash(updated), shortHash(head))}
}
pending.Commit = updated
pending.PreviousCommit = state.PreviousCommit
pending.BackupDir = state.BackupDir
pending.VersionBefore = state.VersionBefore
pending.VersionAfter = state.VersionAfter
}
if pending.Commit == "" {
if hasState && state.BinaryCommit != "" && state.BinarySHA256 != "" {
binaryCommit, commitErr := gitCmd(ctx, snap.Root, "rev-parse", state.BinaryCommit)
binaryHash, hashErr := fileSHA256(bin)
if commitErr == nil && hashErr == nil && binaryCommit == head && binaryHash == state.BinarySHA256 {
if err := clearBuildPending(snap.Root); err != nil {
return &Error{Reason: "state_unwritable", Message: "二进制已经是当前版本,但无法清除待编译标记:" + err.Error()}
}
res.BinaryBuilt = true
if fileExists(bin + ".prev") {
res.PrevBinary = bin + ".prev"
}
res.NeedsRestart = true
return nil
}
}
if revision := binaryRevision(bin); revision != "" {
resolved, resolveErr := gitCmd(ctx, snap.Root, "rev-parse", revision)
if resolveErr == nil && resolved == head {
if err := clearBuildPending(snap.Root); err != nil {
return &Error{Reason: "state_unwritable", Message: "二进制已经是当前版本,但无法清除旧待编译标记:" + err.Error()}
}
res.BinaryBuilt = true
if fileExists(bin + ".prev") {
res.PrevBinary = bin + ".prev"
}
res.NeedsRestart = true
return nil
}
}
pending.Commit = head
}
target, err := gitCmd(ctx, snap.Root, "rev-parse", pending.Commit)
if err != nil {
return &Error{Reason: "bad_state", Message: "待编译状态里的提交不存在:" + pending.Commit}
}
if target != head {
if pending.PreviousCommit != "" {
previous, previousErr := gitCmd(ctx, snap.Root, "rev-parse", pending.PreviousCommit)
if previousErr == nil && previous == head {
if err := restoreInterruptedProtected(ctx, snap.Root, pending, false); err != nil {
return err
}
if err := clearBuildPending(snap.Root); err != nil {
return &Error{Reason: "state_unwritable", Message: "本地内容已恢复,但无法清除待编译标记:" + err.Error()}
}
return nil
}
}
return &Error{Reason: "build_state_mismatch", Message: fmt.Sprintf("待编译目标是 %s,但当前源码是 %s,拒绝用错版本覆盖二进制", shortHash(target), shortHash(head))}
}
if err := restoreInterruptedProtected(ctx, snap.Root, pending, true); err != nil {
return err
}
if len(pending.KeptContent) > 0 {
res.KeptContent = append([]string(nil), pending.KeptContent...)
res.BackupDir = pending.BackupDir
}
if pending.BinarySHA256 != "" && fileExists(bin) {
currentHash, hashErr := fileSHA256(bin)
if hashErr == nil && currentHash == pending.BinarySHA256 {
if pending.PreviousCommit != "" {
state = State{
PreviousCommit: pending.PreviousCommit,
UpdatedCommit: target,
BackupDir: pending.BackupDir,
BinaryCommit: target,
BinarySHA256: currentHash,
PreviousBinarySHA256: pending.PreviousBinarySHA256,
VersionBefore: pending.VersionBefore,
VersionAfter: pending.VersionAfter,
}
if err := writeState(snap.Root, state); err != nil {
return &Error{Reason: "state_unwritable", Message: "二进制已换入,但无法补写回滚状态:" + err.Error()}
}
}
if err := clearBuildPending(snap.Root); err != nil {
return &Error{Reason: "state_unwritable", Message: "二进制已换入,但无法清除待编译标记:" + err.Error()}
}
res.BinaryBuilt = true
if fileExists(bin + ".prev") {
res.PrevBinary = bin + ".prev"
}
res.NeedsRestart = true
return nil
}
}
if !snap.CanBuild {
return &Error{
Reason: "no_toolchain",
Message: fmt.Sprintf("源码已经就位(%s),但二进制还是旧的:本机没有 Go 工具链,无法补编译。装好 go 后再点一次更新即可补上二进制。", shortHash(head)),
}
}
var targetState *State
if pending.PreviousCommit != "" {
state = State{
PreviousCommit: pending.PreviousCommit,
UpdatedCommit: target,
BackupDir: pending.BackupDir,
BinaryCommit: pending.PreviousCommit,
BinarySHA256: pending.PreviousBinarySHA256,
PreviousBinarySHA256: pending.PreviousBinarySHA256,
VersionBefore: pending.VersionBefore,
VersionAfter: pending.VersionAfter,
}
targetState = &state
} else if hasState {
targetState = &state
}
if err := buildAndSwap(ctx, snap.Root, opts, res, step, target, targetState, pending); err != nil {
return err
}
step("done", fmt.Sprintf("源码已经就位(%s),本次补上了二进制", shortHash(head)))
return nil
}
// buildAndSwap compiles the tree and moves the new binary into place, keeping the previous
// one as .prev. The marker records the compiled file's hash before the rename; after a crash
// a retry can prove whether the swap happened instead of doing it twice.
func buildAndSwap(ctx context.Context, root string, opts Options, res *Result, step func(string, string), commit string, state *State, pending buildPendingState) error {
step("build", "开始编译二进制(首次会下载依赖,可能需要几分钟)")
staging := filepath.Join(root, ".update-staging")
if err := os.MkdirAll(staging, 0o755); err != nil {
return &Error{Reason: "staging_failed", Message: err.Error()}
}
defer os.RemoveAll(staging)
newBin := filepath.Join(staging, opts.binaryName())
if err := build(ctx, root, newBin); err != nil {
return &Error{Reason: "build_failed", Message: "编译失败,二进制保持原版本:\n" + err.Error()}
}
newHash, err := fileSHA256(newBin)
if err != nil {
return &Error{Reason: "build_failed", Message: "无法校验新二进制:" + err.Error()}
}
bin := filepath.Join(root, opts.binaryName())
if pending.Legacy && fileExists(bin) && fileExists(bin+".prev") {
currentHash, currentErr := fileSHA256(bin)
previousHash, previousErr := fileSHA256(bin + ".prev")
if currentErr == nil && previousErr == nil && currentHash == newHash {
res.BinaryBuilt = true
res.PrevBinary = bin + ".prev"
res.NeedsRestart = true
if state != nil {
state.BinaryCommit = commit
state.BinarySHA256 = currentHash
state.PreviousBinarySHA256 = previousHash
if err := writeState(root, *state); err != nil {
return &Error{Reason: "state_unwritable", Message: "二进制已经是当前版本,但无法更新回滚状态:" + err.Error()}
}
}
if err := clearBuildPending(root); err != nil {
return &Error{Reason: "state_unwritable", Message: "二进制已经是当前版本,但无法清除旧待编译标记:" + err.Error()}
}
return nil
}
}
if pending.PreviousBinarySHA256 != "" && fileExists(bin) {
currentHash, hashErr := fileSHA256(bin)
if hashErr != nil || currentHash != pending.PreviousBinarySHA256 {
return &Error{Reason: "binary_changed", Message: "编译期间现有二进制发生变化,拒绝覆盖;请确认没有另一场更新在运行"}
}
}
pending.Commit = commit
pending.BinarySHA256 = newHash
if err := writeBuildPending(root, pending); err != nil {
return &Error{Reason: "state_unwritable", Message: "无法记录待换入二进制:" + err.Error()}
}
prev, err := installBinary(newBin, bin)
if err != nil {
return &Error{Reason: "swap_failed", Message: err.Error()}
}
res.BinaryBuilt = true
res.PrevBinary = prev
res.NeedsRestart = true
if state != nil {
state.BinaryCommit = commit
state.BinarySHA256 = newHash
state.PreviousBinarySHA256 = pending.PreviousBinarySHA256
if err := writeState(root, *state); err != nil {
return &Error{Reason: "state_unwritable", Message: "二进制已换入,但无法更新回滚状态:" + err.Error()}
}
}
if err := clearBuildPending(root); err != nil {
return &Error{Reason: "state_unwritable", Message: "二进制已换入,但无法清除待编译标记:" + err.Error()}
}
return nil
}
// stashProtected copies every operator-owned file that stands in the way of the
// fast-forward into backupDir. The caller persists the recovery record before removing
// these paths, so a crash can never strand content without saying where its copy lives.
func stashProtected(ctx context.Context, root, ref string) (backupDir string, kept []string, err error) {
ts := time.Now().Format("20060102_150405")
backupDir = filepath.Join(root, ".update-backup", ts)
tracked, _ := localChanges(ctx, root)
incoming, err := gitRaw(ctx, root, "diff", "--name-only", "-z", "HEAD.."+ref)
if err != nil {
return "", nil, &Error{Reason: "diff_failed", Message: err.Error()}
}
willWrite := map[string]bool{}
for _, f := range nulList(incoming) {
willWrite[filepath.ToSlash(f)] = true
}
// A locally modified content file would refuse the merge; an untracked content file
// whose path upstream now adds would also refuse it. Both are put aside.
targets := map[string]bool{}
for _, c := range tracked {
if c.Protected && willWrite[c.Path] {
targets[c.Path] = true
}
}
for _, p := range untrackedUnder(ctx, root, willWrite) {
targets[p] = true
}
for p := range targets {
abs := filepath.Join(root, p)
info, statErr := os.Stat(abs)
if statErr != nil || info.IsDir() {
continue
}
if err := copyFile(abs, filepath.Join(backupDir, p), info.Mode()); err != nil {
return backupDir, kept, &Error{Reason: "backup_failed", Message: fmt.Sprintf("备份 %s 失败:%v", p, err)}
}
kept = append(kept, p)
}
if len(kept) > 0 {
sort.Strings(kept)
return backupDir, kept, nil
}
// Nothing to put aside: don't leave an empty backup directory behind.
return "", nil, nil
}
func removeProtected(root string, kept []string) error {
for _, rel := range kept {
if err := os.Remove(filepath.Join(root, filepath.FromSlash(rel))); err != nil && !os.IsNotExist(err) {
return &Error{Reason: "backup_failed", Message: fmt.Sprintf("移开 %s 失败:%v", rel, err)}
}
}
return nil
}
// stashProtectedEdits copies the operator's local edits to protected files out of the
// tree before a rollback resets it. Unlike an update's stash it leaves the working copy
// alone - the reset is what replaces those files - because reset --hard does not know
// which local changes are the update's and which are the operator's. A file deleted by
// hand carries no content to copy, so its name is recorded instead and the deletion is
// re-applied after the reset.
func stashProtectedEdits(ctx context.Context, root, target string, changed []Change) (backupDir string, kept, deleted []string, err error) {
ts := time.Now().Format("20060102_150405")
files := map[string]bool{}
for _, c := range changed {
if !c.Protected {
continue
}
if c.Status == "deleted" {
deleted = append(deleted, c.Path)
continue
}
files[c.Path] = true
}
incoming, diffErr := gitRaw(ctx, root, "diff", "--name-only", "-z", "HEAD.."+target)
if diffErr != nil {
return "", nil, nil, &Error{Reason: "diff_failed", Message: diffErr.Error()}
}
willWrite := map[string]bool{}
for _, p := range nulList(incoming) {
willWrite[filepath.ToSlash(p)] = true
}
for _, p := range untrackedUnder(ctx, root, willWrite) {
files[p] = true
}
for p := range files {
abs := filepath.Join(root, filepath.FromSlash(p))
info, statErr := os.Stat(abs)
if statErr != nil || !info.Mode().IsRegular() {
continue
}
if backupDir == "" {
backupDir = filepath.Join(root, ".update-backup", "rollback_"+ts)
}
if err := copyFile(abs, filepath.Join(backupDir, filepath.FromSlash(p)), info.Mode()); err != nil {
return backupDir, kept, deleted, &Error{Reason: "backup_failed", Message: fmt.Sprintf("备份 %s 失败:%v", p, err)}
}
kept = append(kept, p)
}
sort.Strings(kept)
sort.Strings(deleted)
return backupDir, kept, deleted, nil
}
// restoreProtected puts the named operator-owned copies back over whatever the merge
// wrote. The list is the one stashProtected returned: an adoption parks replaced
// non-protected files in the same backup directory (under overwritten/), and those must
// NOT be restored over the target repository's version.
func restoreProtected(backupDir string, kept []string) error {
if backupDir == "" {
return nil
}
root := filepath.Join(filepath.Dir(backupDir), "..")
for _, rel := range kept {
abs := filepath.Join(backupDir, filepath.FromSlash(rel))
info, err := os.Stat(abs)
if err != nil {
return err
}
if err := copyFile(abs, filepath.Join(root, filepath.FromSlash(rel)), info.Mode()); err != nil {
return err
}
}
return nil
}
func restoreInterruptedProtected(ctx context.Context, root string, pending buildPendingState, merged bool) error {
if pending.BackupDir == "" || len(pending.KeptContent) == 0 {
return nil
}
for _, rel := range pending.KeptContent {
rel = filepath.ToSlash(rel)
dst := filepath.Join(root, filepath.FromSlash(rel))
restore := false
if !fileExists(dst) {
if !merged {
restore = true
} else {
_, headErr := gitCmd(ctx, root, "rev-parse", "HEAD:"+rel)
restore = headErr != nil
}
} else if merged {
workHash, workErr := gitCmd(ctx, root, "hash-object", "--", rel)
headHash, headErr := gitCmd(ctx, root, "rev-parse", "HEAD:"+rel)
restore = workErr == nil && headErr == nil && workHash == headHash
}
if !restore {
continue
}
src := filepath.Join(pending.BackupDir, filepath.FromSlash(rel))
info, err := os.Stat(src)
if err != nil {
return &Error{Reason: "backup_missing", Message: fmt.Sprintf("待恢复内容的备份不存在:%s", rel)}
}
if err := copyFile(src, dst, info.Mode()); err != nil {
return &Error{Reason: "backup_failed", Message: fmt.Sprintf("恢复 %s 失败:%v", rel, err)}
}
}
return nil
}
// untrackedUnder lists untracked files under the given repository paths - the operator's
// own additions, which must be backed up but never deleted by an update.
func untrackedUnder(ctx context.Context, root string, willWrite map[string]bool) []string {
out, err := gitRaw(ctx, root, "status", "--porcelain=v1", "-z", "--untracked-files=all", "--no-renames")
if err != nil {
return nil
}
var list []string
for _, entry := range nulList(out) {
if len(entry) < 4 || entry[:2] != "??" {
continue
}
p := filepath.ToSlash(entry[3:])
if willWrite[p] && Protected(p) {
list = append(list, p)
}
}
return list
}
// Rollback returns an installation to the commit it came from, using the binary kept
// before the swap. It refuses unless HEAD is still exactly what the update wrote, so it
// can never discard work done after that update.
func Rollback(ctx context.Context, opts Options) (*Result, error) {
root, err := opts.installRoot()
if err != nil {
return nil, err
}
st, ok := readState(root)
if !ok {
return nil, &Error{Reason: "no_state", Message: "没有可回滚的更新记录"}
}
bin := filepath.Join(root, opts.binaryName())
restoreBinary := true
if st.BinaryCommit != "" {
binaryCommit, resolveErr := gitCmd(ctx, root, "rev-parse", st.BinaryCommit)
if resolveErr != nil {
return nil, &Error{Reason: "bad_state", Message: "更新记录里的二进制提交不存在:" + st.BinaryCommit}
}
previousCommit, resolveErr := gitCmd(ctx, root, "rev-parse", st.PreviousCommit)
if resolveErr != nil {
return nil, &Error{Reason: "bad_state", Message: "更新记录里的回滚提交不存在:" + st.PreviousCommit}
}
updatedCommit, resolveErr := gitCmd(ctx, root, "rev-parse", st.UpdatedCommit)
if resolveErr != nil {
return nil, &Error{Reason: "bad_state", Message: "更新记录里的更新提交不存在:" + st.UpdatedCommit}
}
switch binaryCommit {
case previousCommit:
restoreBinary = false
if st.BinarySHA256 != "" {
got, hashErr := fileSHA256(bin)
if hashErr != nil || got != st.BinarySHA256 {
return nil, &Error{Reason: "binary_changed", Message: "当前二进制与更新前记录不一致,拒绝在无法配对版本时回滚源码"}
}
}
case updatedCommit:
restoreBinary = true
default:
return nil, &Error{Reason: "bad_state", Message: "更新记录里的二进制提交不属于这次更新"}
}
}
if restoreBinary {
if !fileExists(bin + ".prev") {
return nil, &Error{Reason: "no_binary", Message: "上一次更新没有留下旧二进制,无法回滚二进制(源码仍可用 git 处理)"}
}
if st.PreviousBinarySHA256 != "" {
got, hashErr := fileSHA256(bin + ".prev")
if hashErr != nil || got != st.PreviousBinarySHA256 {
return nil, &Error{Reason: "binary_changed", Message: "回滚二进制与更新记录不一致,拒绝把源码回到无法配对的版本"}
}
}
}
head, err := gitCmd(ctx, root, "rev-parse", "HEAD")
if err != nil {
return nil, err
}
updated, err := gitCmd(ctx, root, "rev-parse", st.UpdatedCommit)
if err != nil {
return nil, &Error{Reason: "bad_state", Message: "更新记录里的提交在本仓库不存在:" + st.UpdatedCommit}
}
if head != updated {
return nil, &Error{
Reason: "moved_since_update",
Message: fmt.Sprintf("自那次更新之后 HEAD 已经变了(现在是 %s),回滚只会撤到更新前的提交,因此拒绝执行。", shortHash(head)),
}
}
start := time.Now()
changes, blocking := localChanges(ctx, root)
if len(blocking) > 0 {
items := make([]string, 0, len(blocking))
for _, c := range blocking {
items = append(items, c.Path)
}
return nil, &Error{Reason: "local_source_edits", Message: "有本地源码改动,回滚会覆盖它们", Items: items}
}
// Content the operator owns is not the rollback's to discard either: reset --hard takes
// every local change to tracked files, so protected files that differ right now - edits
// made before the update and edits made after it alike - are copied aside first and set
// back on top of the old commit. An untracked file at a path the old commit tracked is
// included too: reset would otherwise overwrite a role recreated after upstream deleted it.
backupDir, kept, deleted, err := stashProtectedEdits(ctx, root, st.PreviousCommit, changes)
if err != nil {
return nil, err
}
if _, err := gitCmd(ctx, root, "reset", "--hard", "--quiet", st.PreviousCommit); err != nil {
return nil, &Error{Reason: "reset_failed", Message: err.Error()}
}
if err := restoreProtected(backupDir, kept); err != nil {
return nil, err
}
for _, p := range deleted {
// A protected file the operator deleted by hand stays deleted: the reset just
// brought the old commit's copy back, and that deletion is a local change like any
// other this rollback promised not to touch.
if err := os.Remove(filepath.Join(root, filepath.FromSlash(p))); err != nil && !os.IsNotExist(err) {
return nil, err
}
}
if restoreBinary {
if err := restorePrevBinary(bin); err != nil {
return nil, &Error{Reason: "swap_failed", Message: err.Error()}
}
}
// Source and binary agree again (both the previous commit's), so a pending-build
// marker from a failed attempt no longer describes anything - keeping it would invite
// a rebuild of the old tree.
if err := clearBuildPending(root); err != nil {
return nil, &Error{Reason: "state_unwritable", Message: "回滚完成,但无法清除待编译标记:" + err.Error()}
}
// The version number goes back with the code it belongs to (only when it is still the
// one that update wrote - see restoreVersionAfterRollback).
restoreVersionAfterRollback(root, st)
if err := os.Remove(statePath(root)); err != nil && !os.IsNotExist(err) {
return nil, err
}
return &Result{
FromCommit: shortHash(head),
ToCommit: shortHash(st.PreviousCommit),
KeptContent: kept,
BinaryPath: bin,
BinaryBuilt: restoreBinary,
BackupDir: st.BackupDir,
NeedsRestart: true,
Duration: time.Since(start).Round(time.Millisecond).String(),
}, nil
}
func shortHash(h string) string {
if len(h) > 8 {
return h[:8]
}
return h
}
func fileExists(p string) bool {
_, err := os.Stat(p)
return err == nil
}