Created
January 31, 2020 20:01
-
-
Save lak/53eeee3737eb29938aa0a3c96235fb70 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
## git-publish-branch: a simple script to publish the current branch | |
## to your origin repository, and then track that so you can easily | |
## push further updates. | |
## | |
## Opens the URL for a pull request after pushing. | |
## | |
## Usage: git publish-branch | |
## | |
require 'optparse' | |
options = {} | |
option_parser = OptionParser.new do |parser| | |
parser.banner = "Usage: git publish-branch | |
Publishes the current branch to your origin repository, using | |
the same name, so it tracks the remote repo and can easily | |
have updates pushed to it. | |
After publishing, it opens the link to create a pull request | |
on github. | |
" | |
parser.on("-h", "Print program help") do | |
puts parser.help | |
exit(0) | |
end | |
options[:upstream] = "origin" | |
parser.on("-u", "--upstream", "Set the upstream repository. Defaults to 'origin'.") do |upstream| | |
options[:upstream] = upstream | |
end | |
end | |
option_parser.parse! | |
# Figure out the current branch | |
begin | |
current_branch = %x{git branch}.split("\n").find { |line| line =~ /^\*/ }.sub("* ", '') | |
rescue => detail | |
STDERR.print "Could not get current branch: #{detail}" | |
end | |
# Publish it on the server, with tracking to make it easy to push further | |
# updates | |
begin | |
output = %x{git push --set-upstream #{options[:upstream]} #{current_branch} 2>&1} | |
rescue => detail | |
STDERR.print "Could not push branch: #{detail}" | |
end | |
# Find and open the link to create a PR | |
line = output.split("\n").find { |line| line =~ /https:/ } | |
pr_url = line.sub(/remote:\s+/, '').sub(/\s+$/, '') | |
system("open #{pr_url}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment