add git remote selection flag

This commit is contained in:
Cyborus 2023-12-12 19:49:13 -05:00
parent 98e3cceff8
commit 68d255db7a
No known key found for this signature in database
3 changed files with 25 additions and 17 deletions

View file

@ -57,9 +57,9 @@ pub enum ViewCommand {
} }
impl IssueCommand { impl IssueCommand {
pub async fn run(self, keys: &crate::KeyInfo) -> eyre::Result<()> { pub async fn run(self, keys: &crate::KeyInfo, remote_name: Option<&str>) -> eyre::Result<()> {
use IssueCommand::*; use IssueCommand::*;
let repo = RepoInfo::get_current()?; let repo = RepoInfo::get_current(remote_name)?;
let api = keys.get_api(&repo.host_url())?; let api = keys.get_api(&repo.host_url())?;
match self { match self {
Create { title, body } => create_issue(&repo, &api, title, body).await?, Create { title, body } => create_issue(&repo, &api, title, body).await?,

View file

@ -12,6 +12,8 @@ mod repo;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
pub struct App { pub struct App {
#[clap(long, short='R')]
remote: Option<String>,
#[clap(subcommand)] #[clap(subcommand)]
command: Command, command: Command,
} }
@ -36,13 +38,13 @@ async fn main() -> eyre::Result<()> {
let mut keys = KeyInfo::load().await?; let mut keys = KeyInfo::load().await?;
match args.command { match args.command {
Command::Repo(subcommand) => subcommand.run(&keys).await?, Command::Repo(subcommand) => subcommand.run(&keys, args.remote.as_deref()).await?,
Command::Issue(subcommand) => subcommand.run(&keys).await?, Command::Issue(subcommand) => subcommand.run(&keys, args.remote.as_deref()).await?,
Command::User { host } => { Command::User { host } => {
let host = host.map(|host| Url::parse(&host)).transpose()?; let host = host.map(|host| Url::parse(&host)).transpose()?;
let url = match host { let url = match host {
Some(url) => url, Some(url) => url,
None => repo::RepoInfo::get_current()?.url().clone(), None => repo::RepoInfo::get_current(args.remote.as_deref())?.url().clone(),
}; };
let name = keys.get_login(&url)?.username(); let name = keys.get_login(&url)?.username();
eprintln!("currently signed in to {name}@{url}"); eprintln!("currently signed in to {name}@{url}");

View file

@ -10,9 +10,9 @@ pub struct RepoInfo {
} }
impl RepoInfo { impl RepoInfo {
pub fn get_current() -> eyre::Result<Self> { pub fn get_current(name: Option<&str>) -> eyre::Result<Self> {
let repo = git2::Repository::open(".")?; let repo = git2::Repository::open(".")?;
let url = get_remote(&repo)?; let url = get_remote(&repo, name)?;
let mut path = url.path_segments().ok_or_else(|| eyre!("bad path"))?; let mut path = url.path_segments().ok_or_else(|| eyre!("bad path"))?;
let owner = path let owner = path
@ -49,13 +49,19 @@ impl RepoInfo {
} }
} }
fn get_remote(repo: &git2::Repository) -> eyre::Result<Url> { fn get_remote(repo: &git2::Repository, name: Option<&str>) -> eyre::Result<Url> {
let head = repo.head()?; let remote_name;
let branch_name = head.name().ok_or_else(|| eyre!("branch name not UTF-8"))?; let remote_name = match name {
let remote_name = repo.branch_upstream_remote(branch_name)?; Some(name) => name,
let remote_name = remote_name None => {
.as_str() let head = repo.head()?;
.ok_or_else(|| eyre!("remote name not UTF-8"))?; let branch_name = head.name().ok_or_else(|| eyre!("branch name not UTF-8"))?;
remote_name = repo.branch_upstream_remote(branch_name)?;
remote_name
.as_str()
.ok_or_else(|| eyre!("remote name not UTF-8"))?
}
};
let remote = repo.find_remote(remote_name)?; let remote = repo.find_remote(remote_name)?;
let url = Url::parse(std::str::from_utf8(remote.url_bytes())?)?; let url = Url::parse(std::str::from_utf8(remote.url_bytes())?)?;
Ok(url) Ok(url)
@ -85,7 +91,7 @@ pub enum RepoCommand {
} }
impl RepoCommand { impl RepoCommand {
pub async fn run(self, keys: &crate::KeyInfo) -> eyre::Result<()> { pub async fn run(self, keys: &crate::KeyInfo, remote_name: Option<&str>) -> eyre::Result<()> {
match self { match self {
RepoCommand::Create { RepoCommand::Create {
host, host,
@ -140,7 +146,7 @@ impl RepoCommand {
} }
} }
RepoCommand::Info => { RepoCommand::Info => {
let repo = RepoInfo::get_current()?; let repo = RepoInfo::get_current(remote_name)?;
let api = keys.get_api(&repo.host_url())?; let api = keys.get_api(&repo.host_url())?;
let repo = api.get_repo(repo.owner(), repo.name()).await?; let repo = api.get_repo(repo.owner(), repo.name()).await?;
match repo { match repo {
@ -151,7 +157,7 @@ impl RepoCommand {
} }
} }
RepoCommand::Browse => { RepoCommand::Browse => {
let repo = RepoInfo::get_current()?; let repo = RepoInfo::get_current(remote_name)?;
let mut url = repo.host_url().clone(); let mut url = repo.host_url().clone();
let new_path = format!( let new_path = format!(
"{}/{}/{}", "{}/{}/{}",