gitreader

package module
v0.0.0-...-abd1155 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2015 License: BSD-3-Clause Imports: 14 Imported by: 2

README

gitreader

There are times, in a developers life, when they need to read data out of a git repository.

In those times, there are 3 paths to take:

  1. Shell out to the git subcommands to pull the data out
  2. Use an API that just shells out to git subcommands
  3. Use an API that implements parts of git itself

For various reasons, there are times where #1 and #2 are clunky and unacceptable. For those times, we have APIs that implement parts of git. This is one of those APIs.

This specific API implements a golang library that contains only the functionality to read a git repository on disk. No remote protocols, no ability to write new data, only reading.

So when you're in golang and need to read some data out a git repository, reach for gitreader, you'll be happy with yourself.

  • Vektra Devs

Usage

import "fmt"
import "gitreader"

repo, err := gitreader.OpenRepo("/path/to/repo")
if err != nil {
  panic(err)
}

blob, err := repo.CatFile("HEAD", "path/to/file")
if err != nil {
  panic(err)
}

// WARNING: use Blob as an io.Reader instead if you can!
bytes, err := blob.Bytes()
if err != nil {
  panic(err)
}

fmt.Printf("%s", bytes)

repo.Close()

Documentation

Overview

Implements enough of the git format to read data out of an on-disk git repo.

Index

Constants

This section is empty.

Variables

View Source
var ErrBadDelta = errors.New("bad delta")
View Source
var ErrBadIndex = errors.New("bad index format")
View Source
var ErrBadPack = errors.New("bad pack format")
View Source
var ErrInvalidRepo = errors.New("invalid repo")
View Source
var ErrNotBlob = errors.New("object is not a blob")
View Source
var ErrNotCommit = errors.New("ref is not a commit")
View Source
var ErrNotExist = errors.New("object does not exist")
View Source
var ErrNotFound = errors.New("object not found")
View Source
var ErrNotTree = errors.New("object is not a tree")
View Source
var ErrUnknownRef = errors.New("unknown ref")
View Source
var ErrUnknownType = errors.New("unknown type")

Functions

This section is empty.

Types

type Blob

type Blob struct {
	io.Reader
	// contains filtered or unexported fields
}

func (*Blob) Bytes

func (b *Blob) Bytes() ([]byte, error)

Read all the data in the blob and return it. WARNING: use Blob as an io.Reader instead if you can!

type Commit

type Commit struct {
	Parent, Tree, Author, Committer, Message string
}

type Entry

type Entry struct {
	Permissions, Name, Id string
}

type Loader

type Loader interface {
	LoadObject(id string) (*Object, error)
	Close() error
}

type LooseObject

type LooseObject struct {
	Base string
}

Implements reading objects out of the .git/objects directory

func (*LooseObject) Close

func (l *LooseObject) Close() error

func (*LooseObject) LoadObject

func (l *LooseObject) LoadObject(id string) (*Object, error)

type Object

type Object struct {
	Type string
	Size uint64
	// contains filtered or unexported fields
}

func ParseObject

func ParseObject(input io.Reader) (*Object, error)

Read the data and construct a new Object

func (*Object) Blob

func (o *Object) Blob() (*Blob, error)

Return the Object as a Blob

func (*Object) Close

func (o *Object) Close() error

Cleanup the object's resources

func (*Object) Commit

func (o *Object) Commit() (*Commit, error)

Return the Object as a Commit

func (*Object) Tree

func (o *Object) Tree() (*Tree, error)

Return the Object as a Tree

type Pack

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

Implements LoadObject for a pack file

func LoadPack

func LoadPack(path string) (*Pack, error)

Load the pack data from the given path

func (*Pack) Close

func (p *Pack) Close() error

func (*Pack) FindOffset

func (p *Pack) FindOffset(id string) (uint32, error)

func (*Pack) LoadObject

func (p *Pack) LoadObject(id string) (*Object, error)

type Repo

type Repo struct {
	Base    string
	Loaders []Loader
}

func OpenRepo

func OpenRepo(path string) (*Repo, error)

Open up a repository. Can be either normal or bare. Be sure to issue Close() on a repo when you're finished with it because that makes sure that any pack files used by the repo are properly unmapped.

func (*Repo) CatFile

func (r *Repo) CatFile(ref, path string) (*Blob, error)

Given a ref and a path to a blob, return the blob data

func (*Repo) Close

func (r *Repo) Close() error

func (*Repo) LoadObject

func (r *Repo) LoadObject(id string) (*Object, error)

Lookup an object id

func (*Repo) Resolve

func (r *Repo) Resolve(ref, path string) (string, error)

Given a ref and a path, return an object id

func (*Repo) ResolveRef

func (r *Repo) ResolveRef(ref string) (string, error)

Given a reference, return the object id for the commit

type Tree

type Tree struct {
	Entries map[string]*Entry
}

Jump to

Keyboard shortcuts

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