repo

package module
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2024 License: Apache-2.0 Imports: 22 Imported by: 4

README

go-repo

Go-Repo is just wrapper arround git commands.

GoDoc Build Status Go Report Card

Clone a repository

    r, err := Clone(path, "https://github.com/fsamin/go-repo.git")
    ...

Get current branch

    r, err := Clone(path, "https://github.com/fsamin/go-repo.git")
    ...
    b, err := r.CurrentBranch()
    ...
    fmt.Println(b)

Fetch & Pull a remote branch

    r, err := Clone(path, "https://github.com/fsamin/go-repo.git")
    ...
    err = r.FetchRemoteBranch("origin", "tests")
    ...
    err = r.Pull("origin", "tests")
    ...

Git local config

    r, err := Clone(path, "https://github.com/fsamin/go-repo.git")
    ...
    r.LocalConfigSet("foo", "bar", "value"))
    val, err := r.LocalConfigGet("foo", "bar")
    ...

Search and open files

    r, err := Clone(path, "https://github.com/fsamin/go-repo.git")
    ...
    files, err := r.Glob("**/*.md")
    ...
    f, err := r.Open(files[0])
    ...

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthOpts

type AuthOpts struct {
	Username   string
	Password   string
	PrivateKey *SSHKey
	SignKey    *PGPKey
}

AuthOpts is a optional structs for git command

type BareRepo added in v0.1.7

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

func NewBare added in v0.1.7

func NewBare(ctx context.Context, path string, opts ...Option) (b BareRepo, err error)

NewBare instanciance a bare repo instance from the path assuming the repo has already been cloned in.

func (BareRepo) CommitsBetween added in v0.1.9

func (b BareRepo) CommitsBetween(ctx context.Context, from, to time.Time, branch string) ([]Commit, error)

func (BareRepo) DefaultBranch added in v0.1.9

func (b BareRepo) DefaultBranch(ctx context.Context) (string, error)

func (BareRepo) FetchURL added in v0.1.7

func (b BareRepo) FetchURL(ctx context.Context) (string, error)

func (BareRepo) FileSize added in v0.1.7

func (b BareRepo) FileSize(ctx context.Context, filename string) (int64, error)

func (BareRepo) ListFiles added in v0.1.7

func (b BareRepo) ListFiles(ctx context.Context) ([]string, error)

func (BareRepo) Name added in v0.1.7

func (b BareRepo) Name(ctx context.Context) (string, error)

func (BareRepo) Path added in v0.1.7

func (b BareRepo) Path() string

func (BareRepo) ReadFile added in v0.1.7

func (b BareRepo) ReadFile(ctx context.Context, filename string) (io.Reader, error)

func (BareRepo) Tags added in v0.1.9

func (b BareRepo) Tags(ctx context.Context) ([]Tag, error)

type CloneOpts

type CloneOpts struct {
	Recursive               *bool
	NoStrictHostKeyChecking *bool
	Auth                    *AuthOpts
}

CloneOpts is a optional structs for git clone command

type Commit

type Commit struct {
	LongHash    string
	Hash        string
	Author      string
	AuthorEmail string
	Subject     string
	Body        string
	Date        time.Time
	Files       map[string]File
	GPGKeyID    string
}

Commit represent a git commit

type DescribeOpt added in v0.3.0

type DescribeOpt struct {
	DirtySemver      bool
	LongSemver       bool
	Long             bool
	RequireAnnotated bool
	Match            string
	DirtyMark        string
}

type Description added in v0.3.0

type Description struct {
	Dirty        bool            `json:"dirty"`
	Raw          string          `json:"raw"`
	Hash         string          `json:"hash"`
	Distance     int             `json:"distance"`
	Tag          string          `json:"tag"`
	Semver       *semver.Version `json:"semver"`
	Suffix       string          `json:"suffix"`
	SemverString string          `json:"semver_string"`
}

type File

type File struct {
	Filename   string
	Status     string
	Diff       string
	DiffDetail FileDiffDetail
}

type FileDiffDetail

type FileDiffDetail struct {
	Hunks []Hunk
}

func (FileDiffDetail) Matches

func (d FileDiffDetail) Matches(regexp *regexp.Regexp) (hunks []Hunk, addedLinewMatch bool, removedLinewMatch bool)

type Hunk

type Hunk struct {
	Header       string
	Content      string
	RemovedLines []string
	AddedLines   []string
}

type Option

type Option func(ctx context.Context, r *Repo) error

Option is a function option

func InstallPGPKey

func InstallPGPKey(privateKey []byte) Option

InstallPGPKey install a pgp key in the repo configuration

func WithHTTPAuth

func WithHTTPAuth(username string, password string) Option

WithHTTPAuth override the repo configuration to use http auth

func WithSSHAuth

func WithSSHAuth(privateKey []byte) Option

WithSSHAuth configure the git command to use a specific private key

func WithSignKey added in v0.2.0

func WithSignKey(keyId string) Option

func WithUser

func WithUser(email, name string) Option

WithUser configure the git command to use user

func WithVerbose

func WithVerbose(logger func(format string, i ...interface{})) Option

WithVerbose add some logs

type PGPKey

type PGPKey struct {
	Name    string
	Public  string
	Private string
	ID      string
}

PGPKey is a type for a pgp key

type Repo

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

Repo is the main type of this lib

func Clone

func Clone(ctx context.Context, path, url string, opts ...Option) (Repo, error)

Clone a git repository from the specified url to the destination path. Use Options to force the use of SSH Key and or PGP Key on this repo

func CloneBare added in v0.1.7

func CloneBare(ctx context.Context, path, url string, opts ...Option) (Repo, error)

CloneBare a git bare repository from the specified url to the destination path. Use Options to force the use of SSH Key and or PGP Key on this repo

func New

func New(ctx context.Context, path string, opts ...Option) (r Repo, err error)

New instanciance a repo instance from the path assuming the repo has already been cloned in.

func (Repo) Add

func (r Repo) Add(ctx context.Context, s ...string) error

Add file contents to the index

func (Repo) Checkout

func (r Repo) Checkout(ctx context.Context, branch string) error

Checkout checkouts a branch on the local repository

func (Repo) CheckoutNewBranch

func (r Repo) CheckoutNewBranch(ctx context.Context, branch string) error

CheckoutNewBranch checkouts a new branch on the local repository

func (Repo) Commit

func (r Repo) Commit(ctx context.Context, m string, opts ...Option) error

Commit the index

func (Repo) Commits

func (r Repo) Commits(ctx context.Context, from, to string) ([]Commit, error)

Commits returns all the commit between

func (Repo) CommitsBetween added in v0.1.9

func (r Repo) CommitsBetween(ctx context.Context, from, to time.Time, branch string) ([]Commit, error)

func (Repo) CurrentBranch

func (r Repo) CurrentBranch(ctx context.Context) (string, error)

CurrentBranch returns the current branch

func (Repo) CurrentSnapshot

func (r Repo) CurrentSnapshot(ctx context.Context) (map[string]File, error)

func (Repo) DefaultBranch

func (r Repo) DefaultBranch(ctx context.Context) (string, error)

DefaultBranch returns the default branch of the remote origin

func (Repo) DeleteBranch

func (r Repo) DeleteBranch(ctx context.Context, branch string) error

DeleteBranch deletes a branch on the local repository

func (Repo) DeleteHook

func (r Repo) DeleteHook(name string) error

func (Repo) Describe added in v0.3.0

func (r Repo) Describe(ctx context.Context, opt *DescribeOpt) (*Description, error)

func (Repo) Diff

func (r Repo) Diff(ctx context.Context, hash string, filename string) (string, error)

func (Repo) ExistsDiff added in v0.1.6

func (r Repo) ExistsDiff(ctx context.Context) bool

ExistsDiff returns true if there are no commited diff in the repo.

func (Repo) FetchRemoteBranch

func (r Repo) FetchRemoteBranch(ctx context.Context, remote, branch string) error

FetchRemoteBranch runs a git fetch then checkout the remote branch

func (Repo) FetchRemoteTag added in v0.1.3

func (r Repo) FetchRemoteTag(ctx context.Context, remote, tag string) error

FetchRemoteTag deletes given tag if exists, then fetch new tags and checkout given tag.

func (Repo) FetchRemoteTags added in v0.2.0

func (r Repo) FetchRemoteTags(ctx context.Context, remote string) error

FetchRemoteTags fetch all tags

func (Repo) FetchURL

func (r Repo) FetchURL(ctx context.Context) (string, error)

FetchURL returns the git URL the the remote origin

func (Repo) GetCommit

func (r Repo) GetCommit(ctx context.Context, hash string) (Commit, error)

GetCommit returns a commit

func (Repo) GetCommitWithDiff added in v0.1.4

func (r Repo) GetCommitWithDiff(ctx context.Context, hash string) (Commit, error)

GetCommitWithDiff return the commit data with the parsed diff

func (Repo) GetTag added in v0.4.0

func (r Repo) GetTag(ctx context.Context, tagName string) (Tag, error)

GetTag returns a tag

func (Repo) Glob

func (r Repo) Glob(s string) ([]string, error)

Glob returns the matching files in the repo

func (Repo) HasDiverged

func (r Repo) HasDiverged(ctx context.Context) (bool, error)

func (Repo) HookList

func (r Repo) HookList() ([]string, error)

func (Repo) LatestCommit

func (r Repo) LatestCommit(ctx context.Context) (Commit, error)

LatestCommit returns the latest commit of the current branch

func (Repo) LocalBranchExists added in v0.1.6

func (r Repo) LocalBranchExists(ctx context.Context, branch string) (exists, hasUpstream bool)

LocalBranchExists returns if given branch exists locally and has upstream.

func (Repo) LocalConfigGet

func (r Repo) LocalConfigGet(ctx context.Context, section, key string) (string, error)

LocalConfigGet returns data from the local git config

func (Repo) LocalConfigSet

func (r Repo) LocalConfigSet(ctx context.Context, section, key, value string) error

LocalConfigSet set data in the local git config

func (Repo) Name

func (r Repo) Name(ctx context.Context) (string, error)

Name returns the name of the repo, deduced from the remote origin URL

func (Repo) Open

func (r Repo) Open(s string) (*os.File, error)

Open opens a file from the repo

func (Repo) Pull

func (r Repo) Pull(ctx context.Context, remote, branch string) error

Pull pulls a branch from a remote

func (Repo) Push

func (r Repo) Push(ctx context.Context, remote, branch string, opts ...Option) error

Push (always with force) the branch

func (Repo) PushTags added in v0.2.0

func (r Repo) PushTags(ctx context.Context, remote string, opts ...Option) error

PushTags (always with force) the branch

func (Repo) RemoteAdd added in v0.1.7

func (r Repo) RemoteAdd(ctx context.Context, remote, branch, url string) error

RemoteAdd run git remote add

func (Repo) RemoteShow added in v0.1.7

func (r Repo) RemoteShow(ctx context.Context, remote string) (string, error)

RemoteShow run git remote show

func (Repo) Remove

func (r Repo) Remove(ctx context.Context, s ...string) error

Remove file or directory

func (Repo) ResetHard

func (r Repo) ResetHard(ctx context.Context, hash string) error

ResetHard hard resets a ref

func (Repo) Status

func (r Repo) Status(ctx context.Context) (string, error)

Status run the git status command

func (Repo) SubmoduleUpdate added in v0.2.3

func (r Repo) SubmoduleUpdate(ctx context.Context, opt SubmoduleOpt) error

func (Repo) Tags added in v0.1.9

func (r Repo) Tags(ctx context.Context) ([]Tag, error)

func (Repo) VerifyCommit added in v0.2.0

func (r Repo) VerifyCommit(ctx context.Context, commit string) error

func (Repo) VerifyTag added in v0.1.4

func (r Repo) VerifyTag(ctx context.Context, tag string) (string, error)

VerifyTag returns the sha1 of the tag if exists, if it doesn't exist, it returns an error

func (Repo) Write

func (r Repo) Write(s string, content io.Reader) error

Write writes a file in the repo

func (Repo) WriteHook

func (r Repo) WriteHook(name string, content []byte) error

type SSHKey

type SSHKey struct {
	Filename string
	Content  []byte
}

SSHKey is a type for a ssh key

type SubmoduleOpt added in v0.2.3

type SubmoduleOpt struct {
	Init      bool
	Recursive bool
}

type Tag added in v0.1.9

type Tag struct {
	Message string
	Commit
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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