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 ¶
- func GetRepositoryName(repoURL string) string
- func IsValidRepository(path string) bool
- type Client
- func (c *Client) Checkout(repoPath, ref string) error
- func (c *Client) Clone(repoURL, targetDir string) error
- func (c *Client) Fetch(repoPath string) error
- func (c *Client) GetRemoteURL(repoPath string) (string, error)
- func (c *Client) ListTags(repoPath string) ([]string, error)
- func (c *Client) SetRemote(repoPath, name, url string) error
- type Options
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetRepositoryName ¶
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 ¶
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 (*Client) Checkout ¶
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")
}
Output:
func (*Client) Clone ¶
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")
}
Output:
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")
}
Output:
func (*Client) GetRemoteURL ¶
GetRemoteURL returns the URL of the origin remote.
func (*Client) ListTags ¶
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)
}
}
Output: