use super::*;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RepoOp {
Merge,
Rebase {
step: usize,
total: usize,
},
CherryPick,
Revert,
Bisect,
}
impl RepoOp {
pub fn label(self) -> String {
match self {
RepoOp::Merge => String::from("MERGING"),
RepoOp::Rebase { step, total } => format!("REBASING {step}/{total}"),
RepoOp::CherryPick => String::from("CHERRY-PICKING"),
RepoOp::Revert => String::from("REVERTING"),
RepoOp::Bisect => String::from("BISECTING"),
}
}
pub fn command(self) -> &'static str {
match self {
RepoOp::Merge => "merge",
RepoOp::Rebase { .. } => "rebase",
RepoOp::CherryPick => "cherry-pick",
RepoOp::Revert => "revert",
RepoOp::Bisect => "bisect",
}
}
}
pub fn repo_operation(root: &Path) -> Option<RepoOp> {
const NAMES: [&str; 6] = [
"rebase-merge",
"rebase-apply",
"MERGE_HEAD",
"CHERRY_PICK_HEAD",
"REVERT_HEAD",
"BISECT_LOG",
];
let mut args = vec!["rev-parse"];
for name in NAMES {
args.extend(["--git-path", name]);
}
let out = run_git(root, &args).ok()?;
let paths: Vec<PathBuf> = out.lines().map(|l| root.join(l.trim())).collect();
if paths.len() != NAMES.len() {
return None;
}
let counter = |dir: &Path, name: &str| -> usize {
std::fs::read_to_string(dir.join(name))
.ok()
.and_then(|s| s.trim().parse().ok())
.unwrap_or(0)
};
if paths[0].is_dir() {
return Some(RepoOp::Rebase {
step: counter(&paths[0], "msgnum"),
total: counter(&paths[0], "end"),
});
}
if paths[1].is_dir() && !paths[1].join("applying").exists() {
return Some(RepoOp::Rebase {
step: counter(&paths[1], "next"),
total: counter(&paths[1], "last"),
});
}
let op = [
RepoOp::Merge,
RepoOp::CherryPick,
RepoOp::Revert,
RepoOp::Bisect,
]
.into_iter()
.zip(&paths[2..])
.find(|(_, p)| p.is_file())?;
Some(op.0)
}
pub fn abort_operation(root: &Path, op: RepoOp) -> Result<String, String> {
match op {
RepoOp::Bisect => run_mutation(root, &["bisect", "reset"]),
_ => run_mutation(root, &[op.command(), "--abort"]),
}
}
pub fn continue_operation(root: &Path, op: RepoOp) -> Result<String, String> {
match op {
RepoOp::Merge => Err(String::from("Commit to conclude the merge, or abort it")),
RepoOp::Bisect => Err(String::from(
"Mark the commit good or bad to go on with the bisect",
)),
_ => run_mutation_with_env(
root,
&[op.command(), "--continue"],
&[("GIT_EDITOR", "true")],
),
}
}
pub fn skip_operation(root: &Path, op: RepoOp) -> Result<String, String> {
match op {
RepoOp::Merge | RepoOp::Bisect => Err(format!("A {} has no skip", op.command())),
_ => run_mutation_with_env(root, &[op.command(), "--skip"], &[("GIT_EDITOR", "true")]),
}
}
pub fn bisect_mark(root: &Path, good: bool) -> Result<String, String> {
run_mutation(root, &["bisect", if good { "good" } else { "bad" }])
}
pub(super) fn rebasing_branch(root: &Path) -> Option<String> {
let out = run_git(
root,
&[
"rev-parse",
"--git-path",
"rebase-merge/head-name",
"--git-path",
"rebase-apply/head-name",
],
)
.ok()?;
out.lines()
.find_map(|l| std::fs::read_to_string(root.join(l.trim())).ok())
.and_then(|name| {
name.trim()
.strip_prefix("refs/heads/")
.filter(|b| !b.is_empty())
.map(String::from)
})
}
pub(super) fn operation_in_progress(root: &Path) -> Option<&'static str> {
const STATES: [(&str, &str); 5] = [
("MERGE_HEAD", "merge"),
("rebase-merge", "rebase"),
("rebase-apply", "rebase"),
("CHERRY_PICK_HEAD", "cherry-pick"),
("REVERT_HEAD", "revert"),
];
let mut args = vec!["rev-parse"];
for (file, _) in STATES {
args.extend(["--git-path", file]);
}
let paths = run_git(root, &args).ok()?;
paths
.lines()
.zip(STATES)
.find(|(path, _)| root.join(path.trim()).exists())
.map(|(_, (_, op))| op)
}