A3: add untracked files to .gitignore from the Changes context menu
git.ignore appends one anchored, escaped literal pattern per path to the work tree root's .gitignore, skipping patterns already there. The Changes window gets a row context menu (right click, Shift+F10, context-menu key) with Open and Add to .gitignore. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
ce681871ad
commit
68599fa2ac
14 changed files with 633 additions and 5 deletions
86
crates/vcs/tests/ignore.rs
Normal file
86
crates/vcs/tests/ignore.rs
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
//! Ignoring files against a real temp repository: literal patterns for
|
||||
//! odd names, a project in a subfolder, no duplicates, a missing final line
|
||||
//! break, and refused paths. Skips with a message when git is not
|
||||
//! installed.
|
||||
|
||||
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
|
||||
|
||||
use std::path::Path;
|
||||
use std::process::Command;
|
||||
|
||||
use lutra_vcs::{Git, VcsError};
|
||||
|
||||
/// The installed git, or `None` (with a message) when there is none.
|
||||
fn installed() -> Option<Git> {
|
||||
match Git::detect("") {
|
||||
Ok(git) => Some(git),
|
||||
Err(e) => {
|
||||
eprintln!("skipped: no usable git installed ({e})");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Runs git in `dir` and returns its stdout and whether it succeeded.
|
||||
fn git(dir: &Path, args: &[&str]) -> (String, bool) {
|
||||
let out = Command::new("git")
|
||||
.args(args)
|
||||
.current_dir(dir)
|
||||
.env_remove("GIT_DIR")
|
||||
.env_remove("GIT_WORK_TREE")
|
||||
.env_remove("GIT_INDEX_FILE")
|
||||
.output()
|
||||
.unwrap();
|
||||
(
|
||||
String::from_utf8_lossy(&out.stdout).trim().to_string(),
|
||||
out.status.success(),
|
||||
)
|
||||
}
|
||||
|
||||
/// Whether git ignores `path` (root-relative).
|
||||
fn ignored(dir: &Path, path: &str) -> bool {
|
||||
git(dir, &["check-ignore", "-q", "--no-index", "--", path]).1
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ignores_exactly_the_given_paths() {
|
||||
let Some(cli) = installed() else { return };
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let root = dir.path();
|
||||
assert!(git(root, &["init", "-q", "-b", "main"]).1);
|
||||
std::fs::create_dir_all(root.join("app/var")).unwrap();
|
||||
std::fs::write(root.join(".gitignore"), "vendor/").unwrap();
|
||||
for name in [
|
||||
"app/a[1].php",
|
||||
"app/a1.php",
|
||||
"app/var/cache.log",
|
||||
"app/b.php",
|
||||
] {
|
||||
std::fs::write(root.join(name), "x").unwrap();
|
||||
}
|
||||
let repo = cli.open(&root.join("app")).unwrap().expect("a repository");
|
||||
|
||||
let added = repo
|
||||
.ignore(&[
|
||||
"a[1].php".to_string(),
|
||||
"var".to_string(),
|
||||
"var/".to_string(),
|
||||
])
|
||||
.unwrap();
|
||||
assert_eq!(added, vec![r"/app/a\[1].php", "/app/var"]);
|
||||
assert_eq!(
|
||||
std::fs::read_to_string(root.join(".gitignore")).unwrap(),
|
||||
"vendor/\n/app/a\\[1].php\n/app/var\n"
|
||||
);
|
||||
assert!(ignored(root, "app/a[1].php"));
|
||||
assert!(!ignored(root, "app/a1.php"));
|
||||
assert!(ignored(root, "app/var/cache.log"));
|
||||
assert!(!ignored(root, "app/b.php"));
|
||||
|
||||
assert!(repo.ignore(&["a[1].php".to_string()]).unwrap().is_empty());
|
||||
assert!(matches!(
|
||||
repo.ignore(&["../outside".to_string(), "b.php".to_string()]),
|
||||
Err(VcsError::InvalidPath(_))
|
||||
));
|
||||
assert!(!ignored(root, "app/b.php"));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue