git

package
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2023 License: MIT Imports: 12 Imported by: 0

README

goutils/v2/git

The git package is a part of goutils library.

It provides utility functions to interface with git and services that provide git in Go.


Functions

AddFile
func AddFile(filePath string) error

Stage the file at the given file path in its associated Git repository. Returns an error if one occurs.

Commit
func Commit(repo *git.Repository, msg string) error

Create a new commit in the given repository with the provided message. The author of the commit is retrieved from the global Git user settings.

CloneRepo
func CloneRepo(url string, clonePath string, auth transport.AuthMethod)

Clone the repo specified with the input url to provided clonePath.

GetTags
func GetTags(repo *git.Repository) ([]string, error)

Returns the tags for an input repo.

GetGlobalUserCfg
func GetGlobalUserCfg() (ConfigUserInfo, error)

Return the username and email from the global git user settings.

CreateTag
func CreateTag(repo *git.Repository, tag string) error

Create an input tag in the specified repo if it doesn't already exist.

Push
func Push(repo *git.Repository, auth transport.AuthMethod) error

Push the contents of the input repo to the default remote (origin).

PushTag
func PushTag(repo *git.Repository, tag string, auth transport.AuthMethod) error

Push a tag to remote.

DeleteTag
func DeleteTag(repo *git.Repository, tag string) error

Delete the local input tag from the specified repo.

DeletePushedTag
func DeletePushedTag(repo *git.Repository, tag string, auth transport.AuthMethod) error

Delete a tag from a given repository that has been pushed remote. The tag is deleted from both the local repository and the remote repository.

PullRepos
func PullRepos(dirs ...string) error

Update all git repositories found in the given directories by pulling changes from the upstream branch.

It looks for repositories by finding directories with a ".git" subdirectory.

If a repository is not on the default branch, it will switch to the default branch before pulling changes.


Installation

To use the goutils/v2/git package, you need to install it via go get:

go get github.com/l50/goutils/v2/git

Usage

After installation, you can import it in your project:

import "github.com/l50/goutils/v2/git"

Tests

To run the tests for the goutils/v2/git package, navigate to your $GOPATH/src/github.com/l50/goutils/v2/git directory and run go test:

go test -v

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.


License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddFile

func AddFile(filePath string) error

AddFile stages the file at the given file path in its associated Git repository. Returns an error if one occurs.

Parameters:

filePath: A string representing the path to the file to be staged.

Returns:

error: An error if one occurs during the staging process.

Example:

filePath := "/path/to/your/file" err := AddFile(filePath)

if err != nil {
  log.Fatalf("failed to stage file: %v", err)
}

log.Printf("Staged file: %s", filePath)

func CloneRepo

func CloneRepo(url string, clonePath string, auth transport.AuthMethod) (
	*git.Repository, error)

CloneRepo clones the repo specified with the input url to provided clonePath.

func Commit

func Commit(repo *git.Repository, msg string) error

Commit creates a new commit in the given repository with the provided message. The author of the commit is retrieved from the global Git user settings.

Parameters:

repo: A pointer to the Repository struct representing the repository where the commit should be created.

msg: A string representing the commit message.

Returns:

error: An error if the commit cannot be created.

Example:

repo, err := git.PlainOpen("/path/to/repo")

if err != nil {
  log.Fatalf("failed to open repository: %v", err)
}

msg := "Commit message" err = Commit(repo, msg)

if err != nil {
  log.Fatalf("failed to create commit: %v", err)
}

func CreateTag

func CreateTag(repo *git.Repository, tag string) error

CreateTag is used to create an input tag in the specified repo if it doesn't already exist. Resource: https://github.com/go-git/go-git/blob/bf3471db54b0255ab5b159005069f37528a151b7/_examples/tag-create-push/main.go

func DeletePushedTag

func DeletePushedTag(repo *git.Repository, tag string, auth transport.AuthMethod) error

DeletePushedTag deletes a tag from a given repository that has been pushed remote. The tag is deleted from both the local repository and the remote repository.

Parameters:

repo: A pointer to the Repository struct representing the repository where the tag should be deleted.

tag: A string representing the tag that should be deleted.

auth: An AuthMethod representing the method used to authenticate with the remote repository.

Returns:

error: An error if the tag cannot be deleted.

Example:

repo, err := git.PlainOpen("/path/to/repo")

if err != nil {
  log.Fatalf("failed to open repository: %v", err)
}

tag := "v1.0.0" auth, err := ssh.NewSSHAgentAuth("git")

if err != nil {
  log.Fatalf("failed to create authentication method: %v", err)
}

err = DeletePushedTag(repo, tag, auth)

if err != nil {
  log.Fatalf("failed to delete pushed tag: %v", err)
}

func DeleteTag

func DeleteTag(repo *git.Repository, tag string) error

DeleteTag deletes the local input tag from the specified repo.

Parameters:

repo: A pointer to the Repository struct representing the repository where the tag should be deleted.

tag: A string representing the tag that should be deleted.

auth: An AuthMethod representing the method used to authenticate with the remote repository.

Returns:

error: An error if the tag cannot be deleted.

Example:

repo, err := git.PlainOpen("/path/to/repo")

if err != nil {
  log.Fatalf("failed to open repository: %v", err)
}

tag := "v1.0.0" auth, err := ssh.NewSSHAgentAuth("git")

if err != nil {
  log.Fatalf("failed to create authentication method: %v", err)
}

err = DeletePushedTag(repo, tag, auth)

if err != nil {
  log.Fatalf("failed to delete pushed tag: %v", err)
}

func GetTags

func GetTags(repo *git.Repository) ([]string, error)

GetTags returns the tags for an input repo.

func PullRepos

func PullRepos(dirs ...string) error

PullRepos updates all git repositories found in the given directories by pulling changes from the upstream branch. It looks for repositories by finding directories with a ".git" subdirectory. If a repository is not on the default branch, it will switch to the default branch before pulling changes. Returns an error if any step of the process fails.

func Push

func Push(repo *git.Repository, auth transport.AuthMethod) error

Push pushes the contents of the input repo to the default remote (origin).

func PushTag

func PushTag(repo *git.Repository, tag string, auth transport.AuthMethod) error

PushTag is used to push a tag to remote.

func RepoRoot

func RepoRoot() (string, error)

RepoRoot returns the root of the git repo a user is currently in.

Types

type ConfigUserInfo

type ConfigUserInfo struct {
	User  string
	Email string
}

ConfigUserInfo holds a username and email to use for user.name and user.email.

func GetGlobalUserCfg

func GetGlobalUserCfg() (ConfigUserInfo, error)

GetGlobalUserCfg returns the username and email from the global git user settings.

Jump to

Keyboard shortcuts

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