Documentation ¶
Overview ¶
Package vcs provides the ability to work with varying version control systems (VCS), also known as source control systems (SCM) though the same interface.
This package includes a function that attempts to detect the repo type from the remote URL and return the proper type. For example,
remote := "https://github.com/Masterminds/vcs" local, _ := ioutil.TempDir("", "go-vcs") repo, err := NewRepo(remote, local)
In this case repo will be a GitRepo instance. NewRepo can detect the VCS for numerous popular VCS and from the URL. For example, a URL ending in .git that's not from one of the popular VCS will be detected as a Git repo and the correct type will be returned.
If you know the repository type and would like to create an instance of a specific type you can use one of constructors for a type. They are NewGitRepo, NewSvnRepo, NewBzrRepo, and NewHgRepo. The definition and usage is the same as NewRepo.
Once you have an object implementing the Repo interface the operations are the same no matter which VCS you're using. There are some caveats. For example, each VCS has its own version formats that need to be respected and checkout out branches, if a branch is being worked with, is different in each VCS.
Index ¶
- Variables
- func EscapePathSeparator(path string) string
- func NewLocalError(msg string, err error, out string) error
- func NewRemoteError(msg string, err error, out string) error
- type BzrRepo
- func (s *BzrRepo) Branches() ([]string, error)
- func (s *BzrRepo) CheckLocal() bool
- func (b *BzrRepo) CmdFromDir(cmd string, args ...string) *exec.Cmd
- func (s *BzrRepo) CommitInfo(id string) (*CommitInfo, error)
- func (s *BzrRepo) Current() (string, error)
- func (s *BzrRepo) Date() (time.Time, error)
- func (s *BzrRepo) ExportDir(dir string) error
- func (s *BzrRepo) Get() error
- func (s *BzrRepo) Init() error
- func (s *BzrRepo) IsDirty() bool
- func (s *BzrRepo) IsReference(r string) bool
- func (b *BzrRepo) LocalPath() string
- func (s *BzrRepo) Ping() bool
- func (b *BzrRepo) Remote() string
- func (b *BzrRepo) RunFromDir(cmd string, args ...string) ([]byte, error)
- func (s *BzrRepo) Tags() ([]string, error)
- func (s *BzrRepo) TagsFromCommit(id string) ([]string, error)
- func (s *BzrRepo) Update() error
- func (s *BzrRepo) UpdateVersion(version string) error
- func (s BzrRepo) Vcs() Type
- func (s *BzrRepo) Version() (string, error)
- type CommitInfo
- type GitRepo
- func (s *GitRepo) Branches() ([]string, error)
- func (s *GitRepo) CheckLocal() bool
- func (b *GitRepo) CmdFromDir(cmd string, args ...string) *exec.Cmd
- func (s *GitRepo) CommitInfo(id string) (*CommitInfo, error)
- func (s *GitRepo) Current() (string, error)
- func (s *GitRepo) Date() (time.Time, error)
- func (s *GitRepo) ExportDir(dir string) error
- func (s *GitRepo) Get() error
- func (s *GitRepo) Init() error
- func (s *GitRepo) IsDirty() bool
- func (s *GitRepo) IsReference(r string) bool
- func (b *GitRepo) LocalPath() string
- func (s *GitRepo) Ping() bool
- func (b *GitRepo) Remote() string
- func (b *GitRepo) RunFromDir(cmd string, args ...string) ([]byte, error)
- func (s *GitRepo) Tags() ([]string, error)
- func (s *GitRepo) TagsFromCommit(id string) ([]string, error)
- func (s *GitRepo) Update() error
- func (s *GitRepo) UpdateVersion(version string) error
- func (s GitRepo) Vcs() Type
- func (s *GitRepo) Version() (string, error)
- type HgRepo
- func (s *HgRepo) Branches() ([]string, error)
- func (s *HgRepo) CheckLocal() bool
- func (b *HgRepo) CmdFromDir(cmd string, args ...string) *exec.Cmd
- func (s *HgRepo) CommitInfo(id string) (*CommitInfo, error)
- func (s *HgRepo) Current() (string, error)
- func (s *HgRepo) Date() (time.Time, error)
- func (s *HgRepo) ExportDir(dir string) error
- func (s *HgRepo) Get() error
- func (s *HgRepo) Init() error
- func (s *HgRepo) IsDirty() bool
- func (s *HgRepo) IsReference(r string) bool
- func (b *HgRepo) LocalPath() string
- func (s *HgRepo) Ping() bool
- func (b *HgRepo) Remote() string
- func (b *HgRepo) RunFromDir(cmd string, args ...string) ([]byte, error)
- func (s *HgRepo) Tags() ([]string, error)
- func (s *HgRepo) TagsFromCommit(id string) ([]string, error)
- func (s *HgRepo) Update() error
- func (s *HgRepo) UpdateVersion(version string) error
- func (s HgRepo) Vcs() Type
- func (s *HgRepo) Version() (string, error)
- type LocalError
- type RemoteError
- type Repo
- type SvnRepo
- func (s *SvnRepo) Branches() ([]string, error)
- func (s *SvnRepo) CheckLocal() bool
- func (b *SvnRepo) CmdFromDir(cmd string, args ...string) *exec.Cmd
- func (s *SvnRepo) CommitInfo(id string) (*CommitInfo, error)
- func (s *SvnRepo) Current() (string, error)
- func (s *SvnRepo) Date() (time.Time, error)
- func (s *SvnRepo) ExportDir(dir string) error
- func (s *SvnRepo) Get() error
- func (s *SvnRepo) Init() error
- func (s *SvnRepo) IsDirty() bool
- func (s *SvnRepo) IsReference(r string) bool
- func (b *SvnRepo) LocalPath() string
- func (s *SvnRepo) Ping() bool
- func (b *SvnRepo) Remote() string
- func (b *SvnRepo) RunFromDir(cmd string, args ...string) ([]byte, error)
- func (s *SvnRepo) Tags() ([]string, error)
- func (s *SvnRepo) TagsFromCommit(id string) ([]string, error)
- func (s *SvnRepo) Update() error
- func (s *SvnRepo) UpdateVersion(version string) error
- func (s SvnRepo) Vcs() Type
- func (s *SvnRepo) Version() (string, error)
- type Type
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrWrongVCS is returned when an action is tried on the wrong VCS. ErrWrongVCS = errors.New("Wrong VCS detected") // ErrCannotDetectVCS is returned when VCS cannot be detected from URI string. ErrCannotDetectVCS = errors.New("Cannot detect VCS") // ErrWrongRemote occurs when the passed in remote does not match the VCS // configured endpoint. ErrWrongRemote = errors.New("The Remote does not match the VCS endpoint") // unavailable. ErrRevisionUnavailable = errors.New("Revision unavailable") )
var Logger *log.Logger
Logger is where you can provide a logger, implementing the log.Logger interface, where verbose output from each VCS will be written. The default logger does not log data. To log data supply your own logger or change the output location of the provided logger.
Functions ¶
func EscapePathSeparator ¶ added in v1.11.1
EscapePathSeparator escapes the path separator by replacing it with several. Note: this is harmless on Unix, and needed on Windows.
func NewLocalError ¶
NewLocalError constructs a LocalError
Types ¶
type BzrRepo ¶
type BzrRepo struct {
// contains filtered or unexported fields
}
BzrRepo implements the Repo interface for the Bzr source control.
func NewBzrRepo ¶
NewBzrRepo creates a new instance of BzrRepo. The remote and local directories need to be passed in.
func (*BzrRepo) Branches ¶
Branches returns a list of available branches on the repository. In Bazaar (Bzr) clones and branches are the same. A different branch will have a different URL location which we cannot detect from the repo. This is a little different from other VCS.
func (*BzrRepo) CheckLocal ¶
CheckLocal verifies the local location is a Bzr repo.
func (*BzrRepo) CmdFromDir ¶ added in v1.11.0
func (*BzrRepo) CommitInfo ¶
func (s *BzrRepo) CommitInfo(id string) (*CommitInfo, error)
CommitInfo retrieves metadata about a commit.
func (*BzrRepo) Current ¶
Current returns the current version-ish. This means: * -1 if on the tip of the branch (this is the Bzr value for HEAD) * A tag if on a tag * Otherwise a revision
func (*BzrRepo) IsDirty ¶
IsDirty returns if the checkout has been modified from the checked out reference.
func (*BzrRepo) IsReference ¶
IsReference returns if a string is a reference. A reference can be a commit id or tag.
func (*BzrRepo) LocalPath ¶
func (b *BzrRepo) LocalPath() string
LocalPath retrieves the local file system location for a repo.
func (*BzrRepo) Remote ¶
func (b *BzrRepo) Remote() string
Remote retrieves the remote location for a repo.
func (*BzrRepo) RunFromDir ¶
func (*BzrRepo) TagsFromCommit ¶
TagsFromCommit retrieves tags from a commit id.
func (*BzrRepo) UpdateVersion ¶
UpdateVersion sets the version of a package currently checked out via Bzr.
type CommitInfo ¶
type CommitInfo struct { // The commit id Commit string // Who authored the commit Author string // Date of the commit Date time.Time // Commit message Message string }
CommitInfo contains metadata about a commit.
type GitRepo ¶
type GitRepo struct { RemoteLocation string // contains filtered or unexported fields }
GitRepo implements the Repo interface for the Git source control.
func NewGitRepo ¶
NewGitRepo creates a new instance of GitRepo. The remote and local directories need to be passed in.
func (*GitRepo) CheckLocal ¶
CheckLocal verifies the local location is a Git repo.
func (*GitRepo) CmdFromDir ¶ added in v1.11.0
func (*GitRepo) CommitInfo ¶
func (s *GitRepo) CommitInfo(id string) (*CommitInfo, error)
CommitInfo retrieves metadata about a commit.
func (*GitRepo) Current ¶
Current returns the current version-ish. This means: * Branch name if on the tip of the branch * Tag if on a tag * Otherwise a revision id
func (*GitRepo) IsDirty ¶
IsDirty returns if the checkout has been modified from the checked out reference.
func (*GitRepo) IsReference ¶
IsReference returns if a string is a reference. A reference can be a commit id, branch, or tag.
func (*GitRepo) LocalPath ¶
func (b *GitRepo) LocalPath() string
LocalPath retrieves the local file system location for a repo.
func (*GitRepo) Remote ¶
func (b *GitRepo) Remote() string
Remote retrieves the remote location for a repo.
func (*GitRepo) RunFromDir ¶
func (*GitRepo) TagsFromCommit ¶
TagsFromCommit retrieves tags from a commit id.
func (*GitRepo) UpdateVersion ¶
UpdateVersion sets the version of a package currently checked out via Git.
type HgRepo ¶
type HgRepo struct {
// contains filtered or unexported fields
}
HgRepo implements the Repo interface for the Mercurial source control.
func NewHgRepo ¶
NewHgRepo creates a new instance of HgRepo. The remote and local directories need to be passed in.
func (*HgRepo) CheckLocal ¶
CheckLocal verifies the local location is a Git repo.
func (*HgRepo) CmdFromDir ¶ added in v1.11.0
func (*HgRepo) CommitInfo ¶
func (s *HgRepo) CommitInfo(id string) (*CommitInfo, error)
CommitInfo retrieves metadata about a commit.
func (*HgRepo) Current ¶
Current returns the current version-ish. This means: * Branch name if on the tip of the branch * Tag if on a tag * Otherwise a revision id
func (*HgRepo) IsDirty ¶
IsDirty returns if the checkout has been modified from the checked out reference.
func (*HgRepo) IsReference ¶
IsReference returns if a string is a reference. A reference can be a commit id, branch, or tag.
func (*HgRepo) LocalPath ¶
func (b *HgRepo) LocalPath() string
LocalPath retrieves the local file system location for a repo.
func (*HgRepo) Remote ¶
func (b *HgRepo) Remote() string
Remote retrieves the remote location for a repo.
func (*HgRepo) TagsFromCommit ¶
TagsFromCommit retrieves tags from a commit id.
func (*HgRepo) UpdateVersion ¶
UpdateVersion sets the version of a package currently checked out via Hg.
type LocalError ¶
type LocalError struct {
// contains filtered or unexported fields
}
LocalError is returned when a local operation has an error
type RemoteError ¶
type RemoteError struct {
// contains filtered or unexported fields
}
RemoteError is returned when an operation fails against a remote repo
func (*RemoteError) Error ¶
func (e *RemoteError) Error() string
Error implements the Error interface
type Repo ¶
type Repo interface { // Vcs retrieves the underlying VCS being implemented. Vcs() Type // Remote retrieves the remote location for a repo. Remote() string // LocalPath retrieves the local file system location for a repo. LocalPath() string // Get is used to perform an initial clone/checkout of a repository. Get() error // Initializes a new repository locally. Init() error // Update performs an update to an existing checkout of a repository. Update() error // UpdateVersion sets the version of a package of a repository. UpdateVersion(string) error // Version retrieves the current version. Version() (string, error) // Current retrieves the current version-ish. This is different from the // Version method. The output could be a branch name if on the tip of a // branch (git), a tag if on a tag, a revision if on a specific revision // that's not the tip of the branch. The values here vary based on the VCS. Current() (string, error) // Date retrieves the date on the latest commit. Date() (time.Time, error) // CheckLocal verifies the local location is of the correct VCS type CheckLocal() bool // Branches returns a list of available branches on the repository. Branches() ([]string, error) // Tags returns a list of available tags on the repository. Tags() ([]string, error) // IsReference returns if a string is a reference. A reference can be a // commit id, branch, or tag. IsReference(string) bool // IsDirty returns if the checkout has been modified from the checked // out reference. IsDirty() bool // CommitInfo retrieves metadata about a commit. CommitInfo(string) (*CommitInfo, error) // TagsFromCommit retrieves tags from a commit id. TagsFromCommit(string) ([]string, error) // Ping returns if remote location is accessible. Ping() bool // RunFromDir executes a command from repo's directory. RunFromDir(cmd string, args ...string) ([]byte, error) // CmdFromDir creates a new command that will be executed from repo's // directory. CmdFromDir(cmd string, args ...string) *exec.Cmd // ExportDir exports the current revision to the passed in directory. ExportDir(string) error }
Repo provides an interface to work with repositories using different source control systems such as Git, Bzr, Mercurial, and SVN. For implementations of this interface see BzrRepo, GitRepo, HgRepo, and SvnRepo.
func NewRepo ¶
NewRepo returns a Repo based on trying to detect the source control from the remote and local locations. The appropriate implementation will be returned or an ErrCannotDetectVCS if the VCS type cannot be detected. Note, this function may make calls to the Internet to determind help determine the VCS.
Example ¶
remote := "https://github.com/Masterminds/vcs" local, _ := ioutil.TempDir("", "go-vcs") repo, _ := NewRepo(remote, local) // Returns: instance of GitRepo repo.Vcs() // Returns Git as this is a Git repo err := repo.Get() // Pulls down a repo, or a checkout in the case of SVN, and returns an // error if that didn't happen successfully. if err != nil { fmt.Println(err) } err = repo.UpdateVersion("master") // Checkouts out a specific version. In most cases this can be a commit id, // branch, or tag. if err != nil { fmt.Println(err) }
Output:
type SvnRepo ¶
type SvnRepo struct {
// contains filtered or unexported fields
}
SvnRepo implements the Repo interface for the Svn source control.
func NewSvnRepo ¶
NewSvnRepo creates a new instance of SvnRepo. The remote and local directories need to be passed in. The remote location should include the branch for SVN. For example, if the package is https://github.com/Masterminds/cookoo/ the remote should be https://github.com/Masterminds/cookoo/trunk for the trunk branch.
func (*SvnRepo) Branches ¶
Branches returns []string{} as there are no formal branches in SVN. Branches are a convention. They are typically implemented as a copy of the trunk and placed in the /branches/[tag name] directory. Since this is a convention the expectation is to checkout a branch the correct subdirectory will be used as the path. For more information see: http://svnbook.red-bean.com/en/1.7/svn.branchmerge.using.html
func (*SvnRepo) CheckLocal ¶
CheckLocal verifies the local location is an SVN repo.
func (*SvnRepo) CmdFromDir ¶ added in v1.11.0
func (*SvnRepo) CommitInfo ¶
func (s *SvnRepo) CommitInfo(id string) (*CommitInfo, error)
CommitInfo retrieves metadata about a commit.
func (*SvnRepo) Current ¶
Current returns the current version-ish. This means: * HEAD if on the tip. * Otherwise a revision id
func (*SvnRepo) Get ¶
Get is used to perform an initial checkout of a repository. Note, because SVN isn't distributed this is a checkout without a clone.
func (*SvnRepo) IsDirty ¶
IsDirty returns if the checkout has been modified from the checked out reference.
func (*SvnRepo) IsReference ¶
IsReference returns if a string is a reference. A reference is a commit id. Branches and tags are part of the path.
func (*SvnRepo) LocalPath ¶
func (b *SvnRepo) LocalPath() string
LocalPath retrieves the local file system location for a repo.
func (*SvnRepo) Remote ¶
func (b *SvnRepo) Remote() string
Remote retrieves the remote location for a repo.
func (*SvnRepo) RunFromDir ¶
func (*SvnRepo) Tags ¶
Tags returns []string{} as there are no formal tags in SVN. Tags are a convention in SVN. They are typically implemented as a copy of the trunk and placed in the /tags/[tag name] directory. Since this is a convention the expectation is to checkout a tag the correct subdirectory will be used as the path. For more information see: http://svnbook.red-bean.com/en/1.7/svn.branchmerge.tags.html
func (*SvnRepo) TagsFromCommit ¶
TagsFromCommit retrieves tags from a commit id.
func (*SvnRepo) UpdateVersion ¶
UpdateVersion sets the version of a package currently checked out via SVN.
type Type ¶
type Type string
Type describes the type of VCS
VCS types
func DetectVcsFromFS ¶
DetectVcsFromFS detects the type from the local path. Is there a better way to do this?