Skip to content
Merged
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
1 change: 1 addition & 0 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 16 additions & 5 deletions rust/contracts/rust-core-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2177,12 +2177,23 @@ range comparisons.

`git.comparison` accepts `root` plus the same `reference` or `gitReference` /
`targetGitReference` forms and returns the deterministically ordered changed
files. `includeUntracked: true` adds untracked worktree files with status `A`
and `isUntracked: true`, deduplicated by path; omitted or false keeps the
previous response. The flag is invalid for a two-reference range. If status
files. Optional `pathspecs` limit both sides of the comparison as literal
repository-relative paths; naming a directory covers the paths below it, `.`
names the repository root, and empty or `.` path components are normalized as
in Git. Optional `exactPaths: true` restricts each pathspec to exact entries at
that pathspec's depth (excluding descendants when the compared reference has a
directory at the same path) while preserving Git's repository path normalization
(`core.precomposeUnicode`, `core.ignorecase`); it requires at least one non-root
pathspec. They remain repository-relative when `root` is a subdirectory of the
repository, and they also limit two-reference tree comparisons in bare
repositories. Omitting the list keeps the whole-repository response. `includeUntracked: true`
adds untracked worktree files with status `A` and `isUntracked: true`,
deduplicated by path and limited by the same pathspecs; omitted or false keeps
the previous response. The flag is invalid for a two-reference range. If status
cannot be read, the request fails rather than returning an incomplete result.
See `rust/fixtures/git/comparison-untracked-v1.json`. Platforms must not
construct a two-ref range themselves.
See `rust/fixtures/git/comparison-untracked-v1.json` and
`rust/fixtures/git/comparison-pathspecs-v1.json`. Platforms must not construct
a two-ref range themselves.
`git.apply` accepts `root`, `patch`, and `mode`; supported modes are `stage`,
`unstage`, `discard`, `restoreIndex`, `worktree`, `restoreIndexCheck`, and
`worktreeCheck`. The two `*Check` modes only test whether the reverse patch
Expand Down
340 changes: 340 additions & 0 deletions rust/crates/core/src/tests/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,346 @@ fn git_comparison_can_include_untracked_worktree_files() {
assert_eq!(range_response["error"]["code"], "invalid_request");
}

#[test]
fn git_comparison_pathspecs_limit_tracked_and_untracked_files() {
struct Cleanup(std::path::PathBuf);
impl Drop for Cleanup {
fn drop(&mut self) {
let _ = fs::remove_dir_all(&self.0);
}
}
let root = temporary_root("comparison-pathspecs");
let _cleanup = Cleanup(root.clone());
fs::create_dir_all(&root).unwrap();
let run = |arguments: &[&str]| {
let result = Command::new("git")
.args(arguments)
.current_dir(&root)
.output()
.unwrap();
assert!(
result.status.success(),
"{:?}: {}",
arguments,
String::from_utf8_lossy(&result.stderr)
);
};
run(&["init", "--template=", "-q"]);
run(&["config", "core.autocrlf", "false"]);
run(&["config", "user.name", "Lithe Test"]);
run(&["config", "user.email", "test@example.com"]);
fs::write(root.join("a-first.txt"), "before\n").unwrap();
fs::write(root.join("z-target.txt"), "before\n").unwrap();
run(&["add", "a-first.txt", "z-target.txt"]);
run(&["-c", "commit.gpgsign=false", "commit", "-qm", "initial"]);
fs::write(root.join("a-first.txt"), "committed\n").unwrap();
fs::write(root.join("z-target.txt"), "committed\n").unwrap();
run(&["add", "a-first.txt", "z-target.txt"]);
run(&["-c", "commit.gpgsign=false", "commit", "-qm", "committed"]);
fs::write(root.join("a-first.txt"), "after\n").unwrap();
fs::write(root.join("z-target.txt"), "after\n").unwrap();
fs::write(root.join("a-untracked.txt"), "new\n").unwrap();
fs::create_dir(root.join("subdirectory")).unwrap();

let fixture: Value = serde_json::from_str(include_str!(
"../../../../fixtures/git/comparison-pathspecs-v1.json"
))
.unwrap();
let mut payload = fixture["request"].clone();
payload["root"] = serde_json::json!(root);
let response: Value = serde_json::from_str(&execute_json(
&serde_json::json!({
"id": "comparison-pathspecs", "command": "git.comparison", "payload": payload
})
.to_string(),
))
.unwrap();
assert_eq!(response["ok"], true, "{response}");
assert_eq!(response["data"]["files"], fixture["expected"]);

let compare_paths = |command_root: std::path::PathBuf, pathspecs: &[&str]| {
let response: Value = serde_json::from_str(&execute_json(
&serde_json::json!({
"id": "comparison-pathspecs-normalized",
"command": "git.comparison",
"payload": {
"root": command_root,
"reference": "HEAD",
"includeUntracked": true,
"pathspecs": pathspecs,
}
})
.to_string(),
))
.unwrap();
assert_eq!(response["ok"], true, "{response}");
response["data"]["files"]
.as_array()
.unwrap()
.iter()
.map(|file| file["path"].as_str().unwrap().to_string())
.collect::<Vec<_>>()
};
assert_eq!(
compare_paths(root.clone(), &["."]),
vec!["a-first.txt", "a-untracked.txt", "z-target.txt"]
);
assert_eq!(
compare_paths(root.clone(), &["./z-target.txt"]),
vec!["z-target.txt"]
);
assert_eq!(
compare_paths(root.clone(), &["a-untracked.txt/."]),
vec!["a-untracked.txt"]
);
assert_eq!(
compare_paths(root.join("subdirectory"), &["."]),
vec!["a-first.txt", "a-untracked.txt", "z-target.txt"]
);

run(&["clone", "--bare", ".", "bare"]);
let bare_response: Value = serde_json::from_str(&execute_json(
&serde_json::json!({
"id": "comparison-pathspecs-bare",
"command": "git.comparison",
"payload": {
"root": root.join("bare"),
"reference": "HEAD~1..HEAD",
"pathspecs": ["z-target.txt"],
}
})
.to_string(),
))
.unwrap();
assert_eq!(bare_response["ok"], true, "{bare_response}");
assert_eq!(
bare_response["data"]["files"]
.as_array()
.unwrap()
.iter()
.map(|file| file["path"].as_str().unwrap().to_string())
.collect::<Vec<_>>(),
vec!["z-target.txt"]
);
}

#[test]
fn git_comparison_exact_paths_exclude_same_named_directory_contents() {
struct Cleanup(std::path::PathBuf);
impl Drop for Cleanup {
fn drop(&mut self) {
let _ = fs::remove_dir_all(&self.0);
}
}
let root = temporary_root("comparison-exact-paths");
let _cleanup = Cleanup(root.clone());
fs::create_dir_all(&root).unwrap();
let run = |arguments: &[&str]| {
let result = Command::new("git")
.args(arguments)
.current_dir(&root)
.output()
.unwrap();
assert!(
result.status.success(),
"{:?}: {}",
arguments,
String::from_utf8_lossy(&result.stderr)
);
};
run(&["init", "--template=", "-q"]);
run(&["config", "core.autocrlf", "false"]);
run(&["config", "user.name", "Lithe Test"]);
run(&["config", "user.email", "test@example.com"]);
fs::create_dir_all(root.join("z-target.txt")).unwrap();
fs::write(root.join("z-target.txt/nested.txt"), "nested\n").unwrap();
fs::create_dir_all(root.join("u-target.txt")).unwrap();
fs::write(root.join("u-target.txt/nested.txt"), "nested\n").unwrap();
fs::create_dir_all(root.join("sub")).unwrap();
fs::write(root.join("sub/other.txt"), "before\n").unwrap();
run(&["add", "."]);
run(&["-c", "commit.gpgsign=false", "commit", "-qm", "directories"]);
run(&["branch", "dir-branch"]);

// Replace both directories with regular files (one tracked, one untracked).
fs::remove_dir_all(root.join("z-target.txt")).unwrap();
fs::remove_dir_all(root.join("u-target.txt")).unwrap();
fs::write(root.join("z-target.txt"), "tracked\n").unwrap();
run(&["add", "z-target.txt"]);
run(&[
"-c",
"commit.gpgsign=false",
"commit",
"-qm",
"tracked file",
]);
fs::write(root.join("z-target.txt"), "tracked-edit\n").unwrap();
fs::write(root.join("u-target.txt"), "untracked\n").unwrap();
fs::write(root.join("sub/other.txt"), "after\n").unwrap();

// Without exactPaths, pathspecs include the former directory's nested entry.
let broad: Value = serde_json::from_str(&execute_json(
&serde_json::json!({
"id": "comparison-broad",
"command": "git.comparison",
"payload": {
"root": root,
"reference": "dir-branch",
"includeUntracked": true,
"pathspecs": ["z-target.txt"],
}
})
.to_string(),
))
.unwrap();
assert_eq!(broad["ok"], true, "{broad}");
assert_eq!(broad["data"]["files"].as_array().unwrap().len(), 2);

// With exactPaths, tracked and untracked files match while z-target.txt/nested.txt is excluded,
// even when combined with a deeper pathspec (sub/other.txt).
let exact: Value = serde_json::from_str(&execute_json(
&serde_json::json!({
"id": "comparison-exact",
"command": "git.comparison",
"payload": {
"root": root,
"reference": "dir-branch",
"includeUntracked": true,
"pathspecs": ["z-target.txt", "u-target.txt", "sub/other.txt"],
"exactPaths": true,
}
})
.to_string(),
))
.unwrap();
assert_eq!(exact["ok"], true, "{exact}");
let files = exact["data"]["files"].as_array().unwrap();
let paths: Vec<_> = files
.iter()
.map(|file| file["path"].as_str().unwrap())
.collect();
assert_eq!(paths, vec!["sub/other.txt", "u-target.txt", "z-target.txt"]);
assert_eq!(files[1]["isUntracked"], true);

for invalid_pathspecs in [serde_json::json!([]), serde_json::json!(["."])] {
let invalid: Value = serde_json::from_str(&execute_json(
&serde_json::json!({
"id": "comparison-exact-invalid",
"command": "git.comparison",
"payload": {
"root": root,
"reference": "dir-branch",
"pathspecs": invalid_pathspecs,
"exactPaths": true,
}
})
.to_string(),
))
.unwrap();
assert_eq!(invalid["ok"], false, "{invalid}");
assert_eq!(invalid["error"]["code"], "invalid_request");
}

// Selecting both endpoints of a rename with exactPaths keeps the rename pairing.
fs::write(root.join("old.txt"), "same content\n").unwrap();
run(&["add", "old.txt"]);
run(&[
"-c",
"commit.gpgsign=false",
"commit",
"-qm",
"before rename",
]);
run(&["mv", "old.txt", "new.txt"]);
let rename: Value = serde_json::from_str(&execute_json(
&serde_json::json!({
"id": "comparison-exact-rename",
"command": "git.comparison",
"payload": {
"root": root,
"reference": "HEAD",
"pathspecs": ["old.txt", "new.txt"],
"exactPaths": true,
}
})
.to_string(),
))
.unwrap();
assert_eq!(rename["ok"], true, "{rename}");
let rename_files = rename["data"]["files"].as_array().unwrap();
assert_eq!(rename_files.len(), 1);
assert!(rename_files[0]["status"].as_str().unwrap().starts_with('R'));
assert_eq!(rename_files[0]["path"], "new.txt");

// With core.ignorecase=2 (nonstandard Git bool), differently cased exact pathspecs match.
run(&["config", "core.ignorecase", "2"]);
fs::write(root.join("Readme.md"), "before\n").unwrap();
run(&["add", "Readme.md"]);
run(&["-c", "commit.gpgsign=false", "commit", "-qm", "case file"]);
fs::write(root.join("Readme.md"), "after\n").unwrap();
fs::write(root.join("OtherCase.txt"), "untracked\n").unwrap();
let icase: Value = serde_json::from_str(&execute_json(
&serde_json::json!({
"id": "comparison-exact-icase",
"command": "git.comparison",
"payload": {
"root": root,
"reference": "HEAD",
"includeUntracked": true,
"pathspecs": ["README.md", "OTHERCASE.TXT"],
"exactPaths": true,
}
})
.to_string(),
))
.unwrap();
assert_eq!(icase["ok"], true, "{icase}");
let icase_paths: Vec<_> = icase["data"]["files"]
.as_array()
.unwrap()
.iter()
.map(|file| file["path"].as_str().unwrap())
.collect();
assert_eq!(icase_paths, vec!["OtherCase.txt", "Readme.md"]);

// Moving a selected file under another selected path emits a deletion when destination is unselected.
run(&["config", "core.ignorecase", "false"]);
fs::write(root.join("docs-slot"), "slot\n").unwrap();
fs::write(
root.join("notes.md"),
"note body to keep rename detection high\n",
)
.unwrap();
run(&["add", "docs-slot", "notes.md"]);
run(&["-c", "commit.gpgsign=false", "commit", "-qm", "move source"]);
run(&["branch", "before-move"]);
fs::remove_file(root.join("docs-slot")).unwrap();
fs::create_dir_all(root.join("docs-slot")).unwrap();
run(&["mv", "notes.md", "docs-slot/index.md"]);
let moved: Value = serde_json::from_str(&execute_json(
&serde_json::json!({
"id": "comparison-exact-moved-source",
"command": "git.comparison",
"payload": {
"root": root,
"reference": "before-move",
"pathspecs": ["docs-slot", "notes.md"],
"exactPaths": true,
}
})
.to_string(),
))
.unwrap();
assert_eq!(moved["ok"], true, "{moved}");
assert_eq!(
moved["data"]["files"],
serde_json::json!([
{ "path": "docs-slot", "status": "D" },
{ "path": "notes.md", "status": "D" },
])
);
}

#[test]
fn git_status_returns_contract_shape() {
let root = temporary_root("git");
Expand Down
1 change: 1 addition & 0 deletions rust/crates/git/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ regex = "1.11"
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", features = ["preserve_order"] }
sha2 = "0.10"
unicode-normalization = "0.1"
Loading
Loading