Skip to content

Commit

Permalink
Add remote flag (#16)
Browse files Browse the repository at this point in the history
* Add open remote repo with the -r option

* Add error information when error in capturing pr url

* Increase minor version

* Update readme

* Update readme

* Change name in help cli
  • Loading branch information
oren0e authored May 25, 2023
1 parent 90a74aa commit 3ed0ef2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "gitopen"
version = "1.3.3"
version = "1.4.0"
authors = ["Oren Epshtain"]
edition = "2018"
license-file = "LICENSE"
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,12 @@ Basic usages:
> gitopen -l /src/main.rs:10
```

- If you've added a remote (other than `origin` which is the default) e.g. for a forked repository so the remote is the original repository URL, you can use gitopen to open that remote by specifying its name, for example:

```
> gitopen -r upstream
```

Note that `gitopen -r origin` is the same as `gitopen`.

For help, use `gitopen --help`
27 changes: 25 additions & 2 deletions src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use anyhow::Result as AnyhowResult;
use regex::Regex;
use std::process::{Command, Stdio};

// TODO: Add caching (`cached` crate)
fn get_parsed_url() -> AnyhowResult<String> {
let git_repo = Command::new("git")
.args(["config", "--get", "remote.origin.url"])
Expand All @@ -19,12 +18,36 @@ fn get_parsed_url() -> AnyhowResult<String> {
Ok(parsed_url)
}

fn get_parsed_remote_url(remote_name: &str) -> AnyhowResult<String> {
if remote_name == "origin" {
println!("Usage of --remote with `origin` detected, use `gitopen` instead");
}
let git_repo = Command::new("git")
.args(["config", "--get", &format!("remote.{}.url", remote_name)])
.stdout(Stdio::piped())
.output()?;

let stdout = String::from_utf8(git_repo.stdout)?;
if stdout.is_empty() {
return Err(anyhow!("Remote name {} does not exist!", remote_name));
}
let parsed_url = parse_url_from_git(&stdout)?;

Ok(parsed_url)
}

pub fn open_repo() -> AnyhowResult<()> {
let parsed_url = get_parsed_url()?;
webbrowser::open(&parsed_url)?;
Ok(())
}

pub fn open_remote_repo(remote_name: &str) -> AnyhowResult<()> {
let parsed_url = get_parsed_remote_url(remote_name)?;
webbrowser::open(&parsed_url)?;
Ok(())
}

pub fn open_commit(commit_sha: &str) -> AnyhowResult<()> {
let parsed_url = get_parsed_url()?;
let commit_link = get_commit_link(parsed_url, commit_sha);
Expand Down Expand Up @@ -58,7 +81,7 @@ pub fn push_and_open_pr() -> AnyhowResult<()> {
let output_from_push_text = String::from_utf8(output_from_push.stderr)?;
let captured = pr_re
.captures(&output_from_push_text)
.ok_or_else(|| anyhow!("Error capturing PR url"))?;
.ok_or_else(|| anyhow!("Error capturing PR url: {}", &output_from_push_text))?;
webbrowser::open(&captured[1])?;
Ok(())
}
Expand Down
19 changes: 18 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use clap::{crate_version, App, Arg};

use crate::actions::{open_at_line_number, open_commit, open_repo, push_and_open_pr};
use crate::actions::{
open_at_line_number, open_commit, open_remote_repo, open_repo, push_and_open_pr,
};
use anyhow::anyhow;
use anyhow::Result as AnyhowResult;

Expand Down Expand Up @@ -34,6 +36,14 @@ fn main() -> AnyhowResult<()> {
.takes_value(true)
.help("Open the specified filepath at the specified line number"),
)
.arg(
Arg::with_name("open_remote_repo")
.short("r")
.long("remote")
.value_name("remote_name")
.takes_value(true)
.help("Open the repo from the defined remote (e.g. upstream)"),
)
.get_matches();
if matches.is_present("push_and_pr") {
push_and_open_pr()?;
Expand All @@ -51,6 +61,13 @@ fn main() -> AnyhowResult<()> {
.ok_or_else(|| anyhow!("Please supply '<filepath>:<line-number>'"))?,
)?;
Ok(())
} else if matches.is_present("open_remote_repo") {
open_remote_repo(
matches
.value_of("open_remote_repo")
.ok_or_else(|| anyhow!("Please supply a remote name"))?,
)?;
Ok(())
} else {
open_repo()?;
Ok(())
Expand Down

0 comments on commit 3ed0ef2

Please sign in to comment.