Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions windows/tauri/src-tauri/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,13 @@ fn translate(command: &str, args: Value) -> Result<(String, Value), String> {
"git.write"
}
"git_diff_file" | "git_status_diff_stats" => {
paths_from_file(&mut payload);
payload.entry("pathspecs").or_insert_with(|| json!([]));
if let Some(path) = payload.remove("filePath") {
payload.insert("pathspecs".into(), Value::Array(vec![path]));
} else {
// The shared core rejects empty pathspecs; a request without a
// file scope means the whole tree, which git spells as `.`.
payload.insert("pathspecs".into(), json!(["."]));
}
"git.diff"
}
"git_ref_diff" => {
Expand Down Expand Up @@ -436,7 +441,41 @@ mod tests {
translate("git_diff_file", json!({ "repoPath": "C:/work" })).unwrap();

assert_eq!(command, "git.diff");
assert_eq!(payload, json!({ "root": "C:/work", "pathspecs": [] }));
assert_eq!(payload, json!({ "root": "C:/work", "pathspecs": ["."] }));
}

#[test]
fn translates_diff_file_pathspec() {
let (command, payload) = translate(
"git_diff_file",
json!({ "repoPath": "C:/work", "filePath": "src/main.rs", "staged": true }),
)
.unwrap();

assert_eq!(command, "git.diff");
assert_eq!(
payload,
json!({
"root": "C:/work",
"pathspecs": ["src/main.rs"],
"staged": true
})
);
}

#[test]
fn translates_status_diff_stats_whole_tree() {
let (command, payload) = translate(
"git_status_diff_stats",
json!({ "repoPath": "C:/work", "staged": true }),
)
.unwrap();

assert_eq!(command, "git.diff");
assert_eq!(
payload,
json!({ "root": "C:/work", "pathspecs": ["."], "staged": true })
);
}

#[test]
Expand Down
16 changes: 11 additions & 5 deletions windows/tauri/src/features/git/api/git-diff-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,20 @@ export const getStatusDiffStats = async (repoPath: string): Promise<GitDiffStat[
}

const generation = getRepositoryCacheGeneration(resolvedRepoPath);
const request = tauriInvoke<GitDiffStat[]>("git_status_diff_stats", {
repoPath: resolvedRepoPath,
})
.then((stats) => {
const request = Promise.all([
tauriInvoke<GitDiffStat[]>("git_status_diff_stats", {
repoPath: resolvedRepoPath,
}),
tauriInvoke<GitDiffStat[]>("git_status_diff_stats", {
repoPath: resolvedRepoPath,
staged: true,
}),
])
.then(([unstagedStats, stagedStats]) => {
if (generation !== getRepositoryCacheGeneration(resolvedRepoPath)) {
return getStatusDiffStats(resolvedRepoPath);
}
return stats;
return [...unstagedStats, ...stagedStats];
})
.catch((error) => {
if (!isNotGitRepositoryError(error)) {
Expand Down
57 changes: 57 additions & 0 deletions windows/tauri/src/platform/core-result-adapter.diff.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { describe, expect, test } from "bun:test";
import { adaptCoreResult } from "./core-result-adapter";

const twoFilePatch = `diff --git a/a.txt b/a.txt
index 111..222 100644
--- a/a.txt
+++ b/a.txt
@@ -1 +1,2 @@
hello
+world
diff --git a/b.txt b/b.txt
index 333..444 100644
--- a/b.txt
+++ b/b.txt
@@ -1 +0,0 @@
-old line
`;

describe("git diff stats adaptation", () => {
test("maps per-file additions and deletions from the whole tree patch", () => {
const stats = adaptCoreResult(
"git_status_diff_stats",
{ repoPath: "C:/work" },
{ patch: twoFilePatch },
);

expect(stats).toEqual([
{ file_path: "a.txt", staged: false, additions: 1, deletions: 0 },
{ file_path: "b.txt", staged: false, additions: 0, deletions: 1 },
]);
});

test("carries the staged flag from the request into every stat entry", () => {
const stats = adaptCoreResult(
"git_status_diff_stats",
{ repoPath: "C:/work", staged: true },
{ patch: twoFilePatch },
);

expect(Array.isArray(stats)).toBe(true);
for (const stat of stats as Array<{ staged: boolean }>) {
expect(stat.staged).toBe(true);
}
});
});

describe("git single-file diff adaptation", () => {
test("returns the parsed diff for the requested file", () => {
const diff = adaptCoreResult(
"git_diff_file",
{ repoPath: "C:/work", filePath: "a.txt" },
{ patch: twoFilePatch },
);

expect((diff as { file_path: string }).file_path).toBe("a.txt");
});
});
3 changes: 2 additions & 1 deletion windows/tauri/src/platform/core-result-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ function adaptDiff(command: string, args: JsonRecord | undefined, value: unknown
return "files" in parsed ? parsed.files[0] ?? null : parsed;
}
if (command === "git_status_diff_stats") {
const staged = Boolean(argumentsRecord.staged);
const diffs = "files" in parsed ? parsed.files : [parsed];
return diffs.map((diff) => ({
file_path: diff.file_path,
staged: false,
staged,
additions: diff.additions ?? diff.lines.filter((line) => line.line_type === "added").length,
deletions: diff.deletions ?? diff.lines.filter((line) => line.line_type === "removed").length,
}));
Expand Down
Loading