Skip to content

Commit

Permalink
Merge pull request #124 from mang0kitty/tweak/switch-create-default
Browse files Browse the repository at this point in the history
tweak: update git switch to create the branch if it doesn't exist by …
  • Loading branch information
notheotherben authored Oct 17, 2020
2 parents 2654841 + 5a1cd83 commit d3ce226
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 12 deletions.
100 changes: 90 additions & 10 deletions src/commands/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ impl Command for SwitchCommand {
.index(1),
)
.arg(
Arg::new("create")
.short('c')
.long("create")
.about("creates a new branch before switching to it."),
Arg::new("no-create")
.short('N')
.long("no-create")
.about("don't create the branch if it doesn't exist."),
)
}

}

#[async_trait]
Expand All @@ -42,7 +43,7 @@ impl<C: Core> CommandRunnable<C> for SwitchCommand {
Some(branch) => {
let task = tasks::GitSwitch {
branch: branch.to_string(),
create_if_missing: matches.is_present("create"),
create_if_missing: !matches.is_present("no-create"),
};

task.apply_repo(core, &repo).await?;
Expand Down Expand Up @@ -129,7 +130,7 @@ mod tests {
}

#[tokio::test]
async fn switch_branch_not_exists() {
async fn switch_no_create_branch_exists() {
let cmd: SwitchCommand = SwitchCommand {};

let temp = tempdir().unwrap();
Expand All @@ -144,18 +145,97 @@ mod tests {
.build();

// Run a `git init` to setup the repo
tasks::GitInit {}.apply_repo(&core, &repo).await.unwrap();
sequence!(
// Run a `git init` to setup the repo
tasks::GitInit {},
// Create the branch we want to switch to
tasks::GitCheckout {
branch: "feature/test".into(),
},
tasks::WriteFile {
path: "README.md".into(),
content: "This is an example README file.",
},
tasks::GitAdd {
paths: vec!["README.md"],
},
tasks::GitCommit {
message: "Add README.md",
paths: vec!["README.md"],
},
tasks::GitCheckout {
branch: "main".into(),
}
)
.apply_repo(&core, &repo)
.await
.unwrap();

assert!(repo.valid(), "the repository should exist and be valid");

let args: ArgMatches = cmd.app().get_matches_from(vec!["switch", "feature/test"]);
let args: ArgMatches = cmd.app().get_matches_from(vec!["switch","-N", "feature/test"]);
cmd.run(&core, &args)
.await
.expect("this command should have succeeded");

assert_eq!(
git::git_current_branch(&repo.get_path()).await.unwrap(),
"feature/test"
);
}

#[tokio::test]
async fn switch_no_create() {
let cmd: SwitchCommand = SwitchCommand {};

let temp = tempdir().unwrap();
let repo: Repo = core::Repo::new(
"github.com/sierrasoftworks/test-git-switch-command",
temp.path().join("repo").into(),
);

let core = core::CoreBuilder::default()
.with_config(&core::Config::for_dev_directory(temp.path()))
.with_mock_resolver(|r| r.set_repo(repo.clone()))
.build();

// Run a `git init` to setup the repo
sequence!(
// Run a `git init` to setup the repo
tasks::GitInit {},
// Create the branch we want to switch to
tasks::GitCheckout {
branch: "feature/test".into(),
},
tasks::WriteFile {
path: "README.md".into(),
content: "This is an example README file.",
},
tasks::GitAdd {
paths: vec!["README.md"],
},
tasks::GitCommit {
message: "Add README.md",
paths: vec!["README.md"],
},
tasks::GitCheckout {
branch: "main".into(),
}
)
.apply_repo(&core, &repo)
.await
.unwrap();

assert!(repo.valid(), "the repository should exist and be valid");

let args: ArgMatches = cmd.app().get_matches_from(vec!["switch","-N", "feature/test2"]);
cmd.run(&core, &args)
.await
.expect_err("this command should not have succeeded");
}

#[tokio::test]
async fn switch_create_branch() {
async fn switch_branch_not_exists() {
let cmd: SwitchCommand = SwitchCommand {};

let temp = tempdir().unwrap();
Expand All @@ -176,7 +256,7 @@ mod tests {

let args: ArgMatches = cmd
.app()
.get_matches_from(vec!["switch", "-c", "feature/test"]);
.get_matches_from(vec!["switch", "feature/test"]);
cmd.run(&core, &args).await.unwrap();

assert!(repo.valid(), "the repository should still be valid");
Expand Down
2 changes: 1 addition & 1 deletion src/git/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub async fn git_switch(repo: &path::Path, name: &str, create: bool) -> Result<(
Command::new("git")
.current_dir(repo)
.arg("switch")
.arg("-c")
.arg("--create")
.arg(name),
)
.await?;
Expand Down
8 changes: 7 additions & 1 deletion src/tasks/git_switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ pub struct GitSwitch {
#[async_trait::async_trait]
impl<C: Core> Task<C> for GitSwitch {
async fn apply_repo(&self, _core: &C, repo: &core::Repo) -> Result<(), core::Error> {
git::git_switch(&repo.get_path(), &self.branch, self.create_if_missing).await
let mut create = self.create_if_missing;

if create && git::git_branches(&repo.get_path()).await?.contains(&self.branch) {
create = false;
}

git::git_switch(&repo.get_path(), &self.branch, create).await
}

async fn apply_scratchpad(
Expand Down

0 comments on commit d3ce226

Please sign in to comment.