-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew_worktree_dialog.dart
More file actions
587 lines (558 loc) · 19.5 KB
/
Copy pathnew_worktree_dialog.dart
File metadata and controls
587 lines (558 loc) · 19.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../app/theme.dart';
import '../../store/models.dart';
import '../../store/store.dart';
import '../../ui/home/repo_chips.dart' show branchOptionsForRepo;
import 'groups/group_providers.dart';
import 'groups/groups_controller.dart';
import 'selected_worktree.dart';
/// Where a new worktree comes from (SPEC-30).
enum _WorktreeFrom { existing, newBranch, fromPr }
/// Opens the New-worktree dialog (SPEC-30): it asks **only** where the worktree
/// comes from — use an existing worktree, create a New-branch, or fork a
/// From-PR worktree. It does **not** pick a harness, adjust config pills, or
/// take a first message; those belong to the Choose-a-harness starter
/// ([WorktreeStarter]) the user lands on after.
///
/// [projectId] preselects the repository; when null it defaults to the active
/// group's repo (a worktree group knows its repo) and otherwise the first repo,
/// because a board can hold worktrees from **any** repo.
///
/// On confirm it creates the worktree (rolling it back if anything after
/// creation fails) and, when [activateGroup] is true (the default), activates
/// that worktree's group so the user lands on the harness picker. Callers that
/// want to place the worktree in a specific tab/split (the group-aware ⌘T/⌘D on
/// a board) pass `activateGroup: false` and act on the returned worktree.
///
/// Resolves with the created [SelectedWorktree], or null when dismissed.
Future<SelectedWorktree?> showNewWorktreeDialog(
BuildContext context,
WidgetRef ref, {
String? projectId,
bool activateGroup = true,
}) {
return showDialog<SelectedWorktree>(
context: context,
builder: (_) => _NewWorktreeDialog(
initialProjectId: projectId,
activateGroup: activateGroup,
),
);
}
class _NewWorktreeDialog extends ConsumerStatefulWidget {
const _NewWorktreeDialog({
this.initialProjectId,
required this.activateGroup,
});
final String? initialProjectId;
final bool activateGroup;
@override
ConsumerState<_NewWorktreeDialog> createState() => _NewWorktreeDialogState();
}
class _NewWorktreeDialogState extends ConsumerState<_NewWorktreeDialog> {
String? _projectId;
_WorktreeFrom _source = _WorktreeFrom.newBranch;
String? _existingWorktreePath;
String? _targetBranch;
int? _prNumber;
Future<List<OpenPr>>? _prsFuture;
/// Optional name for the branch a `newBranch` worktree forks. Empty means the
/// server auto-generates one.
final TextEditingController _branchNameCtrl = TextEditingController();
bool _creating = false;
String? _error;
@override
void initState() {
super.initState();
final repos = ref.read(reposProvider).repos;
_projectId =
widget.initialProjectId ??
_activeGroupRepo() ??
(repos.isNotEmpty ? repos.first.id : null);
_targetBranch = _defaultBranchFor(_projectId);
}
@override
void dispose() {
_branchNameCtrl.dispose();
super.dispose();
}
/// The active worktree group's repo, when the active group is scoped to one —
/// a board owns no repo, so this is null there.
String? _activeGroupRepo() => ref.read(activeGroupProvider).projectId;
/// Lazily fetch the open-PR list the first time the "From PR" source is
/// selected — no network call while the dialog only shows New branch.
void _loadPrs() {
final projectId = _projectId;
final future = projectId == null
? Future.value(const <OpenPr>[])
: ref.read(storeControllerProvider.notifier).listOpenPrs(projectId);
future.ignore();
_prsFuture = future;
}
String? _defaultBranchFor(String? projectId) {
for (final r in ref.read(reposProvider).repos) {
if (r.id == projectId) {
final options = branchOptionsForRepo(r);
return options.isEmpty ? null : options.first;
}
}
return null;
}
/// The target branch actually used: [_targetBranch] when it is a live option,
/// else the first option (what [_newBranchPanel] displays).
String? _effectiveTargetBranch(String? projectId) {
RepoInfo? repo;
for (final r in ref.read(reposProvider).repos) {
if (r.id == projectId) repo = r;
}
final options = repo == null
? const <String>[]
: branchOptionsForRepo(repo);
if (options.isEmpty) return _targetBranch;
return options.contains(_targetBranch) ? _targetBranch : options.first;
}
void _close() {
if (_creating) return;
Navigator.of(context).pop();
}
/// Whether the Create button should be enabled: a From-PR worktree needs a
/// selected PR, and an Existing worktree needs one to have been resolved.
bool _canCreateWorktree() {
return switch (_source) {
_WorktreeFrom.fromPr => _prNumber != null,
_WorktreeFrom.existing => _existingWorktreePath != null,
_WorktreeFrom.newBranch => true,
};
}
void _onRepoChanged(String? projectId) {
if (projectId == null || projectId == _projectId) return;
setState(() {
_projectId = projectId;
_targetBranch = _defaultBranchFor(projectId);
// A different repo has a different PR list; drop the cached future so the
// panel refetches when From PR is shown again.
_prsFuture = null;
_prNumber = null;
// The old repo's worktree no longer belongs to this repo.
_existingWorktreePath = _source == _WorktreeFrom.existing
? _firstExistingWorktreePath()
: null;
if (_source == _WorktreeFrom.fromPr) _loadPrs();
});
}
/// Creates the worktree from the chosen source, activates its group when
/// asked, then pops with it. A failure after creation removes the fresh
/// worktree so a retry doesn't orphan it; errors surface inline.
Future<void> _create() async {
if (_creating) return;
final projectId = _projectId;
if (projectId == null) return;
setState(() {
_creating = true;
_error = null;
});
final store = ref.read(storeControllerProvider.notifier);
String? createdPath;
try {
final ({String path, String? branch}) created;
switch (_source) {
case _WorktreeFrom.existing:
final path = _existingWorktreePath;
if (path == null) {
// Mirror the fromPr branch: never bare-return after _creating is
// set, or the dialog wedges (Create and Close both disabled).
setState(() {
_creating = false;
_error = 'Select a worktree first.';
});
return;
}
String? existingBranch;
for (final w in _existingWorktrees()) {
if (w.path == path) {
existingBranch = w.branch;
break;
}
}
created = (path: path, branch: existingBranch);
case _WorktreeFrom.newBranch:
final name = _branchNameCtrl.text.trim();
created = await store.createWorktree(
projectId,
targetBranch: _effectiveTargetBranch(projectId),
branchName: name.isEmpty ? null : name,
);
case _WorktreeFrom.fromPr:
final pr = _prNumber;
if (pr == null) {
// Surfaced as UI copy, so it must not be an exception: `'$e'` on a
// StateError renders as "Bad state: Select a pull request first."
setState(() {
_creating = false;
_error = 'Select a pull request first.';
});
return;
}
created = await store.createWorktreeFromPr(projectId, pr);
}
createdPath = created.path;
final worktree = SelectedWorktree(
projectId: projectId,
path: created.path,
branch: created.branch,
);
// Hand off BEFORE activating the group: creating the worktree is the
// side-effectful step and it has succeeded. If activation then threw, a
// rollback would delete a worktree that is fine and that the user asked
// for — losing work to tidy up bookkeeping.
createdPath = null;
if (widget.activateGroup) {
ref
.read(groupsControllerProvider.notifier)
.openWorktreeGroup(
projectId: projectId,
worktreePath: created.path,
label: created.branch ?? created.path.split('/').last,
);
}
if (!mounted) return;
_creating = false; // reset before popping, in case the pop is refused
Navigator.of(context).pop(worktree);
} catch (e) {
if (createdPath != null) {
await store.removeWorktree(projectId, createdPath).catchError((_) {});
}
if (!mounted) return;
setState(() {
_creating = false;
_error = '$e';
});
}
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
// Watch the repos snapshot so the dialog rebuilds when the store broadcasts
// a change while it is open (a worktree added/removed elsewhere, or the
// branch list refreshed); the field helpers then re-read the fresh list.
ref.watch(reposProvider);
return CallbackShortcuts(
bindings: {const SingleActivator(LogicalKeyboardKey.escape): _close},
child: Dialog(
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 480, maxHeight: 560),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.fromLTRB(20, 16, 12, 0),
child: Row(
children: [
Expanded(
child: Text(
'New worktree',
style: theme.textTheme.titleLarge,
),
),
IconButton(
tooltip: 'Close',
icon: const Icon(Icons.close),
onPressed: _creating ? null : _close,
),
],
),
),
Padding(
padding: const EdgeInsets.fromLTRB(20, 0, 20, 8),
child: Text(
'Creates the branch and its folder. You pick the harness and '
'write the first message in the pane you land on.',
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.outline,
),
),
),
Flexible(
child: SingleChildScrollView(
padding: const EdgeInsets.fromLTRB(20, 4, 20, 16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_repositoryField(theme),
const SizedBox(height: kSpace16),
_sourceField(theme),
if (_error != null) ...[
const SizedBox(height: kSpace12),
Text(
_error!,
style: TextStyle(color: theme.colorScheme.error),
),
],
],
),
),
),
Padding(
padding: const EdgeInsets.fromLTRB(20, 0, 20, 16),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
FilledButton(
onPressed: _canCreateWorktree() && !_creating
? _create
: null,
child: const Text('Create worktree'),
),
],
),
),
],
),
),
),
);
}
Widget _fieldLabel(ThemeData theme, String text) => Padding(
padding: const EdgeInsets.only(bottom: 8),
child: Text(
text,
style: theme.textTheme.labelLarge?.copyWith(
color: theme.colorScheme.outline,
),
),
);
Widget _labeledRow(ThemeData theme, String label, Widget child) => Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
SizedBox(
width: 52,
child: Text(
label,
style: theme.textTheme.labelLarge?.copyWith(
color: theme.colorScheme.outline,
),
),
),
Expanded(child: child),
],
);
/// The repository selector — required, because a board can hold worktrees
/// from any repo (the old dialog inferred it from the project).
Widget _repositoryField(ThemeData theme) {
final repos = ref.read(reposProvider).repos;
final value = repos.any((r) => r.id == _projectId)
? _projectId
: (repos.isEmpty ? null : repos.first.id);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_fieldLabel(theme, 'Repository'),
DropdownButtonFormField<String>(
key: const ValueKey('wt-repo-picker'),
initialValue: value,
isExpanded: true,
items: [
for (final r in repos)
DropdownMenuItem(
value: r.id,
child: Text(r.name, overflow: TextOverflow.ellipsis),
),
],
onChanged: _creating ? null : _onRepoChanged,
),
],
);
}
Widget _sourceField(ThemeData theme) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_fieldLabel(theme, 'From'),
SegmentedButton<_WorktreeFrom>(
showSelectedIcon: false,
segments: const [
ButtonSegment(
value: _WorktreeFrom.existing,
label: Text('Existing'),
),
ButtonSegment(
value: _WorktreeFrom.newBranch,
label: Text('New branch'),
),
ButtonSegment(value: _WorktreeFrom.fromPr, label: Text('From PR')),
],
selected: {_source},
onSelectionChanged: _creating
? null
: (s) => setState(() {
_source = s.first;
if (_source == _WorktreeFrom.existing) {
_existingWorktreePath ??= _firstExistingWorktreePath();
}
if (_source == _WorktreeFrom.fromPr && _prsFuture == null) {
_loadPrs();
}
}),
),
const SizedBox(height: kSpace10),
switch (_source) {
_WorktreeFrom.existing => _existingPanel(theme),
_WorktreeFrom.newBranch => _newBranchPanel(theme),
_WorktreeFrom.fromPr => _fromPrPanel(theme),
},
],
);
}
/// Live worktrees in the selected repo (the source list for Existing).
List<Worktree> _existingWorktrees() {
for (final r in ref.read(reposProvider).repos) {
if (r.id == _projectId) return r.worktrees;
}
return const <Worktree>[];
}
/// The first existing worktree's path, or null when the repo has none.
String? _firstExistingWorktreePath() {
final worktrees = _existingWorktrees();
return worktrees.isEmpty ? null : worktrees.first.path;
}
Widget _existingPanel(ThemeData theme) {
final worktrees = _existingWorktrees();
if (worktrees.isEmpty) {
return Text(
'No existing worktrees in this repo.',
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.outline,
),
);
}
final selected = worktrees.any((w) => w.path == _existingWorktreePath)
? _existingWorktreePath
: worktrees.first.path;
return _labeledRow(
theme,
'Worktree',
DropdownButtonFormField<String>(
key: const ValueKey('wt-existing-picker'),
initialValue: selected,
isExpanded: true,
items: [
for (final wt in worktrees)
DropdownMenuItem(
value: wt.path,
child: Text(
wt.branch ?? 'detached',
overflow: TextOverflow.ellipsis,
),
),
],
onChanged: _creating
? null
: (v) => setState(() => _existingWorktreePath = v),
),
);
}
Widget _newBranchPanel(ThemeData theme) {
RepoInfo? repo;
for (final r in ref.read(reposProvider).repos) {
if (r.id == _projectId) repo = r;
}
final options = repo == null
? const <String>[]
: branchOptionsForRepo(repo);
final Widget fromControl = options.isEmpty
? Text(
'the repo’s default branch',
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.outline,
),
)
: DropdownButtonFormField<String>(
key: ValueKey('wt-branch-$_projectId'),
initialValue: options.contains(_targetBranch)
? _targetBranch
: options.first,
isExpanded: true,
items: [
for (final b in options)
DropdownMenuItem(
value: b,
child: Text(b, overflow: TextOverflow.ellipsis),
),
],
onChanged: _creating
? null
: (v) => setState(() => _targetBranch = v),
);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_labeledRow(theme, 'Base', fromControl),
const SizedBox(height: kSpace10),
_labeledRow(
theme,
'Name',
TextField(
controller: _branchNameCtrl,
enabled: !_creating,
// Mirror the server's slugifyBranch length cap (80).
inputFormatters: [LengthLimitingTextInputFormatter(80)],
decoration: const InputDecoration(
isDense: true,
border: OutlineInputBorder(),
hintText: 'Leave blank to auto-generate',
),
),
),
],
);
}
Widget _fromPrPanel(ThemeData theme) {
return FutureBuilder<List<OpenPr>>(
future: _prsFuture,
builder: (context, snap) {
if (snap.connectionState != ConnectionState.done) {
return const Padding(
padding: EdgeInsets.symmetric(vertical: kSpace8),
child: LinearProgressIndicator(),
);
}
if (snap.hasError) {
return Text(
"Couldn't load pull requests:\n${snap.error}",
style: TextStyle(color: theme.colorScheme.error),
);
}
final prs = snap.data ?? const <OpenPr>[];
if (prs.isEmpty) {
return Text(
'No open pull requests (or GitHub CLI is unavailable).',
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.outline,
),
);
}
final value = prs.any((p) => p.number == _prNumber) ? _prNumber : null;
return DropdownButtonFormField<int>(
key: const ValueKey('wt-pr-picker'),
initialValue: value,
isExpanded: true,
hint: const Text('Select a pull request'),
items: [
for (final pr in prs)
DropdownMenuItem(
value: pr.number,
child: Text(
'#${pr.number} ${pr.title.isEmpty ? pr.headRefName : pr.title}',
overflow: TextOverflow.ellipsis,
),
),
],
onChanged: _creating ? null : (v) => setState(() => _prNumber = v),
);
},
);
}
}