Code Monkey home page Code Monkey logo

git-brws's Introduction

git brws

crates.io Github Actions CI Coverage

git brws is a command line tool to open a repository, file, commit, diff, tag, blame, pull request, issue or project's website in your web browser from command line. 'brws' is an abbreviation of 'browse'.

Features:

Installation

git brws is available on Linux, macOS and Windows.

$ brew tap "rhysd/git-brws" "https://github.com/rhysd/git-brws"
$ brew install git-brws

It installs git-brws to /usr/local/bin and git-brws.1 to /usr/local/share/man/man1. This is recommended way for installation on macOS since updating to the new version is easy.

You can install git-brws via the AUR package:

git clone https://aur.archlinux.org/git-brws.git
cd git-brws
makepkg -si

Download from release page

Pre-built binary executables are available at release page for following targets:

  • Linux 64bit (x86_64-unknown-linux-gnu)
  • macOS 64bit (x86_64-apple-darwin)
  • Windows 64bit (x86_64-pc-windows-msvc)
  • Linux 32bit (i686-unknown-linux-gnu)
  • Windows 32bit (i686-pc-windows-msvc)

Download and unarchive the binary then put the executable in $PATH.

Manpage for man command is available. Please find git-brws.1 in the unarchived directory or download it from here and copy it to the man directory in your system (e.g. /usr/local/share/man/man1/).

Note: git brws --help only works when it is installed. git brws -h is always available.

With cargo

$ cargo install git-brws

It requires Rust toolchain to build git-brws from sources.

Usage

Usage: git brws [Options] {Args}

Options:
    -r, --repo REPO     Shorthand format (repo, user/repo, host/user/repo) or
                        Git URL you want to see. When only repo name is
                        specified, most popular repository will be searched
                        from GitHub
    -b, --branch BRANCH Branch name to browse
    -d, --dir PATH      Directory path to the repository. Default value is
                        current working directory
    -R, --remote REMOTE Remote name (e.g. origin). Default value is a remote
                        the current branch is tracking. If current branch
                        tracks no branch, it falls back to 'origin'
    -u, --url           Output URL to stdout instead of opening in browser
    -p, --pr            Open pull request page instead of repository page. If
                        not existing, open 'Create Pull Request' page
    -w, --website       Open website page instead of repository page (homepage
                        URL for GitHub, GitLab pages, Bitbucket Cloud)
    -B, --blame         Open blame page instead of repository page. File path
                        to blame must be passed also.
    -c, --current-branch
                        Open the current branch instead of default branch
    -h, --help          Print this help
    -v, --version       Show version

Open a repository page

  • Repository at current directory
$ git brws
  • develop branch
$ git brws -b develop
  • origin remote of develop branch
$ git brws -r origin -b develop
  • @rhysd's 'Shiba' repository
$ git brws -r rhysd/Shiba
  • Most popular react repository on GitHub
$ git brws -r react

It will open https://github.com/facebook/react.

Note: When only repository name is specified for -r option, git-brws searches GitHub with query {repo} in:name and opens the best-matched repository page.

  • Specify directory of repository
$ git brws -d ~/.go/src/github.com/rhysd/dot-github

Open a file page

  • File of current branch of current remote
$ git brws ./some/file.txt
  • Line of the file
$ git brws ./some/file.txt#L123

Note: The L can be omit.

  • Range from line to line of the file
$ git brws ./some/file.txt#L123-L126

Note: The L can be omit.

Open a commit page

  • HEAD page of current repository
$ git brws HEAD

Open a tag page

  • 0.10.0 tag page of current repository
$ git brws 0.10.0

Open a diff page between commits

  • Diff between HEAD and HEAD~3
$ git brws HEAD~3..HEAD
  • Diff between 113079b and 60024ab
$ git brws 60024ab..113079b

Open a diff page from specific commit and its merge base

In addition to .., diff with ... is supported.

  • Diff between branchB and the merge base commit from branchB into branchA
$ git brws branchA...branchB

If you don't know the difference between .. and ..., please read git diff --help.

Note: Only GitHub and GitHub Enterprise support .... For GitLab, only ... is available.

Open a pull request page

  • Pull request page of current branch of current repository
$ git brws --pr
  • Pull request page of specific branch of specific repository
# Specify my forked repository
$ git brws --pr --repo rhysd/rust.vim -b async-contextual-keyword

# Or specify original repository
$ git brws --pr --repo rust-lang/rust.vim -b async-contextual-keyword

Note: Currently only GitHub and GitHub Enterprise are supported.

Note: If you have created multiple pull requests at the same repository with the same branch name, the command may not open a pull request page you want.

Note: When a pull request page for current branch is not existing yet, it opens 'Create Pull Request' page instead.

Open a website for the repository

# Website for current repository
$ git brws --website

With --repo option, arbitrary repository's website can be opened.

# Opens https://reactjs.org
$ git brws --website --repo react

It opens a website for the repository.

  • For GitHub, URL for 'homepage' configuration of the repository if it's set. Otherwise https://{user}.github.io/{repo}
  • For GitHub Enterprise, https://pages.{host}/{user}/{repo} or https://{host}/pages/{user}/{repo} depending on your GitHub Enterprise configuration of subdomain isolation
  • For GitLab, GitLab Pages
  • For Bitbucket, Bitbucket Cloud

Open an issue page

  • Issue #8
$ git brws '#8'

Note: # is usually used for a line comment in major shells. Please quote the argument

Open a blame page

  • Specific file
$ git brws --blame some/file.txt
  • Specific line at file
$ git brws --blame some/file.txt#L5
  • Specific range at file
$ git brws --blame some/file.txt#L5-L9

Cooperate with other tools

With -u option, git brws outputs URL to stdout.

For example, in Vim, you can write your repository URL to your text instantly.

:r!git brws -u

And below can open editing file in your browser.

:!git brws %

Customization

Some environment variables are available to customize behavior of git-brws.

Variable Description
$GIT_BRWS_GIT_COMMAND Git command to use. If not specified, "git" will be used.
$GIT_BRWS_GHE_URL_HOST When you use your own GitHub Enterprise repository, you can specify its host to this variable. By default, git brws detects ^github\. as GHE host. If your GHE repository host does not match it, please specify this variable. If your repository is https://example-repo.org/user/repo, example-repo.org should be set.
$GIT_BRWS_GHE_SSH_PORT When you set a number to it, the number will be used for the ssh port for GitHub Enterprise URLs.
$GIT_BRWS_GITLAB_URL_HOST When you use self-hosted GitLab instance, you can specify its host to this variable. By default, git brws detects host matching to ^gitlab\. as GitLab. If your GitLab URL host does not match it, please specify this variable. If your repository is hosted at https://your-code.net/user/repo, your-code.net should be set.
$GIT_BRWS_GITLAB_SSH_PORT When you set a number to it, the number will be used for the ssh port for self-hosted GitLab URLs. This is useful when your environment hosts GitLab to non-trivial ssh port URL.
$GIT_BRWS_GITHUB_TOKEN This variable is used for --pr (or -p) only. API access token for github.com. They are optional, but useful for avoiding API rate limit and accessing to private repositories. Please generate a token from https://github.com/settings/tokens/new
$GITHUB_TOKEN Ditto. When GIT_BRWS_GITHUB_TOKEN is not set, GITHUB_TOKEN is looked.
$GIT_BRWS_GHE_TOKEN This variable is used for --pr (or -p) only. API access token for GitHub Enterprise instance. It is sometimes mandatory (depending on your GHE instance configuration). Please generate a token from https://{YOUR GHE HOST}/settings/tokens/new.
$GIT_BRWS_BROWSE_COMMAND Command to open URL. If this value is specified, the command is executed with URL as first argument to browse the URL.
$GIT_BRWS_SHORT_COMMIT_HASH Setting true will use 7-letters short commit hash like 78fbce6 for URLs.
$https_proxy This variable is used for --pr (or -p) only. An HTTPS Proxy server URL if you use a web proxy.

Related Projects

TODOs

Please see the project page.

Development

Please see CONTRIBUTING.md.

License

Distributed under the MIT license.

git-brws's People

Contributors

albertyw avatar eagletmt avatar johnlunney avatar rhysd avatar wezm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

git-brws's Issues

URLs for directory paths should use `/tree/` instead of `/blob/`

Repro

Run:

git brws .

Expected results

It opens https://github.com/{owner}/{repo}/tree/{hash}

Actual results

It opened https://github.com/{owner}/{repo}/blob/{hash}

Additional info

URLs for subdirectories (e.g. git brws ./src) are redirected from /blob/ URL to /tree/ URL automatically. However, only a root directory (e.g. git brws .) does not redirect the URL hence 404 error caused on a browser. I think git-brws should not rely on the redirect.

URL is wrong when opening 'New PullRequest' page with `git brws -p`

Precondition

  • Original: foo/bar
  • Forked: rhysd/bar

Repro

git clone https://github.com/rhysd/bar.git
cd bar
g cob patch-1
g push -u origin patch-1
git brws --pr

Expected Behavior

Open https://github.com/foo/bar/compare/master...rhysd:patch-1

Actual Behavior

Open https://github.com/foo/bar/compare/master...patch-1

-c option is not effective when opening filepaths

git-brws version

v0.11.11

Repro

Run:

git clone https://github.com/rhysd/git-brws.git
cd git-brws
git brws -c ./src/main.rs

Expected behavior

Open https://github.com/rhysd/git-brws/blob/master/src/main.rs

Actual behavior

Opened https://github.com/rhysd/git-brws/blob/{hash}/src/main.rs ignoring -c option

Tag does not work

Repro

cd /path/to/git-brws
git brws 0.10.0

Expected

The commit page of the tag is opened

Actual

404 page

0b412dc

Add Diff support for Bitbucket

It appears that Bitbucket does support diff between two commits /tags/branches using the following URL format โ€“

https://bitbucket.org/<owner>/<repoName>.git/branches/compare/<commitHash1>..<commitHash2>#diff

git brws --help doesn't work

The short option -h does work fine and print the help text:

$ git brws -h
Usage: git brws [Options] {Args}

[...]

    -h, --help          Print this help
    -v, --version       Show version

According to the above help, the long version of the option (--help) is also valid and will show the same help text. However, this doesn't seem working in such a way.

$ git brws --help
No manual entry for git-brws

Followings are some environmental information:

$ sw_vers
ProductName:	Mac OS X
ProductVersion:	10.13.6
BuildVersion:	17G4015

$ git --version
git version 2.20.1
hub version 2.7.0

$ git brws --version
0.8.1

Gitlab subgroups doesn't seem to be supported

I have a Gitlab remote URL with this format:

[email protected]:group/subgroup/repository.git

When trying, for example, to open a commit with the following command:

git-brws aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d

The following URL is opened:

https://example.com/group/subgroup/commit/aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d

It seems like Gitlab subgroups aren't supported.

I'm not a Rust developer so I can't submit a reliable PR within reasonable time, however I've searched a bit and the issue comes from the slug_from_path function:

git-brws/src/service.rs

Lines 396 to 407 in 5a1c351

let mut split = path.split('/').skip_while(|s| s.is_empty());
let user = split.next().ok_or_else(|| {
Error::new(ErrorKind::NoUserInPath {
path: path.to_string(),
})
})?;
let mut repo = split.next().ok_or_else(|| {
Error::new(ErrorKind::NoRepoInPath {
path: path.to_string(),
})
})?;

Instead of considering the URL will be splitted in only two parts, you should use the last element of the iterator as the repository, and join all the other elements with / as the separator.

CI on X86_64 Windows fails due to bad --git-dir

Job: https://ci.appveyor.com/project/rhysd/git-brws/branch/master/job/2rm5eqvclqhw653f

This failure is only on CI (both Appveyor and GitHub Actions). It is not reproducible on my local Windows machine. All tests passed on my local machine.

.canonicalize() uses \\?... path (extended path length) but it seems not supported by --git-dir. On Windows, --git-dir is not a file path actually. It is a kind of slash-path:

> git rev-parse --absolute-git-dir
D:/a/git-brws/git-brws/.git

\\?\D:\a\git-brws\git-brws\.git for --git-dir seems not available on some version of Windows. Following error occurs at running rev-parse --abbrev-ref --symbolic @{u} command:

fatal: not a git repository: '\\?\C:\git-brws\.git'

Actually it was OK on my local Windows machine to use the \\?... path as far as I tried manually.

Path should be clean

Repro

git brws src/../src/argv.rs -u

Expected

https://github.com/rhysd/git-brws/blob/88703f439412d08373e8026b5121a9ed4f30fb88/src/argv.rs

Actual

https://github.com/rhysd/git-brws/blob/88703f439412d08373e8026b5121a9ed4f30fb88/src/../src/argv.rs

Does not open submodule's URL when running it from outside submodule directory

Repro

git clone https://github.com/rhysd/wain.git
cd wain/spec-test
git brws ./wasm-testsuite/f32.wast

Expected

Open https://github.com/WebAssembly/testsuite/blob/xxx/globals.wast

Actual

Opened https://github.com/rhysd/wain/blob/xxx/spec-test/wasm-testsuite/f32.wast

Description

git-brws does not consider submodule. When opening file, running git command within the file's directory would solve this.

Is build with cargo broken?

Is the build of git-brws broken with latest stable?

~/src/git-brws$ git log --oneline -3
828b800 (HEAD -> master, origin/master, origin/HEAD) update homebrew formula to 0.11.2
f5fe9cf update changelog for 0.11.2
78c56a8 (tag: 0.11.2) fix tests broken by 2aacb24


~/src/git-brws$ rustup toolchain list
stable-x86_64-unknown-linux-gnu (default)

~/src/git-brws$ cargo build
error: failed to parse manifest at `/home/bgianf/src/Cargo.toml`

Caused by:
  no targets specified in the manifest
  either src/lib.rs, src/main.rs, a [lib] section, or [[bin]] section must be present

Feature request: Open pull request

@rhysd thanks a lot for creating this program. It certainly made my life easier.

Request

Open pull request from command line.

  1. My remote is origin
  2. I am on branch test which is ahead of master by 1 commit
  3. git-brws --pr or a similar command should open pull request page

For example this page

talha131/tw-scripts-demo@master...jvm-dev

It has a create pull request button.

Currently nearest command is

$ git-brws master..test
$ git-brws master...test

But these pages do not have "Create Pull Request" button.

A comparison of URL shows that only git-brws master...test compares master against hash. If hash is replaced with branch name then "Create PR" button automatically appears.

Invalid port number when user name starts with numbers

I got the following error when executing on Windows 10. Any ideas?

C:\Users\admin\AppData\Local\nvim>git-brws
Error: Broken URL 'ssh://[email protected]:0x7FFFFFFFFFFFFFFF/neovim_config.git': invalid port number

C:\Users\admin\AppData\Local\nvim>cat .git/config
[core]
        repositoryformatversion = 0
        filemode = false
        bare = false
        logallrefupdates = true
        symlinks = false
        ignorecase = true
[remote "origin"]
        url = [email protected]:0x7FFFFFFFFFFFFFFF/neovim_config.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master
[credential]
        helper = store

C:\Users\admin\AppData\Local\nvim>

Option to open the PRs and Issues pages

At the moment, git brws --pr is useful if I have a PR open for the branch or intend to create one.
Would you accept a PR to add --prs and maybe --issues to navigate to "Pull Requests" page and "Issues page"?

Azure dev ops support

I've an azure devops repo which I checked our via ssh: git remote get-url origin returns <org>@vs-ssh.visualstudio.com:v3/<org>/<project>/<repo>. The current branch is feature/whatever and I'm in the repo root.

if I run git brws, I would have expected it to open the branch in the repo root, but my browser opens https://dev.azure.com/<org>/<project>. The real url should have been: https://dev.azure.com/<org>/<project>/_git/<repo>?version=GBfeature%2Fwhatever&path=/ (so the branch has to be url encoded with version=GB in front; project also needed a space URL encoded as %20, but that was already that way in the ssh url).

If I use git brws -c, the browser opens https://dev.azure.com/<org>/_git/<project>?version=GBfeature/whatever, so no URL encoded branch name and the project is in the wrong place.

If I do something similar in a repo which was checked out via https, I get this via git remote get-url origin: https://<org>.visualstudio.com/<project>/_git/<repo> and git brws throws an error: Error: Unknown hosting service for URL https://<org>.visualstudio.com/<project>/_git/<repo>. If you want to use custom URL for GitHub Enterprise ...

Checking upstream does not respect -b

When -b is set, upstream should be detected based on the branch. But current branch is used always.

Repro:

$ git checkout -b tmp
$ git brws -b master

Expected:
It can open a page for master branch

Actual:
It causes following error since tmp does not set upstream remote branch.

Git command exited with non-zero status: fatal: no upstream configured for branch 'tmp'

Opens non-existing file page when the latest commit is not pushed yet

Repro

cd git-brws
git checkout master
git commit --allow-empty -m hi
git brws README.md

Expected behavior

Opens https://github.com/rhysd/git-brws/blob/master/README.md

Actual behavior

Opens not existing page https://github.com/rhysd/git-brws/blob/xxxxxxx/README.md where xxxxxxx is a commit hash not pushed yet

Support for self-hosted BitBucket (Stash)

Hi!
My company uses Stash for repository hosting. URL has format:

ssh://git@<hostname>:<port>/<owner>/<repo>.git

which corresponds to the web URL

https://<hostname>/projects/<owner>/repos/<repo>

It would be cool if git-brws supported such URLs

Long delay with brws'ing a file in a large repository

Howdy, just did a cargo build of the tool to test out simply browsing a file in the browser on its relevant branch, in Github.

I'm seeing times such as:

$ time git brws Makefile

real    0m4.878s
user    0m4.761s

And of course I tested this on a much smaller repo, and it is "instant"

I'm curious what the underlying mechanism is for I suppose finding which URL to open, and what could be a bottleneck within it regarding large repos?

I'm mostly interested in this tool for this single behavior, as it allows me to remove a vim plugin thats sole purpose is also this behavior, and I use it a lot in aforementioned repo

Thanks :)

Consider an alternate http client?

I was just updating the AUR package I maintain for git-brws and noticed that it seemed to have a lot of dependencies for a seemingly simple tool. I ran cargo graph on the project and it produced this:

graph

The details aren't especially important but it shows that a large amount of dependencies come in via reqwest (top center of the graph). Now don't get me wrong, reqwest is great, but it is full featured, high performance, and asynchronous. Given git-brws only needs to make a very small number of requests per invocation it seems that it may be overkill.

Perhaps a simpler HTTP client would be a better choice. I did some searching on crates.io and found minreq, which seems like it might be a good fit for git-brws needs.

What do you think? If this is something you'd be open to I could look into making a PR.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.