Skip to content

Commit 57a4ad8

Browse files
committed
fix(git): keep selected commit and stashPush pathspecs literal
1 parent ad818b8 commit 57a4ad8

5 files changed

Lines changed: 131 additions & 4 deletions

File tree

‎rust/crates/core/src/tests/git_colon_paths.rs‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,3 +367,91 @@ fn untracked_directory_and_diff_preserve_colon_children() {
367367
}
368368
}
369369
}
370+
371+
#[test]
372+
fn selected_commit_keeps_colon_paths_literal() {
373+
let repository = Repository::new("colon-commit");
374+
repository.git(&["config", "user.name", "Fixture"]);
375+
repository.git(&["config", "user.email", "fixture@example.invalid"]);
376+
repository.git(&["config", "commit.gpgsign", "false"]);
377+
for path in PATHS.into_iter().chain(["unselected.txt"]) {
378+
repository.write(path, "initial\n");
379+
}
380+
repository.git(&["add", "--all"]);
381+
repository.commit();
382+
383+
// Selected commit with pathspec-shaped names must not commit `unselected.txt`
384+
// and must reconcile pre-staged colon paths in the real index.
385+
for path in PATHS.into_iter().chain(["unselected.txt"]) {
386+
repository.write(path, "committed\n");
387+
}
388+
repository.mutate("stage", &["a:b.txt", ":(exclude)*.txt"]);
389+
let committed = repository.data(
390+
"git.write",
391+
json!({
392+
"operation": "commit",
393+
"message": "commit colon paths",
394+
"paths": PATHS,
395+
}),
396+
);
397+
assert_eq!(committed["exitCode"], 0, "{committed}");
398+
assert!(committed["operationError"].is_null(), "{committed}");
399+
assert_eq!(
400+
repository.git(&[
401+
"diff-tree",
402+
"--no-commit-id",
403+
"--name-only",
404+
"-r",
405+
"-z",
406+
"HEAD"
407+
]),
408+
":(exclude)*.txt\0:(glob)*.txt\0D:\\logs\0a:b.txt\0"
409+
);
410+
assert_eq!(
411+
fs::read_to_string(repository.0.join("unselected.txt")).unwrap(),
412+
"committed\n"
413+
);
414+
assert_eq!(
415+
repository.git(&["status", "--porcelain=v1", "-z"]),
416+
" M unselected.txt\0"
417+
);
418+
}
419+
420+
#[test]
421+
fn stash_push_keeps_colon_paths_literal() {
422+
let repository = Repository::new("colon-stash");
423+
repository.git(&["config", "user.name", "Fixture"]);
424+
repository.git(&["config", "user.email", "fixture@example.invalid"]);
425+
repository.git(&["config", "commit.gpgsign", "false"]);
426+
for path in PATHS.into_iter().chain(["unselected.txt"]) {
427+
repository.write(path, "initial\n");
428+
}
429+
repository.git(&["add", "--all"]);
430+
repository.commit();
431+
432+
// Selected stash push with pathspec-shaped names must leave `unselected.txt` modified.
433+
for path in PATHS.into_iter().chain(["unselected.txt"]) {
434+
repository.write(path, "stashed\n");
435+
}
436+
let stashed = repository.data(
437+
"git.write",
438+
json!({
439+
"operation": "stashPush",
440+
"message": "stash colon paths",
441+
"paths": PATHS,
442+
}),
443+
);
444+
assert_eq!(stashed["exitCode"], 0, "{stashed}");
445+
assert!(stashed["operationError"].is_null(), "{stashed}");
446+
for path in PATHS {
447+
assert_eq!(
448+
fs::read_to_string(repository.0.join(path)).unwrap(),
449+
"initial\n",
450+
"{path}"
451+
);
452+
}
453+
assert_eq!(
454+
fs::read_to_string(repository.0.join("unselected.txt")).unwrap(),
455+
"stashed\n"
456+
);
457+
}

‎rust/crates/git/src/commits.rs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,12 +391,14 @@ pub(crate) fn commit_selected_paths(
391391
"--pathspec-from-file=-".to_string(),
392392
"--pathspec-file-nul".to_string(),
393393
];
394+
let mut stage_environment = environment.clone();
395+
stage_environment.push(("GIT_LITERAL_PATHSPECS".to_string(), "1".to_string()));
394396
let staged = execute_git_with_environment(
395397
root,
396398
&stage_arguments,
397399
Some(pathspec_input.clone()),
398400
false,
399-
&environment,
401+
&stage_environment,
400402
)?;
401403
if staged.exit_code != 0 {
402404
return Ok(staged);
@@ -498,7 +500,8 @@ pub(crate) fn commit_selected_paths(
498500

499501
// Reconcile only committed paths in the real index. Unrelated staging that
500502
// another Git process created while hooks ran remains intact.
501-
let mut reconciled = execute_git(
503+
let literal = [("GIT_LITERAL_PATHSPECS".to_string(), "1".to_string())];
504+
let mut reconciled = execute_git_with_environment(
502505
root,
503506
&[
504507
"reset".to_string(),
@@ -508,6 +511,8 @@ pub(crate) fn commit_selected_paths(
508511
"--pathspec-file-nul".to_string(),
509512
],
510513
Some(pathspec_input),
514+
false,
515+
&literal,
511516
)?;
512517
if reconciled.exit_code != 0 {
513518
reconciled.warnings.push(GitOperationWarning::new(

‎rust/crates/git/src/repository.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,11 @@ pub(crate) fn empty_tree_oid(root: &str) -> Result<String, CoreError> {
459459
/// Workspace paths follow the host's filename rules, independently of Git refs.
460460
/// Callers passing these paths as pathspecs must keep Git's matching literal.
461461
pub(crate) fn is_safe_pathspec(path: &str) -> bool {
462-
!invalid_native_relative_path(path, Platform::HOST)
462+
is_safe_pathspec_for(path, Platform::HOST)
463+
}
464+
465+
pub(crate) fn is_safe_pathspec_for(path: &str, platform: Platform) -> bool {
466+
!invalid_native_relative_path(path, platform)
463467
}
464468

465469
pub(crate) fn null_device() -> &'static str {

‎rust/crates/git/src/tests.rs‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,29 @@ fn remote_and_branch_refspec_components_do_not_follow_unix_filename_rules() {
4747
}
4848
}
4949

50+
#[test]
51+
fn workspace_pathspecs_follow_platform_filename_rules() {
52+
for path in ["a:b.txt", ":(glob)*.txt", ":(exclude)*.txt", r"D:\logs"] {
53+
assert!(
54+
super::is_safe_pathspec_for(path, lithe_platform::Platform::Unix),
55+
"{path}"
56+
);
57+
assert!(
58+
!super::is_safe_pathspec_for(path, lithe_platform::Platform::Windows),
59+
"{path}"
60+
);
61+
}
62+
for platform in lithe_platform::Platform::ALL {
63+
assert!(super::is_safe_pathspec_for("src/main.rs", platform));
64+
for invalid in ["", "/etc", "../secret", "a/../b", "a\0b"] {
65+
assert!(
66+
!super::is_safe_pathspec_for(invalid, platform),
67+
"{invalid:?} on {platform:?}"
68+
);
69+
}
70+
}
71+
}
72+
5073
/// Only an exact `Name <email>` names an author; the options keep Git's order.
5174
#[test]
5275
fn commit_identity_requires_an_exact_author() {

‎rust/crates/git/src/write.rs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,14 @@ fn write_with_trace(request: GitWriteRequest) -> Result<GitCommandResponse, Core
581581
"--pathspec-from-file=-".into(),
582582
"--pathspec-file-nul".into(),
583583
]);
584-
return execute_git(&root, &arguments, Some(nul_pathspec_input(&paths)));
584+
let literal = [("GIT_LITERAL_PATHSPECS".to_string(), "1".to_string())];
585+
return execute_git_with_environment(
586+
&root,
587+
&arguments,
588+
Some(nul_pathspec_input(&paths)),
589+
false,
590+
&literal,
591+
);
585592
}
586593
}
587594
"operationContinue" | "operationAbort" | "operationSkip" => {

0 commit comments

Comments
 (0)