use crate::error::Result;
use crate::git;
use crate::output;
pub fn fast_forward_home_ref(home_branch: &str, default_remote: &str, verbose: bool) {
if !git::ref_exists(default_remote) || !git::is_ancestor(home_branch, default_remote) {
return;
}
let behind = git::behind_base_count(home_branch, default_remote);
if behind == 0 {
return;
}
match git::force_update_branch(home_branch, default_remote, verbose) {
Ok(()) => output::success(&format!(
"Fast-forwarded {} to {} ({} commit(s), ref only)",
output::bold(home_branch),
default_remote,
behind
)),
Err(e) => {
if verbose {
output::warn(&format!("Could not fast-forward {home_branch}: {e}"));
}
}
}
}
pub fn pull_with_output(default_remote: &str, default_branch: &str, verbose: bool) -> Result<bool> {
output::info(&format!("Syncing with {}...", default_remote));
let before = git::head_commit()?;
if let Err(e) = git::pull_ff_only("origin", default_branch, verbose) {
output::error("Cannot fast-forward. Local branch has diverged from remote.");
output::hints(&[
&format!("git rebase {} # Rebase local changes", default_remote),
"git pull # Merge (creates merge commit)",
]);
return Err(e);
}
let after = git::head_commit()?;
if before != after {
let count = git::commit_count(&before, &after)?;
output::success(&format!(
"Pulled {} commit(s) from {}",
output::bold(&count.to_string()),
default_remote
));
Ok(true)
} else {
output::success(&format!("Already up to date with {}", default_remote));
Ok(false)
}
}