From 9cd3e43b899b23b7f6e75276fa3d19bf9550f8fd Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Fri, 9 Feb 2024 07:21:00 +0000 Subject: refactor(git): find root commit from head this is simpler there is no need to check whether main or master exist because one does 99+% of the time the root commit wil be te same for head as master 99+% of the time --- src/git.rs | 10 ++++------ src/sub_commands/claim.rs | 6 +----- src/sub_commands/prs/create.rs | 16 ++++------------ src/sub_commands/prs/list.rs | 6 +----- src/sub_commands/pull.rs | 2 +- src/sub_commands/push.rs | 2 +- tests/claim.rs | 9 --------- 7 files changed, 12 insertions(+), 39 deletions(-) diff --git a/src/git.rs b/src/git.rs index 6e6091d..067ce24 100644 --- a/src/git.rs +++ b/src/git.rs @@ -36,7 +36,7 @@ pub trait RepoActions { fn get_main_or_master_branch(&self) -> Result<(&str, Sha1Hash)>; fn get_checked_out_branch_name(&self) -> Result; fn get_tip_of_local_branch(&self, branch_name: &str) -> Result; - fn get_root_commit(&self, branch_name: &str) -> Result; + fn get_root_commit(&self) -> Result; fn does_commit_exist(&self, commit: &str) -> Result; fn get_head_commit(&self) -> Result; fn get_commit_parent(&self, commit: &Sha1Hash) -> Result; @@ -144,14 +144,13 @@ impl RepoActions for Repo { Ok(oid_to_sha1(&branch.into_reference().peel_to_commit()?.id())) } - fn get_root_commit(&self, branch_name: &str) -> Result { - let tip = self.get_tip_of_local_branch(branch_name)?; + fn get_root_commit(&self) -> Result { let mut revwalk = self .git_repo .revwalk() .context("revwalk should be created from git repo")?; revwalk - .push(sha1_to_oid(&tip)?) + .push(sha1_to_oid(&self.get_head_commit()?)?) .context("revwalk should accept tip oid")?; Ok(oid_to_sha1( &revwalk @@ -1259,7 +1258,7 @@ mod tests { let git_repo = Repo::from_path(&test_repo.dir)?; generate_patch_event( &git_repo, - &git_repo.get_root_commit("main")?, + &git_repo.get_root_commit()?, &oid_to_sha1(&original_oid), nostr::EventId::all_zeros(), &TEST_KEY_1_KEYS, @@ -1421,7 +1420,6 @@ mod tests { let mut events = generate_pr_and_patch_events( "title", "description", - BRANCH_NAME, &git_repo, &vec![oid_to_sha1(&oid1), oid_to_sha1(&oid2), oid_to_sha1(&oid3)], &TEST_KEY_1_KEYS, diff --git a/src/sub_commands/claim.rs b/src/sub_commands/claim.rs index 05ad961..d3a80c8 100644 --- a/src/sub_commands/claim.rs +++ b/src/sub_commands/claim.rs @@ -33,12 +33,8 @@ pub struct SubCommandArgs { pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { let git_repo = Repo::discover().context("cannot find a git repository")?; - let (main_or_master_branch_name, _) = git_repo - .get_main_or_master_branch() - .context("no main or master branch")?; - let root_commit = git_repo - .get_root_commit(main_or_master_branch_name) + .get_root_commit() .context("failed to get root commit of the repository")?; // TODO: check for empty repo diff --git a/src/sub_commands/prs/create.rs b/src/sub_commands/prs/create.rs index 82824a1..8506303 100644 --- a/src/sub_commands/prs/create.rs +++ b/src/sub_commands/prs/create.rs @@ -103,7 +103,7 @@ pub async fn launch( let repo_ref = repo_ref::fetch( &git_repo, git_repo - .get_root_commit(&to_branch) + .get_root_commit() .context("failed to get root commit of the repository")? .to_string(), &client, @@ -111,15 +111,8 @@ pub async fn launch( ) .await?; - let events = generate_pr_and_patch_events( - &title, - &description, - &to_branch, - &git_repo, - &ahead, - &keys, - &repo_ref, - )?; + let events = + generate_pr_and_patch_events(&title, &description, &git_repo, &ahead, &keys, &repo_ref)?; println!( "posting 1 pull request with {} commits...", @@ -315,14 +308,13 @@ pub static PATCH_KIND: u64 = 1617; pub fn generate_pr_and_patch_events( title: &str, description: &str, - to_branch: &str, git_repo: &Repo, commits: &Vec, keys: &nostr::Keys, repo_ref: &RepoRef, ) -> Result> { let root_commit = git_repo - .get_root_commit(to_branch) + .get_root_commit() .context("failed to get root commit of the repository")?; let mut pr_tags = vec![ diff --git a/src/sub_commands/prs/list.rs b/src/sub_commands/prs/list.rs index 96004d4..88b325b 100644 --- a/src/sub_commands/prs/list.rs +++ b/src/sub_commands/prs/list.rs @@ -27,12 +27,8 @@ pub async fn launch( ) -> Result<()> { let git_repo = Repo::discover().context("cannot find a git repository")?; - let (main_or_master_branch_name, _) = git_repo - .get_main_or_master_branch() - .context("no main or master branch")?; - let root_commit = git_repo - .get_root_commit(main_or_master_branch_name) + .get_root_commit() .context("failed to get root commit of the repository")?; // TODO: check for empty repo diff --git a/src/sub_commands/pull.rs b/src/sub_commands/pull.rs index 342c8bb..c426510 100644 --- a/src/sub_commands/pull.rs +++ b/src/sub_commands/pull.rs @@ -22,7 +22,7 @@ pub async fn launch() -> Result<()> { .context("no main or master branch")?; let root_commit = git_repo - .get_root_commit(main_or_master_branch_name) + .get_root_commit() .context("failed to get root commit of the repository")?; let branch_name = git_repo diff --git a/src/sub_commands/push.rs b/src/sub_commands/push.rs index de2a92f..037d512 100644 --- a/src/sub_commands/push.rs +++ b/src/sub_commands/push.rs @@ -25,7 +25,7 @@ pub async fn launch(cli_args: &Cli) -> Result<()> { .context("no main or master branch")?; let root_commit = git_repo - .get_root_commit(main_or_master_branch_name) + .get_root_commit() .context("failed to get root commit of the repository")?; let branch_name = git_repo diff --git a/tests/claim.rs b/tests/claim.rs index ef669d0..fa7adcc 100644 --- a/tests/claim.rs +++ b/tests/claim.rs @@ -2,15 +2,6 @@ use anyhow::Result; use serial_test::serial; use test_utils::{git::GitTestRepo, *}; -#[test] -fn when_no_main_or_master_branch_return_error() -> Result<()> { - let test_repo = GitTestRepo::new("notmain")?; - test_repo.populate()?; - let mut p = CliTester::new_from_dir(&test_repo.dir, ["claim"]); - p.expect("Error: no main or master branch")?; - Ok(()) -} - fn expect_msgs_first(p: &mut CliTester) -> Result<()> { p.expect("searching for your details...\r\n")?; p.expect("\r")?; -- cgit v1.2.3