gitclone

package
v0.0.0-...-4612dc1 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("git object not found")

ErrNotFound is returned when a git ref or object cannot be resolved.

Functions

This section is empty.

Types

type Commit

type Commit struct {
	SHA        string
	AuthorName string
	AuthoredAt time.Time
	Message    string // first line only
}

Commit holds metadata for a single commit in a PR's history.

type DiffFile

type DiffFile struct {
	Path             string `json:"path"`
	OldPath          string `json:"old_path"`
	Status           string `json:"status"` // added, modified, deleted, renamed, copied
	IsBinary         bool   `json:"is_binary"`
	IsWhitespaceOnly bool   `json:"is_whitespace_only"`
	Additions        int    `json:"additions"`
	Deletions        int    `json:"deletions"`
	Hunks            []Hunk `json:"hunks"`
}

DiffFile represents one file in a diff.

func ParsePatch

func ParsePatch(patch []byte, rawFiles []DiffFile) []DiffFile

ParsePatch parses unified diff patch output and merges it with pre-populated file metadata from ParseRawZ. Files are correlated by output order (git emits them in the same order).

func ParseRawZ

func ParseRawZ(data []byte) []DiffFile

ParseRawZ parses the output of `git diff --raw -z` into file metadata. Returns files in output order (same order as patch output).

type DiffResult

type DiffResult struct {
	Stale               bool       `json:"stale"`
	WhitespaceOnlyCount int        `json:"whitespace_only_count"`
	Files               []DiffFile `json:"files"`
}

DiffResult is the structured output of a git diff operation.

type Hunk

type Hunk struct {
	OldStart int    `json:"old_start"`
	OldCount int    `json:"old_count"`
	NewStart int    `json:"new_start"`
	NewCount int    `json:"new_count"`
	Section  string `json:"section,omitempty"`
	Lines    []Line `json:"lines"`
}

Hunk represents one contiguous section of changes in a file.

type Line

type Line struct {
	Type      string `json:"type"` // context, add, delete
	Content   string `json:"content"`
	OldNum    int    `json:"old_num,omitempty"`
	NewNum    int    `json:"new_num,omitempty"`
	NoNewline bool   `json:"no_newline,omitempty"`
}

Line represents one line in a diff hunk.

type Manager

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

Manager manages bare git clones for diff computation.

func New

func New(baseDir string, tokens map[string]string) *Manager

New creates a Manager that stores bare clones under baseDir. tokens maps each host (e.g., "github.com") to its auth token. A nil or empty map means all operations proceed without auth.

func (*Manager) ClonePath

func (m *Manager) ClonePath(host, owner, name string) string

ClonePath returns the filesystem path for a repo's bare clone. Path is partitioned by host: {baseDir}/{host}/{owner}/{name}.git

func (*Manager) Diff

func (m *Manager) Diff(
	ctx context.Context,
	host, owner, name, mergeBase, headSHA string,
	hideWhitespace bool,
) (*DiffResult, error)

Diff runs a two-dot git diff between mergeBase and headSHA and returns structured diff data. If hideWhitespace is true, passes -w to git diff.

func (*Manager) DiffFiles

func (m *Manager) DiffFiles(
	ctx context.Context,
	host, owner, name, mergeBase, headSHA string,
) ([]DiffFile, error)

DiffFiles returns file metadata (path, status, renames) without patch content. It runs only git diff --raw, which is much faster than a full diff for large PRs.

func (*Manager) EnsureClone

func (m *Manager) EnsureClone(
	ctx context.Context, host, owner, name, remoteURL string,
) error

EnsureClone creates or fetches a bare clone for the given repo. remoteURL is the HTTPS clone URL (e.g., https://github.com/owner/name.git). On first call, clones the repo. On subsequent calls, fetches updates.

func (*Manager) ListCommits

func (m *Manager) ListCommits(
	ctx context.Context,
	host, owner, name, mergeBase, headSHA string,
) ([]Commit, error)

ListCommits returns commits between mergeBase and headSHA, newest first, following only the first-parent chain. If mergeBase is the empty tree sentinel (parentless root), all commits up to headSHA are returned.

func (*Manager) MergeBase

func (m *Manager) MergeBase(
	ctx context.Context, host, owner, name, sha1, sha2 string,
) (string, error)

MergeBase computes the merge base between two commits.

func (*Manager) ParentOf

func (m *Manager) ParentOf(
	ctx context.Context,
	host, owner, name, sha string,
) (string, error)

ParentOf returns the first parent SHA of the given commit. For a parentless (root) commit it returns the empty tree sentinel. The caller must ensure sha exists in the clone; any failure here is a genuine server-side error, not a client-input issue.

func (*Manager) RevParse

func (m *Manager) RevParse(
	ctx context.Context, host, owner, name, ref string,
) (string, error)

RevParse resolves a git ref to its SHA. Returns an empty string if the ref does not exist.

Jump to

Keyboard shortcuts

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