vcs

package module
v0.0.0-...-829ed5c Latest Latest
Warning

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

Go to latest
Published: May 13, 2014 License: BSD-2-Clause Imports: 8 Imported by: 1

README

go-vcs - manipulate and inspect VCS repositories

xrefs top func funcs

go-vcs is a library for manipulating and inspecting VCS repositories in Go. It currently supports Git and Mercurial (hg).

Note: the public API is experimental and subject to change until further notice.

Usage

See full package documentation at godoc.org and Sourcegraph.

Example: example_test.go (Sourcegraph):

package vcs_test

import (
	"fmt"
	"github.com/sourcegraph/go-vcs"
	"io/ioutil"
	"os"
	"path/filepath"
)

func Example() {
	var tmpdir string
	tmpdir, err := ioutil.TempDir("", "go-vcs-TestGit")
	if err != nil {
		panic("TempDir: " + err.Error())
		return
	}
	defer os.RemoveAll(tmpdir)

	r, err := vcs.Git.Clone("https://bitbucket.org/sqs/go-vcs-gittest.git", tmpdir)
	if err != nil {
		fmt.Printf("Clone error: %s\n", err)
		return
	}

	// check out master
	masterDir, err := r.CheckOut("master")
	if err != nil {
		fmt.Printf("CheckOut master: %s\n", err)
		return
	}
	fmt.Printf("master foo: %s", readfile(filepath.Join(masterDir, "foo")))

	// check out a branch
	barbranchDir, err := r.CheckOut("barbranch")
	if err != nil {
		fmt.Printf("CheckOut barbranch: %s\n", err)
		return
	}
	fmt.Printf("barbranch bar: %s", readfile(filepath.Join(barbranchDir, "bar")))

	// check out a commit id
	barcommit := "f411e1ea59ed2b833291efa196e8dab80dbf7cb8"
	barcommitDir, err := r.CheckOut(barcommit)
	if err != nil {
		fmt.Printf("CheckOut barcommit %s: %s", barcommit, err)
		return
	}
	fmt.Printf("barcommit bar: %s", readfile(filepath.Join(barcommitDir, "bar")))

	// output:
	// master foo: Hello, foo
	// barbranch bar: Hello, bar
	// barcommit bar: Hello, bar
}

func readfile(path string) string {
	data, _ := ioutil.ReadFile(path)
	return string(data)
}

Running tests

Run go test.

Contributors

Forked from https://github.com/sourcegraph/go-vcs/.

Documentation

Overview

Package vcs manipulates and inspects VCS (Git and Mercurial) repositories.

Example
package main

import (
	"fmt"
	"github.com/sourcegraph/go-vcs"
	"io/ioutil"
	"os"
	"path/filepath"
)

func main() {
	var tmpdir string
	tmpdir, err := ioutil.TempDir("", "go-vcs-TestGit")
	if err != nil {
		panic("TempDir: " + err.Error())
		return
	}
	defer os.RemoveAll(tmpdir)

	r, err := vcs.Git.Clone("https://bitbucket.org/sqs/go-vcs-gittest.git", tmpdir)
	if err != nil {
		fmt.Printf("Clone error: %s\n", err)
		return
	}

	// check out master
	masterDir, err := r.CheckOut("master")
	if err != nil {
		fmt.Printf("CheckOut master: %s\n", err)
		return
	}
	fmt.Printf("master foo: %s", readfile(filepath.Join(masterDir, "foo")))

	// check out a branch
	barbranchDir, err := r.CheckOut("barbranch")
	if err != nil {
		fmt.Printf("CheckOut barbranch: %s\n", err)
		return
	}
	fmt.Printf("barbranch bar: %s", readfile(filepath.Join(barbranchDir, "bar")))

	// check out a commit id
	barcommit := "f411e1ea59ed2b833291efa196e8dab80dbf7cb8"
	barcommitDir, err := r.CheckOut(barcommit)
	if err != nil {
		fmt.Printf("CheckOut barcommit %s: %s", barcommit, err)
		return
	}
	fmt.Printf("barcommit bar: %s", readfile(filepath.Join(barcommitDir, "bar")))

}

func readfile(path string) string {
	data, _ := ioutil.ReadFile(path)
	return string(data)
}
Output:

master foo: Hello, foo
barbranch bar: Hello, bar
barcommit bar: Hello, bar

Index

Examples

Constants

This section is empty.

Variables

View Source
var VCSByName = map[string]VCS{
	"git": Git,
	"hg":  Hg,
}

Map of VCS name to VCS object.

Functions

This section is empty.

Types

type Commit

type Commit struct {
	ID          string
	AuthorName  string
	AuthorEmail string

	Message string

	// AuthorDate is the date when this commit was originally made. (It may
	// differ from the commit date, which is changed during rebases, etc.)
	AuthorDate time.Time
}

type Repository

type Repository interface {
	Dir() string // The repository's root directory.
	VCS() VCS

	// CurrentCommitID returns the commit ID of the HEAD or tip.
	CurrentCommitID() (string, error)

	// Downloads updates to the repository from the default remote.
	Download() error

	// CommitLog returns a list of commits in the current HEAD/tip.
	CommitLog() ([]*Commit, error)

	// CheckOut returns the path of a directory containing a working tree at revision rev. CheckOut
	// assumes that rev is local or has already been fetched; it does not update the repository.
	CheckOut(rev string) (dir string, err error)

	ReadFileAtRevision(path string, rev string) ([]byte, error)
}

func Clone

func Clone(vcs VCS, url, dir string) (Repository, error)

Clones the VCS repository from a remote URL to dir.

func CloneOrOpen

func CloneOrOpen(vcs VCS, url, dir string) (Repository, error)

If no repository exists at dir, CloneOrOpen clones the VCS repository to dir. Otherwise, it opens the repository at dir (without checking that the repository there is, indeed, cloned from the specified URL).

func Open

func Open(vcs VCS, dir string) (Repository, error)

Opens the VCS repository at dir.

type VCS

type VCS interface {
	// Clones the repository at the given URL into dir. If dir already exists, the error os.ErrExist
	// is returned.
	Clone(url, dir string) (Repository, error)

	// CloneMirror clones a new mirror repository from url at dir. This
	// corresponds to `git clone --mirror`.
	CloneMirror(url, dir string) error

	// UpdateMirror updates a mirror repository from url at dir. This
	// corresponds to `git remote update`.
	UpdateMirror(dir string) error

	Open(dir string) (Repository, error)

	ShortName() string
}
var Git VCS = git{"git"}
var Hg VCS = hg{"hg"}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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