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
6 changes: 3 additions & 3 deletions src/cli/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ pub(crate) fn format_clean_plan(plan: &CleanPlan) -> String {
for candidate in merged_candidates {
let parent_branch = match &candidate.reason {
CleanReason::DeletedLocally => continue,
CleanReason::IntegratedIntoParent { parent_branch } => parent_branch,
CleanReason::IntegratedIntoParent { parent_base } => &parent_base.branch_name,
};

lines.push(format!(
Expand Down Expand Up @@ -306,7 +306,7 @@ mod tests {
BlockedBranch, CleanBlockReason, CleanCandidate, CleanOptions, CleanPlan, CleanReason,
CleanTreeNode,
};
use crate::core::restack::RestackPreview;
use crate::core::restack::{RestackBaseTarget, RestackPreview};
use uuid::Uuid;

#[test]
Expand All @@ -331,7 +331,7 @@ mod tests {
branch_name: "feat/auth".into(),
parent_branch_name: "main".into(),
reason: CleanReason::IntegratedIntoParent {
parent_branch: "main".into(),
parent_base: RestackBaseTarget::local("main"),
},
tree: CleanTreeNode {
branch_name: "feat/auth".into(),
Expand Down
3 changes: 2 additions & 1 deletion src/cli/clean/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ fn visual_node_from_tree(tree: &CleanTreeNode) -> VisualNode {
mod tests {
use super::CleanAnimation;
use crate::core::clean::{CleanCandidate, CleanEvent, CleanPlan, CleanReason, CleanTreeNode};
use crate::core::restack::RestackBaseTarget;
use uuid::Uuid;

#[test]
Expand All @@ -114,7 +115,7 @@ mod tests {
branch_name: "feat/auth".into(),
parent_branch_name: "main".into(),
reason: CleanReason::IntegratedIntoParent {
parent_branch: "main".into(),
parent_base: RestackBaseTarget::local("main"),
},
tree: CleanTreeNode {
branch_name: "feat/auth".into(),
Expand Down
2 changes: 2 additions & 0 deletions src/cli/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub fn confirm_yes_no(prompt: &str) -> io::Result<bool> {

let mut input = String::new();
io::stdin().read_line(&mut input)?;
writeln!(stdout)?;
stdout.flush()?;

Ok(matches!(input.trim(), "y" | "Y" | "yes" | "YES" | "Yes"))
}
Expand Down
122 changes: 120 additions & 2 deletions src/cli/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use clap::Args;

use crate::core::clean;
use crate::core::merge;
use crate::core::sync::{self, SyncCompletion, SyncOptions};
use crate::core::sync::{
self, RemotePushActionKind, RemotePushOutcome, SyncCompletion, SyncOptions,
};
use crate::core::tree;

use super::CommandOutcome;
Expand Down Expand Up @@ -102,19 +104,34 @@ pub fn execute(args: SyncArgs) -> io::Result<CommandOutcome> {
}
SyncCompletion::Full(full_outcome) if outcome.status.success() => {
let summary = format_full_sync_summary(full_outcome);
let mut printed_output = false;
let mut restacked_branch_names = full_outcome
.restacked_branches
.iter()
.map(|branch| branch.branch_name.clone())
.collect::<Vec<_>>();
let excluded_branch_names = full_outcome
.cleanup_plan
.candidates
.iter()
.map(|candidate| candidate.branch_name.clone())
.collect::<Vec<_>>();

if !summary.is_empty() {
println!("{summary}");
printed_output = true;
}

if !full_outcome.cleanup_plan.candidates.is_empty() {
if !summary.is_empty() {
if printed_output {
println!();
}

println!(
"{}",
super::clean::format_clean_plan(&full_outcome.cleanup_plan)
);
printed_output = true;

if !super::clean::confirm_cleanup(&full_outcome.cleanup_plan)? {
println!("Skipped cleanup.");
Expand All @@ -125,6 +142,12 @@ pub fn execute(args: SyncArgs) -> io::Result<CommandOutcome> {
final_status = clean_outcome.status;

if clean_outcome.status.success() {
restacked_branch_names.extend(
clean_outcome
.restacked_branches
.iter()
.map(|branch| branch.branch_name.clone()),
);
let output = super::clean::format_clean_success_output(
&full_outcome.cleanup_plan.trunk_branch,
&clean_outcome,
Expand All @@ -141,6 +164,54 @@ pub fn execute(args: SyncArgs) -> io::Result<CommandOutcome> {
}
}
}

if final_status.success() {
let push_plan =
sync::plan_remote_pushes(&restacked_branch_names, &excluded_branch_names)?;

if !push_plan.actions.is_empty() {
if printed_output {
println!();
}

println!("{}", format_remote_push_plan(&push_plan));

if !confirm_remote_pushes()? {
println!("Skipped remote updates.");
} else {
println!();

let push_outcome = sync::execute_remote_push_plan(&push_plan)?;
final_status = push_outcome.status;

if push_outcome.status.success() {
let output = format_remote_push_success_output(&push_outcome);
if !output.is_empty() {
println!("{output}");
}
} else {
let output = format_partial_remote_push_output(
"Updated before failure:",
&push_outcome,
);
if !output.is_empty() {
println!("{output}");
println!();
}
if let Some(failed_action) = push_outcome.failed_action.as_ref() {
eprintln!(
"Failed to update '{}' on '{}'.",
failed_action.target.branch_name,
failed_action.target.remote_name
);
}
common::print_trimmed_stderr(
push_outcome.failure_output.as_deref(),
);
}
}
}
}
}
_ => {}
}
Expand Down Expand Up @@ -191,6 +262,53 @@ fn format_full_sync_summary(outcome: &sync::FullSyncOutcome) -> String {
common::join_sections(&sections)
}

fn format_remote_push_plan(plan: &sync::RemotePushPlan) -> String {
let mut lines = vec!["Remote branches to update:".to_string()];

for action in &plan.actions {
let action_label = match action.kind {
RemotePushActionKind::CreateRemoteBranch => "create",
RemotePushActionKind::UpdateRemoteBranch => "push",
RemotePushActionKind::ForceUpdateRemoteBranch => "force-push",
};
lines.push(format!(
"- {action_label} {} on {}",
action.target.branch_name, action.target.remote_name
));
}

lines.join("\n")
}

fn confirm_remote_pushes() -> io::Result<bool> {
common::confirm_yes_no("Push these remote updates? [y/N] ")
}

fn format_remote_push_success_output(outcome: &RemotePushOutcome) -> String {
format_partial_remote_push_output("Updated remote branches:", outcome)
}

fn format_partial_remote_push_output(header: &str, outcome: &RemotePushOutcome) -> String {
if outcome.pushed_actions.is_empty() {
return String::new();
}

let mut lines = vec![header.to_string()];
for action in &outcome.pushed_actions {
let action_label = match action.kind {
RemotePushActionKind::CreateRemoteBranch => "created",
RemotePushActionKind::UpdateRemoteBranch => "pushed",
RemotePushActionKind::ForceUpdateRemoteBranch => "force-pushed",
};
lines.push(format!(
"- {action_label} {} on {}",
action.target.branch_name, action.target.remote_name
));
}

lines.join("\n")
}

#[cfg(test)]
mod tests {
use super::SyncArgs;
Expand Down
4 changes: 2 additions & 2 deletions src/core/adopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use uuid::Uuid;

use crate::core::branch;
use crate::core::git;
use crate::core::restack::RestackAction;
use crate::core::restack::{RestackAction, RestackBaseTarget};
use crate::core::store::{
BranchNode, ParentRef, PendingAdoptOperation, PendingOperationKind, PendingOperationState,
now_unix_timestamp_secs, open_initialized, open_or_initialize, record_branch_adopted,
Expand Down Expand Up @@ -149,7 +149,7 @@ pub fn apply(plan: &AdoptPlan) -> io::Result<AdoptOutcome> {
branch_name: plan.branch_name.clone(),
old_upstream_branch_name: plan.parent_branch_name.clone(),
old_upstream_oid: plan.old_upstream_oid.clone(),
new_base_branch_name: plan.parent_branch_name.clone(),
new_base: RestackBaseTarget::local(plan.parent_branch_name.clone()),
new_parent: None,
}],
&mut |_| Ok(()),
Expand Down
27 changes: 10 additions & 17 deletions src/core/clean/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,21 +306,12 @@ where
&node.branch_name,
)?,
)?,
PendingCleanCandidateKind::IntegratedIntoParent => {
let Some(parent_branch_name) = BranchGraph::new(&session.state)
.parent_branch_name(&node, &session.config.trunk_branch)
else {
return Err(io::Error::other(format!(
"tracked parent for '{}' is missing from dig",
node.branch_name
)));
};

PendingCleanCandidateKind::IntegratedIntoParent { ref parent_base } => {
restack::plan_after_branch_detach(
&session.state,
node.id,
&node.branch_name,
&parent_branch_name,
parent_base,
&node.parent,
)?
}
Expand All @@ -341,19 +332,19 @@ where
&mut |event| match event {
RestackExecutionEvent::Started(action) => reporter(CleanEvent::RebaseStarted {
branch_name: action.branch_name.clone(),
onto_branch: action.new_base_branch_name.clone(),
onto_branch: action.new_base.branch_name.clone(),
}),
RestackExecutionEvent::Progress { action, progress } => {
reporter(CleanEvent::RebaseProgress {
branch_name: action.branch_name.clone(),
onto_branch: action.new_base_branch_name.clone(),
onto_branch: action.new_base.branch_name.clone(),
current_commit: progress.current,
total_commits: progress.total,
})
}
RestackExecutionEvent::Completed(action) => reporter(CleanEvent::RebaseCompleted {
branch_name: action.branch_name.clone(),
onto_branch: action.new_base_branch_name.clone(),
onto_branch: action.new_base.branch_name.clone(),
}),
},
)?;
Expand Down Expand Up @@ -411,7 +402,7 @@ where
untracked_branches.push(candidate.branch_name.clone());
git::success_status()
}
PendingCleanCandidateKind::IntegratedIntoParent => {
PendingCleanCandidateKind::IntegratedIntoParent { .. } => {
let status = delete_clean_candidate(session, &candidate.branch_name, reporter)?;
if status.success() {
deleted_branches.push(candidate.branch_name.clone());
Expand All @@ -428,8 +419,10 @@ fn pending_clean_candidate_from_clean_candidate(
branch_name: candidate.branch_name.clone(),
kind: match &candidate.reason {
CleanReason::DeletedLocally => PendingCleanCandidateKind::DeletedLocally,
CleanReason::IntegratedIntoParent { .. } => {
PendingCleanCandidateKind::IntegratedIntoParent
CleanReason::IntegratedIntoParent { parent_base } => {
PendingCleanCandidateKind::IntegratedIntoParent {
parent_base: parent_base.clone(),
}
}
},
}
Expand Down
5 changes: 4 additions & 1 deletion src/core/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ mod plan;
mod types;

pub(crate) use apply::{apply, apply_with_reporter, resume_after_sync};
pub(crate) use plan::{branch_is_integrated, plan};
pub(crate) use plan::{
CleanPlanMode, branch_is_integrated, cleanup_candidate_for_branch, mode_for_sync, plan,
plan_for_sync,
};
pub(crate) use types::{
BlockedBranch, CleanApplyOutcome, CleanBlockReason, CleanCandidate, CleanEvent, CleanOptions,
CleanPlan, CleanReason, CleanTreeNode,
Expand Down
Loading