Documentation
¶
Overview ¶
Package backup implements atomic JSON backup creation, bash-era format compatibility, backup management (list/clean), and rollback for SSU.
Index ¶
- func AtomicWrite(path string, data []byte, perm os.FileMode) error
- func BackupDir(projectName string) (string, error)
- func Clean(backupDir string, keep string) (int, error)
- func Create(backupDir string, submodules map[string]SubmoduleState) (string, error)
- func IsBashEraFilename(name string) bool
- func ParseKeepArg(arg string) (mode string, value int, err error)
- func ProjectName(projectRoot string) string
- type Backup
- type BackupInfo
- type GetCurrentStatesFunc
- type GitCheckoutFunc
- type GitResetHardFunc
- type RestoredSubmodule
- type RollbackOpts
- type RollbackResult
- type SubmoduleState
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AtomicWrite ¶
AtomicWrite writes data to path atomically using temp-file + fsync + rename. The temp file is created in the same directory as path to ensure same-device rename, which is atomic on POSIX systems.
func BackupDir ¶
BackupDir returns the Go-era backup directory for a project: ~/.ssu/<projectName>/backups/
func Clean ¶
Clean removes old backups from backupDir based on the keep argument. Only Go-era backups are cleaned (bash-era backups in the parent dir are not touched). Returns the count of removed files.
func Create ¶
func Create(backupDir string, submodules map[string]SubmoduleState) (string, error)
Create writes an atomic JSON backup file to backupDir and returns the filename.
func IsBashEraFilename ¶
IsBashEraFilename returns true if name matches the bash-era backup pattern: .submodule-backup-YYYYMMDD-HHMMSS.json (starts with dot).
func ParseKeepArg ¶
ParseKeepArg parses a keep argument string into a mode and value. Examples: "5" -> ("count", 5), "7d" -> ("time", 7). Returns error for non-positive values or unparseable strings.
func ProjectName ¶
ProjectName returns the project name derived from the project root path. This matches the bash-era behavior: use the base directory name.
Types ¶
type Backup ¶
type Backup struct {
Version int `json:"version"`
Timestamp string `json:"timestamp"`
Submodules map[string]SubmoduleState `json:"submodules"`
}
Backup is the Go-era backup format (version 2).
func Read ¶
Read loads a backup file and returns a normalized Backup struct. It auto-detects Go-era (version field present) vs bash-era format.
func ReadBashEra ¶
ReadBashEra parses a bash-era backup file (no version field) and normalizes it into the Go-era Backup struct with Version=1.
type BackupInfo ¶
BackupInfo describes a backup file for listing purposes.
func List ¶
func List(backupDir string) ([]BackupInfo, error)
List returns all backup files found in backupDir (Go-era) and its parent directory (bash-era), sorted by timestamp descending (newest first).
type GetCurrentStatesFunc ¶
type GetCurrentStatesFunc func(projectRoot string, paths []string) (map[string]SubmoduleState, error)
GetCurrentStatesFunc retrieves current submodule states for safety backup. It accepts the project root and a list of submodule paths, returning a map of path -> SubmoduleState. This is injected to keep the backup package independent from the git package.
type GitCheckoutFunc ¶
GitCheckoutFunc checks out a branch in a directory.
type GitResetHardFunc ¶
GitResetHardFunc resets a directory to a specific SHA.
type RestoredSubmodule ¶
type RestoredSubmodule struct {
Path string // Submodule relative path
SHA string // Target SHA that was (or would be) restored
Branch string // Target branch that was (or would be) checked out
Error error // Non-nil if this submodule failed to restore
}
RestoredSubmodule holds the result of restoring a single submodule.
type RollbackOpts ¶
type RollbackOpts struct {
BackupPath string // Path to the backup file to restore from
ProjectRoot string // Git repository root directory
BackupDir string // Directory for creating safety backups
DryRun bool // If true, only show what would be restored
}
RollbackOpts configures a rollback operation.
type RollbackResult ¶
type RollbackResult struct {
SafetyBackupFile string // Filename of safety backup created before restoring
RestoredCount int // Number of submodules successfully restored
Submodules []RestoredSubmodule // Per-submodule results
}
RollbackResult holds the outcome of a rollback operation.
func Rollback ¶
func Rollback( opts RollbackOpts, getCurrentStates GetCurrentStatesFunc, gitCheckout GitCheckoutFunc, gitResetHard GitResetHardFunc, ) (*RollbackResult, error)
Rollback restores submodules to the state recorded in a backup file.
The process:
- Read the backup file
- Create a safety backup of the current state (unless dry-run)
- For each submodule: checkout branch, then reset to SHA
- Continue on per-submodule errors (the safety backup is the undo mechanism)
Git operations are injected as function parameters to keep the backup package independent from the git package. They are wired together in Phase 5.
type SubmoduleState ¶
SubmoduleState holds the recorded state of a single submodule.