use std::path::Path;
use std::time::Duration;
use anyhow::{Context as _, Result};
use jiff::Timestamp;
use crate::agent::{self, Invocation};
use crate::git;
use crate::land::seat_of;
use crate::prompt;
use crate::run::{QuotaLoss, RebaseFixRecord, RunState};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Rebased {
Applied,
Stopped(String),
}
const HUNK_PER_FILE: usize = 4_000;
const HUNK_TOTAL: usize = 16_000;
const SUBJECTS: usize = 20;
const PATHS_IN_REASON: usize = 8;
pub async fn rebase_with_fixer(
state: &mut RunState,
scratch: &Path,
branch: &str,
onto: &str,
) -> Result<Rebased> {
let repo = state.repo.clone();
let cap = state.config.graph.review_rounds;
let orig = git::rev_parse(&repo, &format!("refs/heads/{branch}")).await?;
let mut said = String::new();
if !git::rebase_in_progress(scratch).await {
match git::rebase_start(&repo, scratch, branch, onto).await? {
git::RebaseStart::Applied => return Ok(Rebased::Applied),
git::RebaseStart::Failed(why) => return Ok(Rebased::Stopped(why)),
git::RebaseStart::Conflicted(why) => said = why,
}
}
let onto_sha = git::rev_parse(&repo, onto).await?;
let mut touched: Vec<String> = Vec::new();
loop {
if !git::rebase_in_progress(scratch).await {
return finish(state, scratch, branch, &orig, &onto_sha, &touched, &said).await;
}
let paths = git::unmerged_paths(scratch).await.unwrap_or_default();
for p in &paths {
if !touched.contains(p) {
touched.push(p.clone());
}
}
let spent = state.rebase_fixes.len();
if spent >= cap {
let why = reason(spent, cap, &paths, &said, "the rounds are spent");
abandon(&repo, scratch, branch, &orig).await;
return Ok(Rebased::Stopped(why));
}
let winner = state
.winner()
.cloned()
.context("resolving a rebase conflict needs a winning candidate")?;
let roles = state
.config
.resolve_roles()
.context("resolve the roster for the rebase fix")?;
let attempts = crate::fixer::attempts(state, &roles, &winner);
let ids: Vec<String> = attempts.iter().map(|(s, _)| s.id.clone()).collect();
let round = spent + 1;
let branch_subjects = subjects(scratch, &format!("{onto}..{branch}")).await;
let onto_subjects = subjects(scratch, &format!("{branch}..{onto}")).await;
let hunks = hunks(scratch, &paths);
let prompt_text = prompt::rebase_conflict(&prompt::RebaseConflict {
instruction: &state.instruction,
worktree: scratch,
branch,
onto,
paths: &paths,
branch_subjects: &branch_subjects,
onto_subjects: &onto_subjects,
hunks: &hunks,
round,
cap,
language: &state.config.graph.language,
github_language: &state.config.graph.github_language,
});
let prompt_text = if state.config.cache_dir().is_some() {
format!("{prompt_text}\n\n{}", prompt::build_cache_note("fix", true))
} else {
prompt_text
};
state.rebase_fixes.push(RebaseFixRecord {
agent: ids[0].clone(),
paths: paths.clone(),
from: Some(orig.clone()),
finished: false,
error: None,
});
state.event(
"rebase",
format!(
"{branch} conflicts with {onto} ({} path(s)); fixer round {round} of {cap}",
paths.len()
),
);
state.save()?;
let artifacts = agent::artifacts_dir(&state.dir());
let mut last = None;
for (i, (spec, seat_key)) in attempts.into_iter().enumerate() {
if i > 0 {
if let Some(r) = state.rebase_fixes.last_mut() {
r.agent = spec.id.clone();
}
state.save()?;
}
let mut seat = seat_of(state, &seat_key, &spec.id);
let stem = if i == 0 {
format!("rebase-fix-{round}")
} else {
format!("rebase-fix-{round}-{}", spec.id)
};
let out = agent::invoke(
&spec,
&mut seat,
&Invocation {
scope: crate::agent::InvocationScope::Repository,
coordination: state.config.coordination.as_ref(),
fetched_base: state.fetched_base.as_ref(),
cwd: scratch,
prompt: &prompt_text,
timeout: Duration::from_secs(state.config.graph.timeout_fix),
allow_write: true,
unsandboxed: false,
sessions: state.config.graph.sessions,
artifacts: &artifacts,
stem: &stem,
run: &state.id,
node: "rebase",
cache_dir: state.config.cache_dir().as_deref(),
attachments: &[],
writable: &[],
},
)
.await;
let seat_name = seat.key.clone();
state.seats.insert(seat.key.clone(), seat);
if let Some(next) = ids.get(i + 1)
&& agent::chain_advances(&out)
{
let (class, why) = crate::fixer::failure_of(&out);
crate::graph::record_handover(
state, "rebase", &seat_name, &spec.id, next, &class, &why,
);
continue;
}
last = Some((out, seat_name));
break;
}
let (out, seat_name) = last.expect("the fixer chain always has an entry");
let mut error = None;
let mut quota = false;
match out {
Ok(o) if o.quota_exhausted() => {
state.quota.push(QuotaLoss {
seat: seat_name,
node: "rebase".to_owned(),
at: Timestamp::now(),
reset: o.quota.as_ref().and_then(|q| q.reset.clone()),
});
error = Some("rate limited (quota); the fixer could not run".to_owned());
quota = true;
}
Ok(o) if !o.usable() => {
error = Some(format!(
"the fixer produced nothing usable ({})",
crate::notices::turn_status(o.exit_code, o.timed_out, o.quota_exhausted())
));
}
Ok(_) => {}
Err(e) => error = Some(format!("{e:#}")),
}
let finished = !git::rebase_in_progress(scratch).await;
if let Some(r) = state.rebase_fixes.last_mut() {
r.finished = finished;
r.error = error.clone();
}
state.save()?;
if quota {
let paths = git::unmerged_paths(scratch).await.unwrap_or_default();
let why = reason(
state.rebase_fixes.len(),
cap,
&paths,
&said,
"the fixer hit its rate limit",
);
abandon(&repo, scratch, branch, &orig).await;
return Ok(Rebased::Stopped(why));
}
}
}
async fn finish(
state: &mut RunState,
scratch: &Path,
branch: &str,
orig: &str,
onto_sha: &str,
touched: &[String],
said: &str,
) -> Result<Rebased> {
let repo = state.repo.clone();
let spent = state.rebase_fixes.len();
let cap = state.config.graph.review_rounds;
let unmerged = git::unmerged_paths(scratch).await.unwrap_or_default();
let head = git::rev_parse(scratch, "HEAD").await.unwrap_or_default();
let mut candidates: Vec<String> = touched.to_vec();
if let Ok(changed) = git::git(scratch, &["diff", "--name-only", onto_sha, "HEAD"]).await {
for p in changed.lines().map(str::trim).filter(|l| !l.is_empty()) {
if !candidates.iter().any(|c| c == p) {
candidates.push(p.to_owned());
}
}
}
let marked: Vec<String> = candidates
.into_iter()
.filter(|p| has_markers(scratch, p))
.collect();
let emptied = head == onto_sha
&& git::cherry(&repo, onto_sha, orig)
.await
.map_or(true, |(unmatched, _)| !unmatched.is_empty());
let dropped = if unmerged.is_empty() && marked.is_empty() && !emptied && !head.is_empty() {
dropped_commits(&repo, scratch, onto_sha, orig, &head).await
} else {
Ok(Vec::new())
};
let problem = if !unmerged.is_empty() {
Some(("paths are still unmerged", unmerged))
} else if !marked.is_empty() {
Some(("conflict markers were left in the tree", marked))
} else if emptied {
Some((
"the rebase ended with none of the branch's commits applied (all skipped)",
touched.to_vec(),
))
} else if head.is_empty() || !git::is_ancestor(&repo, onto_sha, &head).await {
Some((
"the rebase ended without the base in the result (abandoned or skipped)",
touched.to_vec(),
))
} else if let Err(error) = &dropped {
Some((
"the rebase record could not be verified",
vec![format!("{error:#}")],
))
} else if !dropped.as_ref().unwrap().is_empty() {
Some((
"the rebase dropped some of the branch's commits (skipped?)",
dropped.unwrap(),
))
} else {
None
};
match problem {
None => {
git::worktree_remove(&repo, scratch).await.ok();
state.event(
"rebase",
format!("{branch} rebased after {spent} fixer round(s)"),
);
state.save()?;
Ok(Rebased::Applied)
}
Some((what, paths)) => {
let why = reason(spent, cap, &paths, said, what);
abandon(&repo, scratch, branch, orig).await;
Ok(Rebased::Stopped(why))
}
}
}
async fn dropped_commits(
repo: &Path,
scratch: &Path,
onto_sha: &str,
orig: &str,
head: &str,
) -> Result<Vec<String>> {
let record = git::rebase_record(scratch).await?;
anyhow::ensure!(
record.heads.last().map(String::as_str) == Some(head),
"rebase final HEAD disagrees with record"
);
let mut previous = onto_sha;
let mut lost = Vec::new();
for (sha, picked_head) in record.picks.iter().zip(&record.heads) {
if picked_head == previous
&& !absorbed_in_result(repo, sha, onto_sha, orig, head).await?
&& !already_in_result(repo, sha, orig, head).await?
{
lost.push(git::git(repo, &["show", "-s", "--format=%s", sha]).await?);
}
previous = picked_head;
}
Ok(lost)
}
async fn commit_paths(repo: &Path, sha: &str) -> Result<Vec<String>> {
git::git(
repo,
&[
"diff-tree",
"--no-commit-id",
"--name-only",
"-r",
"--root",
"-z",
sha,
],
)
.await
.map(|o| {
o.split('\0')
.filter(|l| !l.is_empty())
.map(str::to_owned)
.collect()
})
}
async fn entry(repo: &Path, rev: &str, path: &str) -> Result<Option<String>> {
let out = git::git(repo, &["--literal-pathspecs", "ls-tree", rev, "--", path]).await?;
Ok(out
.split('\t')
.next()
.filter(|e| !e.is_empty())
.map(str::to_owned))
}
async fn already_in_result(repo: &Path, sha: &str, orig: &str, head: &str) -> Result<bool> {
if replay_is_noop(repo, sha, head).await? {
return Ok(true);
}
for p in &commit_paths(repo, sha).await? {
let got = entry(repo, head, p).await?;
if got != entry(repo, sha, p).await? && got != entry(repo, orig, p).await? {
return Ok(false);
}
}
Ok(true)
}
async fn replay_is_noop(repo: &Path, sha: &str, head: &str) -> Result<bool> {
let base = format!("{sha}^");
let out = git::git_raw(
repo,
&[
"merge-tree",
"--write-tree",
&format!("--merge-base={base}"),
head,
sha,
],
)
.await?;
anyhow::ensure!(
out.code == Some(0) || out.code == Some(1),
"merge-tree failed: {}",
out.stderr
);
if !out.ok() {
return Ok(false);
}
let merged = out
.stdout
.lines()
.next()
.context("merge-tree returned no tree")?
.trim();
Ok(merged == git::tree_of(repo, head).await?)
}
async fn absorbed_in_result(
repo: &Path,
sha: &str,
onto: &str,
orig: &str,
head: &str,
) -> Result<bool> {
if !replay_is_noop(repo, sha, onto).await? {
return Ok(false);
}
let paths = commit_paths(repo, sha).await?;
if paths.is_empty() {
return Ok(false);
}
let base = path_tree(repo, sha, &paths).await?;
let upstream = path_tree(repo, onto, &paths).await?;
let branch = path_tree(repo, orig, &paths).await?;
let result = path_tree(repo, head, &paths).await?;
let out = git::git_raw(
repo,
&[
"merge-tree",
"--write-tree",
&format!("--merge-base={base}"),
&upstream,
&branch,
],
)
.await?;
anyhow::ensure!(
out.code == Some(0) || out.code == Some(1),
"merge-tree failed: {}",
out.stderr
);
if !out.ok() {
return Ok(false);
}
Ok(out
.stdout
.lines()
.next()
.context("merge-tree returned no tree")?
== result)
}
async fn path_tree(repo: &Path, rev: &str, paths: &[String]) -> Result<String> {
use crate::proc::Quiet as _;
use std::fmt::Write as _;
use std::process::Stdio;
use tokio::io::AsyncWriteExt as _;
let mut entries = String::new();
for (i, path) in paths.iter().enumerate() {
let out = git::git(
repo,
&["--literal-pathspecs", "ls-tree", "-z", rev, "--", path],
)
.await?;
if !out.is_empty() {
let (entry, _) = out
.split_once('\t')
.context("ls-tree returned an invalid entry")?;
writeln!(entries, "{entry}\t{i:016x}")?;
}
}
#[cfg(test)]
crate::web::request_cost::git();
let mut child = tokio::process::Command::new("git")
.args(["mktree"])
.current_dir(repo)
.quiet()
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.kill_on_drop(true)
.spawn()?;
let mut stdin = child.stdin.take().context("mktree stdin missing")?;
stdin.write_all(entries.as_bytes()).await?;
drop(stdin);
let out = child.wait_with_output().await?;
anyhow::ensure!(
out.status.success(),
"mktree failed: {}",
String::from_utf8_lossy(&out.stderr)
);
Ok(String::from_utf8(out.stdout)?.trim().to_owned())
}
async fn abandon(repo: &Path, scratch: &Path, branch: &str, orig: &str) {
git::rebase_abort(repo, scratch).await;
let full = format!("refs/heads/{branch}");
if git::rev_parse(repo, &full).await.ok().as_deref() != Some(orig) {
git::git_raw(repo, &["update-ref", &full, orig]).await.ok();
}
}
fn reason(spent: usize, cap: usize, paths: &[String], said: &str, what: &str) -> String {
let shown: Vec<&str> = paths
.iter()
.take(PATHS_IN_REASON)
.map(String::as_str)
.collect();
let mut list = shown.join(", ");
if paths.len() > shown.len() {
list.push_str(&format!(" and {} more", paths.len() - shown.len()));
}
if list.is_empty() {
list.push_str("none recorded");
}
let mut s = format!(
"conflict not resolved after {spent} of {cap} fixer round(s) ({what}); remaining \
conflicted path(s): {list}"
);
let said = git::rebase_said(said);
let said = said.trim();
if !said.is_empty() {
s.push_str("; git said: ");
s.extend(said.chars().take(250));
}
s
}
async fn subjects(worktree: &Path, range: &str) -> Vec<String> {
let n = format!("-n{SUBJECTS}");
git::git(worktree, &["log", "--format=%s", &n, range])
.await
.map(|o| o.lines().map(str::to_owned).collect())
.unwrap_or_default()
}
fn has_markers(worktree: &Path, path: &str) -> bool {
std::fs::read_to_string(worktree.join(path)).is_ok_and(|t| {
t.lines().any(|l| l.starts_with("<<<<<<< ")) && t.lines().any(|l| l.starts_with(">>>>>>> "))
})
}
fn hunks(worktree: &Path, paths: &[String]) -> String {
let mut out = String::new();
for p in paths {
if out.len() >= HUNK_TOTAL {
out.push_str("\n(more conflicted files omitted)\n");
break;
}
out.push_str(&format!("=== {p} ===\n"));
let Ok(text) = std::fs::read_to_string(worktree.join(p)) else {
out.push_str("(not readable as text; use git to inspect it)\n");
continue;
};
let mut file = String::new();
let mut inside = false;
for line in text.lines() {
if line.starts_with("<<<<<<< ") {
inside = true;
}
if inside {
file.push_str(line);
file.push('\n');
}
if line.starts_with(">>>>>>> ") {
inside = false;
}
}
if file.len() > HUNK_PER_FILE {
file = file.chars().take(HUNK_PER_FILE).collect();
file.push_str("\n(truncated)\n");
}
out.push_str(&file);
}
out
}
#[cfg(test)]
mod tests {
use super::*;
use crate::proc::Quiet as _;
fn sh(dir: &Path, args: &[&str]) {
let o = std::process::Command::new("git")
.quiet()
.args(args)
.current_dir(dir)
.output()
.unwrap();
assert!(
o.status.success(),
"{args:?}: {}",
String::from_utf8_lossy(&o.stderr)
);
}
async fn record(d: &Path, picks: &[String], heads: &[String]) {
for (name, values) in [("magi-rebase-picks", picks), ("magi-rebase-heads", heads)] {
let path = git::git(d, &["rev-parse", "--git-path", name])
.await
.unwrap();
std::fs::write(d.join(path), values.join("\n") + "\n").unwrap();
}
}
#[tokio::test]
async fn a_lost_mode_change_is_not_already_in_the_result() {
let t = tempfile::tempdir().unwrap();
let d = t.path();
sh(d, &["init", "-q", "-b", "main"]);
sh(d, &["config", "user.name", "t"]);
sh(d, &["config", "user.email", "t@example.com"]);
sh(d, &["config", "core.fileMode", "true"]);
std::fs::write(d.join("script.sh"), "echo\n").unwrap();
sh(d, &["add", "-A"]);
sh(d, &["commit", "-q", "-m", "base"]);
sh(d, &["update-index", "--chmod=+x", "script.sh"]);
sh(d, &["commit", "-q", "-m", "chmod"]);
let sha = git::git(d, &["rev-parse", "HEAD"]).await.unwrap();
let base = git::git(d, &["rev-parse", "HEAD~1"]).await.unwrap();
assert!(!already_in_result(d, &sha, &sha, &base).await.unwrap());
assert!(already_in_result(d, &sha, &sha, &sha).await.unwrap());
}
async fn commit(d: &Path, msg: &str) -> String {
sh(d, &["add", "-A"]);
sh(d, &["commit", "-q", "-m", msg]);
git::git(d, &["rev-parse", "HEAD"]).await.unwrap()
}
#[tokio::test]
async fn candidate_path_trees_preserve_modes_deletions_and_unusual_names() {
let t = tempfile::tempdir().unwrap();
let d = t.path();
sh(d, &["init", "-q", "-b", "main"]);
sh(d, &["config", "user.name", "t"]);
sh(d, &["config", "user.email", "t@example.com"]);
sh(d, &["config", "core.fileMode", "true"]);
let path = if cfg!(windows) {
"nested/日本語 [literal].txt"
} else {
"nested/日本語\tline\n.txt"
};
std::fs::create_dir(d.join("nested")).unwrap();
std::fs::write(d.join(path), "contents\n").unwrap();
std::fs::write(d.join("g.txt"), "old\n").unwrap();
let base = commit(d, "base").await;
let paths = vec![path.to_owned()];
let tree = path_tree(d, &base, &paths).await.unwrap();
std::fs::write(d.join("g.txt"), "unrelated\n").unwrap();
let other = commit(d, "unrelated path").await;
assert_eq!(path_tree(d, &other, &paths).await.unwrap(), tree);
sh(d, &["update-index", "--chmod=+x", path]);
sh(d, &["commit", "-q", "-m", "chmod"]);
assert_ne!(path_tree(d, "HEAD", &paths).await.unwrap(), tree);
std::fs::remove_file(d.join(path)).unwrap();
let removed = commit(d, "delete").await;
let absent = path_tree(d, &removed, &paths).await.unwrap();
assert_ne!(absent, tree);
assert_eq!(absent, path_tree(d, &removed, &[]).await.unwrap());
assert!(path_tree(d, "not-a-revision", &paths).await.is_err());
}
#[tokio::test]
async fn a_change_inside_a_larger_upstream_edit_is_in_the_result() {
let t = tempfile::tempdir().unwrap();
let d = t.path();
sh(d, &["init", "-q", "-b", "main"]);
sh(d, &["config", "user.name", "t"]);
sh(d, &["config", "user.email", "t@example.com"]);
let body = "old\n1\n2\n3\n4\n5\n6\n7\n8\n9\nend\n";
std::fs::write(d.join("f.txt"), body).unwrap();
commit(d, "base").await;
std::fs::write(d.join("f.txt"), body.replacen("old", "new", 1)).unwrap();
let c = commit(d, "c1").await;
sh(d, &["checkout", "-q", "-b", "up", "HEAD~1"]);
let up = body.replacen("old", "new", 1).replace("end", "end plus");
std::fs::write(d.join("f.txt"), up).unwrap();
let head = commit(d, "upstream").await;
assert!(already_in_result(d, &c, &c, &head).await.unwrap());
}
#[tokio::test]
async fn a_dropped_commit_on_a_non_ascii_path_is_still_noticed() {
let t = tempfile::tempdir().unwrap();
let d = t.path();
sh(d, &["init", "-q", "-b", "main"]);
sh(d, &["config", "user.name", "t"]);
sh(d, &["config", "user.email", "t@example.com"]);
std::fs::write(d.join("a.txt"), "a\n").unwrap();
let base = commit(d, "base").await;
std::fs::write(d.join("日本語.txt"), "x\n").unwrap();
let c = commit(d, "c").await;
assert!(!already_in_result(d, &c, &c, &base).await.unwrap());
}
async fn commit_dated(d: &Path, msg: &str) -> String {
sh(d, &["add", "-A"]);
let st = std::process::Command::new("git")
.quiet()
.current_dir(d)
.env("GIT_AUTHOR_DATE", "2020-01-01T00:00:00+0000")
.env("GIT_COMMITTER_DATE", "2020-01-01T00:00:00+0000")
.args(["commit", "-q", "-m", msg])
.status()
.unwrap();
assert!(st.success());
git::git(d, &["rev-parse", "HEAD"]).await.unwrap()
}
#[tokio::test]
async fn a_recorded_resolved_pick_after_an_absorbed_one_is_not_lost() {
let t = tempfile::tempdir().unwrap();
let d = t.path();
sh(d, &["init", "-q", "-b", "main"]);
sh(d, &["config", "user.name", "t"]);
sh(d, &["config", "user.email", "t@example.com"]);
std::fs::write(d.join("z.txt"), "z\n").unwrap();
let base = commit(d, "base").await;
std::fs::write(d.join("a.txt"), "a\n").unwrap();
commit_dated(d, "same").await;
std::fs::write(d.join("b.txt"), "b\n").unwrap();
let orig = commit_dated(d, "same").await;
sh(d, &["checkout", "-q", "-b", "up", &base]);
std::fs::write(d.join("a.txt"), "a\n").unwrap();
std::fs::write(d.join("y.txt"), "y\n").unwrap();
std::fs::write(d.join("b.txt"), "other\n").unwrap();
let onto = commit(d, "upstream").await;
std::fs::write(d.join("b.txt"), "other\nb\n").unwrap();
let head = commit_dated(d, "same").await;
record(
d,
&[
git::rev_parse(d, &format!("{orig}^")).await.unwrap(),
orig.clone(),
],
&[onto.clone(), head.clone()],
)
.await;
assert!(
dropped_commits(d, d, &onto, &orig, &head)
.await
.unwrap()
.is_empty()
);
}
#[tokio::test]
async fn a_skipped_commit_is_not_masked_by_a_surviving_one_in_the_same_file() {
let t = tempfile::tempdir().unwrap();
let d = t.path();
sh(d, &["init", "-q", "-b", "main"]);
sh(d, &["config", "user.name", "t"]);
sh(d, &["config", "user.email", "t@example.com"]);
let body = "first\n1\n2\n3\n4\n5\n6\n7\n8\n9\nlast\n";
std::fs::write(d.join("f.txt"), body).unwrap();
let base = commit(d, "base").await;
std::fs::write(d.join("f.txt"), body.replacen("first", "mine", 1)).unwrap();
commit_dated(d, "same").await;
let two = body
.replacen("first", "mine", 1)
.replacen("last", "tail", 1);
std::fs::write(d.join("f.txt"), &two).unwrap();
let orig = commit_dated(d, "same").await;
sh(d, &["checkout", "-q", "-b", "up", &base]);
std::fs::write(d.join("f.txt"), body.replacen("first", "theirs", 1)).unwrap();
let onto = commit(d, "upstream").await;
std::fs::write(
d.join("f.txt"),
body.replacen("first", "theirs", 1)
.replacen("last", "tail", 1),
)
.unwrap();
let head = commit_dated(d, "same").await;
record(
d,
&[
git::rev_parse(d, &format!("{orig}^")).await.unwrap(),
orig.clone(),
],
&[onto.clone(), head.clone()],
)
.await;
assert_eq!(
dropped_commits(d, d, &onto, &orig, &head).await.unwrap(),
vec!["same"]
);
}
#[tokio::test]
async fn git_records_a_skipped_middle_pick_after_an_applied_pick() {
let t = tempfile::tempdir().unwrap();
let d = t.path();
sh(d, &["init", "-q", "-b", "main"]);
sh(d, &["config", "user.name", "t"]);
sh(d, &["config", "user.email", "t@example.com"]);
std::fs::write(d.join("f"), "base\n").unwrap();
let base = commit(d, "base").await;
sh(d, &["checkout", "-q", "-b", "topic"]);
std::fs::write(d.join("absorbed"), "upstream too\n").unwrap();
commit(d, "already upstream").await;
std::fs::write(d.join("a"), "first\n").unwrap();
let first = commit(d, "first").await;
std::fs::write(d.join("f"), "middle\n").unwrap();
let middle = commit(d, "lost middle").await;
std::fs::write(d.join("z"), "last\n").unwrap();
let orig = commit(d, "last").await;
sh(d, &["checkout", "-q", "main"]);
std::fs::write(d.join("absorbed"), "upstream too\n").unwrap();
commit(d, "patch twin").await;
std::fs::write(d.join("f"), "upstream\n").unwrap();
let onto = commit(d, "upstream").await;
let scratch = d.join("scratch");
let git::RebaseStart::Conflicted(why) = git::rebase_start(d, &scratch, "topic", &onto)
.await
.unwrap()
else {
panic!("expected a conflict");
};
assert!(!why.contains("Executing:"), "{why}");
let skipped = git::git_raw(&scratch, &["rebase", "--skip"]).await.unwrap();
assert!(skipped.ok(), "{}", skipped.stderr);
let head = git::rev_parse(&scratch, "HEAD").await.unwrap();
let record = git::rebase_record(&scratch).await.unwrap();
assert_eq!(record.picks, vec![first, middle, orig.clone()]);
assert_eq!(record.heads[0], record.heads[1]);
assert_ne!(record.heads[1], record.heads[2]);
assert_eq!(
dropped_commits(d, &scratch, &onto, &orig, &head)
.await
.unwrap(),
vec!["lost middle"]
);
git::rebase_abort(d, &scratch).await;
assert!(git::is_ancestor(d, &base, &head).await);
}
#[test]
fn recording_diagnostics_do_not_hide_other_git_output() {
assert_eq!(
git::rebase_said("before\nExecuting: record\nafter\n"),
"before\nafter"
);
}
async fn rebase_successive_edits(
absorbed: bool,
extra_hunk: bool,
unrelated_conflict: bool,
) -> Rebased {
rebase_successive_edits_record(absorbed, extra_hunk, unrelated_conflict, None).await
}
async fn rebase_successive_edits_record(
absorbed: bool,
extra_hunk: bool,
unrelated_conflict: bool,
damage: Option<&str>,
) -> Rebased {
crate::run::pin_test_home();
let t = tempfile::tempdir().unwrap();
let d = t.path();
sh(d, &["init", "-q", "-b", "main"]);
sh(d, &["config", "user.name", "t"]);
sh(d, &["config", "user.email", "t@example.com"]);
sh(d, &["config", "core.autocrlf", "false"]);
let body = "old\n1\n2\n3\n4\n5\n6\n7\n8\n9\nend\n";
std::fs::write(d.join("f.txt"), body).unwrap();
std::fs::write(d.join("conflict.txt"), "old\n").unwrap();
if unrelated_conflict {
std::fs::write(d.join("g.txt"), "old\n").unwrap();
}
let base = commit(d, "base").await;
sh(d, &["checkout", "-q", "-b", "topic"]);
std::fs::write(d.join("conflict.txt"), "branch\n").unwrap();
commit(d, "c0").await;
let mut middle = body.replacen("old", "new", 1);
if extra_hunk {
middle = middle.replace("\n5\n", "\nmissing\n");
}
std::fs::write(d.join("f.txt"), &middle).unwrap();
let c1 = commit(d, "c1").await;
std::fs::write(d.join("f.txt"), middle.replacen("new", "newer", 1)).unwrap();
if unrelated_conflict {
std::fs::write(d.join("g.txt"), "branch\n").unwrap();
}
let orig = commit(d, "c2").await;
sh(d, &["checkout", "-q", "main"]);
std::fs::write(d.join("conflict.txt"), "branch plus upstream\n").unwrap();
let mut upstream = body
.replacen("old", if absorbed { "new" } else { "theirs" }, 1)
.replace("end", "upstream tail");
if extra_hunk {
upstream = upstream.replace("\n5\n", "\nupstream middle\n");
}
std::fs::write(d.join("f.txt"), &upstream).unwrap();
if unrelated_conflict {
std::fs::write(d.join("g.txt"), "upstream\n").unwrap();
}
let onto = commit(d, "upstream composite").await;
assert!(git::cherry(d, &onto, &orig).await.unwrap().0.contains(&c1));
let scratch = d.join("scratch");
assert!(matches!(
git::rebase_start(d, &scratch, "topic", &onto)
.await
.unwrap(),
git::RebaseStart::Conflicted(_)
));
std::fs::write(
scratch.join("conflict.txt"),
"branch plus upstream resolved\n",
)
.unwrap();
sh(&scratch, &["add", "conflict.txt"]);
let continued = git::git_raw(
&scratch,
&["-c", "core.editor=true", "rebase", "--continue"],
)
.await
.unwrap();
if absorbed && !extra_hunk {
if unrelated_conflict {
assert!(!continued.ok());
assert_eq!(git::unmerged_paths(&scratch).await.unwrap(), vec!["g.txt"]);
assert_eq!(git::rev_parse(&scratch, "REBASE_HEAD").await.unwrap(), orig);
std::fs::write(scratch.join("g.txt"), "branch plus upstream resolved\n").unwrap();
sh(&scratch, &["add", "g.txt"]);
let resolved = git::git_raw(&scratch, &["rebase", "--continue"])
.await
.unwrap();
assert!(resolved.ok(), "{}", resolved.stderr);
let full = git::git_raw(
d,
&[
"merge-tree",
"--write-tree",
&format!("--merge-base={c1}"),
&onto,
&orig,
],
)
.await
.unwrap();
assert_eq!(full.code, Some(1));
} else {
assert!(continued.ok(), "{}", continued.stderr);
}
} else {
assert!(!continued.ok());
assert_eq!(git::unmerged_paths(&scratch).await.unwrap(), vec!["f.txt"]);
let skipped = git::git_raw(&scratch, &["rebase", "--skip"]).await.unwrap();
if !absorbed {
assert!(!skipped.ok());
std::fs::write(
scratch.join("f.txt"),
upstream.replacen("theirs", "newer", 1),
)
.unwrap();
sh(&scratch, &["add", "f.txt"]);
let resolved = git::git_raw(
&scratch,
&["-c", "core.editor=true", "rebase", "--continue"],
)
.await
.unwrap();
assert!(resolved.ok(), "{}", resolved.stderr);
} else {
assert!(skipped.ok(), "{}", skipped.stderr);
}
}
assert!(!git::rebase_in_progress(&scratch).await);
assert_eq!(
std::fs::read_to_string(scratch.join("f.txt")).unwrap(),
upstream.replacen(if absorbed { "new" } else { "theirs" }, "newer", 1)
);
let head = git::rev_parse(&scratch, "HEAD").await.unwrap();
assert!(!already_in_result(d, &c1, &orig, &head).await.unwrap());
let mut state = RunState::new(
d.to_path_buf(),
"main".into(),
base,
"rebase".into(),
crate::config::Config::default(),
);
if let Some(damage) = damage {
let path = git::git(&scratch, &["rev-parse", "--git-path", "magi-rebase-heads"])
.await
.unwrap();
match damage {
"missing" => std::fs::remove_file(&path).unwrap(),
"count" => std::fs::write(&path, "").unwrap(),
"final" => {
let text = std::fs::read_to_string(&path).unwrap();
let mut heads: Vec<_> = text.lines().map(str::to_owned).collect();
*heads.last_mut().unwrap() = onto.clone();
std::fs::write(&path, heads.join("\n") + "\n").unwrap();
}
"invalid" => std::fs::write(&path, "invalid\n").unwrap(),
_ => unreachable!(),
}
}
let outcome = finish(
&mut state,
&scratch,
"topic",
&orig,
&onto,
&["conflict.txt".into()],
"",
)
.await
.unwrap();
std::fs::remove_dir_all(state.dir()).ok();
outcome
}
#[tokio::test]
async fn missing_or_inconsistent_rebase_records_stop_the_rebase() {
for damage in ["missing", "count", "final", "invalid"] {
let outcome = rebase_successive_edits_record(true, false, false, Some(damage)).await;
assert!(
matches!(outcome, Rebased::Stopped(why) if why.contains("record could not be verified")),
"{damage}"
);
}
}
#[tokio::test]
async fn an_absorbed_middle_commit_superseded_by_a_later_edit_is_applied() {
assert_eq!(
rebase_successive_edits(true, false, false).await,
Rebased::Applied
);
}
#[tokio::test]
async fn an_unrelated_later_conflict_does_not_hide_an_absorbed_commit() {
assert_eq!(
rebase_successive_edits(true, false, true).await,
Rebased::Applied
);
}
#[tokio::test]
async fn a_skipped_middle_commit_superseded_by_a_later_edit_is_dropped() {
let Rebased::Stopped(why) = rebase_successive_edits(false, false, false).await else {
panic!("a skipped middle commit must stop the rebase");
};
assert!(why.contains("the rebase dropped some of the branch's commits (skipped?)"));
assert!(why.contains("c1"));
}
#[tokio::test]
async fn a_partially_absorbed_middle_commit_with_a_skipped_hunk_is_dropped() {
let Rebased::Stopped(why) = rebase_successive_edits(true, true, false).await else {
panic!("an unabsorbed hunk must stop the rebase");
};
assert!(why.contains("the rebase dropped some of the branch's commits (skipped?)"));
assert!(why.contains("c1"));
}
#[tokio::test]
async fn an_absorbed_commit_erased_by_the_fixer_is_dropped() {
crate::run::pin_test_home();
let t = tempfile::tempdir().unwrap();
let d = t.path();
sh(d, &["init", "-q", "-b", "main"]);
sh(d, &["config", "user.name", "t"]);
sh(d, &["config", "user.email", "t@example.com"]);
sh(d, &["config", "rerere.enabled", "false"]);
let body =
"old\n1\n2\n3\n4\n5\n6\n7\n8\n9\nlater\n10\n11\n12\n13\n14\n15\n16\n17\n18\nend\n";
std::fs::write(d.join("f.txt"), body).unwrap();
std::fs::write(d.join("conflict.txt"), "old\n").unwrap();
let base = commit(d, "base").await;
sh(d, &["checkout", "-q", "-b", "topic"]);
std::fs::write(d.join("conflict.txt"), "branch\n").unwrap();
commit(d, "c0").await;
std::fs::write(d.join("f.txt"), body.replacen("old", "new", 1)).unwrap();
let c1 = commit(d, "c1").await;
std::fs::write(
d.join("f.txt"),
body.replacen("old", "new", 1)
.replace("later", "branch tail"),
)
.unwrap();
let orig = commit(d, "c2").await;
sh(d, &["checkout", "-q", "main"]);
std::fs::write(d.join("conflict.txt"), "upstream\n").unwrap();
let upstream = body
.replacen("old", "new", 1)
.replace("end", "upstream tail");
std::fs::write(d.join("f.txt"), &upstream).unwrap();
let onto = commit(d, "upstream composite").await;
let scratch = d.join("scratch");
assert!(matches!(
git::rebase_start(d, &scratch, "topic", &onto)
.await
.unwrap(),
git::RebaseStart::Conflicted(_)
));
std::fs::write(scratch.join("conflict.txt"), "resolved\n").unwrap();
std::fs::write(scratch.join("f.txt"), upstream.replacen("new", "theirs", 1)).unwrap();
sh(&scratch, &["add", "conflict.txt", "f.txt"]);
let continued = git::git_raw(&scratch, &["rebase", "--continue"])
.await
.unwrap();
assert_eq!(
continued.code,
Some(1),
"{}\n{}",
continued.stdout,
continued.stderr
);
assert_eq!(git::rev_parse(&scratch, "REBASE_HEAD").await.unwrap(), c1);
let skipped = git::git_raw(&scratch, &["rebase", "--skip"]).await.unwrap();
assert!(skipped.ok(), "{}", skipped.stderr);
let head = git::rev_parse(&scratch, "HEAD").await.unwrap();
assert!(replay_is_noop(d, &c1, &onto).await.unwrap());
let expected = git::git_raw(
d,
&[
"merge-tree",
"--write-tree",
&format!("--merge-base={c1}"),
&onto,
&orig,
],
)
.await
.unwrap();
assert!(expected.ok(), "{}", expected.stderr);
assert!(
!absorbed_in_result(d, &c1, &onto, &orig, &head)
.await
.unwrap()
);
assert_eq!(
dropped_commits(d, &scratch, &onto, &orig, &head)
.await
.unwrap(),
vec!["c1"]
);
let mut state = RunState::new(
d.to_path_buf(),
"main".into(),
base,
"rebase".into(),
crate::config::Config::default(),
);
let outcome = finish(
&mut state,
&scratch,
"topic",
&orig,
&onto,
&["conflict.txt".into()],
"",
)
.await
.unwrap();
assert!(
matches!(outcome, Rebased::Stopped(why) if why.contains("the rebase dropped some of the branch's commits (skipped?)") && why.contains("c1"))
);
std::fs::remove_dir_all(state.dir()).ok();
}
#[tokio::test]
async fn a_partially_absorbed_commit_missing_an_unrelated_hunk_is_dropped() {
let t = tempfile::tempdir().unwrap();
let d = t.path();
sh(d, &["init", "-q", "-b", "main"]);
sh(d, &["config", "user.name", "t"]);
sh(d, &["config", "user.email", "t@example.com"]);
std::fs::write(d.join("c0.txt"), "base c0\n").unwrap();
let body = "old\n1\n2\n3\n4\n5\n6\n7\n8\n9\nlast\n";
std::fs::write(d.join("f.txt"), body).unwrap();
std::fs::write(d.join("extra.txt"), "initial\n").unwrap();
let base = commit(d, "base").await;
std::fs::write(d.join("c0.txt"), "branch c0\n").unwrap();
commit_dated(d, "c0").await;
std::fs::write(d.join("f.txt"), body.replacen("old", "new", 1)).unwrap();
std::fs::write(d.join("extra.txt"), "c1 edit\n").unwrap();
commit_dated(d, "c1").await;
std::fs::write(d.join("f.txt"), body.replacen("old", "newer", 1)).unwrap();
let orig = commit_dated(d, "c2").await;
sh(d, &["checkout", "-q", "-b", "up", &base]);
std::fs::write(d.join("c0.txt"), "upstream c0\n").unwrap();
let up = body.replacen("old", "new", 1).replace("last", "tail");
std::fs::write(d.join("f.txt"), &up).unwrap();
let onto = commit(d, "upstream").await;
std::fs::write(d.join("c0.txt"), "resolved c0\n").unwrap();
commit_dated(d, "c0").await;
let res = up.replacen("new", "newer", 1);
std::fs::write(d.join("f.txt"), &res).unwrap();
let head = commit_dated(d, "c2").await;
let c0 = git::rev_parse(d, &format!("{orig}~2")).await.unwrap();
let c1 = git::rev_parse(d, &format!("{orig}^")).await.unwrap();
let resolved = git::rev_parse(d, &format!("{head}^")).await.unwrap();
record(
d,
&[c0, c1, orig.clone()],
&[resolved.clone(), resolved, head.clone()],
)
.await;
assert_eq!(
dropped_commits(d, d, &onto, &orig, &head).await.unwrap(),
vec!["c1"]
);
}
}