Skip to content

Commit

Permalink
feat: simplify repo url command
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffjay committed Jan 16, 2024
1 parent fd2ae20 commit bd21e69
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 40 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_url2() {
match command.repo_url() {
Ok(url) => println!("{}", url),
Err(e) => {
error!("error: {}", e);
Expand Down
44 changes: 5 additions & 39 deletions git-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,47 +87,13 @@ impl Command {
Ok(branch.to_string())
}

/// 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
.repo
.find_remote("origin")
.expect("Couldn't find remote 'origin'");
let r = remote.clone();
let url = r.url().unwrap();
let config = &self.config;

let result = with_authentication(url, config, |f| {
let mut proxy_options = ProxyOptions::new();
proxy_options.auto();

let mut callbacks = RemoteCallbacks::new();
callbacks.credentials(f);

let _ = remote
.connect_auth(Direction::Fetch, Some(callbacks), Some(proxy_options))
.map_err(CommandError::GitError);

match remote.url() {
Some(url) => Ok(url.to_string()),
None => Err(CommandError::GitError(Error::from_str(
"Couldn't find remote 'origin'",
))),
}
});

match result {
Ok(name) => Ok(name),
Err(CommandError::GitError(e)) => Err(e),
}
}

/// 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> {
pub fn repo_url(self: Command) -> Result<String, Error> {
let url = self
.config
.repo
.config()
.unwrap()
.get_string("remote.origin.url")
.expect("Invalid key: 'remote.origin.url'");

Expand All @@ -144,7 +110,7 @@ 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_url2()?;
let url = self.repo_url()?;

// match the owner and repository name from the url
let re = Regex::new(
Expand Down

0 comments on commit bd21e69

Please sign in to comment.