Skip to content

Commit 6b7c980

Browse files
committed
fix(review): refine desktop review layouts and partial reverts
1 parent 7f11433 commit 6b7c980

23 files changed

Lines changed: 667 additions & 256 deletions

‎apps/server/src/review/ReviewService.test.ts‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,63 @@ describe("ReviewService", () => {
270270
}).pipe(Effect.provide(NodeServices.layer)),
271271
);
272272

273+
it.effect("reverts a hunk or selected addition without changing the index or other edits", () =>
274+
Effect.gen(function* () {
275+
const fs = yield* FileSystem.FileSystem;
276+
const workspaceRoot = yield* fs.makeTempDirectoryScoped({
277+
prefix: "t3-review-partial-revert-",
278+
});
279+
const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-review-state-" });
280+
yield* Effect.gen(function* () {
281+
const git = yield* GitVcsDriver.GitVcsDriver;
282+
const service = yield* ReviewService.ReviewService;
283+
const run = (args: string[]) =>
284+
git
285+
.execute({ operation: "review.test", cwd: workspaceRoot, args })
286+
.pipe(Effect.map((result) => result.stdout));
287+
yield* run(["init"]);
288+
yield* run(["config", "user.name", "Test"]);
289+
yield* run(["config", "user.email", "[email protected]"]);
290+
yield* fs.writeFileString(`${workspaceRoot}/file.txt`, "alpha\nbeta\ngamma\ndelta\n");
291+
yield* run(["add", "."]);
292+
yield* run(["commit", "-m", "initial"]);
293+
const index = "ALPHA\nbeta\ngamma\ndelta\n";
294+
yield* fs.writeFileString(`${workspaceRoot}/file.txt`, index);
295+
yield* run(["add", "."]);
296+
yield* fs.writeFileString(
297+
`${workspaceRoot}/file.txt`,
298+
"ALPHA\nBETA\nextra\nGAMMA\ndelta\n",
299+
);
300+
const header = "diff --git a/file.txt b/file.txt\n--- a/file.txt\n+++ b/file.txt\n";
301+
yield* service.revert({
302+
cwd: workspaceRoot,
303+
paths: [],
304+
patch: header + "@@ -3,0 +3,1 @@\n+extra\n",
305+
});
306+
assert.strictEqual(
307+
yield* fs.readFileString(`${workspaceRoot}/file.txt`),
308+
"ALPHA\nBETA\nGAMMA\ndelta\n",
309+
);
310+
const hunk = header + "@@ -2,1 +2,1 @@\n-beta\n+BETA\n";
311+
yield* service.revert({ cwd: workspaceRoot, paths: [], patch: hunk });
312+
assert.strictEqual(
313+
yield* fs.readFileString(`${workspaceRoot}/file.txt`),
314+
"ALPHA\nbeta\nGAMMA\ndelta\n",
315+
);
316+
assert.strictEqual(yield* run(["show", ":file.txt"]), index);
317+
const stale = yield* service
318+
.revert({ cwd: workspaceRoot, paths: [], patch: hunk })
319+
.pipe(Effect.flip);
320+
assert.strictEqual(stale._tag, "GitCommandError");
321+
assert.strictEqual(
322+
yield* fs.readFileString(`${workspaceRoot}/file.txt`),
323+
"ALPHA\nbeta\nGAMMA\ndelta\n",
324+
);
325+
assert.strictEqual(yield* run(["show", ":file.txt"]), index);
326+
}).pipe(Effect.provide(layer({ workspaceRoot, baseDir, gitLayer: GitVcsDriver.layer })));
327+
}).pipe(Effect.provide(NodeServices.layer)),
328+
);
329+
273330
it.effect("rejects diff preview cwd outside the configured workspace roots", () =>
274331
Effect.gen(function* () {
275332
const fs = yield* FileSystem.FileSystem;

‎apps/server/src/review/ReviewService.ts‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,14 @@ export const make = Effect.gen(function* () {
175175
yield* git.execute({
176176
operation: "ReviewService.setStaged.patch",
177177
cwd: input.cwd,
178-
args: ["apply", "--cached", ...(input.staged ? [] : ["--reverse"]), "--", "-"],
178+
args: [
179+
"apply",
180+
"--cached",
181+
"--unidiff-zero",
182+
...(input.staged ? [] : ["--reverse"]),
183+
"--",
184+
"-",
185+
],
179186
stdin: input.patch,
180187
});
181188
yield* workflow.invalidateLocalStatus(input.cwd);
@@ -238,7 +245,7 @@ export const make = Effect.gen(function* () {
238245
const revert = Effect.fn("ReviewService.revert")(function* (input: ReviewRevertInput) {
239246
yield* assertWorkspaceBoundCwd("ReviewService.revert", input.cwd);
240247
if (
241-
input.paths.length === 0 ||
248+
(input.paths.length === 0 && input.patch === undefined) ||
242249
input.paths.some(
243250
(file) => path.isAbsolute(file) || file === "." || file.split(/[\\/]/).includes(".."),
244251
)
@@ -250,6 +257,16 @@ export const make = Effect.gen(function* () {
250257
detail: "Choose repository-relative changed files to revert.",
251258
});
252259
}
260+
if (input.patch !== undefined) {
261+
yield* git.execute({
262+
operation: "ReviewService.revert.patch",
263+
cwd: input.cwd,
264+
args: ["apply", "--reverse", "--unidiff-zero", "--", "-"],
265+
stdin: input.patch,
266+
});
267+
yield* workflow.invalidateLocalStatus(input.cwd);
268+
return;
269+
}
253270
const paths = [...new Set(input.paths)].map((file) => `:(literal)${file}`);
254271
const tracked = yield* git.execute({
255272
operation: "ReviewService.revert.tracked",

‎apps/web/src/components/diffs/AnnotatableCodeView.tsx‎

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ interface AnnotatableCodeViewProps {
131131
stageLinesAction?: "stage" | "unstage" | undefined;
132132
onStageLines?: ((fileDiff: FileDiffMetadata, range: SelectedLineRange) => void) | undefined;
133133
canStageLines?: ((fileDiff: FileDiffMetadata, range: SelectedLineRange) => boolean) | undefined;
134+
onRevertLines?: ((fileDiff: FileDiffMetadata, range: SelectedLineRange) => void) | undefined;
135+
canRevertLines?: ((fileDiff: FileDiffMetadata, range: SelectedLineRange) => boolean) | undefined;
134136
renderHeaderFilenameSuffix: (fileDiff: FileDiffMetadata) => ReactNode;
135137
renderHeaderPrefix: (
136138
fileDiff: FileDiffMetadata,
@@ -161,6 +163,8 @@ export function AnnotatableCodeView({
161163
stageLinesAction,
162164
onStageLines,
163165
canStageLines,
166+
onRevertLines,
167+
canRevertLines,
164168
renderHeaderFilenameSuffix,
165169
renderHeaderPrefix,
166170
onRevealSearchMatch,
@@ -339,7 +343,7 @@ export function AnnotatableCodeView({
339343

340344
const hasOpenComment = draft !== null;
341345
useEffect(() => {
342-
if (!container || !onStageLines || !stageLinesAction) return;
346+
if (!container || (!onStageLines && !onRevertLines)) return;
343347
const onContextMenu = (event: MouseEvent) => {
344348
const path = event
345349
.composedPath()
@@ -357,36 +361,54 @@ export function AnnotatableCodeView({
357361
(selectedLines?.id === file.fileKey && line.hasAttribute("data-selected-line")
358362
? selectedLines.range
359363
: { start: point.lineNumber, end: point.lineNumber, side: point.side });
360-
if (canStageLines && !canStageLines(file.fileDiff, range)) return;
364+
const stageAvailable =
365+
onStageLines && stageLinesAction && (!canStageLines || canStageLines(file.fileDiff, range));
366+
const revertAvailable =
367+
onRevertLines && (!canRevertLines || canRevertLines(file.fileDiff, range));
368+
if (!stageAvailable && !revertAvailable) return;
361369
const api = readLocalApi();
362370
if (!api) return;
363371
event.preventDefault();
364372
event.stopPropagation();
365373
setSelectedLines({ id: file.fileKey, range });
366374
const label = stageLinesAction === "stage" ? "Stage" : "Unstage";
375+
const singleLine = range.start === range.end && (range.endSide ?? range.side) === range.side;
367376
void api.contextMenu
368377
.show(
369378
[
379+
...(stageAvailable
380+
? [
370381
{
371382
id: "stage-lines",
372-
label: `${label} ${
373-
range.start === range.end && (range.endSide ?? range.side) === range.side
374-
? "line"
375-
: "selected lines"
376-
}`,
383+
label: `${label} ${singleLine ? "line" : "selected lines"}`,
377384
},
385+
]
386+
: []),
387+
...(revertAvailable
388+
? [{ id: "revert-lines", label: `Revert ${singleLine ? "line" : "lines"}` }]
389+
: []),
378390
],
379391
{ x: event.clientX, y: event.clientY },
380392
)
381393
.then((action) => {
382-
if (action !== "stage-lines") return;
394+
if (action !== "stage-lines" && action !== "revert-lines") return;
383395
setSelectedLines(null);
384-
onStageLines(file.fileDiff, range);
396+
if (action === "stage-lines") onStageLines?.(file.fileDiff, range);
397+
else onRevertLines?.(file.fileDiff, range);
385398
});
386399
};
387400
container.addEventListener("contextmenu", onContextMenu);
388401
return () => container.removeEventListener("contextmenu", onContextMenu);
389-
}, [container, filesByKey, selectedLines, onStageLines, stageLinesAction, canStageLines]);
402+
}, [
403+
container,
404+
filesByKey,
405+
selectedLines,
406+
onStageLines,
407+
stageLinesAction,
408+
canStageLines,
409+
onRevertLines,
410+
canRevertLines,
411+
]);
390412

391413
return (
392414
<StyledDiffCodeView<DiffCommentAnnotationGroup>

‎apps/web/src/components/diffs/StyledDiffCodeView.tsx‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ export const DIFF_VIEW_UNSAFE_CSS = `${DIFF_SURFACE_THEME_UNSAFE_CSS}
8888
position: sticky !important;
8989
top: 0;
9090
z-index: 4;
91-
background-color: var(--code-background) !important;
9291
border-bottom-color: transparent !important;
9392
align-items: center !important;
9493
font-family: var(--font-sans) !important;
@@ -105,8 +104,8 @@ export const DIFF_VIEW_UNSAFE_CSS = `${DIFF_SURFACE_THEME_UNSAFE_CSS}
105104
[data-diffs-header]:hover {
106105
/* A native scrollbar gutter cannot be painted by descendants. Use an inset edge cue instead
107106
of a full-width band that would look accidentally clipped at the gutter. */
108-
background-color: var(--code-background) !important;
109-
box-shadow: inset 3px 0 color-mix(in srgb, var(--code-foreground) 24%, transparent);
107+
box-shadow: inset 3px 0 color-mix(in srgb, var(--code-foreground) 24%, transparent),
108+
inset 0 -1px color-mix(in srgb, var(--code-foreground) 14%, transparent) !important;
110109
}
111110
112111
/* Hunk separators are the way to the unchanged lines between hunks. They keep the library's

0 commit comments

Comments
 (0)