use std::path::{Path, PathBuf};
use super::git_run::{Git, Unrunnable};
const SCOPE: [&str; 5] = [
".",
":(exclude,glob)**/node_modules/**",
":(exclude,glob)**/dist/**",
":(exclude,glob)**/.fusible/**",
":(exclude,glob)**/*.lck",
];
#[derive(Debug)]
pub(super) enum Opened {
NotRepo,
Missing,
Ready(Git),
}
pub(super) fn scoped<'a>(head: &[&'a str]) -> Vec<&'a str> {
head.iter()
.copied()
.chain(["--"])
.chain(SCOPE.iter().copied())
.collect()
}
pub(super) fn rebase_dir(git: &Git) -> Option<PathBuf> {
["rebase-merge", "rebase-apply"].iter().find_map(|name| {
let path = git.text(&["rev-parse", "--git-path", name])?;
Some(git.dir().join(path)).filter(|dir| dir.is_dir())
})
}
fn has_git_marker(dir: &Path) -> bool {
dir.ancestors().any(|d| d.join(".git").exists())
}
pub(super) fn open(program: &str, dir: &Path) -> Opened {
let git = Git::new(program, dir);
match git.run(&["rev-parse", "--show-toplevel"]) {
Err(Unrunnable::Missing) if has_git_marker(dir) => Opened::Missing,
Ok(out) if out.ok() => Opened::Ready(git.at(PathBuf::from(out.stdout.trim()))),
_ => Opened::NotRepo,
}
}