Skip to content

Commit 7ded875

Browse files
committed
Bind stash mutations to reviewed working-tree snapshots
1 parent f5d0344 commit 7ded875

6 files changed

Lines changed: 63 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ tokens also bind the current symbolic HEAD target, so an external switch or
5151
rename to a branch at the same commit invalidates the old review. Granular staging
5252
uses an ephemeral alternate index while holding the actual Git index lock, then
5353
atomically installs that index; it never rewrites working files to stage them.
54+
Stash actions that replace working files also require the reviewed snapshot's
55+
working token, so an external edit invalidates a previously reviewed request.
5456
Repository/common-directory advisory locks coordinate BusyStack processes. Git's
5557
locks and sequencer remain authoritative for other clients. Open-worktree leases
5658
protect worktrees used by BusyStack views. A cancelled process is reconciled;

‎lib/src/engine.dart‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,16 @@ final class GitEngine implements GitClient, GitWorkflowClient, GitPatchClient {
421421
'Working files changed after reset review. Refresh and review their effects before resetting.',
422422
);
423423
}
424+
if (request is StashMutation &&
425+
request.replacesWorkingFiles &&
426+
(request.expectedWorkingToken == null ||
427+
request.expectedWorkingToken != before.token)) {
428+
throw const GitException(
429+
GitErrorCode.staleReview,
430+
diagnostic:
431+
'Working files changed after stash review. Refresh and review their contents before applying this action.',
432+
);
433+
}
424434
if (before.operation.kind != GitOperationKind.none &&
425435
request is! StagePaths &&
426436
request is! WriteIndex &&

‎lib/src/workflow_codec.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ GitMutation decodeGitMutation(Json j, {Uint8List? contentBytes}) {
149149
StashAction.values.byName(s('action')),
150150
expectedHead: head,
151151
expectedIndex: index,
152+
expectedWorkingToken: j['expectedWorkingToken'] as String?,
152153
identity: oidOrNull(j['identity']),
153154
message: s('message'),
154155
paths: paths(),

‎lib/src/workflow_requests.dart‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,13 +419,18 @@ final class StashMutation extends GitMutation {
419419
this.stagedOnly = false,
420420
this.restoreIndex = false,
421421
this.branchName,
422+
this.expectedWorkingToken,
422423
});
423424
final StashAction action;
424425
final GitOid? identity;
425426
final String message;
426427
final List<RepoPath> paths;
427428
final bool includeUntracked, keepIndex, stagedOnly, restoreIndex;
428429
final String? branchName;
430+
431+
/// Required when this action can replace working files. Obtained from the
432+
/// reviewed status snapshot, independently of the HEAD/index preconditions.
433+
final String? expectedWorkingToken;
429434
@override
430435
bool get replacesWorkingFiles => action != StashAction.drop;
431436
@override
@@ -441,6 +446,7 @@ final class StashMutation extends GitMutation {
441446
'stagedOnly': stagedOnly,
442447
'restoreIndex': restoreIndex,
443448
'branchName': branchName,
449+
'expectedWorkingToken': expectedWorkingToken,
444450
};
445451
}
446452

‎test/organization_semantics_test.dart‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,43 @@ void main() {
88
late RepositoryFixture f;
99
setUp(() async => f = await RepositoryFixture.create());
1010
tearDown(() async => f.close());
11+
test(
12+
'stash rejects changed working contents after review without moving the index or stash ref',
13+
() async {
14+
await f.commit('note.md', utf8.encode('base\n'), 'base');
15+
final file = File('${f.root.path}/note.md');
16+
await file.writeAsString('reviewed\n');
17+
final reviewed = await f.engine.status(f.repository);
18+
final beforeIndex = await f.git(['ls-files', '--stage', '-z']);
19+
final request = decodeGitMutation({
20+
...StashMutation(
21+
StashAction.create,
22+
expectedHead: reviewed.head,
23+
expectedIndex: reviewed.indexToken,
24+
message: 'Reviewed prose',
25+
).toJson(),
26+
'expectedWorkingToken': reviewed.token,
27+
});
28+
await file.writeAsString('external\n');
29+
final result = await f.engine.mutate(f.repository, request);
30+
expect(result.error?.code, GitErrorCode.staleReview);
31+
expect(await file.readAsBytes(), utf8.encode('external\n'));
32+
expect(await f.git(['ls-files', '--stage', '-z']), beforeIndex);
33+
expect(await f.git(['stash', 'list']), isEmpty);
34+
expect(request.toJson()['expectedWorkingToken'], reviewed.token);
35+
final refreshed = await f.engine.status(f.repository);
36+
final retry = await f.engine.mutate(
37+
f.repository,
38+
decodeGitMutation({
39+
...request.toJson(),
40+
'expectedWorkingToken': refreshed.token,
41+
}),
42+
);
43+
expect(retry.completed, isTrue, reason: '${retry.error}');
44+
expect(await f.git(['show', 'refs/stash:note.md']), 'external');
45+
expect(await file.readAsBytes(), utf8.encode('base\n'));
46+
},
47+
);
1148
test(
1249
'recorded and remote submodule updates remain separate from parent staging and commits',
1350
() async {
@@ -120,6 +157,7 @@ void main() {
120157
keepIndex: true,
121158
expectedHead: s.head,
122159
expectedIndex: s.indexToken,
160+
expectedWorkingToken: s.token,
123161
),
124162
);
125163
expect(outcome.completed, isTrue, reason: '${outcome.error}');
@@ -141,6 +179,7 @@ void main() {
141179
stagedOnly: true,
142180
expectedHead: s.head,
143181
expectedIndex: s.indexToken,
182+
expectedWorkingToken: s.token,
144183
),
145184
);
146185
expect(outcome.completed, isTrue, reason: '${outcome.error}');
@@ -228,6 +267,7 @@ void main() {
228267
includeUntracked: true,
229268
expectedHead: s.head,
230269
expectedIndex: s.indexToken,
270+
expectedWorkingToken: s.token,
231271
),
232272
);
233273
expect(result.completed, isTrue, reason: '${result.error}');
@@ -244,6 +284,7 @@ void main() {
244284
identity: stash,
245285
expectedHead: s.head,
246286
expectedIndex: s.indexToken,
287+
expectedWorkingToken: s.token,
247288
),
248289
);
249290
expect(applied.completed, isTrue, reason: '${applied.error}');

‎test/workflow_test.dart‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ void main() {
269269
message: 'first',
270270
expectedHead: s.head,
271271
expectedIndex: s.indexToken,
272+
expectedWorkingToken: s.token,
272273
),
273274
)).completed,
274275
isTrue,
@@ -289,6 +290,7 @@ void main() {
289290
message: 'second',
290291
expectedHead: s.head,
291292
expectedIndex: s.indexToken,
293+
expectedWorkingToken: s.token,
292294
),
293295
);
294296
await commit('a', 'third\n', 'third');
@@ -298,6 +300,7 @@ void main() {
298300
identity: first,
299301
expectedHead: s.head,
300302
expectedIndex: s.indexToken,
303+
expectedWorkingToken: s.token,
301304
),
302305
);
303306
expect(popped.phase, GitOperationPhase.stopped);

0 commit comments

Comments
 (0)