backup

package
v1.1.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 28, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package backup implements atomic JSON backup creation, bash-era format compatibility, backup management (list/clean), and rollback for SSU.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AtomicWrite

func AtomicWrite(path string, data []byte, perm os.FileMode) error

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

func BackupDir(projectName string) (string, error)

BackupDir returns the Go-era backup directory for a project: ~/.ssu/<projectName>/backups/

func Clean

func Clean(backupDir string, keep string) (int, error)

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

func IsBashEraFilename(name string) bool

IsBashEraFilename returns true if name matches the bash-era backup pattern: .submodule-backup-YYYYMMDD-HHMMSS.json (starts with dot).

func ParseKeepArg

func ParseKeepArg(arg string) (mode string, value int, err error)

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

func ProjectName(projectRoot string) string

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

func Read(path string) (*Backup, error)

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

func ReadBashEra(data []byte) (*Backup, error)

ReadBashEra parses a bash-era backup file (no version field) and normalizes it into the Go-era Backup struct with Version=1.

type BackupInfo

type BackupInfo struct {
	Filename  string
	Path      string
	Timestamp time.Time
	IsBashEra bool
}

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

type GitCheckoutFunc func(dir, branch string) error

GitCheckoutFunc checks out a branch in a directory.

type GitResetHardFunc

type GitResetHardFunc func(dir, sha string) error

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:

  1. Read the backup file
  2. Create a safety backup of the current state (unless dry-run)
  3. For each submodule: checkout branch, then reset to SHA
  4. 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

type SubmoduleState struct {
	SHA    string `json:"sha"`
	Branch string `json:"branch"`
}

SubmoduleState holds the recorded state of a single submodule.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL