g

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

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

Go to latest
Published: Nov 4, 2024 License: MIT Imports: 19 Imported by: 0

README

G

About

Learning Git Internals by writing a Git in Go.

CLI Usage

See go run ./cmd/gitg --help

Documentation

Index

Constants

View Source
const (
	DefaultGitDirectory       = ".git"
	DefaultPath               = "."
	DefaultHeadFile           = "HEAD"
	DefaultIndexFile          = "index"
	DefaultObjectsDirectory   = "objects"
	DefaultRefsDirectory      = "refs"
	DefaultRefsHeadsDirectory = "heads"
	DefaultBranchName         = "main"
	DefaultEditor             = "vim"
	DefaultPackedRefsFile     = "info/refs"
	DefaultPackfileDirectory  = "pack"
	DefaultGitIgnoreFileName  = ".gitignore"
)
View Source
const (
	ObjectTypeInvalid objectType = iota
	ObjectTypeBlob
	ObjectTypeTree
	ObjectTypeCommit
)

Variables

View Source
var PackFileReadCloser = func(path string, offset int64) func() (io.ReadCloser, error) {
	return func() (io.ReadCloser, error) {
		fh, err := os.Open(path)
		if err != nil {
			return nil, err
		}
		defer func() { _ = fh.Close() }()
		_, err = fh.Seek(offset, io.SeekStart)
		if err != nil {
			return nil, err
		}
		return zlib.NewReader(fh)
	}
}

PackFileReadCloser is a Factory that creates a ReadCloser for reading Object content from a Pack File that is Not Deltified.

View Source
var PackFileReadCloserOfsDelta = func(path string, offset int64) func() (io.ReadCloser, error) {
	return func() (io.ReadCloser, error) {
		return nil, errors.New("not implemented")
	}
}
View Source
var PackFileReadCloserRefDelta = func(path string, offset int64) func() (io.ReadCloser, error) {
	return func() (io.ReadCloser, error) {
		return nil, errors.New("not implemented")
	}
}

Functions

func AuthorEmail

func AuthorEmail() string

func AuthorName

func AuthorName() string

func CommitterEmail

func CommitterEmail() string

func CommitterName

func CommitterName() string

func Configure

func Configure(opts ...Opt) error

func CreateBranch

func CreateBranch(name string) error

func CurrentBranch

func CurrentBranch() (string, error)

CurrentBranch returns the name of the current branch

func DefaultBranch

func DefaultBranch() string

func DeleteBranch

func DeleteBranch(name string) error

func Editor

func Editor() (string, []string)

func EditorFile

func EditorFile() string

func GitHeadPath

func GitHeadPath() string

func GitPath

func GitPath() string

func IndexFilePath

func IndexFilePath() string

func Init

func Init() error

Init initializes a git repository

func IsIgnored

func IsIgnored(path string, rules [][]byte) bool

func ListBranches

func ListBranches() ([]string, error)

ListBranches lists Git branches from refs/heads and info/refs It does not currently allow listing remote tracking branches

func ObjectPackfileDirectory

func ObjectPackfileDirectory() string

func ObjectPath

func ObjectPath() string

func ObjectReadCloser

func ObjectReadCloser(sha []byte) func() (io.ReadCloser, error)

func PackedRefsFile

func PackedRefsFile() string

func Pager

func Pager() (string, []string)

func Path

func Path() string

func ReadHeadBytes

func ReadHeadBytes(r io.ReadCloser, obj *Object) error

func RefsDirectory

func RefsDirectory() string

func RefsHeadPrefix

func RefsHeadPrefix() string

func RefsHeadsDirectory

func RefsHeadsDirectory() string

func Restore

func Restore(path string, staged bool) error

func RestoreStaged

func RestoreStaged(path string) error

RestoreStaged removes a staged change from the index. If the file is in the previous commit, removing it from the index means updating the index to specify the commit sha. The timestamp for which would be the same as when the files were originally switched to.

If the file is not in a previous commit, removing it from the index means simply removing it from the index.

func SwitchBranch

func SwitchBranch(name string) ([]string, error)

func UpdateBranchHead

func UpdateBranchHead(branch string, sha Sha) error

UpdateBranchHead updates the sha hash pointed to by a branch

func UpdateHead

func UpdateHead(branch string) error

UpdateHead writes branch name as a reference in the Git HEAD file

func WorkingDirectory

func WorkingDirectory() string

Types

type Cnf

type Cnf struct {
	// GitDirector configures where the name of the git directory
	// This is usually .git
	GitDirectory string
	// Path configures where the Git Directory to interact with is
	// relative to the present working directory. This is usually .
	Path string

	HeadFile           string
	IndexFile          string
	ObjectsDirectory   string
	RefsDirectory      string
	RefsHeadsDirectory string
	PackedRefsFile     string
	PackfileDirectory  string
	DefaultBranch      string
	GitIgnore          [][]byte
	Editor             string
	EditorArgs         []string
	GitIgnoreFileName  string
}

type Commit

type Commit struct {
	Sha            Sha
	Tree           Sha
	Parents        []Sha
	Author         string
	AuthorEmail    string
	AuthoredTime   time.Time
	Committer      string
	CommitterEmail string
	CommittedTime  time.Time
	Sig            []byte
	Message        []byte
}

func ReadCommit

func ReadCommit(sha Sha) (*Commit, error)

func (Commit) String

func (c Commit) String() string

type FfileSet

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

func CommittedFilesForBranchHead

func CommittedFilesForBranchHead(name string) (*FfileSet, error)

func CurrentStatus

func CurrentStatus() (*FfileSet, error)

func FsStatus

func FsStatus(path string) (*FfileSet, error)

FsStatus returns a FfileSet containing all files from the index and working directory with the corresponding status.

func NewFfileSet

func NewFfileSet(c []*FileStatus, i []*FileStatus, w []*FileStatus) (*FfileSet, error)

func Status

func Status(idx *Index, commitSha Sha) (*FfileSet, error)

Status returns a FfileSet containing all files from commit, index and working directory with the corresponding status.

func (*FfileSet) Contains

func (f *FfileSet) Contains(path string) (*FileStatus, bool)

func (*FfileSet) Files

func (f *FfileSet) Files() []*FileStatus

type FileStatus

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

func CommittedFiles

func CommittedFiles(sha Sha) ([]*FileStatus, error)

func Ls

func Ls(path string) ([]*FileStatus, error)

Ls recursively lists files in path that are not ignored

func (FileStatus) IndexStatus

func (f FileStatus) IndexStatus() IndexStatus

func (FileStatus) Path

func (f FileStatus) Path() string

func (FileStatus) WorkingDirectoryStatus

func (f FileStatus) WorkingDirectoryStatus() WDStatus

type Finfo

type Finfo struct {
	CTimeS uint32
	CTimeN uint32
	MTimeS uint32
	MTimeN uint32
	Dev    uint32
	Ino    uint32
	MMode  uint32
	Uid    uint32
	Gid    uint32
	SSize  uint32
	Sha    [20]byte
	NName  string
}

func (*Finfo) IsDir

func (fi *Finfo) IsDir() bool

func (*Finfo) ModTime

func (fi *Finfo) ModTime() time.Time

func (*Finfo) Mode

func (fi *Finfo) Mode() os.FileMode

func (*Finfo) Name

func (fi *Finfo) Name() string

func (*Finfo) Size

func (fi *Finfo) Size() int64

func (*Finfo) Sys

func (fi *Finfo) Sys() any

type Index

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

Index represents the Git Index

func NewIndex

func NewIndex() *Index

func ReadIndex

func ReadIndex() (*Index, error)

ReadIndex reads the Git Index into an Index struct

func (*Index) Add

func (idx *Index) Add(f *FileStatus) error

Add adds a fs.FileStatus to the Index Struct. A call to idx.Write is required to flush the changes to the filesystem.

func (*Index) File

func (idx *Index) File(path string) *FileStatus

func (*Index) Files

func (idx *Index) Files() []*FileStatus

Files lists the files in the index

func (*Index) Rm

func (idx *Index) Rm(path string) error

Rm removes a item from the Index A call to idx.Write is required to persist the change.

func (*Index) Write

func (idx *Index) Write() error

Write writes an Index struct to the Git Index

type IndexStatus

type IndexStatus uint8
const (
	// NotUpdated means that the modification time of the file in the
	// commit is the same as the one in the index.
	NotUpdated IndexStatus = iota

	// UpdatedInIndex means that the modification time of the file in the
	// index is newer than that of the commit
	UpdatedInIndex

	// TypeChangedInIndex mentioned in the Git docs but not implemented
	// here - file type changed (regular file, symbolic link or submodule)
	TypeChangedInIndex

	// AddedInIndex means that the file is in the index but not in the
	// commit.
	AddedInIndex

	// DeletedInIndex means that the file has been removed from the index
	DeletedInIndex

	// RenamedInIndex - @todo I do not understand how to implement this
	RenamedInIndex

	// CopiedInIndex - @todo not sure about this one either
	// - copied (if config option status.renames is set to "copies")
	CopiedInIndex

	// UntrackedInIndex means that the file is not in the Index
	UntrackedInIndex
)

func (IndexStatus) StatusString

func (is IndexStatus) StatusString() string

func (IndexStatus) String

func (is IndexStatus) String() string

type Mtime

type Mtime struct {
	Sec  uint32
	Nsec uint32
}

type Object

type Object struct {
	Path         string
	Typ          objectType
	Sha          Sha
	Objects      []*Object
	Length       int
	HeaderLength int
	ReadCloser   func() (io.ReadCloser, error)
}

func ObjectTree

func ObjectTree(files []*FileStatus) *Object

ObjectTree creates a Tree Object with child Objects representing the files and paths in the provided files.

func ReadObject

func ReadObject(sha Sha) (*Object, error)

func ReadObjectTree

func ReadObjectTree(sha Sha) (*Object, error)

ReadObjectTree reads an object from the object store

func WriteBlob

func WriteBlob(path string) (*Object, error)

WriteBlob writes a file to the object store as a blob and returns a Blob Object representation.

func (*Object) FlattenTree

func (o *Object) FlattenTree() []*FileStatus

FlattenTree turns a TreeObject structure into a flat list of file paths

func (*Object) WriteTree

func (o *Object) WriteTree() (Sha, error)

WriteTree writes an Object Tree to the object store.

type Opt

type Opt func(m *Cnf) error

func WithGitDirectory

func WithGitDirectory(name string) Opt

func WithPath

func WithPath(path string) Opt

type PackObjectType

type PackObjectType uint8
const (
	ObjCommit PackObjectType
	ObjTree
	ObjBlob
	ObjTag
	ObjOfsDelta
	ObjRefDelta
)

type Sha

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

func CreateCommit

func CreateCommit(commit *Commit) (Sha, error)

CreateCommit writes the Commit provided in the Object Store

func CurrentCommit

func CurrentCommit() (Sha, error)

CurrentCommit return the current commit SHA @todo this probably breaks in detached head...

func HeadSHA

func HeadSHA(currentBranch string) (Sha, error)

HeadSHA returns the hash pointed to by a branch

func NewSha

func NewSha(b []byte) (Sha, error)

NewSha creates a Sha from either a binary or hex encoded byte slice

func PreviousCommits

func PreviousCommits() ([]Sha, error)

func ShaFromHexString

func ShaFromHexString(s string) (Sha, error)

func WriteObject

func WriteObject(header []byte, content []byte, contentFile string, path string) (Sha, error)

WriteObject writes an object to the object store

func (Sha) AsArray

func (s Sha) AsArray() [20]byte

func (Sha) AsByteSlice

func (s Sha) AsByteSlice() []byte

AsByteSlice returns a Sha as a byte slice

func (Sha) AsHexBytes

func (s Sha) AsHexBytes() []byte

func (Sha) AsHexString

func (s Sha) AsHexString() string

func (Sha) IsSet

func (s Sha) IsSet() bool

func (Sha) Matches

func (s Sha) Matches(ss Sha) bool

func (Sha) String

func (s Sha) String() string

type Tree

type Tree struct {
	Sha   Sha
	Typ   objectType
	Path  string
	Items []*TreeItem
}

func ReadTree

func ReadTree(obj *Object) (*Tree, error)

type TreeItem

type TreeItem struct {
	Sha  []byte
	Typ  objectType
	Path string
}

type WDStatus

type WDStatus uint8
const (
	// IndexAndWorkingTreeMatch means the file modification time in the
	// working directory is the same as in the index
	IndexAndWorkingTreeMatch WDStatus = iota

	// WorktreeChangedSinceIndex means the file in the working directory has
	// a newer modification time than the file in the index
	WorktreeChangedSinceIndex

	// TypeChangedInWorktreeSinceIndex is not implemented
	TypeChangedInWorktreeSinceIndex

	// DeletedInWorktree means that the file has been removed from the working
	// directory but exists in the commit
	DeletedInWorktree

	// RenamedInWorktree not implemented
	RenamedInWorktree

	// CopiedInWorktree not implemented
	CopiedInWorktree

	// Untracked means that the file is in the working directory but not in
	// the index or commit
	Untracked
)

func (WDStatus) StatusString

func (wds WDStatus) StatusString() string

func (WDStatus) String

func (wds WDStatus) String() string

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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