From 784aee17d37d5f8e092efcc9ce8499d939d9ce6a Mon Sep 17 00:00:00 2001 From: sgoudham Date: Sun, 19 Jun 2022 21:08:25 +0100 Subject: [PATCH] refactor: Implement functionality to retrieve git default branch --- src/git.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/git.rs b/src/git.rs index fd1ae33..16d0422 100644 --- a/src/git.rs +++ b/src/git.rs @@ -11,6 +11,7 @@ pub(crate) enum GitCommand<'a> { DefaultRemote, TrackedRemote(&'a str), UpstreamBranch(&'a str), + DefaultBranch(&'a str), IsValidRemote(&'a str), CurrentTag, CurrentCommit, @@ -44,6 +45,7 @@ pub trait GitTrait { fn get_default_remote(&self) -> Result; fn get_tracked_remote(&self, tracked: &str) -> Result; fn get_upstream_branch(&self, branch: &str) -> Result; + fn get_default_branch(&self, remote: &str) -> Result; fn is_valid_remote(&self, remote: &str) -> Result; fn get_current_tag(&self) -> Result; fn get_current_commit(&self) -> Result; @@ -70,6 +72,10 @@ impl GitTrait for Git { execute(command(GitCommand::UpstreamBranch(branch))?) } + fn get_default_branch(&self, remote: &str) -> Result { + execute(command(GitCommand::DefaultBranch(remote))?) + } + fn is_valid_remote(&self, remote: &str) -> Result { execute(command(GitCommand::IsValidRemote(remote))?) } @@ -107,6 +113,11 @@ fn command(git_command: GitCommand) -> Result { .arg("config") .arg(format!("branch.{}.merge", branch)) .output(), + GitCommand::DefaultBranch(remote) => Command::new("git") + .arg("rev-parse") + .arg("--abbrev-ref") + .arg(format!("{}/HEAD", remote)) + .output(), GitCommand::IsValidRemote(remote) => Command::new("git") .arg("ls-remote") .arg("--get-url")