start refactoring `branch` subcommand into `checkout`

master
Aleš Katona 2 years ago
parent daf5c7170e
commit ea94a69502
Signed by: almindor
GPG Key ID: 2F773149BF38B48F

@ -8,8 +8,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased]
### Changed
- make `status` subcommand more ergonomic
- refactor `branch` subcommand into `checkout`
## [v0.1.0] - 2022-07-17

@ -12,7 +12,7 @@ fn main() {
let args = parse_args();
let work_dir = args
.path
.workspace
.unwrap_or_else(|| std::env::current_dir().expect("Unable to obtain current_dir"));
let mut vcs = match vcs::Vcs::new(work_dir) {
Ok(val) => val,
@ -23,8 +23,8 @@ fn main() {
};
match args.command {
Commands::Branch(BranchCommand::Set { branch }) => {
vcs.set_branch(&branch)
Commands::Branch(BranchCommand::Set { branch, projects }) => {
vcs.set_branch(&branch, projects)
.expect("Error setting branch on projects");
}
Commands::Profile(ProfileCommand::Save { profile }) => {

@ -230,20 +230,37 @@ impl Vcs {
return Ok(());
}
self.set_projects_using(profile_name, |project| {
self.set_projects_using(profile_name, None, |project| {
project.switch_to_profile(profile_name)
})
}
pub fn set_branch(&mut self, branch_name: &str) -> Result<(), Error> {
self.set_projects_using(branch_name, |project| project.repo.checkout(branch_name))
pub fn set_branch(
&mut self,
branch_name: &str,
projects: Option<Vec<String>>,
) -> Result<(), Error> {
self.set_projects_using(branch_name, projects, |project| {
project.repo.checkout(branch_name)
})
}
fn set_projects_using<F>(&mut self, dest_name: &str, setter: F) -> Result<(), Error>
fn set_projects_using<F>(
&mut self,
dest_name: &str,
projects: Option<Vec<String>>,
setter: F,
) -> Result<(), Error>
where
F: Fn(&mut Project) -> Result<String, Error>,
{
for project in &mut self.projects {
if let Some(only_projects) = &projects {
if !only_projects.iter().any(|p| p == project.name()) {
continue;
}
}
let current_ref = project.repo.current_ref()?;
// stash changes before moving project off to a new ref
let stashed_changes = project.repo.stash_changes(&current_ref)?;

@ -9,8 +9,8 @@ pub struct Cli {
#[clap(subcommand)]
pub command: Commands,
/// Path to workspace folder, defaults to CWD
#[clap(short = 'p')]
pub path: Option<PathBuf>,
#[clap(short = 'w')]
pub workspace: Option<PathBuf>,
}
#[derive(Debug, Parser)]
@ -45,5 +45,10 @@ pub enum ProfileCommand {
pub enum BranchCommand {
/// Switch all projects in workspace to given branch
#[clap(arg_required_else_help = true)]
Set { branch: String },
Set {
branch: String,
/// Project list, do any operation only on projects in this list (*comma separated*)
#[clap(short = 'p')]
projects: Option<Vec<String>>,
},
}

Loading…
Cancel
Save