upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDanConwayDev <DanConwayDev@protonmail.com>2024-02-09 07:21:00 +0000
committerDanConwayDev <DanConwayDev@protonmail.com>2024-02-09 07:21:00 +0000
commit9cd3e43b899b23b7f6e75276fa3d19bf9550f8fd (patch)
treef88539e7988e13a7fdd9736a6a672048ee00e59a
parent4b0a35583644e703eb615e0724d33fe93aec932b (diff)
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
-rw-r--r--src/git.rs10
-rw-r--r--src/sub_commands/claim.rs6
-rw-r--r--src/sub_commands/prs/create.rs16
-rw-r--r--src/sub_commands/prs/list.rs6
-rw-r--r--src/sub_commands/pull.rs2
-rw-r--r--src/sub_commands/push.rs2
-rw-r--r--tests/claim.rs9
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 {
36 fn get_main_or_master_branch(&self) -> Result<(&str, Sha1Hash)>; 36 fn get_main_or_master_branch(&self) -> Result<(&str, Sha1Hash)>;
37 fn get_checked_out_branch_name(&self) -> Result<String>; 37 fn get_checked_out_branch_name(&self) -> Result<String>;
38 fn get_tip_of_local_branch(&self, branch_name: &str) -> Result<Sha1Hash>; 38 fn get_tip_of_local_branch(&self, branch_name: &str) -> Result<Sha1Hash>;
39 fn get_root_commit(&self, branch_name: &str) -> Result<Sha1Hash>; 39 fn get_root_commit(&self) -> Result<Sha1Hash>;
40 fn does_commit_exist(&self, commit: &str) -> Result<bool>; 40 fn does_commit_exist(&self, commit: &str) -> Result<bool>;
41 fn get_head_commit(&self) -> Result<Sha1Hash>; 41 fn get_head_commit(&self) -> Result<Sha1Hash>;
42 fn get_commit_parent(&self, commit: &Sha1Hash) -> Result<Sha1Hash>; 42 fn get_commit_parent(&self, commit: &Sha1Hash) -> Result<Sha1Hash>;
@@ -144,14 +144,13 @@ impl RepoActions for Repo {
144 Ok(oid_to_sha1(&branch.into_reference().peel_to_commit()?.id())) 144 Ok(oid_to_sha1(&branch.into_reference().peel_to_commit()?.id()))
145 } 145 }
146 146
147 fn get_root_commit(&self, branch_name: &str) -> Result<Sha1Hash> { 147 fn get_root_commit(&self) -> Result<Sha1Hash> {
148 let tip = self.get_tip_of_local_branch(branch_name)?;
149 let mut revwalk = self 148 let mut revwalk = self
150 .git_repo 149 .git_repo
151 .revwalk() 150 .revwalk()
152 .context("revwalk should be created from git repo")?; 151 .context("revwalk should be created from git repo")?;
153 revwalk 152 revwalk
154 .push(sha1_to_oid(&tip)?) 153 .push(sha1_to_oid(&self.get_head_commit()?)?)
155 .context("revwalk should accept tip oid")?; 154 .context("revwalk should accept tip oid")?;
156 Ok(oid_to_sha1( 155 Ok(oid_to_sha1(
157 &revwalk 156 &revwalk
@@ -1259,7 +1258,7 @@ mod tests {
1259 let git_repo = Repo::from_path(&test_repo.dir)?; 1258 let git_repo = Repo::from_path(&test_repo.dir)?;
1260 generate_patch_event( 1259 generate_patch_event(
1261 &git_repo, 1260 &git_repo,
1262 &git_repo.get_root_commit("main")?, 1261 &git_repo.get_root_commit()?,
1263 &oid_to_sha1(&original_oid), 1262 &oid_to_sha1(&original_oid),
1264 nostr::EventId::all_zeros(), 1263 nostr::EventId::all_zeros(),
1265 &TEST_KEY_1_KEYS, 1264 &TEST_KEY_1_KEYS,
@@ -1421,7 +1420,6 @@ mod tests {
1421 let mut events = generate_pr_and_patch_events( 1420 let mut events = generate_pr_and_patch_events(
1422 "title", 1421 "title",
1423 "description", 1422 "description",
1424 BRANCH_NAME,
1425 &git_repo, 1423 &git_repo,
1426 &vec![oid_to_sha1(&oid1), oid_to_sha1(&oid2), oid_to_sha1(&oid3)], 1424 &vec![oid_to_sha1(&oid1), oid_to_sha1(&oid2), oid_to_sha1(&oid3)],
1427 &TEST_KEY_1_KEYS, 1425 &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 {
33pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { 33pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> {
34 let git_repo = Repo::discover().context("cannot find a git repository")?; 34 let git_repo = Repo::discover().context("cannot find a git repository")?;
35 35
36 let (main_or_master_branch_name, _) = git_repo
37 .get_main_or_master_branch()
38 .context("no main or master branch")?;
39
40 let root_commit = git_repo 36 let root_commit = git_repo
41 .get_root_commit(main_or_master_branch_name) 37 .get_root_commit()
42 .context("failed to get root commit of the repository")?; 38 .context("failed to get root commit of the repository")?;
43 39
44 // TODO: check for empty repo 40 // 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(
103 let repo_ref = repo_ref::fetch( 103 let repo_ref = repo_ref::fetch(
104 &git_repo, 104 &git_repo,
105 git_repo 105 git_repo
106 .get_root_commit(&to_branch) 106 .get_root_commit()
107 .context("failed to get root commit of the repository")? 107 .context("failed to get root commit of the repository")?
108 .to_string(), 108 .to_string(),
109 &client, 109 &client,
@@ -111,15 +111,8 @@ pub async fn launch(
111 ) 111 )
112 .await?; 112 .await?;
113 113
114 let events = generate_pr_and_patch_events( 114 let events =
115 &title, 115 generate_pr_and_patch_events(&title, &description, &git_repo, &ahead, &keys, &repo_ref)?;
116 &description,
117 &to_branch,
118 &git_repo,
119 &ahead,
120 &keys,
121 &repo_ref,
122 )?;
123 116
124 println!( 117 println!(
125 "posting 1 pull request with {} commits...", 118 "posting 1 pull request with {} commits...",
@@ -315,14 +308,13 @@ pub static PATCH_KIND: u64 = 1617;
315pub fn generate_pr_and_patch_events( 308pub fn generate_pr_and_patch_events(
316 title: &str, 309 title: &str,
317 description: &str, 310 description: &str,
318 to_branch: &str,
319 git_repo: &Repo, 311 git_repo: &Repo,
320 commits: &Vec<Sha1Hash>, 312 commits: &Vec<Sha1Hash>,
321 keys: &nostr::Keys, 313 keys: &nostr::Keys,
322 repo_ref: &RepoRef, 314 repo_ref: &RepoRef,
323) -> Result<Vec<nostr::Event>> { 315) -> Result<Vec<nostr::Event>> {
324 let root_commit = git_repo 316 let root_commit = git_repo
325 .get_root_commit(to_branch) 317 .get_root_commit()
326 .context("failed to get root commit of the repository")?; 318 .context("failed to get root commit of the repository")?;
327 319
328 let mut pr_tags = vec![ 320 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(
27) -> Result<()> { 27) -> Result<()> {
28 let git_repo = Repo::discover().context("cannot find a git repository")?; 28 let git_repo = Repo::discover().context("cannot find a git repository")?;
29 29
30 let (main_or_master_branch_name, _) = git_repo
31 .get_main_or_master_branch()
32 .context("no main or master branch")?;
33
34 let root_commit = git_repo 30 let root_commit = git_repo
35 .get_root_commit(main_or_master_branch_name) 31 .get_root_commit()
36 .context("failed to get root commit of the repository")?; 32 .context("failed to get root commit of the repository")?;
37 33
38 // TODO: check for empty repo 34 // 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<()> {
22 .context("no main or master branch")?; 22 .context("no main or master branch")?;
23 23
24 let root_commit = git_repo 24 let root_commit = git_repo
25 .get_root_commit(main_or_master_branch_name) 25 .get_root_commit()
26 .context("failed to get root commit of the repository")?; 26 .context("failed to get root commit of the repository")?;
27 27
28 let branch_name = git_repo 28 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<()> {
25 .context("no main or master branch")?; 25 .context("no main or master branch")?;
26 26
27 let root_commit = git_repo 27 let root_commit = git_repo
28 .get_root_commit(main_or_master_branch_name) 28 .get_root_commit()
29 .context("failed to get root commit of the repository")?; 29 .context("failed to get root commit of the repository")?;
30 30
31 let branch_name = git_repo 31 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;
2use serial_test::serial; 2use serial_test::serial;
3use test_utils::{git::GitTestRepo, *}; 3use test_utils::{git::GitTestRepo, *};
4 4
5#[test]
6fn when_no_main_or_master_branch_return_error() -> Result<()> {
7 let test_repo = GitTestRepo::new("notmain")?;
8 test_repo.populate()?;
9 let mut p = CliTester::new_from_dir(&test_repo.dir, ["claim"]);
10 p.expect("Error: no main or master branch")?;
11 Ok(())
12}
13
14fn expect_msgs_first(p: &mut CliTester) -> Result<()> { 5fn expect_msgs_first(p: &mut CliTester) -> Result<()> {
15 p.expect("searching for your details...\r\n")?; 6 p.expect("searching for your details...\r\n")?;
16 p.expect("\r")?; 7 p.expect("\r")?;