Skip to content

Instantly share code, notes, and snippets.

@shinypb
Created December 5, 2014 17:25
Show Gist options
  • Save shinypb/7d6014025cb7be2484d0 to your computer and use it in GitHub Desktop.
Save shinypb/7d6014025cb7be2484d0 to your computer and use it in GitHub Desktop.
Create GitHub pull request from the command line
#!/usr/bin/env ruby
remote_url = `git config --get remote.origin.url`.strip
matches = remote_url.match(/github\.com:(.+)\/(.+)\.git/)
if matches
branch_name = `git rev-parse --abbrev-ref HEAD`.strip
exec "open https://github.com/#{matches[1]}/#{matches[2]}/compare/#{branch_name}?expand=1"
end
# Save this file as ~/bin/git-pr and then `chmod +x ~/bin/git-pr`.
# Then, you can create a pull request for your current branch by typing `git pr`. Woohoo!
@stephanwehner
Copy link

This looks safer (in case the matches have unexpected, evil characters):

 exec "open", "https://github.com/#{matches[1]}/#{matches[2]}/compare/#{branch_name}?expand=1"

From http://www.ruby-doc.org/core-2.1.5/Kernel.html#method-i-exec :

In the second form (exec("command1", "arg1", ...)), the first is taken as a command name and the rest are passed as parameters to command with no shell expansion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment