Skip to content

Commit

Permalink
fix: formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffjay committed Jan 16, 2024
1 parent 0372bdc commit fd2ae20
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion git-repo-url/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fn main() {
log_prepare(module_path!(), "git-repo-url");
let command = Command::new("git-repo-url".to_string());

match command.repo_url() {
match command.repo_url2() {
Ok(url) => println!("{}", url),
Err(e) => {
error!("error: {}", e);
Expand Down
22 changes: 18 additions & 4 deletions git-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl Command {
Ok(branch.to_string())
}

/// Print the name of the repository. This does the rough equivalent of
/// Print the name of the repository. This does the equivalent of
/// `git remote show origin | grep Fetch | awk '{print $3}'`
pub fn repo_url(self: Command) -> Result<String, Error> {
let mut remote = self
Expand Down Expand Up @@ -123,6 +123,17 @@ impl Command {
}
}

/// Print the url of the repository using local config. This does the equivalent of
/// `git config --get remote.origin.url`
pub fn repo_url2(self: Command) -> Result<String, Error> {
let url = self
.config
.get_string("remote.origin.url")
.expect("Invalid key: 'remote.origin.url'");

Ok(url)
}

/// Print the owner and name of the repository. This does the equivalent of
/// `git remote show origin | grep Fetch | sed "s/^.*\:\(.*\)\.git/\1/"`
///
Expand All @@ -133,18 +144,21 @@ impl Command {
//
// at some point adding a mocking library should be done to test each.
pub fn repo_title(self: Command) -> Result<String, Error> {
let url = self.repo_url()?;
let url = self.repo_url2()?;

// match the owner and repository name from the url
let re = Regex::new(r"(?x)
let re = Regex::new(
r"(?x)
^((https://)|(git@))
([\w\.]*)
([:\/])
(?P<owner>[\w\-_\.]*)
\/
(?P<name>[\w\-_\.]*)
(\.git)$
").unwrap();
",
)
.unwrap();
let res = re.replace_all(&url, "$owner/$name");

Ok(res.to_string())
Expand Down

0 comments on commit fd2ae20

Please sign in to comment.