use super::{git, git_optional};
use crate::git_tree::GitTreeError;
use std::path::Path;
pub(crate) fn for_each_ref_config(repo: &Path) -> Result<Vec<u8>, GitTreeError> {
git(
repo,
&[
"for-each-ref",
"--format=%(refname:short) %(objectname) %(committerdate:unix)",
"refs/heads/config/",
],
)
}
pub(crate) fn ls_tree(repo: &Path, refspec: &str) -> Result<Vec<u8>, GitTreeError> {
git(repo, &["ls-tree", "-r", "--name-only", refspec])
}
pub(crate) fn ls_tree_long(repo: &Path, refspec: &str) -> Result<Vec<u8>, GitTreeError> {
git(repo, &["ls-tree", "-r", "-l", refspec])
}
pub(crate) fn show_file(repo: &Path, refspec: &str, path: &str) -> Result<Vec<u8>, GitTreeError> {
git(repo, &["show", &format!("{refspec}:{path}")])
}
pub(crate) fn diff_names(
repo: &Path,
from: &str,
to: &str,
prefix: &str,
) -> Result<Vec<String>, GitTreeError> {
let out = git(
repo,
&[
"diff",
"--name-only",
"--diff-filter=AM",
from,
to,
"--",
prefix,
],
)?;
Ok(String::from_utf8_lossy(&out)
.lines()
.map(str::to_owned)
.filter(|line| !line.is_empty())
.collect())
}
pub(crate) fn merge_base(repo: &Path, a: &str, b: &str) -> Result<Option<String>, GitTreeError> {
Ok(git_optional(repo, &["merge-base", a, b])?
.map(|out| String::from_utf8_lossy(&out).trim().to_string()))
}
pub(crate) fn is_ancestor(repo: &Path, a: &str, b: &str) -> Result<bool, GitTreeError> {
Ok(git_optional(repo, &["merge-base", "--is-ancestor", a, b])?.is_some())
}