gogit

package module
Version: v0.0.0-...-c5cbd8f Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2020 License: MIT Imports: 17 Imported by: 4

README

gogit

Pure Go read access to a git repository.

State: Not really actively maintained, but used in production site. Without warranty, of course.
Maturity level: 4/5 (works well in all tested repositories, there will be no API change (unless a critical flaw is found), few corner cases not implemented yet)
License: Free software (MIT License)
Installation: Just run go get github.com/speedata/gogit
API documentation: https://godoc.org/github.com/speedata/gogit
Contact: gundlach@speedata.de, @speedata
Repository: https://github.com/speedata/gogit
Dependencies: None
Contribution: We like to get any kind of feedback (success stories, bug reports, merge requests, ...)

Example

Sample application to list the latest directory (recursively):

package main

import (
    "github.com/speedata/gogit"
    "log"
    "os"
    "path"
    "path/filepath"
)

func walk(dirname string, te *gogit.TreeEntry) int {
    log.Println(path.Join(dirname, te.Name))
    return 0
}

func main() {
    wd, err := os.Getwd()
    if err != nil {
        log.Fatal(err)
    }
    repository, err := gogit.OpenRepository(filepath.Join(wd, "src/github.com/speedata/gogit/_testdata/testrepo.git"))
    if err != nil {
        log.Fatal(err)
    }
    ref, err := repository.LookupReference("HEAD")
    if err != nil {
        log.Fatal(err)
    }
    ci, err := repository.LookupCommit(ref.Oid)
    if err != nil {
        log.Fatal(err)
    }
    ci.Tree.Walk(walk)
}

Sample application

We use gogit as the backend in https://ctanmirror.speedata.de. This is a mirror of CTAN, the comprehensive TeX archive network with approx. 30GB of data. We rsync it from the main site at ctan.org every night and add the changes to a git repository (with the regular git command). Then we use this web front end to retrieve the historic files.

The git repository is around 500 GB (May 2017) and dates back to 2013.

Documentation

Overview

Read access to a git repository.

The api is inspired but not compatible with the Go bindings of libgit2 (git2go).

Index

Constants

View Source
const (
	VersionMajor      = 0
	VersionMinor      = 0
	VersionPatchlevel = 0
)

Version information: ‹Major›.‹Minor›.‹Patchlevel›

View Source
const (
	FileModeBlob     = 0100644
	FileModeBlobExec = 0100755
	FileModeSymlink  = 0120000
	FileModeCommit   = 0160000
	FileModeTree     = 0040000
)

There are only a few file modes in Git. They look like unix file modes, but they can only be one of these.

Variables

This section is empty.

Functions

This section is empty.

Types

type Blob

type Blob struct {
	// contains filtered or unexported fields
}

func (*Blob) Contents

func (b *Blob) Contents() []byte

func (*Blob) Size

func (b *Blob) Size() int

type Commit

type Commit struct {
	Author        *Signature
	Committer     *Signature
	Oid           *Oid // The id of this commit object
	CommitMessage string
	Tree          *Tree
	// contains filtered or unexported fields
}

func (*Commit) Id

func (ci *Commit) Id() *Oid

Get the id of the commit.

func (*Commit) Message

func (ci *Commit) Message() string

Return the commit message. Same as retrieving CommitMessage directly.

func (*Commit) Parent

func (ci *Commit) Parent(n int) *Commit

Return parent number n (0-based index)

func (*Commit) ParentCount

func (ci *Commit) ParentCount() int

Return the number of parents of the commit. 0 if this is the root commit, otherwise 1,2,...

func (*Commit) ParentId

func (ci *Commit) ParentId(n int) *Oid

Return oid of the parent number n (0-based index). Return nil if no such parent exists.

func (*Commit) TreeId

func (ci *Commit) TreeId() *Oid

Return oid of the (root) tree of this commit.

type Object

type Object struct {
	Type ObjectType
	Oid  *Oid
}

type ObjectType

type ObjectType int

Who am I?

const (
	ObjectCommit ObjectType = 0x10
	ObjectTree   ObjectType = 0x20
	ObjectBlob   ObjectType = 0x30
	ObjectTag    ObjectType = 0x40
)

func (ObjectType) String

func (t ObjectType) String() string

type Oid

type Oid struct {
	Bytes SHA1
}

Oid is the representation of a sha1-string

func NewOid

func NewOid(b []byte) (*Oid, error)

Create a new Oid from a 20 byte slice.

func NewOidFromArray

func NewOidFromArray(a SHA1) *Oid

Create a new Oid from a 20 byte array

func NewOidFromByteString

func NewOidFromByteString(b []byte) (*Oid, error)

Create a new Oid from a 40 byte hex-encoded slice.

func NewOidFromString

func NewOidFromString(sha1 string) (*Oid, error)

Create a new Oid from a Sha1 string of length 40. In performance-sensitive paths, use NewOidFromByteString.

func (*Oid) Equal

func (o *Oid) Equal(oid2 *Oid) bool

Equal reports whether o and oid2 have the same sha1.

func (*Oid) String

func (o *Oid) String() string

Return string (hex) representation of the Oid

type Reference

type Reference struct {
	Name string
	Oid  *Oid
	// contains filtered or unexported fields
}

func (*Reference) Target

func (r *Reference) Target() *Oid

For compatibility with git2go. Return Oid from referece (same as getting .Oid directly)

type Repository

type Repository struct {
	Path string
	// contains filtered or unexported fields
}

A Repository is the base of all other actions. If you need to lookup a commit, tree or blob, you do it from here.

func OpenRepository

func OpenRepository(path string) (*Repository, error)

Open the repository at the given path.

func (*Repository) LookupBlob

func (repos *Repository) LookupBlob(oid *Oid) (*Blob, error)

Find the blob object in the repository.

func (*Repository) LookupCommit

func (repos *Repository) LookupCommit(oid *Oid) (*Commit, error)

Find the commit object in the repository.

func (*Repository) LookupReference

func (repos *Repository) LookupReference(name string) (*Reference, error)

A typical Git repository consists of objects (path objects/ in the root directory) and of references to HEAD, branches, tags and such.

func (*Repository) LookupTag

func (repos *Repository) LookupTag(oid *Oid) (*Tag, error)

Find the tag object in the repository.

func (*Repository) LookupTree

func (repos *Repository) LookupTree(oid *Oid) (*Tree, error)

Find the tree object in the repository.

func (*Repository) ObjectSize

func (repos *Repository) ObjectSize(oid *Oid) (int64, error)

Get (inflated) size of an object.

func (*Repository) Type

func (repos *Repository) Type(oid *Oid) (ObjectType, error)

Get the type of an object.

type SHA1

type SHA1 [20]byte

type Signature

type Signature struct {
	Email string
	Name  string
	When  time.Time
}

Author and Committer information

type Tag

type Tag struct {
	Type     TagType
	Name     string
	Message  string
	TargetId *Oid
	Tagger   *Signature
	// contains filtered or unexported fields
}

type TagType

type TagType int

Who am I?

const (
	TagCommit TagType = iota
)

type Tree

type Tree struct {
	TreeEntries []*TreeEntry
	Oid         *Oid
	// contains filtered or unexported fields
}

A tree is a flat directory listing.

func (*Tree) EntryByIndex

func (t *Tree) EntryByIndex(index int) *TreeEntry

Get the n-th entry of this tree (0 = first entry). You can also access t.TreeEntries[index] directly.

func (*Tree) EntryByName

func (t *Tree) EntryByName(name string) *TreeEntry

Find the entry in this directory (tree) with the given name.

func (*Tree) EntryCount

func (t *Tree) EntryCount() int

Get the number of entries in the directory (tree). Same as len(t.TreeEntries).

func (*Tree) Walk

func (t *Tree) Walk(callback TreeWalkCallback) error

The entries will be traversed in the specified order, children subtrees will be automatically loaded as required, and the callback will be called once per entry with the current (relative) root for the entry and the entry data itself.

If the callback returns a positive value, the passed entry will be skipped on the traversal (in pre mode). A negative value stops the walk.

Walk will panic() if an error occurs

type TreeEntry

type TreeEntry struct {
	Filemode int
	Name     string
	Id       *Oid
	Type     ObjectType
}

A tree entry is similar to a directory entry (file name, type) in a real file system.

type TreeWalkCallback

type TreeWalkCallback func(string, *TreeEntry) int

Jump to

Keyboard shortcuts

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