repo

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2026 License: MIT Imports: 11 Imported by: 2

Documentation

Overview

Package repo provides GitHub repository operations.

Index

Constants

This section is empty.

Variables

View Source
var ErrBatchCommitted = errors.New("batch already committed")

ErrBatchCommitted is returned when operations are attempted on a committed batch.

View Source
var ErrEmptyPath = errors.New("empty path not allowed")

ErrEmptyPath is returned when an empty path is provided.

Functions

func BranchExists

func BranchExists(ctx context.Context, gh *github.Client, owner, repo, branch string) (bool, error)

BranchExists checks if a branch exists.

func CreateBranch

func CreateBranch(ctx context.Context, gh *github.Client, owner, repo, branch, baseSHA string) error

CreateBranch creates a new branch from the given base SHA.

func CreateCommit

func CreateCommit(ctx context.Context, gh *github.Client, owner, repo, branch, message string, files []FileContent) (string, error)

CreateCommit creates a commit with the given files using the Git tree API.

func DeleteBranch

func DeleteBranch(ctx context.Context, gh *github.Client, owner, repo, branch string) error

DeleteBranch deletes a branch.

func DownloadDirectory added in v0.10.0

func DownloadDirectory(ctx context.Context, gh *github.Client, owner, repo, dirPath string, opts *ContentOptions) (map[string][]byte, error)

DownloadDirectory downloads all files from a directory recursively. Returns a map of relative file path to content.

func EnsureFork

func EnsureFork(ctx context.Context, gh *github.Client, upstreamOwner, upstreamRepo, forkOwner string) (string, string, error)

EnsureFork ensures a fork exists for the given repository. Returns the fork owner and repo name.

func FileExists added in v0.10.0

func FileExists(ctx context.Context, gh *github.Client, owner, repo, filePath string, opts *ContentOptions) (bool, error)

FileExists checks if a file exists in a repository.

func GetBranchSHA

func GetBranchSHA(ctx context.Context, gh *github.Client, owner, repo, branch string) (string, error)

GetBranchSHA returns the SHA of the given branch.

func GetContributorStats added in v0.8.0

func GetContributorStats(ctx context.Context, gh *github.Client, owner, repo, username string) (*github.ContributorStats, error)

GetContributorStats returns statistics for a specific contributor in a repository. Returns nil if the user has not contributed to the repository.

func GetDefaultBranch

func GetDefaultBranch(ctx context.Context, gh *github.Client, owner, repo string) (string, error)

GetDefaultBranch returns the default branch of a repository.

func GetFileContent added in v0.10.0

func GetFileContent(ctx context.Context, gh *github.Client, owner, repo, filePath string, opts *ContentOptions) ([]byte, error)

GetFileContent fetches the content of a single file from a repository. Returns the decoded file content as bytes.

func GetFileContentString added in v0.10.0

func GetFileContentString(ctx context.Context, gh *github.Client, owner, repo, filePath string, opts *ContentOptions) (string, error)

GetFileContentString fetches the content of a single file as a string.

func GetMultipleFiles added in v0.10.0

func GetMultipleFiles(ctx context.Context, gh *github.Client, owner, repo string, filePaths []string, opts *ContentOptions) (map[string][]byte, error)

GetMultipleFiles fetches multiple files from a repository. Returns a map of file path to content.

func GetRawFileURL added in v0.10.0

func GetRawFileURL(owner, repo, ref, filePath string) string

GetRawFileURL returns the raw content URL for a file. This URL can be used to download the file without authentication for public repos.

func GetRepo

func GetRepo(ctx context.Context, gh *github.Client, owner, repo string) (*github.Repository, error)

GetRepo retrieves a repository by owner and name.

func JoinPath added in v0.10.0

func JoinPath(segments ...string) string

JoinPath joins path segments for repository file paths.

func ListContributorStats added in v0.8.0

func ListContributorStats(ctx context.Context, gh *github.Client, owner, repo string) ([]*github.ContributorStats, error)

ListContributorStats returns contributor statistics for a repository. This wraps the GitHub REST API endpoint: GET /repos/{owner}/{repo}/stats/contributors

Note: GitHub may return 202 Accepted while computing statistics. This function automatically retries with exponential backoff until stats are available or the context is cancelled.

func ListOrgRepos

func ListOrgRepos(ctx context.Context, gh *github.Client, org string) ([]*github.Repository, error)

ListOrgRepos lists all repositories for an organization with pagination.

func ListUserRepos

func ListUserRepos(ctx context.Context, gh *github.Client, user string) ([]*github.Repository, error)

ListUserRepos lists all repositories for a user with pagination.

func ParseRepoName

func ParseRepoName(fullName string) (owner, repo string, err error)

ParseRepoName splits a full repo name (owner/repo) into owner and repo.

func ParseRepoURL added in v0.10.0

func ParseRepoURL(repoURL string) (owner, repo string, err error)

ParseRepoURL parses a GitHub repository URL and returns owner and repo. Supports formats:

Types

type Batch

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

Batch accumulates multiple file operations to be committed atomically. Use NewBatch to create a batch, then call Write/Delete to queue operations, and finally Commit to apply all changes in a single commit.

func NewBatch

func NewBatch(ctx context.Context, gh *github.Client, owner, repo, branch, message string, opts ...BatchOption) (*Batch, error)

NewBatch creates a new batch for accumulating file operations. The message is used as the commit message when Commit is called.

func (*Batch) Commit

func (b *Batch) Commit(ctx context.Context) (string, error)

Commit applies all queued operations in a single commit. This uses the Git Data API (Trees and Commits) to create an atomic commit.

The process:

  1. Get the current commit SHA from the branch reference
  2. Get the current tree SHA from that commit
  3. Create blobs for all new/updated file contents
  4. Create a new tree with the changes
  5. Create a new commit pointing to the new tree
  6. Update the branch reference to the new commit

Returns the new commit SHA on success.

func (*Batch) Committed

func (b *Batch) Committed() bool

Committed returns whether the batch has been committed.

func (*Batch) Delete

func (b *Batch) Delete(filePath string) error

Delete queues a file delete operation. The file will be deleted when Commit is called. If the file doesn't exist at commit time, it is ignored (no error).

func (*Batch) Len

func (b *Batch) Len() int

Len returns the number of operations in the batch.

func (*Batch) Operations

func (b *Batch) Operations() []BatchOperation

Operations returns a copy of the queued operations.

func (*Batch) Write

func (b *Batch) Write(filePath string, content []byte) error

Write queues a file write operation. The file will be created or updated when Commit is called.

type BatchError

type BatchError struct {
	Op  string
	Err error
}

BatchError indicates a failure during batch operations.

func (*BatchError) Error

func (e *BatchError) Error() string

func (*BatchError) Unwrap

func (e *BatchError) Unwrap() error

type BatchOperation

type BatchOperation struct {
	Type    BatchOperationType
	Path    string
	Content []byte
}

BatchOperation represents a single operation in a batch.

type BatchOperationType

type BatchOperationType int

BatchOperationType indicates the type of batch operation.

const (
	// BatchOpWrite represents a write (create/update) operation.
	BatchOpWrite BatchOperationType = iota
	// BatchOpDelete represents a delete operation.
	BatchOpDelete
)

type BatchOption

type BatchOption func(*Batch)

BatchOption configures a Batch.

func WithCommitAuthor

func WithCommitAuthor(name, email string) BatchOption

WithCommitAuthor sets the commit author for the batch.

type BranchError

type BranchError struct {
	Branch string
	Err    error
}

BranchError indicates a failure to create or update a branch.

func (*BranchError) Error

func (e *BranchError) Error() string

func (*BranchError) Unwrap

func (e *BranchError) Unwrap() error

type CommitError

type CommitError struct {
	Message string
	Err     error
}

CommitError indicates a failure to create a commit.

func (*CommitError) Error

func (e *CommitError) Error() string

func (*CommitError) Unwrap

func (e *CommitError) Unwrap() error

type ContentOptions added in v0.10.0

type ContentOptions struct {
	// Ref is the git reference (branch, tag, or commit SHA). Default: default branch.
	Ref string
}

ContentOptions specifies options for fetching repository content.

type ContributorSummary added in v0.8.0

type ContributorSummary struct {
	Username       string
	TotalCommits   int
	TotalAdditions int
	TotalDeletions int
	FirstCommit    time.Time
	LastCommit     time.Time
}

ContributorSummary provides a simplified view of contributor statistics.

func GetContributorSummary added in v0.8.0

func GetContributorSummary(ctx context.Context, gh *github.Client, owner, repo, username string) (*ContributorSummary, error)

GetContributorSummary returns a summarized view of a contributor's statistics.

type FileContent

type FileContent struct {
	Path    string
	Content []byte
}

FileContent represents a file to be committed.

func ReadLocalFiles

func ReadLocalFiles(dir, prefix string) ([]FileContent, error)

ReadLocalFiles reads all files from a local directory recursively. The prefix is prepended to relative paths for the destination.

type FileInfo added in v0.10.0

type FileInfo struct {
	Path        string
	Name        string
	Type        string // "file" or "dir"
	Size        int
	SHA         string
	DownloadURL string
}

FileInfo represents information about a file or directory in a repository.

func ListDirectory added in v0.10.0

func ListDirectory(ctx context.Context, gh *github.Client, owner, repo, dirPath string, opts *ContentOptions) ([]FileInfo, error)

ListDirectory lists the contents of a directory in a repository.

func ListDirectoryRecursive added in v0.10.0

func ListDirectoryRecursive(ctx context.Context, gh *github.Client, owner, repo, dirPath string, opts *ContentOptions) ([]FileInfo, error)

ListDirectoryRecursive lists all files in a directory recursively.

type ForkError

type ForkError struct {
	Owner string
	Repo  string
	Err   error
}

ForkError indicates a failure to fork a repository.

func (*ForkError) Error

func (e *ForkError) Error() string

func (*ForkError) Unwrap

func (e *ForkError) Unwrap() error

Jump to

Keyboard shortcuts

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