git

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package git implements the Git CLI wrapper for tracking repository changes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TruncateDiff

func TruncateDiff(diff string, maxSizeKB int) (string, bool)

TruncateDiff caps a diff string to maxSizeKB and returns whether it was truncated. If maxSizeKB <= 0, the diff is dropped and marked truncated.

Types

type BranchInfo

type BranchInfo struct {
	Name      string `json:"name"`
	IsCurrent bool   `json:"is_current"`
	IsRemote  bool   `json:"is_remote"`
	Upstream  string `json:"upstream,omitempty"`
	Ahead     int    `json:"ahead,omitempty"`
	Behind    int    `json:"behind,omitempty"`
}

BranchInfo represents information about a git branch.

type BranchesResult

type BranchesResult struct {
	Current  string       `json:"current"`
	Upstream string       `json:"upstream,omitempty"`
	Ahead    int          `json:"ahead"`
	Behind   int          `json:"behind"`
	Branches []BranchInfo `json:"branches"`
}

BranchesResult represents the result of listing branches.

type CheckoutResult

type CheckoutResult struct {
	Success    bool   `json:"success"`
	Branch     string `json:"branch,omitempty"`
	FromBranch string `json:"from_branch,omitempty"` // Previous branch (for git_branch_changed event)
	Message    string `json:"message,omitempty"`
	Error      string `json:"error,omitempty"`
}

CheckoutResult represents the result of a checkout operation.

type CommitResult

type CommitResult struct {
	Success        bool   `json:"success"`
	SHA            string `json:"sha,omitempty"`
	Message        string `json:"message,omitempty"`
	FilesCommitted int    `json:"files_committed,omitempty"`
	Pushed         bool   `json:"pushed,omitempty"`
	Error          string `json:"error,omitempty"`
}

CommitResult represents the result of a commit operation.

type DeleteBranchResult

type DeleteBranchResult struct {
	Success bool   `json:"success"`
	Branch  string `json:"branch"`
	Message string `json:"message,omitempty"`
	Error   string `json:"error,omitempty"`
}

DeleteBranchResult represents the result of a branch delete operation.

type DirectoryEntry

type DirectoryEntry struct {
	Name          string  `json:"name"`
	Type          string  `json:"type"` // "file" or "directory"
	Size          *int64  `json:"size,omitempty"`
	Modified      *string `json:"modified,omitempty"`
	ChildrenCount *int    `json:"children_count,omitempty"`
}

DirectoryEntry represents a file or directory entry.

type EnhancedStatus

type EnhancedStatus struct {
	Branch     string      `json:"branch"`
	Upstream   string      `json:"upstream,omitempty"`
	Ahead      int         `json:"ahead"`
	Behind     int         `json:"behind"`
	Staged     []FileEntry `json:"staged"`
	Unstaged   []FileEntry `json:"unstaged"`
	Untracked  []FileEntry `json:"untracked"`
	Conflicted []FileEntry `json:"conflicted"`
	RepoName   string      `json:"repo_name"`
	RepoRoot   string      `json:"repo_root"`
}

EnhancedStatus represents enhanced git status with staging information.

type FetchResult

type FetchResult struct {
	Success        bool     `json:"success"`
	Message        string   `json:"message,omitempty"`
	Error          string   `json:"error,omitempty"`
	UpdatedRefs    []string `json:"updated_refs,omitempty"`
	NewBranches    []string `json:"new_branches,omitempty"`
	PrunedBranches []string `json:"pruned_branches,omitempty"`
}

FetchResult represents the result of a fetch operation.

type FileEntry

type FileEntry struct {
	Path      string `json:"path"`
	Status    string `json:"status"`
	Additions int    `json:"additions,omitempty"`
	Deletions int    `json:"deletions,omitempty"`
}

FileEntry represents a file in git status with diff stats.

type GraphLine

type GraphLine struct {
	FromColumn int           `json:"from_column"`
	ToColumn   int           `json:"to_column"`
	Type       GraphLineType `json:"type"`
}

GraphLine represents a line segment in the git graph.

type GraphLineType

type GraphLineType string

GraphLineType represents the type of line in a git graph.

const (
	GraphLineStraight    GraphLineType = "straight"
	GraphLineMergeLeft   GraphLineType = "merge_left"
	GraphLineMergeRight  GraphLineType = "merge_right"
	GraphLineBranchLeft  GraphLineType = "branch_left"
	GraphLineBranchRight GraphLineType = "branch_right"
	GraphLinePass        GraphLineType = "pass"
)

type GraphPosition

type GraphPosition struct {
	Column int         `json:"column"`
	Lines  []GraphLine `json:"lines"`
}

GraphPosition represents the position of a commit in the git graph.

type InitResult

type InitResult struct {
	Success        bool   `json:"success"`
	Message        string `json:"message,omitempty"`
	Error          string `json:"error,omitempty"`
	Branch         string `json:"branch,omitempty"`
	CommitSHA      string `json:"commit_sha,omitempty"`
	FilesCommitted int    `json:"files_committed,omitempty"`
}

InitResult represents the result of a git init operation.

type LogEntry

type LogEntry struct {
	SHA           string         `json:"sha"`
	ShortSHA      string         `json:"short_sha"`
	Author        string         `json:"author"`
	AuthorEmail   string         `json:"author_email"`
	Date          string         `json:"date"`
	RelativeDate  string         `json:"relative_date"`
	Subject       string         `json:"subject"`
	Body          string         `json:"body,omitempty"`
	ParentSHAs    []string       `json:"parent_shas,omitempty"`
	IsMerge       bool           `json:"is_merge"`
	GraphPosition *GraphPosition `json:"graph_position,omitempty"`
}

LogEntry represents a single commit in the log.

type LogResult

type LogResult struct {
	Commits    []LogEntry `json:"commits"`
	TotalCount int        `json:"total_count"`
	HasMore    bool       `json:"has_more"`
	MaxColumns int        `json:"max_columns,omitempty"`
}

LogResult represents the result of a log operation.

type MergeResult

type MergeResult struct {
	Success         bool     `json:"success"`
	Message         string   `json:"message,omitempty"`
	Error           string   `json:"error,omitempty"`
	CommitSHA       string   `json:"commit_sha,omitempty"`
	HasConflicts    bool     `json:"has_conflicts"`
	ConflictedFiles []string `json:"conflicted_files,omitempty"`
}

MergeResult represents the result of a merge operation.

type PullResult

type PullResult struct {
	Success         bool     `json:"success"`
	Message         string   `json:"message,omitempty"`
	CommitsPulled   int      `json:"commits_pulled,omitempty"`
	FilesChanged    int      `json:"files_changed,omitempty"`
	ConflictedFiles []string `json:"conflicted_files,omitempty"`
	Error           string   `json:"error,omitempty"`
}

PullResult represents the result of a pull operation.

type PushResult

type PushResult struct {
	Success       bool   `json:"success"`
	Message       string `json:"message,omitempty"`
	CommitsPushed int    `json:"commits_pushed,omitempty"`
	Error         string `json:"error,omitempty"`
}

PushResult represents the result of a push operation.

type RemoteAddResult

type RemoteAddResult struct {
	Success         bool        `json:"success"`
	Message         string      `json:"message,omitempty"`
	Error           string      `json:"error,omitempty"`
	Remote          *RemoteInfo `json:"remote,omitempty"`
	FetchedBranches []string    `json:"fetched_branches,omitempty"`
}

RemoteAddResult represents the result of adding a remote.

type RemoteInfo

type RemoteInfo struct {
	Name             string   `json:"name"`
	FetchURL         string   `json:"fetch_url"`
	PushURL          string   `json:"push_url"`
	Provider         string   `json:"provider,omitempty"`
	TrackingBranches []string `json:"tracking_branches,omitempty"`
}

RemoteInfo represents information about a git remote.

type RemoteListResult

type RemoteListResult struct {
	Remotes []RemoteInfo `json:"remotes"`
}

RemoteListResult represents the result of listing remotes.

type RemoteRemoveResult

type RemoteRemoveResult struct {
	Success bool   `json:"success"`
	Message string `json:"message,omitempty"`
	Error   string `json:"error,omitempty"`
}

RemoteRemoveResult represents the result of removing a remote.

type SetUpstreamResult

type SetUpstreamResult struct {
	Success  bool   `json:"success"`
	Message  string `json:"message,omitempty"`
	Error    string `json:"error,omitempty"`
	Branch   string `json:"branch,omitempty"`
	Upstream string `json:"upstream,omitempty"`
}

SetUpstreamResult represents the result of setting upstream.

type StashEntry

type StashEntry struct {
	Index   int    `json:"index"`
	Name    string `json:"name"`
	Branch  string `json:"branch"`
	Message string `json:"message"`
}

StashEntry represents a single stash entry.

type StashListResult

type StashListResult struct {
	Stashes []StashEntry `json:"stashes"`
	Count   int          `json:"count"`
}

StashListResult represents the result of listing stashes.

type StashResult

type StashResult struct {
	Success bool   `json:"success"`
	Message string `json:"message,omitempty"`
	Error   string `json:"error,omitempty"`
	Name    string `json:"name,omitempty"`
}

StashResult represents the result of a stash operation.

type Status

type Status struct {
	IsGitRepo    bool              `json:"is_git_repo"`
	HasCommits   bool              `json:"has_commits"`
	State        WorkspaceGitState `json:"state"`
	Branch       string            `json:"branch,omitempty"`
	Upstream     string            `json:"upstream,omitempty"`
	Ahead        int               `json:"ahead"`
	Behind       int               `json:"behind"`
	Staged       []FileEntry       `json:"staged"`
	Unstaged     []FileEntry       `json:"unstaged"`
	Untracked    []FileEntry       `json:"untracked"`
	Conflicted   []FileEntry       `json:"conflicted"`
	HasConflicts bool              `json:"has_conflicts"`
	Remotes      []RemoteInfo      `json:"remotes,omitempty"`
	RepoName     string            `json:"repo_name,omitempty"`
	RepoRoot     string            `json:"repo_root,omitempty"`
}

Status represents the full git status of a workspace.

type Tracker

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

Tracker implements the GitTracker port interface.

func NewTracker

func NewTracker(repoPath, command string, hub ports.EventHub) *Tracker

NewTracker creates a new Git tracker.

func (*Tracker) Checkout

func (t *Tracker) Checkout(ctx context.Context, branch string, create bool) (*CheckoutResult, error)

Checkout switches branches or creates a new branch.

func (*Tracker) Commit

func (t *Tracker) Commit(ctx context.Context, message string, push bool) (*CommitResult, error)

Commit commits staged changes.

func (*Tracker) DeleteBranch

func (t *Tracker) DeleteBranch(ctx context.Context, branch string, force bool) (*DeleteBranchResult, error)

DeleteBranch deletes a local branch.

func (*Tracker) Diff

func (t *Tracker) Diff(ctx context.Context, path string) (string, error)

Diff returns the diff for a specific file.

func (*Tracker) DiffAll

func (t *Tracker) DiffAll(ctx context.Context) (map[string]string, error)

DiffAll returns diffs for all changed files.

func (*Tracker) DiffNewFile

func (t *Tracker) DiffNewFile(ctx context.Context, path string) (string, error)

DiffNewFile generates a diff-like output for untracked/new files. This reads the file content and formats it as if it were a git diff with all additions.

func (*Tracker) DiffStaged

func (t *Tracker) DiffStaged(ctx context.Context, path string) (string, error)

DiffStaged returns the staged diff for a specific file.

func (*Tracker) Discard

func (t *Tracker) Discard(ctx context.Context, paths []string) error

Discard discards uncommitted changes (restore files to last commit). Handles tracked files (checkout), staged files (unstage + checkout), and untracked files (delete).

func (*Tracker) Fetch

func (t *Tracker) Fetch(ctx context.Context, remote string, prune bool) (*FetchResult, error)

Fetch fetches from remote without merging.

func (*Tracker) GetDiffForEvent

func (t *Tracker) GetDiffForEvent(ctx context.Context, path string, maxDiffSizeKB int) *events.BaseEvent

GetDiffForEvent generates a git diff event for a file change.

func (*Tracker) GetEnhancedStatus

func (t *Tracker) GetEnhancedStatus(ctx context.Context) (*EnhancedStatus, error)

GetEnhancedStatus returns comprehensive git status including staging info.

func (*Tracker) GetFileContent

func (t *Tracker) GetFileContent(ctx context.Context, path string, maxSizeKB int) (string, bool, error)

GetFileContent returns the content of a file from the repository. Security: Uses os.ReadFile instead of shell command, with proper path validation.

func (*Tracker) GetRepoName

func (t *Tracker) GetRepoName() string

GetRepoName returns the name of the repository.

func (*Tracker) GetRepoRoot

func (t *Tracker) GetRepoRoot() string

GetRepoRoot returns the root path of the git repository.

func (*Tracker) GetStatus

func (t *Tracker) GetStatus(ctx context.Context) (*Status, error)

GetStatus returns the full git status including state machine.

func (*Tracker) Init

func (t *Tracker) Init(ctx context.Context, initialBranch string, initialCommit bool, commitMessage string) (*InitResult, error)

Init initializes a git repository.

func (*Tracker) IsGitRepo

func (t *Tracker) IsGitRepo() bool

IsGitRepo checks if the configured path is a git repository.

func (*Tracker) ListBranches

func (t *Tracker) ListBranches(ctx context.Context) (*BranchesResult, error)

ListBranches lists all branches with info.

func (*Tracker) ListDirectory

func (t *Tracker) ListDirectory(ctx context.Context, path string) ([]DirectoryEntry, error)

ListDirectory returns entries in a directory.

func (*Tracker) Log

func (t *Tracker) Log(ctx context.Context, limit int, skip int, branch string, path string, graph bool) (*LogResult, error)

Log returns commit history.

func (*Tracker) Merge

func (t *Tracker) Merge(ctx context.Context, branch string, noFastForward bool, message string) (*MergeResult, error)

Merge merges a branch into the current branch.

func (*Tracker) MergeAbort

func (t *Tracker) MergeAbort(ctx context.Context) (*MergeResult, error)

MergeAbort aborts an in-progress merge.

func (*Tracker) Pull

func (t *Tracker) Pull(ctx context.Context, rebase bool) (*PullResult, error)

Pull pulls changes from remote.

func (*Tracker) Push

func (t *Tracker) Push(ctx context.Context, force bool, setUpstream bool, remote, branch string) (*PushResult, error)

Push pushes commits to remote.

func (*Tracker) RemoteAdd

func (t *Tracker) RemoteAdd(ctx context.Context, name string, url string, fetch bool) (*RemoteAddResult, error)

RemoteAdd adds a new remote.

func (*Tracker) RemoteList

func (t *Tracker) RemoteList(ctx context.Context) (*RemoteListResult, error)

RemoteList returns all configured remotes.

func (*Tracker) RemoteRemove

func (t *Tracker) RemoteRemove(ctx context.Context, name string) (*RemoteRemoveResult, error)

RemoteRemove removes a remote.

func (*Tracker) SetUpstream

func (t *Tracker) SetUpstream(ctx context.Context, branch string, upstream string) (*SetUpstreamResult, error)

SetUpstream sets the upstream tracking branch.

func (*Tracker) Stage

func (t *Tracker) Stage(ctx context.Context, paths []string) error

Stage stages files for commit.

func (*Tracker) Stash

func (t *Tracker) Stash(ctx context.Context, message string, includeUntracked bool) (*StashResult, error)

Stash creates a new stash.

func (*Tracker) StashApply

func (t *Tracker) StashApply(ctx context.Context, index int) (*StashResult, error)

StashApply applies a stash without removing it.

func (*Tracker) StashDrop

func (t *Tracker) StashDrop(ctx context.Context, index int) (*StashResult, error)

StashDrop removes a stash.

func (*Tracker) StashList

func (t *Tracker) StashList(ctx context.Context) (*StashListResult, error)

StashList returns all stash entries.

func (*Tracker) StashPop

func (t *Tracker) StashPop(ctx context.Context, index int) (*StashResult, error)

StashPop applies and removes a stash.

func (*Tracker) Status

func (t *Tracker) Status(ctx context.Context) ([]ports.GitFileStatus, error)

Status returns the current git status.

func (*Tracker) Unstage

func (t *Tracker) Unstage(ctx context.Context, paths []string) error

Unstage removes files from staging area.

type WorkspaceGitState

type WorkspaceGitState string

WorkspaceGitState represents the git state of a workspace.

const (
	GitStateNoGit       WorkspaceGitState = "no_git"
	GitStateInitialized WorkspaceGitState = "git_init"
	GitStateNoRemote    WorkspaceGitState = "no_remote"
	GitStateNoPush      WorkspaceGitState = "no_push"
	GitStateSynced      WorkspaceGitState = "synced"
	GitStateDiverged    WorkspaceGitState = "diverged"
	GitStateConflict    WorkspaceGitState = "conflict"
)

Jump to

Keyboard shortcuts

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