git

package
v0.0.0-...-854ad97 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2021 License: BSD-3-Clause Imports: 23 Imported by: 5

Documentation

Overview

Package git provides functions for running git and git-annex shell commands.

Index

Constants

View Source
const (

	// Constant errors
	NotRepository   = Error("Not a repository")
	NotAnnex        = Error("No annex found")
	UpgradeRequired = Error("Repository upgrade needed")
)

Variables

View Source
var RawMode bool = false

RawMode disables --json output for annex commands

Functions

func Add

func Add(filepaths []string, addchan chan<- RepoFileStatus)

Add adds paths to git directly (not annex). In direct mode, files that are already in the annex are explicitly ignored. In indirect mode, adding annexed files to git has no effect. The status channel 'addchan' is closed when this function returns. (git add)

func AnnexAdd

func AnnexAdd(filepaths []string, addchan chan<- RepoFileStatus)

AnnexAdd adds paths to the annex. Files specified for exclusion in the configuration are ignored automatically. The status channel 'addchan' is closed when this function returns. (git annex add)

func AnnexCommand

func AnnexCommand(args ...string) shell.Cmd

AnnexCommand sets up a git annex command with the provided arguments and returns a GinCmd struct.

func AnnexContentLocation

func AnnexContentLocation(key string) (string, error)

AnnexContentLocation returns the location of the content for a given annex key. This is the location of the content file in the object store. If the annexed content is not available locally, the function returns an error.

func AnnexDescribe

func AnnexDescribe(repository, description string) error

AnnexDescribe changes the description of a repository. (git annex describe)

func AnnexDrop

func AnnexDrop(filepaths []string, dropchan chan<- RepoFileStatus)

AnnexDrop drops the content of specified files. The status channel 'dropchan' is closed when this function returns. (git annex drop)

func AnnexFind

func AnnexFind(paths []string) (map[string]AnnexFindRes, error)

AnnexFind lists available annexed files in the current directory. Specifying 'paths' limits the search to files matching a given path. Returned items are indexed by their annex key. (git annex find)

func AnnexFromKey

func AnnexFromKey(key, filepath string) error

AnnexFromKey creates an Annex placeholder file at a given location with a specific key. The creation is forced, so there is no guarantee that the key refers to valid repository content, nor that the content is still available in any of the remotes. The location where the file is to be created must be available (no directories are created). (git annex fromkey --force)

func AnnexFsck

func AnnexFsck(paths []string) error

AnnexFsck runs fsck (filesystem check) on the specified files, fixing any issues with the annexed files in the working tree.

func AnnexGet

func AnnexGet(filepaths []string, getchan chan<- RepoFileStatus)

AnnexGet retrieves the content of specified files. The status channel 'getchan' is closed when this function returns. (git annex get)

func AnnexGetKey

func AnnexGetKey(key string, getchan chan<- RepoFileStatus)

AnnexGetKey retrieves the content of a single specified key. The status channel 'getchan' is closed when this function returns. (git annex get)

func AnnexInit

func AnnexInit(description string) error

AnnexInit initialises the repository for annex. (git annex init)

func AnnexLock

func AnnexLock(filepaths []string, lockchan chan<- RepoFileStatus)

AnnexLock locks the specified files and directory contents if they are annexed. If an unlocked file has modifications, it wont be locked and an error will be returned for that file. The status channel 'lockchan' is closed when this function returns. (git annex lock)

func AnnexPull

func AnnexPull(remote string) error

AnnexPull downloads all annexed files. Optionally also downloads all file content. (git annex sync --no-push [--content])

func AnnexPush

func AnnexPush(paths []string, remote string, pushchan chan<- RepoFileStatus)

AnnexPush uploads all changes and new content to the default remote. The status channel 'pushchan' is closed when this function returns. (git annex sync --no-pull; git annex copy --to=<defaultremote>)

func AnnexStatus

func AnnexStatus(paths []string, statuschan chan<- AnnexStatusRes)

AnnexStatus returns the status of a file or files in a directory The output channel 'statuschan' is closed when this function returns. (git annex status)

func AnnexSync

func AnnexSync(content bool) error

AnnexSync performs a bidirectional synchronisation between local and remote repositories, automatically resolving merge conflicts. (git annex sync --resolvemerge)

func AnnexUnlock

func AnnexUnlock(filepaths []string, unlockchan chan<- RepoFileStatus)

AnnexUnlock unlocks the specified files and directory contents if they are annexed The status channel 'unlockchan' is closed when this function returns. (git annex unlock)

func AnnexWhereis

func AnnexWhereis(paths []string, wichan chan<- AnnexWhereisRes)

AnnexWhereis returns information about annexed files in the repository The output channel 'wichan' is closed when this function returns. (git annex whereis)

func BranchSetUpstream

func BranchSetUpstream(name string) error

BranchSetUpstream sets the default upstream remote for the current branch. (git branch --set-upstream-to=)

func CatFileContents

func CatFileContents(revision, filepath string) ([]byte, error)

CatFileContents performs a git-cat-file of a specific file from a specific commit and returns the file contents (as bytes).

func CatFileType

func CatFileType(object string) (string, error)

CatFileType returns the type of a given object at a given revision (blob, tree, or commit)

func Checkout

func Checkout(hash string, paths []string) error

Checkout performs a git checkout of a specific commit. Individual files or directories may be specified, otherwise the entire tree is checked out.

func Checkwd

func Checkwd() error

Checkwd checks whether the current working directory is in a git repository. Returns NotRepository if the working directory is not inside a repository. Returns NotAnnex if the working directory is inside a repository but there is no annex. Returns UpgradeRequired if the annex is an old version (< v7).

func Clone

func Clone(remotepath string, repopath string, clonechan chan<- RepoFileStatus)

Clone downloads a repository and sets the remote fetch and push urls. The status channel 'clonechan' is closed when this function returns. (git clone ...)

func Command

func Command(args ...string) shell.Cmd

Command sets up an external git command with the provided arguments and returns a GinCmd struct.

func Commit

func Commit(commitmsg string) error

Commit records changes that have been added to the repository with a given message. (git commit)

func CommitEmpty

func CommitEmpty(commitmsg string) error

CommitEmpty performs a commit even when there are no new changes added to the index. This is useful for initialising new repositories with a usable HEAD. In indirect mode (non-bare repositories) simply uses git commit with the '--allow-empty' flag. In direct mode it uses git-annex sync. (git commit --allow-empty or git annex sync --commit)

func ConfigGet

func ConfigGet(key string) (string, error)

ConfigGet returns the value of a given git configuration key using the default git configuration. The returned key is always a string. (git config --get)

func ConfigGetLocal

func ConfigGetLocal(key string) (string, error)

ConfigGetLocal returns the value of a given git configuration key using the local git configuration only. The returned key is always a string. (git config --local --get)

func ConfigSet

func ConfigSet(key, value string) error

ConfigSet sets a configuration value in the local git config. (git config --local)

func ConfigUnset

func ConfigUnset(key string) error

ConfigUnset unsets a configuration value in the local git config. (git config unset --local)

func CopyFile

func CopyFile(src, dest string) error

CopyFile copies the contents of src file into dest file.

func DescribeIndex

func DescribeIndex() (string, error)

DescribeIndex returns a string which describes the git (annex) index. It is constructed using the result of 'git annex status'. The resulting message can be used to inform the user of changes that are about to be uploaded and as a long commit message.

func DescribeIndexShort

func DescribeIndexShort(paths []string) (string, error)

DescribeIndexShort returns a string which represents a condensed form of the git (annex) index. It is constructed using the result of 'git annex status'. The description is composed of the file count for each status: added, modified, deleted If 'paths' are specified, the status output is limited to files and directories matching those paths.

func DiffUpstream

func DiffUpstream(paths []string, upstream string, diffchan chan<- string)

DiffUpstream returns, through the provided channel, the names of all files that differ from the default remote branch. The output channel 'diffchan' is closed when this function returns. (git diff --name-only --relative @{upstream})

func FindRepoRoot

func FindRepoRoot(path string) (string, error)

FindRepoRoot returns the absolute path to the root of the repository. For bare repositories, it returns an empty string, but no error. (git rev-parse --show-toplevel)

func GetAnnexVersion

func GetAnnexVersion() (string, error)

GetAnnexVersion returns the version string of the system's git-annex.

func GetGitVersion

func GetGitVersion() (string, error)

GetGitVersion returns the version string of the system's git binary.

func GetHostKey

func GetHostKey(gitconf config.GitCfg) (hostkeystr, fingerprint string, err error)

GetHostKey takes a git server configuration, queries the server via SSH, and returns the public key of the host (in the format required for the known_hosts file) and the key fingerprint.

func GetKnownHosts

func GetKnownHosts() (string, error)

GetKnownHosts returns the path to the known_hosts file. If the file does not exist it is created by calling WriteKnownHosts.

func Init

func Init(bare bool) error

Init initialises the current directory as a git repository. The repository is optionally initialised as bare. (git init [--bare])

func IsDirect

func IsDirect() bool

IsDirect returns true if the repository in a given path is working in git annex 'direct' mode. If path is not a repository, or is not an initialised annex repository, the result defaults to false. If the path is a repository and no error was raised, the result it cached so that subsequent checks are faster.

func IsVersion6

func IsVersion6() bool

IsVersion6 returns true if the repository in a given path is working in git annex 'direct' mode. If path is not a repository, or is not an initialised annex repository, the result defaults to false.

func LogDiffStat

func LogDiffStat(count uint, paths []string, showdeletes bool) (map[string]DiffStat, error)

func LsFiles

func LsFiles(args []string, lschan chan<- string)

LsFiles lists all files known to git. The output channel 'lschan' is closed when this function returns. (git ls-files)

func LsRemote

func LsRemote(remote string) (string, error)

LsRemote performs a git ls-remote of a specific remote. The argument can be a name or a URL. (git ls-remote)

func PrivKeyPath

func PrivKeyPath() map[string]string

PrivKeyPath returns a map with the full path for all the currently available private key files indexed by the server alias for each key.

func Pull

func Pull(remote string) error

Pull downloads all small (git) files from the server. (git pull --ff-only)

func Push

func Push(remote string, pushchan chan<- RepoFileStatus)

Push uploads all small (git) files to the server. (git push)

func RemoteAdd

func RemoteAdd(name, url string) error

RemoteAdd adds a remote named name for the repository at URL.

func RemoteRemove

func RemoteRemove(name string) error

RemoteRemove removes the remote named name from the repository configuration.

func RemoteShow

func RemoteShow() (map[string]string, error)

RemoteShow returns the configured remotes and their URL. (git remote -v show -n)

func RevCount

func RevCount(a, b string) (int, error)

RevCount returns the number of commits between two revisions.

func RevParse

func RevParse(rev string) (string, error)

RevParse parses an argument and returns the unambiguous, SHA1 representation. (git rev-parse)

func SetBare

func SetBare(state bool)

func SetGitUser

func SetGitUser(name, email string) error

SetGitUser sets the user.name and user.email configuration values for the local git repository.

func WriteKnownHosts

func WriteKnownHosts() error

WriteKnownHosts creates a known_hosts file in the config directory with all configured host keys.

Types

type AnnexFindRes

type AnnexFindRes struct {
	Hashdirlower string
	Hashdirmixed string
	Key          string
	Humansize    string
	Backend      string
	File         string
	Keyname      string
	Bytesize     string
	Mtime        string
}

AnnexFindRes holds the result of a git annex find invocation for one file.

type AnnexInfoRes

type AnnexInfoRes struct {
	TransfersInProgress             []interface{} `json:"transfers in progress"`
	LocalAnnexKeys                  int           `json:"local annex keys"`
	AvailableLocalDiskSpace         string        `json:"available local disk space"`
	AnnexedFilesInWorkingTree       int           `json:"annexed files in working tree"`
	File                            interface{}   `json:"file"`
	TrustedRepositories             []interface{} `json:"trusted repositories"`
	SizeOfAnnexedFilesInWorkingTree string        `json:"size of annexed files in working tree"`
	LocalAnnexSize                  string        `json:"local annex size"`
	Command                         string        `json:"command"`
	UntrustedRepositories           []interface{} `json:"untrusted repositories"`
	SemitrustedRepositories         []struct {
		Description string `json:"description"`
		Here        bool   `json:"here"`
		UUID        string `json:"uuid"`
	} `json:"semitrusted repositories"`
	Success         bool   `json:"success"`
	BloomFilterSize string `json:"bloom filter size"`
	BackendUsage    struct {
		SHA256E int `json:"SHA256E"`
		WORM    int `json:"WORM"`
	} `json:"backend usage"`
	RepositoryMode string `json:"repository mode"`
}

AnnexInfoRes holds the information returned by AnnexInfo

func AnnexInfo

func AnnexInfo() (AnnexInfoRes, error)

AnnexInfo returns the annex information for a given repository (git annex info)

type AnnexStatusRes

type AnnexStatusRes struct {
	Status string `json:"status"`
	File   string `json:"file"`
	Err    error  `json:"err"`
}

AnnexStatusRes for getting the (annex) status of individual files

type AnnexWhereisRes

type AnnexWhereisRes struct {
	File      string   `json:"file"`
	Command   string   `json:"command"`
	Note      string   `json:"note"`
	Success   bool     `json:"success"`
	Untrusted []string `json:"untrusted"`
	Key       string   `json:"key"`
	Whereis   []struct {
		Here        bool     `json:"here"`
		UUID        string   `json:"uuid"`
		URLs        []string `json:"urls"`
		Description string   `json:"description"`
	}
	Err error `json:"err"`
}

AnnexWhereisRes holds the output of a "git annex whereis" command

type DiffStat

type DiffStat struct {
	NewFiles      []string
	DeletedFiles  []string
	ModifiedFiles []string
}

type Error

type Error string

func (Error) Error

func (e Error) Error() string

type GinCommit

type GinCommit struct {
	Hash            string    `json:"hash"`
	AbbreviatedHash string    `json:"abbrevhash"`
	AuthorName      string    `json:"authorname"`
	AuthorEmail     string    `json:"authoremail"`
	Date            time.Time `json:"date"`
	Subject         string    `json:"subject"`
	Body            string    `json:"body"`
	FileStats       DiffStat
}

GinCommit describes a commit, retrieved from the git log.

func Log

func Log(count uint, revrange string, paths []string, showdeletes bool) ([]GinCommit, error)

Log returns the commit logs for the repository. The number of commits can be limited by the count argument. If count <= 0, the entire commit history is returned. Revisions which match only the deletion of the matching paths can be filtered using the showdeletes argument.

type KeyFile

type KeyFile struct {
	Dir      string
	FileName string
	Active   bool
}

KeyFile holds the absolute path and filename of the current logged in user's key file.

type KeyPair

type KeyPair struct {
	Private string
	Public  string
}

KeyPair simply holds a private-public key pair as strings, with no extra information.

func MakeKeyPair

func MakeKeyPair() (*KeyPair, error)

MakeKeyPair generates and returns a private-public key pair.

type Object

type Object struct {
	Name string
	Hash string
	Type string
	Mode string
}

Object contains the information for a tree or blob object in git

func LsTree

func LsTree(revision string, paths []string) ([]Object, error)

LsTree performs a recursive git ls-tree with a given revision (hash) and a list of paths. For each item, it returns a struct which contains the type (blob, tree), the mode, the hash, and the absolute (repo rooted) path to the object (name).

type RepoFileStatus

type RepoFileStatus struct {
	// The name of the file.
	FileName string `json:"filename"`
	// The state of the operation.
	State string `json:"state"`
	// Progress of the operation, if available. If partial progress isn't available or applicable, this will be empty.
	Progress string `json:"progress"`
	// The data rate, if available.
	Rate string `json:"rate"`
	// original cmd input
	RawInput string `json:"rawinput"`
	// original command output
	RawOutput string `json:"rawoutput"`
	// Errors
	Err error
}

RepoFileStatus describes the status of files when being added to the repo or transferred to/from remotes.

func (RepoFileStatus) MarshalJSON

func (s RepoFileStatus) MarshalJSON() ([]byte, error)

MarshalJSON overrides the default marshalling of RepoFileStatus to return the error string for the Err field.

Directories

Path Synopsis
Package shell augments the standard library os/exec Cmd struct and functions with convenience functions for reading piped output.
Package shell augments the standard library os/exec Cmd struct and functions with convenience functions for reading piped output.

Jump to

Keyboard shortcuts

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