Documentation
¶
Overview ¶
Package git provides utilities for working with Git repositories.
This package shells out to the system git command rather than using go-git library, ensuring compatibility with all git features and configurations.
Parser ¶
The parser subpackage provides URI parsing for Git repositories:
info, err := git.ParseGitURI("git@github.com:user/repo.git")
if err != nil {
log.Fatal(err)
}
fmt.Println(info.Host) // "github.com"
fmt.Println(info.Owner) // "user"
fmt.Println(info.Repo) // "repo"
Supported URI formats:
- HTTPS: https://github.com/user/repo.git
- HTTPS without .git: https://github.com/user/repo
- SSH: git@github.com:user/repo.git
- SSH without .git: git@github.com:user/repo
- SSH protocol: ssh://git@github.com:user/repo.git
Operations ¶
The operations subpackage provides functions for common git operations:
Clone a repository:
err := git.Clone("https://github.com/user/repo.git", "/path/to/dest")
Create a worktree:
err := git.CreateWorktree("/path/to/repo", "/path/to/worktree", "feature-branch")
Remove a worktree:
err := git.RemoveWorktree("/path/to/repo", "/path/to/worktree")
List all worktrees:
worktrees, err := git.ListWorktrees("/path/to/repo")
Check if a path is a git repository:
if git.IsGitRepository("/path/to/check") {
fmt.Println("This is a git repository")
}
Error Handling ¶
All functions return descriptive errors that include the git command output when operations fail, making debugging easier.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Clone ¶
Clone clones a Git repository from the given URI to the destination path. This function shells out to the system git command.
func CreateWorktree ¶
CreateWorktree creates a new Git worktree at the specified path. If the branch exists, it checks it out. If not, it creates a new branch.
func IsGitRepository ¶
IsGitRepository checks if the given path is a Git repository.
func ListWorktrees ¶
ListWorktrees lists all worktrees for a repository.
func RemoveWorktree ¶
RemoveWorktree removes a Git worktree at the specified path.
Types ¶
type RepoInfo ¶
type RepoInfo struct {
Host string // github.com, gitlab.com, etc.
Owner string // user or org
Repo string // repository name
Original string // original URI
}
RepoInfo contains parsed information from a Git repository URI
func ParseGitURI ¶
ParseGitURI parses a Git repository URI and extracts repository information. Supports multiple formats: - https://github.com/user/repo.git - https://github.com/user/repo (no .git) - git@github.com:user/repo.git - git@github.com:user/repo
Example ¶
package main
import (
"fmt"
"log"
"github.com/bscott/try/internal/git"
)
func main() {
// Parse an HTTPS URL
info, err := git.ParseGitURI("https://github.com/user/repo.git")
if err != nil {
log.Fatal(err)
}
fmt.Println(info.Host)
fmt.Println(info.Owner)
fmt.Println(info.Repo)
}
Output: github.com user repo
Example (Ssh) ¶
package main
import (
"fmt"
"log"
"github.com/bscott/try/internal/git"
)
func main() {
// Parse an SSH URL
info, err := git.ParseGitURI("git@github.com:user/repo.git")
if err != nil {
log.Fatal(err)
}
fmt.Println(info.Host)
fmt.Println(info.Owner)
fmt.Println(info.Repo)
}
Output: github.com user repo
func (*RepoInfo) CloneURL ¶
CloneURL returns the HTTPS clone URL for the repository
Example ¶
package main
import (
"fmt"
"github.com/bscott/try/internal/git"
)
func main() {
info := &git.RepoInfo{
Host: "github.com",
Owner: "user",
Repo: "repo",
}
fmt.Println(info.CloneURL())
}
Output: https://github.com/user/repo.git
func (*RepoInfo) SSHURL ¶
SSHURL returns the SSH clone URL for the repository
Example ¶
package main
import (
"fmt"
"github.com/bscott/try/internal/git"
)
func main() {
info := &git.RepoInfo{
Host: "github.com",
Owner: "user",
Repo: "repo",
}
fmt.Println(info.SSHURL())
}
Output: git@github.com:user/repo.git
func (*RepoInfo) String ¶
String returns a human-readable representation of the RepoInfo
Example ¶
package main
import (
"fmt"
"github.com/bscott/try/internal/git"
)
func main() {
info := &git.RepoInfo{
Host: "github.com",
Owner: "user",
Repo: "repo",
}
fmt.Println(info.String())
}
Output: github.com/user/repo