Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ Planned operations:
- **Zero configuration** — dependencies are inferred from git history
- **Fork-point tracking** — stores the parent SHA at each sync so `--onto` rebases work correctly even after the parent is rewritten
- **Integrated branch detection** — branches merged into their parent (or into the default branch) are detected; their children are reparented automatically
- **Conflict handling** — before rebasing a branch, `git merge-tree` checks whether it would conflict. If so, the branch and everything stacked on it are skipped with a warning (and not pushed), their worktrees are left untouched, and the rest of the sync goes on. A conflict the check didn't predict stops the sync with instructions to resolve; fork-points for already-rebased branches are saved so re-running `wt sync` picks up where it left off
- **Conflict handling** — before rebasing a branch, `git merge-tree` checks whether it would conflict, and a branch with merge commits of its own (an integration branch, say) is never rebased, since a rebase would flatten the merges. Either way the branch and everything stacked on it are skipped with a warning (and not pushed), their worktrees are left untouched, and the rest of the sync goes on. A conflict the check didn't predict stops the sync with instructions to resolve; fork-points for already-rebased branches are saved so re-running `wt sync` picks up where it left off
- **Non-zero exit on conflict** — when a branch was skipped or a rebase stopped, so `wt sync && wt sync --push` won't silently push past a conflict
- **From the default branch, every stack** — run from `main`'s worktree, the current stack is all of them, and `wt sync` says so; run from a stack's worktree to sync only that stack

Expand Down
69 changes: 68 additions & 1 deletion src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1320,6 +1320,15 @@ pub fn handle_sync(opts: SyncOptions) -> anyhow::Result<()> {
eprintln!("{}", hint_message(reason));
}

let merges = merge_commits(&repo, &rebase_base, parent, branch);
if merges > 0 {
conflicted.insert(branch);
eprintln!(
"{}",
warning_message(merge_skip_reason(branch, parent, merges))
);
continue;
}
if let Some(files) = would_conflict(&repo, &rebase_base, parent, branch) {
conflicted.insert(branch);
eprintln!(
Expand Down Expand Up @@ -1504,7 +1513,7 @@ pub fn handle_sync(opts: SyncOptions) -> anyhow::Result<()> {
let mut names: Vec<&str> = conflicted.into_iter().collect();
names.sort();
bail!(
"{} not rebased because of conflicts: {}",
"{} not rebased (conflicts or merge commits): {}",
if names.len() == 1 {
"1 branch"
} else {
Expand Down Expand Up @@ -1580,6 +1589,41 @@ fn would_conflict(
Some(files)
}

/// Merge commits a rebase of `branch` from `base` onto `onto` would flatten.
/// A plain rebase drops merges and replays the merged commits one by one, so
/// an integration branch built from hand-resolved merges loses its
/// resolutions and conflicts again; such a branch is skipped instead.
///
/// A merge whose merged-in side is already in `onto` (catching up by merging
/// `main` into a feature branch) doesn't count: the rebase drops those
/// commits as already upstream and loses nothing.
fn merge_commits(repo: &Repository, base: &str, onto: &str, branch: &str) -> usize {
let range = format!("{base}..{branch}");
let Ok(out) = repo.run_command(&["rev-list", "--merges", "--parents", &range]) else {
return 0;
};
out.lines()
.filter(|line| {
line.split_whitespace().skip(2).any(|merged| {
!repo
.run_command_check(&["merge-base", "--is-ancestor", merged, onto])
.unwrap_or(false)
})
})
.count()
}

fn merge_skip_reason(branch: &str, parent: &str, merges: usize) -> String {
let commits = if merges == 1 {
"a merge commit".to_string()
} else {
format!("{merges} merge commits")
};
format!(
"Skipping {branch}: it has {commits}, which a rebase would flatten. Rebase it by hand (`git rebase --rebase-merges {parent}` in its worktree), then run wt sync."
)
}

/// Why a branch is left out of this run's rebases.
fn conflict_skip_reason(branch: &str, parent: &str, files: &[String]) -> String {
let shown: Vec<&str> = files.iter().take(3).map(String::as_str).collect();
Expand Down Expand Up @@ -1736,6 +1780,8 @@ fn print_sync_plan(
let mut printed = false;
let mut has_rebases = false;
let mut conflicted: HashSet<&str> = HashSet::new();
// Branches the real run moves: rebased ones and everything above them.
let mut moving: HashSet<&str> = HashSet::new();
for &branch in branches {
let Some(node) = tree.nodes.get(branch) else {
continue;
Expand All @@ -1749,6 +1795,7 @@ fn print_sync_plan(
printed = true;
continue;
}
let parent_moves = moving.contains(parent.as_str());

// Same up-to-date check as the real run, against the parent's current
// tip. With a pending `--pull` fast-forward the real run moves the root
Expand All @@ -1765,11 +1812,24 @@ fn print_sync_plan(
.run_command(&["rev-parse", parent_rev])
.map(|s| s.trim().to_string())
.unwrap_or_default();
// Shown as up to date against the parent's current tip, but the real
// run rebases it once the parent has moved, so merges count here too.
if parent_moves {
moving.insert(branch);
let merges = merge_commits(repo, &mb, parent_rev, branch);
if merges > 0 {
conflicted.insert(branch);
eprintln!(" {}", merge_skip_reason(branch, parent, merges));
printed = true;
continue;
}
}
if mb == parent_sha {
eprintln!(" {branch} is up to date with {parent}");
printed = true;
continue;
}
moving.insert(branch);

let (base, reason) = choose_rebase_base(
repo,
Expand All @@ -1779,6 +1839,13 @@ fn print_sync_plan(
fork_points.get(branch).map(String::as_str),
&mb,
);
let merges = merge_commits(repo, &base, parent_rev, branch);
if merges > 0 {
conflicted.insert(branch);
eprintln!(" {}", merge_skip_reason(branch, parent, merges));
printed = true;
continue;
}
if let Some(files) = would_conflict(repo, &base, parent_rev, branch) {
conflicted.insert(branch);
eprintln!(" {}", conflict_skip_reason(branch, parent, &files));
Expand Down
58 changes: 57 additions & 1 deletion tests/conflict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ fn conflicting_branch_is_skipped_and_the_rest_is_synced() {
);
assert!(text.contains("Rebased fine onto main"), "{text}");
assert!(
text.contains("not rebased because of conflicts: on-stale, stale"),
text.contains("not rebased (conflicts or merge commits): on-stale, stale"),
"{text}"
);
// stale's worktree was never touched: same commit, no rebase in progress.
Expand All @@ -130,3 +130,59 @@ fn running_from_main_says_it_syncs_every_stack() {
let (_, plan) = wt_sync(&repo, &["-n", "--all"]);
assert!(!plan.contains("syncing all"), "--all asked for it:\n{plan}");
}

#[test]
fn branch_with_merge_commits_is_not_flattened() {
// Like diffnav's all-features: integration sits on a and merges in a
// sibling, b. main then moves, so a is rebased and integration would be.
let tmp = tempfile::tempdir().unwrap();
let repo = tmp.path().join("repo");
std::fs::create_dir_all(&repo).unwrap();
git(&repo, &["init", "-q", "-b", "main"]);
write_commit(&repo, "base.txt", "base");
let a = add_worktree(&repo, "a", "main");
write_commit(&a, "a.txt", "a");
let b = add_worktree(&repo, "b", "main");
write_commit(&b, "b.txt", "b");
let integration = add_worktree(&repo, "integration", "a");
write_commit(&integration, "i.txt", "i");
git(
&integration,
&["merge", "-q", "--no-ff", "-m", "merge b", "b"],
);
write_commit(&repo, "main.txt", "main moves");
let before = git(&repo, &["rev-parse", "integration"]);

let (_, plan) = wt_sync(&repo, &["--all", "-n"]);
assert!(
plan.contains("Skipping integration: it has a merge commit, which a rebase would flatten"),
"{plan}"
);

let (output, text) = wt_sync(&repo, &["--all"]);
assert!(!output.status.success(), "{text}");
assert!(text.contains("Rebased a onto main"), "{text}");
assert!(text.contains("Rebased b onto main"), "{text}");
assert_eq!(git(&repo, &["rev-parse", "integration"]), before);
}

#[test]
fn merging_main_into_a_branch_does_not_block_its_rebase() {
let tmp = tempfile::tempdir().unwrap();
let repo = tmp.path().join("repo");
std::fs::create_dir_all(&repo).unwrap();
git(&repo, &["init", "-q", "-b", "main"]);
write_commit(&repo, "base.txt", "base");
let feature = add_worktree(&repo, "feature", "main");
write_commit(&feature, "f.txt", "f");
write_commit(&repo, "m1.txt", "m1");
git(
&feature,
&["merge", "-q", "--no-ff", "-m", "catch up", "main"],
);
write_commit(&repo, "m2.txt", "m2");

let (output, text) = wt_sync(&repo, &["--all"]);
assert!(output.status.success(), "{text}");
assert!(text.contains("Rebased feature onto main"), "{text}");
}
Loading