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] ## [Unreleased]
### Changed ### Changed
- make `status` subcommand more ergonomic - make `status` subcommand more ergonomic
- refactor `branch` subcommand into `checkout`
## [v0.1.0] - 2022-07-17 ## [v0.1.0] - 2022-07-17

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

@ -230,20 +230,37 @@ impl Vcs {
return Ok(()); return Ok(());
} }
self.set_projects_using(profile_name, |project| { self.set_projects_using(profile_name, None, |project| {
project.switch_to_profile(profile_name) project.switch_to_profile(profile_name)
}) })
} }
pub fn set_branch(&mut self, branch_name: &str) -> Result<(), Error> { pub fn set_branch(
self.set_projects_using(branch_name, |project| project.repo.checkout(branch_name)) &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 where
F: Fn(&mut Project) -> Result<String, Error>, F: Fn(&mut Project) -> Result<String, Error>,
{ {
for project in &mut self.projects { 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()?; let current_ref = project.repo.current_ref()?;
// stash changes before moving project off to a new ref // stash changes before moving project off to a new ref
let stashed_changes = project.repo.stash_changes(&current_ref)?; let stashed_changes = project.repo.stash_changes(&current_ref)?;

@ -9,8 +9,8 @@ pub struct Cli {
#[clap(subcommand)] #[clap(subcommand)]
pub command: Commands, pub command: Commands,
/// Path to workspace folder, defaults to CWD /// Path to workspace folder, defaults to CWD
#[clap(short = 'p')] #[clap(short = 'w')]
pub path: Option<PathBuf>, pub workspace: Option<PathBuf>,
} }
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
@ -45,5 +45,10 @@ pub enum ProfileCommand {
pub enum BranchCommand { pub enum BranchCommand {
/// Switch all projects in workspace to given branch /// Switch all projects in workspace to given branch
#[clap(arg_required_else_help = true)] #[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