Skip to content

Commit

Permalink
tracking branches awarness; git browse subpages (e.g. `git browse r…
Browse files Browse the repository at this point in the history
…epo issues`)

New abilities:

  $ hub compare
  -> When on a branch that tracks "remotebranch", for example,
     this opens "github.com/user/repo/compare/remotebranch".
     In effect if the tracked branch is not "master".

  $ hub browse reque issues
  -> open http://github.com/user/resque/issues

  $ hub browse
  -> When on a branch that tracks "remotebranch", for example,
     this opens "github.com/user/repo/tree/remotebranch".
     In effect if the tracked branch is not "master".

  $ hub browse -- commits
  -> open http://github.com/user/repo/commits/master

  If this branch is tracking a remote branch:
  -> open http://github.com/user/repo/commits/remotebranch

Supported subpages:

  - "tree" (default)
  - "wiki"
  - "commits"
  - "issues", "network", "downloads" or any other arbitrary
    string is simply appended to URL path
  • Loading branch information
mislav committed Apr 27, 2010
1 parent 26af67a commit 231366d
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 54 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -153,6 +153,9 @@ login" below for details.
$ git browse
> open http://github.com/CURRENT_REPO

$ git browse -- issues
> open http://github.com/CURRENT_REPO/issues

$ git browse schacon/ticgit
> open http://github.com/schacon/ticgit

Expand All @@ -162,6 +165,9 @@ login" below for details.
$ git browse resque
> open http://github.com/YOUR_USER/resque

$ git browse resque network
> open http://github.com/YOUR_USER/resque/network

$ git browse -p resque
> open https://github.com:YOUR_USER/resque

Expand Down
43 changes: 35 additions & 8 deletions lib/hub/commands.rb
Expand Up @@ -193,6 +193,9 @@ def push(args)
# $ hub browse
# > open http://github.com/CURRENT_REPO
#
# $ hub browse -- issues
# > open http://github.com/CURRENT_REPO/issues
#
# $ hub browse pjhyett/github-services
# > open http://github.com/pjhyett/github-services
#
Expand All @@ -202,13 +205,17 @@ def push(args)
# $ hub browse github-services
# > open http://github.com/YOUR_LOGIN/github-services
#
# $ hub browse github-services wiki
# > open http://wiki.github.com/YOUR_LOGIN/github-services
#
# $ hub browse -p github-fi
# > open https://github.com/YOUR_LOGIN/github-fi
def browse(args)
args.shift
browse_command(args) do
user = repo = nil
dest = args.pop
dest = args.shift
dest = nil if dest == '--'

if dest
# $ hub browse pjhyett/github-services
Expand All @@ -222,7 +229,23 @@ def browse(args)
exit(1)
end

{ :user => user, :repo => repo }
params = { :user => user, :repo => repo }

# $ hub browse -- wiki
case subpage = args.shift
when 'wiki'
params[:web] = 'wiki'
when 'commits'
branch = (!dest && tracked_branch) || 'master'
params[:web] = "/commits/#{branch}"
when 'tree', NilClass
branch = !dest && tracked_branch
params[:web] = "/tree/#{branch}" if branch and branch != 'master'
else
params[:web] = "/#{subpage}"
end

params
end
end

Expand All @@ -235,17 +258,21 @@ def browse(args)
# $ hub compare -p myfork topsecret
# > open https://github.com/myfork/REPO/compare/topsecret
# $ hub compare -u 1.0...2.0
# (Prints the URL for the compare view)
# prints "http://github.com/CURRENT_REPO/compare/1.0...2.0"
def compare(args)
args.shift
browse_command(args) do
if args.empty?
warn "Usage: hub compare [USER] [<START>...]<END>"
exit(1)
if branch = tracked_branch and branch != 'master'
range, user = branch, repo_user
else
warn "Usage: hub compare [USER] [<START>...]<END>"
exit(1)
end
else
range = args.pop
user = args.pop || repo_user
end

range = args.pop
user = args.pop || repo_user
{ :user => user, :web => "/compare/#{range}" }
end
end
Expand Down
49 changes: 42 additions & 7 deletions lib/hub/context.rb
Expand Up @@ -24,12 +24,15 @@ module Context
private

def repo_owner
REMOTES['origin'][:user]
REMOTES[default_remote][:user]
end

def repo_user
REMOTES[current_remote][:user]
end
alias repo_user repo_owner

def repo_name
REMOTES['origin'][:repo] || File.basename(Dir.pwd)
REMOTES[default_remote][:repo] || File.basename(Dir.pwd)
end

# Either returns the GitHub user as set by git-config(1) or aborts
Expand All @@ -44,18 +47,50 @@ def github_token(fatal = true)
fatal ? abort("** No GitHub token set. See #{LGHCONF}") : nil
end

def current_branch
GIT_CONFIG['symbolic-ref -q HEAD']
end

def tracked_branch
branch = current_branch && tracked_for(current_branch)
normalize_branch(branch) if branch
end

def current_remote
(current_branch && remote_for(current_branch)) || default_remote
end

def default_remote
'origin'
end

def normalize_branch(branch)
branch.sub('refs/heads/', '')
end

def remote_for(branch)
GIT_CONFIG['config branch.%s.remote' % normalize_branch(branch)]
end

def tracked_for(branch)
GIT_CONFIG['config branch.%s.merge' % normalize_branch(branch)]
end

def http_clone?
GIT_CONFIG['config --bool hub.http-clone'] == 'true'
end

def github_url(options = {})
user, repo = options[:user], options[:repo]
user, repo = repo.split('/') if user.nil? and repo and repo.index('/')
user ||= github_user
repo = options[:repo]
user, repo = repo.split('/') if repo and repo.index('/')
user ||= options[:user] || github_user
repo ||= repo_name
secure = options[:private]

if options[:web]
if options[:web] == 'wiki'
scheme = secure ? 'https:' : 'http:'
'%s//wiki.github.com/%s/%s/' % [scheme, user, repo]
elsif options[:web]
scheme = secure ? 'https:' : 'http:'
path = options[:web] == true ? '' : options[:web].to_s
'%s//github.com/%s/%s%s' % [scheme, user, repo, path]
Expand Down
35 changes: 26 additions & 9 deletions man/hub.1
Expand Up @@ -7,7 +7,10 @@
\fBhub\fR \-\- git + hub = github
.
.SH "SYNOPSIS"
\fBhub\fR \fICOMMAND\fR \fIOPTIONS\fR \fBhub alias\fR [\fB\-s\fR] \fISHELL\fR
\fBhub\fR \fICOMMAND\fR \fIOPTIONS\fR
.
.br
\fBhub alias\fR [\fB\-s\fR] \fISHELL\fR
.
.P
\fBgit init \-g\fR \fIOPTIONS\fR
Expand Down Expand Up @@ -37,7 +40,10 @@ alias command displays information on configuring your environment:
.TP
\fBhub alias\fR [\fB\-s\fR] \fISHELL\fR
Writes shell aliasing code for \fISHELL\fR (\fBbash\fR, \fBsh\fR, \fBzsh\fR, \fBcsh\fR) to standard output. With the \fB\-s\fR option, the output of
this command can be evaluated directly within the shell: \fBeval $(hub alias \-s bash)\fR
this command can be evaluated directly within the shell:
.
.br
\fBeval $(hub alias \-s bash)\fR
.
.TP
\fBgit init\fR \fB\-g\fR \fIOPTIONS\fR
Expand Down Expand Up @@ -70,17 +76,19 @@ username. It requires your GitHub login and token to be present. See
CONFIGURATION below.
.
.TP
\fBgit browse\fR [\fB\-p\fR] [[\fIUSER\fR\fB/\fR]\fIREPOSITORY\fR]
\fBgit browse\fR [\fB\-p\fR] [\fB\-u\fR] [[\fIUSER\fR\fB/\fR]\fIREPOSITORY\fR] [SUBPAGE]
Open repository's GitHub page in the system's default web browser
using \fBopen(1)\fR or the \fBBROWSER\fR env variable. Use \fB\-p\fR to open a
page with https. If the repository isn't specified, \fBbrowse\fR opens
the page of the repository found in the current directory.
the page of the repository found in the current directory. If SUBPAGE
is specified, the browser will open on the specified subpage: one of
"wiki", "commits", "issues" or other (the default is "tree").
.
.TP
\fBgit compare\fR [\fB\-u\fR] [\fIUSER\fR] [\fISTART\fR...]\fIEND\fR
\fBgit compare\fR [\fB\-p\fR] [\fB\-u\fR] [\fIUSER\fR] [\fISTART\fR...]\fIEND\fR
Open a GitHub compare view page in the system's default web browser. \fISTART\fR to \fIEND\fR are branch names, tag names, or commit SHA1s specifying
the range of history to compare. If \fISTART\fR is omitted,
the repository's default branch is assumed.
the range of history to compare. If \fISTART\fR is omitted, GitHub will
compare against the base branch (the default is "master").
.
.TP
\fBgit submodule add\fR [\fB\-p\fR] \fIOPTIONS\fR [\fIUSER\fR/]\fIREPOSITORY\fR \fIDIRECTORY\fR
Expand Down Expand Up @@ -137,7 +145,7 @@ cloning:
.
.nf

$ git config \-\-global \-\-add hub.http\-clone yes
$ git config \-\-global \-\-bool hub.http\-clone true
.
.fi
.
Expand Down Expand Up @@ -213,6 +221,12 @@ $ git push origin,staging,qa bert_timeout
.
.nf

$ git browse
> open http://github.com/CURRENT_REPO

$ git browse \-\- issues
> open http://github.com/CURRENT_REPO/issues

$ git browse schacon/ticgit
> open http://github.com/schacon/ticgit

Expand All @@ -222,8 +236,11 @@ $ git browse \-p schacon/ticgit
$ git browse resque
> open http://github.com/YOUR_USER/resque

$ git browse resque network
> open http://github.com/YOUR_USER/resque/network

$ git browse \-p resque
> open https://github.com:YOUR_USER/resque
> open https://github.com/YOUR_USER/resque
.
.fi
.
Expand Down
31 changes: 21 additions & 10 deletions man/hub.1.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 231366d

Please sign in to comment.