git

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2026 License: MIT Imports: 5 Imported by: 0

README

Git Package

The git package provides utilities for working with Git repositories by shelling out to the system git command.

Features

  • URI Parsing: Parse various Git URI formats (HTTPS, SSH, etc.)
  • Clone Operations: Clone repositories to local paths
  • Worktree Management: Create, remove, and list Git worktrees
  • Repository Detection: Check if a path is a Git repository

Installation

This package is part of the try CLI tool. No separate installation is required.

URI Parser

Parse Git repository URIs in various formats:

import "github.com/bscott/try/internal/git"

// Parse HTTPS URL
info, err := git.ParseGitURI("https://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"

// Parse SSH URL
info, err = git.ParseGitURI("git@github.com:user/repo.git")

// Get clone URLs
fmt.Println(info.CloneURL())  // "https://github.com/user/repo.git"
fmt.Println(info.SSHURL())    // "git@github.com:user/repo.git"
Supported URI Formats
  • https://github.com/user/repo.git - HTTPS with .git
  • https://github.com/user/repo - HTTPS without .git
  • git@github.com:user/repo.git - SSH with .git
  • git@github.com:user/repo - SSH without .git
  • ssh://git@github.com:user/repo.git - SSH protocol with .git
  • Works with GitHub, GitLab, and any custom Git host

Git Operations

Clone a Repository
err := git.Clone("https://github.com/user/repo.git", "/path/to/destination")
if err != nil {
    log.Fatal(err)
}
Create a Worktree
// Creates a new worktree with a new branch
err := git.CreateWorktree("/path/to/repo", "/path/to/worktree", "feature-branch")
if err != nil {
    log.Fatal(err)
}

If the branch already exists, it will be checked out. Otherwise, a new branch will be created.

Remove a Worktree
err := git.RemoveWorktree("/path/to/repo", "/path/to/worktree")
if err != nil {
    log.Fatal(err)
}
List Worktrees
worktrees, err := git.ListWorktrees("/path/to/repo")
if err != nil {
    log.Fatal(err)
}

for _, wt := range worktrees {
    fmt.Println(wt)
}
Check if Path is a Git Repository
if git.IsGitRepository("/path/to/check") {
    fmt.Println("This is a git repository")
} else {
    fmt.Println("Not a git repository")
}

Error Handling

All functions return descriptive errors that include git command output when operations fail:

err := git.Clone("invalid-uri", "/tmp/dest")
if err != nil {
    // Error will include:
    // - What went wrong
    // - Git command output/stderr
    log.Printf("Clone failed: %v", err)
}

Implementation Details

This package shells out to the system git command using os/exec rather than using the go-git library. This approach:

  • Ensures compatibility with all Git features and configurations
  • Respects system Git configuration and credentials
  • Provides familiar error messages from Git itself
  • Requires Git to be installed on the system

Testing

Run the test suite:

go test ./internal/git/

Run tests with verbose output:

go test -v ./internal/git/

The test suite includes:

  • Unit tests for URI parsing (21 test cases)
  • Unit tests for all operations
  • Integration tests for the full Git workflow
  • Example tests demonstrating usage

License

Part of the try CLI tool - see LICENSE file in repository root.

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:

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

func Clone(uri, destPath string) error

Clone clones a Git repository from the given URI to the destination path. This function shells out to the system git command.

func CreateWorktree

func CreateWorktree(repoPath, worktreePath, branch string) error

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

func IsGitRepository(path string) bool

IsGitRepository checks if the given path is a Git repository.

func ListWorktrees

func ListWorktrees(repoPath string) ([]string, error)

ListWorktrees lists all worktrees for a repository.

func RemoveWorktree

func RemoveWorktree(repoPath, worktreePath string) error

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

func ParseGitURI(uri string) (*RepoInfo, error)

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

func (r *RepoInfo) CloneURL() string

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

func (r *RepoInfo) SSHURL() string

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

func (r *RepoInfo) String() 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

Jump to

Keyboard shortcuts

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