git-browse-remote

helps viewing repositories on the web e.g. GitHub in your browser.

View on GitHub
gem install git-browse-remote

Just run git browse-remote on your GitHub-cloned repository. This opens URL according to what branch you are on e.g.

On master
opens project page (for example).
On some branch
opens branch page (for example).

To point your commit to someone

Sometimes you will need to include revision SHA-1 in the URL for later reference. (ex. sharing an URL of an intensively developed branch on IRC)

Use --rev option to force revision-based page.

git browse-remote --rev

will navigate you to the commit page (for example).

To browse a specific file (at specific line)

Use -- path and -L line to specify a file and a line number.

git browse-remote -L1 -- .gitignore

will navigate you to the file at line 1 (for example).

Specifying a commit

To inspect a specific branch/commit, just pass it in the arguments.

git browse-remote refactor

will navigate you to the branch page (for example).

git browse-remote HEAD~1

will navigate you to the commit page (for example).

git browse-remote [-r|--remote <remote>] [--top|--rev|--ref] [-l <n>] [<commit-or-remote>] [--] [<file-or-directory>]

Parameters

-r,--remote REMOTE

Specifies remote (default: "origin"). Remote is used to decide the URL to open.

--top

Forces "top" mode (opens project top page).

--rev

Forces "rev" mode (opens commit page).

--ref

Forces "ref" mode (opens branch/tag page, may fail if none available).

-L LINE

Specifies file line (only meaningful when FILE specified).

COMMIT-or-REMOTE

Specifies target commit (default: "HEAD"). Recognized as remote if equals to one of remotes.

FILE-or-DIRECTORY

Specifies a single file to view.

Determining which view to open

If none of --top, --rev, --ref is supplied,

  1. If the target (specified as the argument or "HEAD") points to a branch that is named "master", opens project top ("top" mode).
  2. If the target points to a branch or a tag, opens that ref's page ("ref" mode).
  3. Otherwise, opens the corresponding revision's page ("rev" mode).
  4. If FILE is specified, opens that file's page. URL depends on whether the mode is "ref" or "rev".

If you have GitHub-like repository browser, ex. GitHub Enterprise, at gh.example.com, set up by:

git browse-remote --init gh.example.com=github

Then you can open this non-github-com repository viewer. The settings are wrote to ~/.gitconfig.

For GitHub repositories, this initialization is not required.

If you want to write your own repository viewer's URL rules, these variables are available:

host
Repository's host (e.g. "github.com")
path
Repository's path (e.g. "motemen/git-browse-remote")
Array methods are available (e.g. path[-2,-1])
ref
The ref name (e.g. "refs/heads/master")
short_ref
The ref name in short form (e.g. "master")
commit
The commit SHA-1 (e.g. "04f7c64ba9d524cf311a673ddce5722b2441e2ea")
short_commit
The commit SHA-1 in short form (e.g. "04f7c64b")
rev
ref or commit
short_rev
short_ref or short_commit
file
File (or directory) specified
Pathname methods are available
line
Line specified by "-L"

For example, URL mappings for github.com are below by default:

[browse-remote "github.com"]
top = https://{host}/{path}
ref = https://{host}/{path}/tree/{short_ref}
rev = https://{host}/{path}/commit/{commit}
file = "https://{host}/{path}/{file.directory? and :tree or :blob}/{short_rev}/{file}{line && \"#L%d\" % line}"
motemen <motemen@gmail.com>