Add rebase option for worktrees

This commit is contained in:
2021-12-29 19:02:42 +01:00
parent 7a2fa7ae3f
commit ef8a57c60e
5 changed files with 445 additions and 1 deletions

View File

@@ -91,6 +91,8 @@ pub enum WorktreeAction {
Fetch(WorktreeFetchArgs),
#[clap(about = "Fetch refs from remotes and update local branches")]
Pull(WorktreePullArgs),
#[clap(about = "Rebase worktree onto default branch")]
Rebase(WorktreeRebaseArgs),
}
#[derive(Parser)]
@@ -137,6 +139,14 @@ pub struct WorktreePullArgs {
pub rebase: bool,
}
#[derive(Parser)]
pub struct WorktreeRebaseArgs {
#[clap(long = "--pull", about = "Perform a pull before rebasing")]
pub pull: bool,
#[clap(long = "--rebase", about = "Perform a rebase when doing a pull")]
pub rebase: bool,
}
pub fn parse() -> Opts {
Opts::parse()
}

View File

@@ -362,6 +362,76 @@ fn main() {
}
}
}
cmd::WorktreeAction::Rebase(args) => {
if args.rebase && !args.pull {
print_error("There is no point in using --rebase without --pull");
process::exit(1);
}
let repo = grm::Repo::open(&cwd, true).unwrap_or_else(|error| {
if error.kind == grm::RepoErrorKind::NotFound {
print_error("Directory does not contain a git repository");
} else {
print_error(&format!("Opening repository failed: {}", error));
}
process::exit(1);
});
if args.pull {
repo.fetchall().unwrap_or_else(|error| {
print_error(&format!("Error fetching remotes: {}", error));
process::exit(1);
});
}
let config =
grm::repo::read_worktree_root_config(&cwd).unwrap_or_else(|error| {
print_error(&format!(
"Failed to read worktree configuration: {}",
error
));
process::exit(1);
});
let worktrees = repo.get_worktrees().unwrap_or_else(|error| {
print_error(&format!("Error getting worktrees: {}", error));
process::exit(1);
});
for worktree in &worktrees {
if args.pull {
if let Some(warning) = worktree
.forward_branch(args.rebase)
.unwrap_or_else(|error| {
print_error(&format!(
"Error updating worktree branch: {}",
error
));
process::exit(1);
})
{
print_warning(&format!("{}: {}", worktree.name(), warning));
}
}
}
for worktree in &worktrees {
if let Some(warning) =
worktree
.rebase_onto_default(&config)
.unwrap_or_else(|error| {
print_error(&format!(
"Error rebasing worktree branch: {}",
error
));
process::exit(1);
})
{
print_warning(&format!("{}: {}", worktree.name(), warning));
} else {
print_success(&format!("{}: Done", worktree.name()));
}
}
}
}
}
}