Skip to content

Commit

Permalink
Merge pull request #394 from siketyan/feat/GH-385
Browse files Browse the repository at this point in the history
feat: browse: Find a URL to open from the current repository
  • Loading branch information
siketyan authored Jul 1, 2024
2 parents c6f6ed6 + f1ed265 commit 5087ae0
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions src/cmd/browse.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::{anyhow, Result};
use anyhow::{anyhow, bail, Result};
use clap::Parser;
use git2::Repository;

use crate::config::Config;
use crate::root::Root;
Expand Down Expand Up @@ -71,19 +72,35 @@ fn open_url(url: &url::Url) -> Result<()> {
#[derive(Debug, Parser)]
pub struct Cmd {
/// URL or pattern of the repository to be browsed.
repo: String,
/// Defaults to the default remote of the repository at the current directory.
repo: Option<String>,
}

impl Cmd {
pub async fn run(self) -> Result<()> {
let root = Root::find()?;
let config = Config::load_from(&root)?;

let url = Url::from_str(
&self.repo,
&config.patterns,
config.defaults.owner.as_deref(),
)?;
let url = match self.repo.as_deref() {
Some(path) => path.to_owned(),
_ => {
let repo = Repository::open_from_env()?;

let remotes = repo.remotes()?;
let remote = match remotes.iter().flatten().next() {
Some(r) => r.to_owned(),
_ => bail!("The repository has no remote."),
};

let remote = repo.find_remote(&remote)?;
match remote.url() {
Some(url) => url.to_string(),
_ => bail!("Could not find the remote URL from the repository."),
}
}
};

let url = Url::from_str(&url, &config.patterns, config.defaults.owner.as_deref())?;

let platform = config
.platforms
Expand Down

0 comments on commit 5087ae0

Please sign in to comment.