Documentation
¶
Overview ¶
Package worklog records the worktrees the app made for pull requests and issues, which item each is for and the sessions started in it, and the worktrees it cleaned up. It owns the worktree_records and worktree_cleanups tables.
The worktrees service records a worktree when it makes one for a launch, or finds one on an item's branch, and the launcher records each session started in it. The worktrees service reads the records to link each worktree to its item exactly, to resume it on the next launch, and to find the clones its automatic cleanup checks, and it checks every record against git each time, dropping one whose worktree is gone. A record is only a pointer: whether a worktree may be removed is always decided from git and GitHub at the time.
The worktrees service records a cleanup once the worktree is gone, whether the user asked for it or the automatic sweep did it, so the Worktrees view can show what was removed and when, across restarts. Only the newest KeepCleanups records are kept.
It also records the pull request a worktree's commits went into as copies (Copy), when a listing finds each of them copied, as a cherry-pick or a rebase copies a commit, onto one pushed branch whose pull request it knows, so a squash merge and the deletion of that branch don't undo it. It owns the worktree_copies table for these.
Index ¶
- Constants
- Variables
- type Cleanup
- type Copy
- type Record
- type Store
- func (s *Store) ClearCleanups(ctx context.Context) error
- func (s *Store) Copies(ctx context.Context) ([]Copy, error)
- func (s *Store) Forget(ctx context.Context, clonePath, worktree string) error
- func (s *Store) ForgetCopy(ctx context.Context, worktreePath string) error
- func (s *Store) RecentCleanups(ctx context.Context, limit int) ([]Cleanup, error)
- func (s *Store) Record(ctx context.Context, r Record) error
- func (s *Store) RecordCleanup(ctx context.Context, c Cleanup) error
- func (s *Store) RecordCopy(ctx context.Context, c Copy) error
- func (s *Store) Touch(ctx context.Context, clonePath, worktree string, id agent.ID, at time.Time) error
- func (s *Store) Worktrees(ctx context.Context) ([]Record, error)
Constants ¶
const KeepCleanups = 100
KeepCleanups is how many cleanups are kept; recording one more drops the oldest.
const LegacyDir = ".claude/worktrees"
LegacyDir is where, under a clone, Claude Code made the worktrees of launches before the app made them itself: <clone>/.claude/worktrees/<name>.
Variables ¶
var ( // ErrInvalidRecord is returned for a worktree record missing a field, or with an // unknown kind, a number that is not positive, or a relative path. ErrInvalidRecord = errors.New("invalid worktree record") // ErrNotRecorded is returned when touching a worktree that has no record. ErrNotRecorded = errors.New("the worktree is not recorded") )
var ErrInvalidCleanup = errors.New("invalid worktree cleanup")
ErrInvalidCleanup is returned for a cleanup without a repository or worktree name.
var ErrInvalidCopy = errors.New("invalid worktree copy record")
ErrInvalidCopy is returned for a copy record missing a field, or with a relative path, a number that is not positive, or no commit.
Functions ¶
This section is empty.
Types ¶
type Cleanup ¶
type Cleanup struct {
// ID identifies the record; RecordCleanup ignores it.
ID int64
// CleanedAt is when the worktree was removed.
CleanedAt time.Time
// Automatic reports a cleanup by the sweep rather than by the user.
Automatic bool
// Repo is the clone's directory name, and Worktree the worktree's, e.g. "pr-184".
Repo string
Worktree string
// DisplayPath is the worktree's path relative to home with ~.
DisplayPath string
// DeletedBranch is the branch deleted with it, or "" when it was kept.
DeletedBranch string
// Head is the commit the worktree had checked out.
Head string
// The pull request or issue it was for: kind ("pr" or "issue"), repository, number,
// title, URL, and state ("merged" or "closed").
ItemKind string
ItemOwner string
ItemName string
ItemNumber int
ItemTitle string
ItemURL string
ItemState string
}
Cleanup is one worktree the app cleaned up.
type Copy ¶ added in v0.7.1
type Copy struct {
// ClonePath is the worktree's clone and WorktreePath its directory, both resolved.
ClonePath string
WorktreePath string
// Head is the commit the worktree had checked out.
Head string
// Ref is the remote-tracking branch the copies were on, as in
// refs/remotes/origin/feature-x.
Ref string
// Owner, Name, and Number are the pull request that branch heads.
Owner string
Name string
Number int
// CopyTip is the branch's commit when last seen holding a copy of every one of Commits.
CopyTip string
// Commits are the worktree's commits that weren't on GitHub then, never empty.
Commits []string
// CreatedAt is when the record was first written for Head, and UpdatedAt when last;
// RecordCopy stamps both.
CreatedAt time.Time
UpdatedAt time.Time
}
Copy is the pull request a worktree's commits went into as copies: each of its commits not on GitHub had an identical copy, as a cherry-pick or a rebase makes, on one pushed branch, whose pull request was known. The worktrees service takes it only while the worktree's HEAD is still Head.
type Record ¶
type Record struct {
// ClonePath is the clone the worktree belongs to.
ClonePath string
// Worktree is the item's worktree name, pr-<n> or issue-<n>.
Worktree string
// Path is the worktree's absolute directory, or "" for a record kept before the app
// made worktrees itself (see Dir).
Path string
// Branch is the branch the worktree was made on, worktree-<name>.
Branch string
// Owner and Name are the item's repository.
Owner string
Name string
// Kind is the item's kind.
Kind item.Kind
// Number is the item's number.
Number int
// Agent is the agent of the latest session started there, or agent.Inherit when none
// has started.
Agent agent.ID
// CreatedAt is when the worktree was made or first recorded.
CreatedAt time.Time
// LastAgentAt is when the latest session started, or zero when none has.
LastAgentAt time.Time
// Launches counts the sessions started there.
Launches int
// FetchedHead is the commit the app made the worktree at, just fetched from GitHub: a
// pull request's head, or the default branch an issue's worktree starts from; "" for
// a worktree it found, or added on a branch left from an earlier one. Every commit it
// reaches came from GitHub. Record keeps the one stored when this is "".
FetchedHead string
}
Record is one worktree the app made for a pull request or issue, or found on its branch: where it is, the item it is for, and the sessions started in it.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store reads and writes the launch, cleanup, and copy records.
func New ¶
New returns a Store over the pool db, which stamps the copy records with c, or the system clock for nil.
func (*Store) ClearCleanups ¶
ClearCleanups forgets every cleanup.
func (*Store) Copies ¶ added in v0.7.1
Copies returns every worktree's copy record, in the order they were first written.
func (*Store) ForgetCopy ¶ added in v0.7.1
ForgetCopy removes the copy record of the worktree at worktreePath, if there is one.
func (*Store) RecentCleanups ¶
RecentCleanups returns up to limit cleanups, newest first. A negative limit returns every one kept.
func (*Store) Record ¶
Record stores where a clone's worktree for an item is, and the item it is for, replacing the place an earlier record of the same worktree named. A worktree recorded already keeps when it was made and its sessions, and its FetchedHead unless r names one.
func (*Store) RecordCleanup ¶
RecordCleanup stores a cleanup and drops the oldest records past KeepCleanups, in one transaction.
func (*Store) RecordCopy ¶ added in v0.7.1
RecordCopy stores the pull request a worktree's copied commits went into, replacing what was stored for the worktree before, stamped with the store's clock. A record for the same HEAD keeps when it was first written.