dotfile

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2023 License: GPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package dotfile provides functions and interfaces for dotfile operations.

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrNoChanges is returned when a diff operation finds no changes.
	ErrNoChanges = usererror.New("No changes")
)

Functions

func Alias

func Alias(alias, path string) (string, error)

Alias creates an alias when the passed in alias is empty. It works by removing leading dots and file extensions from the path. Examples:

     ~/.vimrc: vimrc
	~/.config/i3/config: config
	~/.config/alacritty/alacritty.yml: alacritty

func CheckAlias

func CheckAlias(alias string) error

CheckAlias checks whether the alias is a valid format.

func CheckPath

func CheckPath(path string) error

CheckPath checks whether the path is a valid format.

func Checkout

func Checkout(r Reverter, hash string) error

Checkout reverts a tracked file to its state at hash.

func Compress

func Compress(uncompressed []byte) (*bytes.Buffer, error)

Compress compresses bytes with zlib.

func Diff

func Diff(g Getter, hash1, hash2 string) (*gotextdiff.Unified, error)

Runs a diff on the revision at hash1 against the revision at hash2. If hash2 is empty, compares the dirty content of the file. Returns an usererror when there is no difference.

func Init

func Init(c Committer, path, alias string) error

Init creates a new commit with the initial commit message.

func IsClean

func IsClean(g Getter, hash string) (bool, error)

IsClean returns whether the dirty content matches the hash. Returns true when there is no dirty content.

func NewCommit

func NewCommit(c Committer, message string) error

NewCommit saves a revision of the file at its current state.

func ShortenHash

func ShortenHash(hash string) string

ShortenHash shortens a hash to a more friendly size.

func Uncompress

func Uncompress(compressed []byte) (*bytes.Buffer, error)

Uncompress uncompresses bytes. Bytes are expected to be zlib compressed.

func UncompressRevision

func UncompressRevision(g Getter, hash string) (*bytes.Buffer, error)

UncompressRevision reads a revision and uncompresses it. Returns the uncompressed bytes of alias at hash.

Types

type Commit

type Commit struct {
	Hash      string `json:"hash"`
	Message   string `json:"message"`
	Timestamp int64  `json:"timestamp"` // Unix timestamp.
}

Commit represents a file revision.

type Committer

type Committer interface {
	Getter
	HasCommit(hash string) (exists bool, err error)
	SaveCommit(buff *bytes.Buffer, c *Commit) error
}

Committer is an interface for saving saving commits.

type Getter

type Getter interface {
	DirtyContent() (contents []byte, err error)        // Uncommitted changes.
	Revision(hash string) (revision []byte, err error) // Revision at hash.
}

Getter is an interface that wraps methods for reading tracked files.

type Reverter

type Reverter interface {
	Getter
	HasCommit(hash string) (exists bool, err error)
	Revert(buff *bytes.Buffer, hash string) (err error)
}

Reverter is the interface that wraps methods needed for reverting a tracked file.

type TrackingData

type TrackingData struct {
	Path     string   `json:"path"`
	Revision string   `json:"revision"`
	Commits  []Commit `json:"commits"`
}

TrackingData is the data that dotfile uses to track files.

func MergeTrackingData

func MergeTrackingData(old, new *TrackingData) (merged *TrackingData, newHashes []string, err error)

MergeTrackingData merges the new data into old. Returns the merged data and a slice of the hashes that are new.

func (*TrackingData) MapCommits

func (td *TrackingData) MapCommits() map[string]*Commit

MapCommits maps hashes to commits.

Jump to

Keyboard shortcuts

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