fix(worktree): stale-clone reclaim keeps clones with uncommitted or unpushed work (#1829)

This commit is contained in:
sam committed 2026-11-09 09:41:33 +11:00
1 parent 055f2da9cd
commit c98666d6b7
3 files changed
+65 -5

No files matched your search

+55
View File
@@ -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
}