diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2024-02-09 07:21:00 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2024-02-09 07:21:00 +0000 |
| commit | 9cd3e43b899b23b7f6e75276fa3d19bf9550f8fd (patch) | |
| tree | f88539e7988e13a7fdd9736a6a672048ee00e59a /src/git.rs | |
| parent | 4b0a35583644e703eb615e0724d33fe93aec932b (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
Diffstat (limited to 'src/git.rs')
| -rw-r--r-- | src/git.rs | 10 |
1 files changed, 4 insertions, 6 deletions
| @@ -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, |