git: Simplify the git-opr command
Rewritten in bash and as a wrapper around GitHub's `gh` command. - Pushed an unpushed branch to origin. - Opens an existing pull request if one exists. - Creates a new pull request if one doesn't exist.
This commit is contained in:
parent
fc8cdb3ee4
commit
e76f02f64b
118
bin/git-opr
118
bin/git-opr
|
@ -1,106 +1,26 @@
|
||||||
#!/usr/bin/env ruby
|
#!/bin/bash
|
||||||
#/ Usage: git pr [<branch>]
|
|
||||||
#/ Open the pull request page for <branch>, or the current branch if not
|
|
||||||
#/ specified. Lands on the new pull request page when no PR exists yet.
|
|
||||||
#/ The local branch must be tracking the remote branch.
|
|
||||||
|
|
||||||
# Based on script from @rtomayko. Translated into ruby and now uses branch
|
set -e
|
||||||
# tracking rather than expecting local and remote branch to have same name
|
|
||||||
|
|
||||||
require "json"
|
ensure_is_published() {
|
||||||
require "yaml"
|
[[ ! $is_published ]] && git publish
|
||||||
|
}
|
||||||
|
|
||||||
OPEN_SWITCHES = %{-o --open}
|
is_published() {
|
||||||
|
echo $(git upstream)
|
||||||
|
}
|
||||||
|
|
||||||
def remote_url
|
open_or_build_pull_request() {
|
||||||
url = chomped_system_call('git config --get remote.origin.url')
|
type gh &>/dev/null
|
||||||
repo_with_owner = url.match(/:(.*)\.git/)
|
|
||||||
|
|
||||||
if repo_with_owner
|
if [ $? -ne 0 ]; then
|
||||||
repo_with_owner[1]
|
echo "Error: gh command not found."
|
||||||
else
|
|
||||||
die 'Unable to determine repo/owner for remote origin. Using https?'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def oauth_token
|
|
||||||
hub_config = YAML.load_file(File.expand_path('~/.config/hub'))
|
|
||||||
hub_config['github.com'][0]['oauth_token']
|
|
||||||
end
|
|
||||||
|
|
||||||
def open_or_build_pull_request
|
|
||||||
auth = "Authorization: token #{oauth_token}"
|
|
||||||
pulls_endpoint = "https://api.github.com/repos/#{remote_url}/pulls"
|
|
||||||
response = `curl --silent -H '#{auth}' #{pulls_endpoint}`
|
|
||||||
pulls = JSON.parse(response)
|
|
||||||
pull = pulls.detect { |pull| pull["head"]["ref"] == remote_tracking_branch }
|
|
||||||
if !pull.nil?
|
|
||||||
system "open-tab --reload #{pull['html_url']}"
|
|
||||||
else
|
|
||||||
system("open https://github.com/#{remote_url}/pull/#{remote_tracking_branch}")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def remote_tracking_branch
|
|
||||||
command = 'git rev-parse --abbrev-ref --symbolic-full-name @{u} 2>/dev/null'
|
|
||||||
tracking = chomped_system_call(command)
|
|
||||||
|
|
||||||
if tracking == '@{u}'
|
|
||||||
die 'Current local branch not setup to track remote branch.'
|
|
||||||
else
|
|
||||||
branch_without_remote_name(tracking)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def branch_without_remote_name(branch_and_remote)
|
|
||||||
branch = branch_and_remote.match(/origin\/(.*)/)
|
|
||||||
|
|
||||||
if branch
|
|
||||||
branch[1]
|
|
||||||
else
|
|
||||||
die 'Expected remote to be \'origin\''
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def chomped_system_call(command)
|
|
||||||
`#{command}`.chomp
|
|
||||||
end
|
|
||||||
|
|
||||||
def open_pull_request_for_repo_branch(repo, branch)
|
|
||||||
system("open https://github.com/#{repo}/pull/#{branch}")
|
|
||||||
end
|
|
||||||
|
|
||||||
def opening?
|
|
||||||
argument = ARGV.pop
|
|
||||||
argument && OPEN_SWITCHES.include?(argument)
|
|
||||||
end
|
|
||||||
|
|
||||||
def ensure_published
|
|
||||||
if not_published?
|
|
||||||
system("git publish")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def not_published?
|
|
||||||
!system("git upstream > /dev/null 2>&1")
|
|
||||||
end
|
|
||||||
|
|
||||||
def main
|
|
||||||
# repo = remote_url
|
|
||||||
# branch = remote_tracking_branch
|
|
||||||
if opening?
|
|
||||||
system 'git log --reverse master..HEAD --format="%H%n%s%n%b" > .git/pr-commits'
|
|
||||||
system 'gh compare'
|
|
||||||
system 'gh pull-request'
|
|
||||||
else
|
|
||||||
ensure_published
|
|
||||||
open_or_build_pull_request
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def die(msg)
|
|
||||||
puts msg
|
|
||||||
exit 1
|
exit 1
|
||||||
end
|
fi
|
||||||
|
|
||||||
main
|
# Load an existing PR, or create a new one.
|
||||||
|
gh pr view --web || gh pr create --assignee opdavies --web
|
||||||
|
}
|
||||||
|
|
||||||
|
ensure_is_published
|
||||||
|
open_or_build_pull_request
|
||||||
|
|
Loading…
Reference in a new issue