upleb.uk

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

summaryrefslogtreecommitdiff
path: root/src/git.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/git.rs')
-rw-r--r--src/git.rs33
1 files changed, 32 insertions, 1 deletions
diff --git a/src/git.rs b/src/git.rs
index e2d8196..29670eb 100644
--- a/src/git.rs
+++ b/src/git.rs
@@ -1,6 +1,6 @@
1use std::env::current_dir;
2#[cfg(test)] 1#[cfg(test)]
3use std::path::PathBuf; 2use std::path::PathBuf;
3use std::{env::current_dir, path::Path};
4 4
5use anyhow::{bail, Context, Result}; 5use anyhow::{bail, Context, Result};
6use git2::{Oid, Revwalk}; 6use git2::{Oid, Revwalk};
@@ -30,6 +30,8 @@ impl Repo {
30// pub type Sha1 = [u8; 20]; 30// pub type Sha1 = [u8; 20];
31 31
32pub trait RepoActions { 32pub trait RepoActions {
33 fn get_path(&self) -> Result<&Path>;
34 fn get_origin_url(&self) -> Result<String>;
33 fn get_local_branch_names(&self) -> Result<Vec<String>>; 35 fn get_local_branch_names(&self) -> Result<Vec<String>>;
34 fn get_main_or_master_branch(&self) -> Result<(&str, Sha1Hash)>; 36 fn get_main_or_master_branch(&self) -> Result<(&str, Sha1Hash)>;
35 fn get_checked_out_branch_name(&self) -> Result<String>; 37 fn get_checked_out_branch_name(&self) -> Result<String>;
@@ -61,6 +63,23 @@ pub trait RepoActions {
61} 63}
62 64
63impl RepoActions for Repo { 65impl RepoActions for Repo {
66 fn get_path(&self) -> Result<&Path> {
67 self.git_repo
68 .path()
69 .parent()
70 .context("cannot find repositiory path as .git has no parent")
71 }
72
73 fn get_origin_url(&self) -> Result<String> {
74 Ok(self
75 .git_repo
76 .find_remote("origin")
77 .context("cannot find origin")?
78 .url()
79 .context("cannot find origin url")?
80 .to_string())
81 }
82
64 fn get_main_or_master_branch(&self) -> Result<(&str, Sha1Hash)> { 83 fn get_main_or_master_branch(&self) -> Result<(&str, Sha1Hash)> {
65 let main_branch_name = { 84 let main_branch_name = {
66 let local_branches = self 85 let local_branches = self
@@ -860,6 +879,18 @@ mod tests {
860 } 879 }
861 } 880 }
862 881
882 mod get_origin_url {
883 use super::*;
884
885 #[test]
886 fn returns_origin_url() -> Result<()> {
887 let test_repo = GitTestRepo::default();
888 test_repo.add_remote("origin", "https://localhost:1000")?;
889 let git_repo = Repo::from_path(&test_repo.dir)?;
890 assert_eq!(git_repo.get_origin_url()?, "https://localhost:1000");
891 Ok(())
892 }
893 }
863 mod get_checked_out_branch_name { 894 mod get_checked_out_branch_name {
864 use super::*; 895 use super::*;
865 896