Skip to content
go-libfossil Artifact [9ba3a9315f]
Login

Artifact [9ba3a9315f]

Artifact 9ba3a9315ff5a20f9e186d732fd8dccf0a6b3aba78160d322aac0a1651ce6b24:


//go:build !js

package checkout

import (
	"database/sql"

	"github.com/danmestas/go-libfossil/internal/undo"
	"github.com/danmestas/go-libfossil/internal/vfile"
)

// updateUndo is the undo state Update saves: the files its steps change, as
// they were before, and how they were seen on disk.
type updateUndo struct {
	disk  vfile.Disk
	files []undo.File
}

// readUpdateUndo reads the file of every step that writes or removes one,
// under its name in the checkout, as fossil's update calls undo_save.
func (c *Checkout) readUpdateUndo(steps []updateStep) (updateUndo, error) {
	disk, err := c.newDiskView()
	if err != nil {
		return updateUndo{}, err
	}
	u := updateUndo{disk: disk}
	for _, s := range steps {
		switch s.action {
		case updateWrite, updateAddOver, updateMerge, updateRemove:
		default:
			continue
		}
		f, err := undo.ReadFile(u.disk, c.dir, s.name)
		if err != nil {
			return updateUndo{}, err
		}
		u.files = append(u.files, f)
	}
	return u, nil
}

// save records u as the undo state within tx, before vfile moves.
func (u updateUndo) save(tx *sql.Tx) error {
	return undo.SaveTx(tx, u.files)
}

// rollBackUpdate puts u's files back after an update failed partway.
func (c *Checkout) rollBackUpdate(u updateUndo) error {
	return undo.RestoreFiles(u.disk, c.dir, u.files)
}