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:
Oliver Davies 2021-05-21 23:23:36 +01:00
parent fc8cdb3ee4
commit e76f02f64b

View file

@ -1,106 +1,26 @@
#!/usr/bin/env ruby
#/ 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.
#!/bin/bash
# Based on script from @rtomayko. Translated into ruby and now uses branch
# tracking rather than expecting local and remote branch to have same name
set -e
require "json"
require "yaml"
ensure_is_published() {
[[ ! $is_published ]] && git publish
}
OPEN_SWITCHES = %{-o --open}
is_published() {
echo $(git upstream)
}
def remote_url
url = chomped_system_call('git config --get remote.origin.url')
repo_with_owner = url.match(/:(.*)\.git/)
open_or_build_pull_request() {
type gh &>/dev/null
if repo_with_owner
repo_with_owner[1]
else
die 'Unable to determine repo/owner for remote origin. Using https?'
end
end
if [ $? -ne 0 ]; then
echo "Error: gh command not found."
exit 1
fi
def oauth_token
hub_config = YAML.load_file(File.expand_path('~/.config/hub'))
hub_config['github.com'][0]['oauth_token']
end
# Load an existing PR, or create a new one.
gh pr view --web || gh pr create --assignee opdavies --web
}
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
end
main
ensure_is_published
open_or_build_pull_request