8 releases (5 breaking)

0.6.0 Feb 16, 2024
0.5.0 Apr 27, 2023
0.4.0 Feb 15, 2023
0.3.1 Jan 22, 2023
0.1.0 Jul 8, 2022

#1381 in Command line utilities

Download history 14/week @ 2023-12-18 19/week @ 2023-12-25 3/week @ 2024-01-01 19/week @ 2024-01-08 12/week @ 2024-01-15 12/week @ 2024-01-22 9/week @ 2024-01-29 67/week @ 2024-02-05 131/week @ 2024-02-12 90/week @ 2024-02-19 53/week @ 2024-02-26 33/week @ 2024-03-04 50/week @ 2024-03-11 34/week @ 2024-03-18 17/week @ 2024-03-25 187/week @ 2024-04-01

291 downloads per month
Used in labelmaker

MIT license

63KB
795 lines

Project Status: Active – The project has reached a stable, usable state and is being actively developed. CI Status codecov.io Minimum Supported Rust Version MIT License

GitHub | crates.io | Documentation | Issues | Changelog

ghrepo extracts a GitHub repository's owner & name from various GitHub URL formats (or just from a string of the form OWNER/REPONAME or REPONAME), and the resulting object provides properties for going in reverse to determine the possible URLs. Also included is a struct for performing a couple useful inspections on local Git repositories, including determining the corresponding GitHub owner & repository name.

Example

use std::error::Error;
use std::str::FromStr;
use ghrepo::GHRepo;

fn main() -> Result<(), Box<dyn Error>> {
    let repo = GHRepo::new("octocat", "repository")?;
    assert_eq!(repo.owner(), "octocat");
    assert_eq!(repo.name(), "repository");
    assert_eq!(repo.to_string(), "octocat/repository");
    assert_eq!(repo.html_url(), "https://github.com/octocat/repository");

    let repo2 = GHRepo::from_str("octocat/repository")?;
    assert_eq!(repo, repo2);

    let repo3 = GHRepo::from_str("https://github.com/octocat/repository")?;
    assert_eq!(repo, repo3);
    Ok(())
}

Command

ghrepo also provides a command of the same name for getting the GitHub repository associated with a local Git repository. To install this command on your system, run:

cargo install ghrepo

Usage

ghrepo [<options>] [<dirpath>]

By default, the ghrepo command just outputs the repository "fullname" (a string of the form {owner}/{name}). If the -J or --json option is supplied, a JSON object is instead output, containing fields for the repository owner, name, fullname, and individual URLs, like so:

{
    "owner": "jwodder",
    "name": "ghrepo-rust",
    "fullname": "jwodder/ghrepo-rust",
    "api_url": "https://api.github.com/repos/jwodder/ghrepo-rust",
    "clone_url": "https://github.com/jwodder/ghrepo-rust.git",
    "git_url": "git://github.com/jwodder/ghrepo-rust.git",
    "html_url": "https://github.com/jwodder/ghrepo-rust",
    "ssh_url": "git@github.com:jwodder/ghrepo-rust.git"
}

Options

  • -J, --json — Output JSON
  • -r REMOTE, --remote REMOTE — Parse the GitHub URL from the given remote [default: origin]

Dependencies

~195KB