fix(worktree): stale-clone reclaim keeps clones with uncommitted or unpushed work (#1829)
This commit is contained in:
3 files changed
+65
-5
No files matched your search
@@ -1848,12 +1848,13 @@ func (d *Daemon) prepareWorkspace(ctx context.Context, task *workflow.Task, tree
|
||||
}
|
||||
|
||||
// reclaimStaleClones frees the work volume before a clone waits on it, by
|
||||
// removing the oldest clones nothing has touched for cloneRetention.
|
||||
// removing the oldest clones nothing has touched for cloneRetention. A clone
|
||||
// with uncommitted or unpushed work is kept.
|
||||
func (d *Daemon) reclaimStaleClones(ctx context.Context, task *workflow.Task, trees *worktree.Manager) {
|
||||
if have, err := trees.FreeBytes(); err != nil || have >= minCloneSpace {
|
||||
return
|
||||
}
|
||||
n, err := worktree.ReclaimStale(d.Cfg.Get().WorkDir, trees.Dir(task.Owner, task.Repo, task.IssueNumber), cloneRetention, trees.FreeBytes, minCloneSpace)
|
||||
n, err := worktree.ReclaimStale(d.Cfg.Get().WorkDir, trees.Dir(task.Owner, task.Repo, task.IssueNumber), cloneRetention, trees.FreeBytes, minCloneSpace, func(dir string) bool { return trees.HoldsUnsharedWork(ctx, dir) })
|
||||
if err != nil {
|
||||
d.Log.WarnContext(ctx, "stale clone reclaim failed", "task", task.ID, "err", err)
|
||||
}
|
||||
|
||||
@@ -58,8 +58,9 @@ func AwaitSpace(ctx context.Context, free func() (uint64, error), floor uint64,
|
||||
|
||||
// ReclaimStale removes task clones under root that have been untouched for
|
||||
// longer than minAge, oldest first, until free reports at least floor bytes.
|
||||
// keep is never removed. It returns how many clones it removed.
|
||||
func ReclaimStale(root, keep string, minAge time.Duration, free func() (uint64, error), floor uint64) (int, error) {
|
||||
// keep is never removed, nor is a clone holdsWork reports as holding work
|
||||
// that exists nowhere else. It returns how many clones it removed.
|
||||
func ReclaimStale(root, keep string, minAge time.Duration, free func() (uint64, error), floor uint64, holdsWork func(dir string) bool) (int, error) {
|
||||
type clone struct {
|
||||
dir string
|
||||
used time.Time
|
||||
@@ -88,7 +89,10 @@ func ReclaimStale(root, keep string, minAge time.Duration, free func() (uint64,
|
||||
removed := 0
|
||||
for _, c := range stale {
|
||||
if have, err := free(); err != nil || have >= floor {
|
||||
return removed, nil
|
||||
return removed, nil //nolint:nilerr // an unmeasurable volume stops the reclaim, it is not an error
|
||||
}
|
||||
if holdsWork(c.dir) {
|
||||
continue
|
||||
}
|
||||
if err := os.RemoveAll(c.dir); err != nil {
|
||||
return removed, err
|
||||
|
||||
@@ -1193,3 +1193,58 @@ func (m *Manager) CheckAccess(ctx context.Context, owner, repo, base string) err
|
||||
func openRepo(dir string) (*git.Repository, error) {
|
||||
return git.PlainOpenWithOptions(dir, &git.PlainOpenOptions{AlternatesFS: osfs.New(string(filepath.Separator))})
|
||||
}
|
||||
|
||||
// HoldsUnsharedWork reports whether the clone at dir has uncommitted changes
|
||||
// or a local branch whose tip its remote does not have. An unreadable clone
|
||||
// counts as holding work, so it is never deleted on a guess.
|
||||
func (m *Manager) HoldsUnsharedWork(ctx context.Context, dir string) bool {
|
||||
if dirty, err := m.HasUncommittedChanges(ctx, dir); err != nil || dirty {
|
||||
return true
|
||||
}
|
||||
r, err := git.PlainOpen(dir)
|
||||
if err != nil {
|
||||
return true
|
||||
}
|
||||
branches, err := r.Branches()
|
||||
if err != nil {
|
||||
return true
|
||||
}
|
||||
unshared := false
|
||||
_ = branches.ForEach(func(ref *plumbing.Reference) error {
|
||||
remote, err := r.Reference(plumbing.NewRemoteReferenceName(git.DefaultRemoteName, ref.Name().Short()), true)
|
||||
if err != nil || remote.Hash() != ref.Hash() {
|
||||
if _, err := r.CommitObject(ref.Hash()); err == nil && !m.reachableFromRemotes(r, ref.Hash()) {
|
||||
unshared = true
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return unshared
|
||||
}
|
||||
|
||||
// reachableFromRemotes reports whether any remote-tracking ref contains hash.
|
||||
func (m *Manager) reachableFromRemotes(r *git.Repository, hash plumbing.Hash) bool {
|
||||
refs, err := r.References()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
found := false
|
||||
_ = refs.ForEach(func(ref *plumbing.Reference) error {
|
||||
if found || !ref.Name().IsRemote() {
|
||||
return nil
|
||||
}
|
||||
tip, err := r.CommitObject(ref.Hash())
|
||||
if err != nil {
|
||||
return nil //nolint:nilerr // an unreadable ref is skipped
|
||||
}
|
||||
target, err := r.CommitObject(hash)
|
||||
if err != nil {
|
||||
return nil //nolint:nilerr // an unreadable commit is skipped
|
||||
}
|
||||
if ok, err := target.IsAncestor(tip); err == nil && ok {
|
||||
found = true
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return found
|
||||
}
|
||||
Reference in new issue
Block a user