Status inspects the worktree and reports whether it holds uncommitted changes, untracked files, or commits added since creation.
(ctx context.Context)
| 269 | // Status inspects the worktree and reports whether it holds uncommitted |
| 270 | // changes, untracked files, or commits added since creation. |
| 271 | func (wt *Worktree) Status(ctx context.Context) (Status, error) { |
| 272 | out, err := gitOutput(ctx, wt.Dir, "status", "--porcelain") |
| 273 | if err != nil { |
| 274 | return Status{}, fmt.Errorf("inspecting worktree: %w", err) |
| 275 | } |
| 276 | |
| 277 | var st Status |
| 278 | for line := range strings.SplitSeq(out, "\n") { |
| 279 | if line == "" { |
| 280 | continue |
| 281 | } |
| 282 | if strings.HasPrefix(line, "??") { |
| 283 | st.Untracked = true |
| 284 | } else { |
| 285 | st.Modified = true |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | // HEAD moving away from the recorded branch point means the session |
| 290 | // committed something. When BaseCommit is empty (worktree created in a |
| 291 | // repo without commits) any resolvable HEAD counts as new work. |
| 292 | if head, err := gitOutput(ctx, wt.Dir, "rev-parse", "HEAD"); err == nil { |
| 293 | st.NewCommits = head != wt.BaseCommit |
| 294 | } |
| 295 | |
| 296 | return st, nil |
| 297 | } |
| 298 | |
| 299 | // Remove deletes the worktree's directory and its branch, discarding any |
| 300 | // uncommitted changes, untracked files, and commits. Callers decide when |