Skip to content

Commit

Permalink
Merge pull request #235 from SierraSoftworks/feat/improved-switch
Browse files Browse the repository at this point in the history
fix: Ensure that we get the correct list of branches for gt switch
  • Loading branch information
notheotherben authored Mar 14, 2021
2 parents 9cfbcf5 + 23ead45 commit da1dd87
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 4 deletions.
117 changes: 117 additions & 0 deletions src/commands/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,123 @@ mod tests {
);
}

#[tokio::test]
async fn switch_branch_exists_remote_only() {
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 origin_repo = core::Repo::new(
"github.com/sierrasoftworks/test-git-switch-command2",
temp.path().join("repo2").into(),
);

let core = core::Core::builder()
.with_config(&core::Config::for_dev_directory(temp.path()))
.build();

Resolver::get_current_repo.mock_safe(move |_| {
MockResult::Return(Ok(core::Repo::new(
"github.com/sierrasoftworks/test-git-switch-command",
temp.path().join("repo").into(),
)))
});

sequence!(
// Run a `git init` to setup the repo
tasks::GitInit {},
tasks::GitRemote { name: "origin" },
// 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, &origin_repo)
.await
.unwrap();

sequence!(tasks::GitInit {}, tasks::GitRemote { name: "origin" })
.apply_repo(&core, &repo)
.await
.unwrap();

git::git_remote_set_url(
&repo.get_path(),
"origin",
origin_repo.get_path().to_str().unwrap(),
)
.await
.unwrap();

git::git_fetch(&repo.get_path(), "origin").await.unwrap();

sequence!(
tasks::GitCheckout {
branch: "main".into(),
},
tasks::WriteFile {
path: "README.md".into(),
content: "This is an example README file with some changes.",
},
tasks::GitAdd {
paths: vec!["README.md"],
},
tasks::GitCommit {
message: "Update README.md",
paths: vec!["README.md"],
}
)
.apply_repo(&core, &repo)
.await
.unwrap();

eprintln!(
"branches: {:?}",
git::git_branches(&repo.get_path()).await.unwrap()
);

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

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

assert!(repo.valid(), "the repository should still be valid");
assert_eq!(
git::git_current_branch(&repo.get_path()).await.unwrap(),
"feature/test"
);

let target_ref = git::git_rev_parse(&repo.get_path(), "origin/feature/test")
.await
.unwrap();
let true_ref = git::git_rev_parse(&repo.get_path(), "feature/test")
.await
.unwrap();

assert_eq!(
true_ref, target_ref,
"the current branch should be pointing at the original origin ref"
);
}

#[tokio::test]
async fn switch_no_create_branch_exists() {
let cmd: SwitchCommand = SwitchCommand {};
Expand Down
6 changes: 3 additions & 3 deletions src/git/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ pub async fn git_branches(repo: &path::Path) -> Result<Vec<String>, errors::Erro
let output = git_cmd(
Command::new("git")
.current_dir(repo)
.arg("for-each-ref")
.arg("--format=%(refname:lstrip=2)")
.arg("refs/heads/"),
.arg("branch")
.arg("-a")
.arg("--format=%(refname:lstrip=2)"),
)
.await?;

Expand Down
18 changes: 18 additions & 0 deletions src/git/fetch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use super::git_cmd;
use crate::errors;
use std::path;
use tokio::process::Command;

#[allow(dead_code)]
pub async fn git_fetch(repo: &path::Path, origin: &str) -> Result<(), errors::Error> {
info!("Running `git fetch $ORIGIN`");
git_cmd(
Command::new("git")
.current_dir(repo)
.arg("fetch")
.arg(origin),
)
.await?;

Ok(())
}
2 changes: 2 additions & 0 deletions src/git/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod checkout;
mod clone;
mod cmd;
mod commit;
mod fetch;
mod init;
mod remote;
mod switch;
Expand All @@ -17,6 +18,7 @@ pub use checkout::git_checkout;
pub use clone::git_clone;
pub use cmd::git_cmd;
pub use commit::git_commit;
pub use fetch::git_fetch;
pub use init::git_init;
pub use remote::{git_remote_add, git_remote_list, git_remote_set_url};
pub use switch::git_switch;
Expand Down
3 changes: 2 additions & 1 deletion src/tasks/git_switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ impl Task for GitSwitch {
if create
&& git::git_branches(&repo.get_path())
.await?
.contains(&self.branch)
.iter()
.any(|v| v == &self.branch || v == &format!("origin/{}", &self.branch))
{
create = false;
}
Expand Down

0 comments on commit da1dd87

Please sign in to comment.