git

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package git provides a simple wrapper around go-git for common Git operations.

Package git provides a simple wrapper around go-git for common Git operations. It supports cloning repositories, fetching updates, checking out specific versions, and listing tags. The package handles various Git URL formats including GitHub shorthand notation and supports authentication via HTTP basic auth or SSH keys.

Basic usage:

client, err := git.New(nil)
if err != nil {
    log.Fatal(err)
}

err = client.Clone("user/repo", "/tmp/repo")
if err != nil {
    log.Fatal(err)
}

With authentication:

client, err := git.New(&git.Options{
    Username: "username",
    Password: "token",
})
if err != nil {
    log.Fatal(err)
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetRepositoryName

func GetRepositoryName(repoURL string) string

GetRepositoryName extracts the repository name from a Git URL.

Example
package main

import (
	"fmt"

	"github.com/gifflet/ccmd/pkg/git"
)

func main() {
	// Extract repository name from various URL formats
	urls := []string{
		"https://github.com/user/repo.git",
		"git@github.com:user/repo.git",
		"/path/to/local/repo",
		"user/repo",
	}

	for _, url := range urls {
		name := git.GetRepositoryName(url)
		fmt.Printf("%s -> %s\n", url, name)
	}
}
Output:
https://github.com/user/repo.git -> repo
git@github.com:user/repo.git -> repo
/path/to/local/repo -> repo
user/repo -> repo

func IsValidRepository

func IsValidRepository(path string) bool

IsValidRepository checks if a directory is a valid Git repository.

Types

type Client

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

Client provides Git operations using go-git.

func New

func New(opts *Options) (*Client, error)

New creates a new Git client with optional authentication.

func (*Client) Checkout

func (c *Client) Checkout(repoPath, ref string) error

Checkout checks out a specific version, tag, or branch.

Example
package main

import (
	"fmt"
	"log"

	"github.com/gifflet/ccmd/pkg/git"
)

func main() {
	client, err := git.New(nil)
	if err != nil {
		log.Fatal(err)
	}

	// Checkout a specific tag
	err = client.Checkout("/path/to/repo", "v1.0.0")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("Checked out tag v1.0.0")
}

func (*Client) Clone

func (c *Client) Clone(repoURL, targetDir string) error

Clone clones a repository to the specified directory.

Example
package main

import (
	"fmt"
	"log"

	"github.com/gifflet/ccmd/pkg/git"
)

func main() {
	// Create a new Git client without authentication
	client, err := git.New(nil)
	if err != nil {
		log.Fatal(err)
	}

	// Clone a repository
	err = client.Clone("https://github.com/user/repo.git", "/tmp/repo")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("Repository cloned successfully")
}
Example (WithAuth)
package main

import (
	"fmt"
	"log"

	"github.com/gifflet/ccmd/pkg/git"
)

func main() {
	// Create a Git client with basic authentication
	client, err := git.New(&git.Options{
		Username: "username",
		Password: "password",
	})
	if err != nil {
		log.Fatal(err)
	}

	// Clone a private repository
	err = client.Clone("https://github.com/user/private-repo.git", "/tmp/private-repo")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("Private repository cloned successfully")
}

func (*Client) Fetch

func (c *Client) Fetch(repoPath string) error

Fetch fetches updates from the remote repository.

func (*Client) GetRemoteURL

func (c *Client) GetRemoteURL(repoPath string) (string, error)

GetRemoteURL returns the URL of the origin remote.

func (*Client) ListTags

func (c *Client) ListTags(repoPath string) ([]string, error)

ListTags returns all tags in the repository.

Example
package main

import (
	"fmt"
	"log"

	"github.com/gifflet/ccmd/pkg/git"
)

func main() {
	client, err := git.New(nil)
	if err != nil {
		log.Fatal(err)
	}

	// List all tags in a repository
	tags, err := client.ListTags("/path/to/repo")
	if err != nil {
		log.Fatal(err)
	}

	for _, tag := range tags {
		fmt.Println(tag)
	}
}

func (*Client) SetRemote

func (c *Client) SetRemote(repoPath, name, url string) error

SetRemote adds or updates a remote in the repository.

type Options

type Options struct {
	// Authentication options
	Username string
	Password string
	SSHKey   string
}

Options holds configuration for Git operations.

Jump to

Keyboard shortcuts

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