Commit 0f62bdf
authored
feat: auto-detect parent branches for untracked local branches (#53)
* feat(git): add ListLocalBranches method
Lists all local branch names via `git for-each-ref`.
Needed for auto-detection of untracked branches.
* feat(git): add RevListCount method
Counts commits between two refs using `git rev-list --count`.
Used by parent auto-detection to rank candidate parents by distance.
* feat(detect): add parent branch auto-detection package
Introduces `DetectParentLocal` (merge-base only) and `DetectParent`
(PR + merge-base) for inferring parent branches of untracked
local branches. Returns confidence levels: High (PR match),
Medium (unique merge-base winner), Ambiguous (tie/no signal).
Also includes `FindUntrackedCandidates` for discovering local
branches not yet in a stack.
* feat(tree): add Detected and Confidence fields to Node
Detected nodes are displayed with an annotation like '(detected)'
in tree output. This supports read-only auto-detection preview
in the log command.
* feat(log): auto-detect untracked branches in tree display
The log command now shows untracked branches that can be inferred
via merge-base analysis, annotated with '(detected)'. This is
strictly read-only — no git config is modified. Use --no-detect
to disable.
* feat(adopt): support auto-detection when parent is omitted
Running `gh stack adopt` without a parent argument now auto-detects
the parent via PR base branch and merge-base analysis. Prompts
when ambiguous in interactive mode; errors in non-interactive.
* feat(sync): auto-detect and adopt untracked branches
The sync command now detects untracked branches via PR base refs
and merge-base analysis, adopting them before restacking. Use
`--no-detect` to skip.
A shared `autoDetectAndAdopt` helper in `cmd/detect_helpers.go`
handles the full workflow: finding candidates, detecting parents
(with PR + merge-base fallback), prompting for ambiguous cases,
checking for cycles, and committing the adoption.
* feat(restack): auto-detect untracked branches before restacking
The restack/cascade command now detects and adopts untracked
branches before processing. Use `--no-detect` to skip.
* fix: address PR review comments for robustness and correctness
- Add deterministic tie-breaker (by name) to `rankCandidates` sort and
sort tied candidates for stable `Result.Candidates` ordering
- Return captured error when all merge-base comparisons fail instead of
silently returning `Ambiguous` with no error
- Handle errors in test helpers (`addCommit`, `setupTestRepo`,
`setupInternalTestRepo`) — fail fast on `WriteFile`/`git` failures
- Assert `CreateBranch` return values in `TestListLocalBranches`
- Check `WriteFile`/`git add`/`commit` errors in `TestRevListCount`
- Remove `TestAdoptAutoDetect_PrintsConfidence` (trivially-true assertion
that tested `style.Muted()` not actual adopt behavior)
- Re-sort parent children after injecting detected nodes in
`injectDetectedNodes` to maintain alphabetical ordering
- Loop `autoDetectAndAdopt` until no progress to handle untracked chains
where a branch depends on another untracked branch
* fix: address second round of PR review comments
- Check all `CreateAndCheckout`/`CreateBranch`/`Checkout`/`CurrentBranch`
errors in test helpers and test functions (detect_test.go,
log_internal_test.go, adopt_test.go) using `must*` helpers to avoid
variable shadowing lint errors
- Check `config.Load`, `SetTrunk`, and `SetParent` errors in tests
- Replace tree-based cycle check in `autoDetectAndAdopt` with a
config-based parent chain walk (`wouldCycle`) that catches cycles the
tree model misses (e.g., nodes omitted due to broken parent links)
* fix: address third round of PR review comments
- Skip auto-detection in `--porcelain` mode since porcelain format has
no column to distinguish detected branches from tracked ones
- Remove unused `--all` flag from `log` command
- Update README: document auto-detect behavior for `adopt`, document
`--no-detect` flag for `log`, remove stale `--all` flag docs
- Replace tree-based cycle check in `adopt.go` with config-based
`wouldCycle()` (same fix as detect_helpers.go from round 2)
* fix: sort candidates by trunk distance and document --no-detect flags
- Sort `autoDetectAndAdopt` candidates by merge-base distance from trunk
(ascending) so parents are processed before children in untracked
chains, preventing mis-adoption when a child branch alphabetically
sorts before its untracked parent
- Document `--no-detect` flag in README for `restack` and `sync`
commands (was already implemented but undocumented)
* fix: default detected annotation and check CreateAndCheckout error
- Add default case in `formatNode` so detected nodes with any
confidence level (including zero value) always show "(detected)"
- Check `CreateAndCheckout` error in `TestRevListCount`
* fix: filter out descendant branches in rankCandidates
When `branch` is an ancestor of a candidate, the merge-base equals
`branch`'s tip, giving distance 0 and incorrectly selecting the
descendant as the parent. Now resolves `branch` tip up front and
skips candidates where merge-base == branch tip AND the candidate
has commits ahead (i.e., is a true descendant, not just at the same
commit).1 parent 6e7329f commit 0f62bdf
15 files changed
Lines changed: 1292 additions & 36 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
168 | 168 | | |
169 | 169 | | |
170 | 170 | | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
171 | 175 | | |
172 | 176 | | |
173 | | - | |
174 | | - | |
175 | | - | |
176 | | - | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
177 | 181 | | |
178 | 182 | | |
179 | 183 | | |
| |||
208 | 212 | | |
209 | 213 | | |
210 | 214 | | |
| 215 | + | |
| 216 | + | |
211 | 217 | | |
212 | 218 | | |
213 | 219 | | |
214 | | - | |
| 220 | + | |
215 | 221 | | |
216 | 222 | | |
217 | 223 | | |
| |||
296 | 302 | | |
297 | 303 | | |
298 | 304 | | |
299 | | - | |
300 | | - | |
301 | | - | |
302 | | - | |
303 | | - | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
304 | 311 | | |
305 | 312 | | |
306 | 313 | | |
| |||
327 | 334 | | |
328 | 335 | | |
329 | 336 | | |
| 337 | + | |
330 | 338 | | |
331 | 339 | | |
332 | 340 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
| 12 | + | |
| 13 | + | |
11 | 14 | | |
12 | | - | |
13 | 15 | | |
14 | 16 | | |
15 | 17 | | |
16 | 18 | | |
17 | | - | |
| 19 | + | |
18 | 20 | | |
19 | 21 | | |
20 | 22 | | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
21 | 27 | | |
22 | | - | |
| 28 | + | |
23 | 29 | | |
24 | 30 | | |
25 | 31 | | |
| |||
42 | 48 | | |
43 | 49 | | |
44 | 50 | | |
45 | | - | |
46 | | - | |
47 | | - | |
| 51 | + | |
48 | 52 | | |
49 | 53 | | |
50 | 54 | | |
| |||
73 | 77 | | |
74 | 78 | | |
75 | 79 | | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
76 | 126 | | |
77 | 127 | | |
78 | 128 | | |
79 | 129 | | |
80 | 130 | | |
81 | 131 | | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
95 | 136 | | |
96 | 137 | | |
97 | 138 | | |
| |||
105 | 146 | | |
106 | 147 | | |
107 | 148 | | |
108 | | - | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
109 | 154 | | |
110 | 155 | | |
111 | 156 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
5 | 8 | | |
6 | 9 | | |
7 | 10 | | |
| 11 | + | |
8 | 12 | | |
9 | 13 | | |
10 | 14 | | |
11 | 15 | | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
12 | 32 | | |
13 | 33 | | |
14 | 34 | | |
| |||
148 | 168 | | |
149 | 169 | | |
150 | 170 | | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
| |||
30 | 31 | | |
31 | 32 | | |
32 | 33 | | |
| 34 | + | |
33 | 35 | | |
34 | 36 | | |
35 | 37 | | |
36 | 38 | | |
37 | 39 | | |
38 | 40 | | |
| 41 | + | |
39 | 42 | | |
40 | 43 | | |
41 | 44 | | |
| |||
64 | 67 | | |
65 | 68 | | |
66 | 69 | | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
67 | 78 | | |
68 | 79 | | |
69 | 80 | | |
| |||
0 commit comments