Skip to content

Instantly share code, notes, and snippets.

@gorenje
Created May 6, 2012 09:54
Show Gist options
  • Save gorenje/2621377 to your computer and use it in GitHub Desktop.
Save gorenje/2621377 to your computer and use it in GitHub Desktop.
hub extenstion commands
class String
def remove_indent
self =~ /\A([ \t]+)/ ? gsub(/\n#{$1}/, "\n").strip : self
end
end
module Hub
module Commands
def feature_branch(args)
user_arg, issue_num, postfix = nil, nil, nil
while arg = args.shift
case arg
when '-u' then user_arg = args.shift
when '-i' then issue_num = args.shift.to_i
when '-p' then postfix = args.shift
when '-h'
puts (<<-EOF).remove_indent
git feature-branch [-i <num>|-p <postfix>|-u <owner>|-h]
-u <username> Provide a repo owner, defaults to git config hub.owner
-i <num> Issue for which to create the feature branch
-p <postfix> Postfix to the name of the branch incase branch exists
-h Show this help.
EOF
exit
end
end
user_arg = Hub::Context::GitReader.new.read_config("hub.owner") if user_arg.nil?
abort "Provide an issue number, -i <number>" if issue_num.nil?
abort "Need to provide user, -u <username>" if user_arg.nil?
base_project = local_repo.main_project
feature = show_issues(:user => user_arg, :project => base_project, :type => :open).
select do |issue|
issue["number"] == issue_num
end.first
if feature.nil?
feature = show_issues(:user => user_arg, :project => base_project, :type => :closed).
select do |issue|
issue["number"] == issue_num
end.first
end
abort "No issue found" if feature.nil?
branchname = "issue_%d_%s%s" % [feature["number"],
feature["title"][0..30].
gsub(/[[:space:][:punct:]]/,'_'),
postfix.nil? ? "" : "_#{postfix}"]
`git checkout -b #{branchname}`
exit
end
end
end
module Hub
module Context
class LocalRepo
class GithubProject
def api_show_url(type)
api_url(type, 'repos', "show/#{owner}/#{name}")
end
end
end
end
end
class String
def remove_indent
self =~ /\A([ \t]+)/ ? gsub(/\n#{$1}/, "\n").strip : self
end
end
module Hub
module Commands
# hub github-issues -u defunkt -c
# > <list of all closed issues>
# hub github-issues -u defunkt
# > <list of all open issues>
def github_issues(args)
user_arg, issue_type, open_ticket = nil, :open, nil
while arg = args.shift
case arg
when '-u' then user_arg = args.shift
when '-o' then open_ticket = args.shift
when '-c' then issue_type = :closed
when '-h'
puts (<<-EOF).remove_indent
git github-issues [-u <owner>|-o <num>|-c|-h]
-u <username> Provide a repo owner, defaults to git config hub.owner
-o <ticket> Open a ticket in browser
-c Show closed tickets instead of open
-h Show this help.
EOF
exit
end
end
user_arg = Hub::Context::GitReader.new.read_config("hub.owner") if user_arg.nil?
abort "Need to provide user, -u <username>" if user_arg.nil?
base_project = local_repo.main_project
if open_ticket
url = "https://github.com/#{user_arg}/#{repo_name}/issues/#{open_ticket}"
puts "Opening #{url}"
`open #{url}`
exit
end
show_issues(:user => user_arg,
:project => base_project, :type => issue_type).each do |issue|
puts "%3d. %s" % [ issue["number"], issue["title"]]
end
exit
end
private
# Helper to retrieve all issues by type.
#
# options - is not options hash containing at least
# . user - the owner of the 'master' repo i.e. where the issues are
# . project - the local_repos project
# . type - issue type to be used, either :open or :closed.
#
# Example
# show_issues(:user => "defunkt", :project => local_repo.main_project,
# :type => :open)
# => [<Issue:instance>, <Issue:instance>, <Issue:instance>, ...
#
# Return an array with Issues where an issue taken from the Github REST API, i.e.
# http://developer.github.com/v3/issues/ provides a description.
def show_issues(options)
project = options.fetch(:project)
user = options.fetch(:user)
issue_types = options.fetch(:type) || :open
load_net_http
response = http_request(project.api_issues_show_url('json', user, issue_types))
response.error! unless Net::HTTPSuccess === response
JSON.parse(response.body)["issues"].sort_by { |a| a["number"]}
end
end
end
module Hub
module Commands
def install_commit_hook(args)
dest_file = File.join(git_dir, "hooks", "commit-msg")
src_url = "https://raw.github.com/gist/1012062/"+
"e2f6ff6cb78bc362fd70c4d936a5f08b253bf5b1/commit-msg-goclone"
abort "commit hook already exists: #{dest_file}" if File.exists?(dest_file)
system("curl '#{src_url}' > #{dest_file} && chmod 755 #{dest_file}")
exit
end
end
end
module Hub
module Commands
def to_pull_request(args)
owner = Hub::Context::GitReader.new.read_config("hub.owner")
origin = $1 if Hub::Context::GitReader.new.
read_config("remote.origin.url") =~ /git@github.com:([^\/]+)\/.+/
curbranch = `git symbolic-ref HEAD`.split(/\//).last.strip rescue nil
issue = $1 if curbranch =~ /issue_([[:digit:]]+)_/
errmsg = [ issue.nil? ? "no issue" : nil,
origin.nil? ? "no origin" : nil,
curbranch.nil? ? "no current branch" : nil,
owner.nil? ? "no owner" : nil,
].compact
abort "Y U Fail? #{errmsg.join(',')}" unless errmsg.empty?
puts "git pull-request -i #{issue} -h #{origin}:#{curbranch} -b #{owner}:master"
exit
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment