use std::borrow::ToOwned;
use take_until::TakeUntilExt;
use crate::{newtype, GitDir, RepoBranches};
use super::RepoDetails;
newtype!(Log, Vec<String>, Default, Hash, "Git log of branches");
fn ancesstor(gitdir: &GitDir, branches: &RepoBranches) -> Option<String> {
let result = std::process::Command::new("/usr/bin/git")
.current_dir(gitdir.to_path_buf())
.args([
"merge-base",
"--octopus",
format!("origin/{}", branches.main()).as_str(),
format!("origin/{}", branches.next()).as_str(),
format!("origin/{}", branches.dev()).as_str(),
])
.output();
if let Ok(output) = result {
return String::from_utf8_lossy(output.stdout.as_slice())
.split('\n')
.take(1)
.map(|line| line.chars().take(7).collect::<String>())
.collect::<Vec<_>>()
.first()
.cloned();
}
None
}
pub fn log(repo_details: &RepoDetails) -> Log {
if let Some(repo_config) = &repo_details.repo_config {
let branches = repo_config.branches();
let result = std::process::Command::new("/usr/bin/git")
.current_dir(repo_details.gitdir().to_path_buf())
.args([
"log",
"--oneline",
"--graph",
"--decorate",
format!("origin/{}", branches.main()).as_str(),
format!("origin/{}", branches.next()).as_str(),
format!("origin/{}", branches.dev()).as_str(),
])
.output();
if let Ok(output) = result {
if let Some(ancestor) = ancesstor(repo_details.gitdir(), branches) {
return String::from_utf8_lossy(output.stdout.as_slice())
.split('\n')
.take_until(|line| line.contains(&ancestor))
.map(str::trim)
.map(ToOwned::to_owned)
.collect::<Vec<_>>()
.into();
}
}
}
Log::default()
}