git

package module
v0.0.0-...-2ee2aa9 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2022 License: MIT Imports: 10 Imported by: 0

README

Go Reference codecov Go Report Card Build Workflow

git-go

Basic git implementation in pure Go

Current features

CLI
Porcelain
  • init
Plumbing
  • hash-object
  • cat-file
Library
  • Retrieve objects
  • Write loose objects
  • Read/Write References

Roadmap

CLI

See the CLI project

Library

See the Library project

Dev

We use task as task runner / build tool. The main commands are:

  • task test to run the tests
  • task install to install the git-go to the GOPATH
  • task build to create a git binary in the ./bin directory
  • task dev -w to have the binary at ./bin/git automatically rebuilt with every change in the code

Getting Started with the lib

The git package should contain everything you need to do most of the common operations. For more advanced operations, you should use the ginternals package.

You can take a look at our smoke tests for examples of usage.

Documentation

Overview

Package git contains methods to deal with git internals

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRepositoryNotExist           = errors.New("repository does not exist")
	ErrRepositoryUnsupportedVersion = errors.New("repository nor supported")
	ErrTagNotFound                  = errors.New("tag not found")
	ErrTagExists                    = errors.New("tag already exists")
	ErrNotADirectory                = errors.New("not a directory")
	ErrInvalidBranchName            = errors.New("invalid branch name")
)

List of errors returned by the Repository struct

Functions

This section is empty.

Types

type InitOptions

type InitOptions struct {
	// GitBackend represents the underlying backend to use to init the
	// repository and interact with the odb
	// By default the filesystem will be used
	GitBackend *backend.Backend
	// WorkingTreeBackend represents the underlying backend to use to
	// interact with the working tree.
	// By default the filesystem will be used
	// Setting this is useless if IsBare is set to true
	WorkingTreeBackend afero.Fs
	// InitialBranchName represents the name of the default branch to use
	// Defaults to master
	InitialBranchName string
	// IsBare represents whether a bare repository will be created or not
	IsBare bool
	// Symlink will create a .git text file in the working tree that points
	// toward the actual repository
	Symlink bool
}

InitOptions contains all the optional data used to initialized a repository

type OpenOptions

type OpenOptions struct {
	// GitBackend represents the underlying backend to use to init the
	// repository and interact with the odb
	// By default the filesystem will be used
	GitBackend *backend.Backend
	// WorkingTreeBackend represents the underlying backend to use to
	// interact with the working tree.
	// By default the filesystem will be used
	// Setting this is useless if IsBare is set to true
	WorkingTreeBackend afero.Fs
	// GitDirPath represents the path to the .git directory
	// Defaults to .git
	// IsBare represents whether a bare repository will be created or not
	IsBare bool
}

OpenOptions contains all the optional data used to open a repository

type Repository

type Repository struct {
	Config *config.Config
	// contains filtered or unexported fields
}

Repository represent a git repository A Git repository is the .git/ folder inside a project. This repository tracks all changes made to files in your project, building a history over time. https://blog.axosoft.com/learning-git-repository/

func InitRepository

func InitRepository(workTreePath string) (*Repository, error)

InitRepository initialize a new git repository by creating the .git directory in the given path, which is where almost everything that Git stores and manipulates is located. https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain#ch10-git-internals

This assumes: - The repo is not bare (see WithOptions) - We're not interested in env vars (see WithParams) - The git dir is in the working tree under .git

func InitRepositoryWithOptions

func InitRepositoryWithOptions(rootPath string, opts InitOptions) (r *Repository, err error)

InitRepositoryWithOptions initialize a new git repository by creating the .git directory in the given path, which is where almost everything that Git stores and manipulates is located. https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain#ch10-git-internals

This assumes: - We're not interested in env vars (see WithParams) - The git dir is in the working tree under .git

func InitRepositoryWithParams

func InitRepositoryWithParams(cfg *config.Config, opts InitOptions) (r *Repository, err error)

InitRepositoryWithParams initialize a new git repository by creating the .git directory in the given path, which is where almost everything that Git stores and manipulates is located. https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain#ch10-git-internals

This method makes no assumptions

func OpenRepository

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

OpenRepository loads an existing git repository by reading its config file, and returns a Repository instance

This assumes: - The repo is not bare (see WithOptions) - We're not interested in env vars (see WithParams) - The git dir is in the working tree under .git

func OpenRepositoryWithOptions

func OpenRepositoryWithOptions(rootPath string, opts OpenOptions) (r *Repository, err error)

OpenRepositoryWithOptions loads an existing git repository by reading its config file, and returns a Repository instance

This assumes: - We're not interested in env vars (see WithParams) - The git dir is in the working tree under .git

func OpenRepositoryWithParams

func OpenRepositoryWithParams(cfg *config.Config, opts OpenOptions) (r *Repository, err error)

OpenRepositoryWithParams loads an existing git repository by reading its config file, and returns a Repository instance

This method makes no assumptions

func (*Repository) Blob

func (r *Repository) Blob(oid ginternals.Oid) (*object.Blob, error)

Blob returns the blob matching the given ID This method will always work as long as the OID points to a valid object. Calling Blob with a commit OID, will return the raw data of the commit.

func (*Repository) Close

func (r *Repository) Close() error

Close frees the resources used by the repository

func (*Repository) Commit

func (r *Repository) Commit(oid ginternals.Oid) (*object.Commit, error)

Commit returns the commit matching the given SHA

func (*Repository) IsBare

func (r *Repository) IsBare() bool

IsBare returns whether the repo is bare or not. A bare repo doesn't have a workign tree

func (*Repository) NewBlob

func (r *Repository) NewBlob(data []byte) (*object.Blob, error)

NewBlob creates, stores, and returns a new Blob object

func (*Repository) NewCommit

func (r *Repository) NewCommit(refname string, tree *object.Tree, author object.Signature, opts *object.CommitOptions) (*object.Commit, error)

NewCommit creates, stores, and returns a new Commit object The head of the reference $refname will be updated to this new commit. An empty refName will create a detached (loose) commit If the reference doesn't exists, it will be created

func (*Repository) NewDetachedCommit

func (r *Repository) NewDetachedCommit(tree *object.Tree, author object.Signature, opts *object.CommitOptions) (*object.Commit, error)

NewDetachedCommit creates, stores, and returns a new Commit object not attached to any reference

func (*Repository) NewLightweightTag

func (r *Repository) NewLightweightTag(tag string, targetID ginternals.Oid) (*ginternals.Reference, error)

NewLightweightTag creates, stores, and returns a lightweight tag

func (*Repository) NewReference

func (r *Repository) NewReference(name string, target ginternals.Oid) (*ginternals.Reference, error)

NewReference creates, stores, and returns a new reference If the reference already exists, it will be overwritten

func (*Repository) NewReferenceSafe

func (r *Repository) NewReferenceSafe(name string, target ginternals.Oid) (*ginternals.Reference, error)

NewReferenceSafe creates, stores, and returns a new reference If the reference already exists, the method will fail

func (*Repository) NewSymbolicReference

func (r *Repository) NewSymbolicReference(name, target string) (*ginternals.Reference, error)

NewSymbolicReference creates, stores, and returns a new symbolic reference If the reference already exists, it will be overwritten

func (*Repository) NewSymbolicReferenceSafe

func (r *Repository) NewSymbolicReferenceSafe(name, target string) (*ginternals.Reference, error)

NewSymbolicReferenceSafe creates, stores, and returns a new symbolic reference. If the reference already exists, the method will fail

func (*Repository) NewTag

func (r *Repository) NewTag(p *object.TagParams) (*object.Tag, error)

NewTag creates, stores, and returns a new annoted tag

func (*Repository) NewTreeBuilder

func (r *Repository) NewTreeBuilder() *TreeBuilder

NewTreeBuilder create a new empty tree builder

func (*Repository) NewTreeBuilderFromTree

func (r *Repository) NewTreeBuilderFromTree(t *object.Tree) *TreeBuilder

NewTreeBuilderFromTree create a new tree builder containing the entries of another tree

func (*Repository) Object

func (r *Repository) Object(oid ginternals.Oid) (*object.Object, error)

Object returns the object matching the given ID

func (*Repository) Reference

func (r *Repository) Reference(name string) (*ginternals.Reference, error)

Reference returns the reference matching the given name

func (*Repository) Tag

func (r *Repository) Tag(name string) (*ginternals.Reference, error)

Tag returns the reference for the given tag To know if the tag is annoted or lightweight, call repo.GetObject() on the reference's target ad make sure that the returned object is not a tag with the same name (note that it's technically possible for a tag to target another tag)

func (*Repository) Tree

func (r *Repository) Tree(oid ginternals.Oid) (*object.Tree, error)

Tree returns the tree matching the given SHA

type TreeBuilder

type TreeBuilder struct {
	Backend *backend.Backend
	// contains filtered or unexported fields
}

TreeBuilder is used to build trees

func (*TreeBuilder) Insert

func (tb *TreeBuilder) Insert(path string, oid ginternals.Oid, mode object.TreeObjectMode) error

Insert inserts a new object in a tree

func (*TreeBuilder) Remove

func (tb *TreeBuilder) Remove(path string)

Remove removes an object from tree

func (*TreeBuilder) Write

func (tb *TreeBuilder) Write() (*object.Tree, error)

Write creates and persists a new Tree object

Directories

Path Synopsis
Package backend contains structs and methods to store and retrieve data from the .git directory
Package backend contains structs and methods to store and retrieve data from the .git directory
cmd
Package env contains a wrapper around the environment, making it easier to write code and tests that use environment variables.
Package env contains a wrapper around the environment, making it easier to write code and tests that use environment variables.
Package ginternals contains objects and methods to work on git internals
Package ginternals contains objects and methods to work on git internals
config
Package config contains structs to interact with git configuration as well as to configure the library
Package config contains structs to interact with git configuration as well as to configure the library
object
Package object contains methods and objects to work with git objects
Package object contains methods and objects to work with git objects
packfile
Package packfile contains methods and structs to read and write packfiles
Package packfile contains methods and structs to read and write packfiles
internal
cache
Package cache contains structs and methods to cache data
Package cache contains structs and methods to cache data
errutil
Package errutil contains methods to simplify working with error
Package errutil contains methods to simplify working with error
pathutil
Package pathutil contains helpers to work with paths
Package pathutil contains helpers to work with paths
readutil
Package readutil contains helper methods to simplify reading operations
Package readutil contains helper methods to simplify reading operations
syncutil
Package syncutil contains structs and helpers to simplify concurrency
Package syncutil contains structs and helpers to simplify concurrency
testutil
Package testutil contains helpers to simplify tests
Package testutil contains helpers to simplify tests
testutil/confutil
Package confutil contains helpers and function to generate basic configuration
Package confutil contains helpers and function to generate basic configuration
testutil/exe
Package exe contains helpers to help running commands
Package exe contains helpers to help running commands
Package smoke contains a bunch of smoke tests
Package smoke contains a bunch of smoke tests

Jump to

Keyboard shortcuts

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