Skip to content

Commit

Permalink
resolves #138: git-pr
Browse files Browse the repository at this point in the history
  • Loading branch information
charlierudolph committed Dec 2, 2014
1 parent 9d0a532 commit 40d22eb
Show file tree
Hide file tree
Showing 17 changed files with 177 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and assumes you use a central code repository like
* [git extract](/documentation/git-extract.md) - copy selected commits from the current branch into their own branch
* [git hack](/documentation/git-hack.md) - cut a new feature branch off the main branch
* [git kill](/documentation/git-kill.md) - remove an obsolete feature branch
* [git pr](/documentation/git-pr.md) - create a new pull request
* [git prune-branches](/documentation/git-prune-branches.md) - delete merged branches
* [git ship](/documentation/git-ship.md) - deliver a completed feature branch
* [git sync](/documentation/git-sync.md) - update the current branch with all relevant changes
Expand Down
16 changes: 16 additions & 0 deletions documentation/git-pr.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#### NAME

git-pr - create a new pull request


#### SYNOPSIS

```
git pr
```


#### DESCRIPTION

Opens a browser to a new pull request for the current branch.
Supported only for repositories hosted on Github and Bitbucket.
11 changes: 11 additions & 0 deletions features/git-pr/bitbucket_https.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Feature: git-pr when origin is on Bitbucket over HTTPS

Background:
Given I have a feature branch named "feature"
And my remote origin is on Bitbucket through HTTPS
And I am on the "feature" branch
When I run `git pr`


Scenario:
Then I see a browser window for a new pull request on Bitbucket for the "feature" branch
11 changes: 11 additions & 0 deletions features/git-pr/bitbucket_ssh.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Feature: git-pr when origin is on Bitbucket over SSH

Background:
Given I have a feature branch named "feature"
And my remote origin is on Bitbucket through SSH
And I am on the "feature" branch
When I run `git pr`


Scenario:
Then I see a browser window for a new pull request on Bitbucket for the "feature" branch
11 changes: 11 additions & 0 deletions features/git-pr/github_https.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Feature: git-pr when origin is on Github over HTTPS

Background:
Given I have a feature branch named "feature"
And my remote origin is on Github through HTTPS
And I am on the "feature" branch
When I run `git pr`


Scenario:
Then I see a browser window for a new pull request on Github for the "feature" branch
11 changes: 11 additions & 0 deletions features/git-pr/github_ssh.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Feature: git-pr when origin is on Github over SSH

Background:
Given I have a feature branch named "feature"
And my remote origin is on Github through SSH
And I am on the "feature" branch
When I run `git pr`


Scenario:
Then I see a browser window for a new pull request on Github for the "feature" branch
10 changes: 10 additions & 0 deletions features/git-pr/unsupported_hosting_service.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Feature: git-pr when origin is unsupported

Background:
Given I have a feature branch named "feature"
And I am on the "feature" branch
When I run `git pr` while allowing errors


Scenario:
Then I get the error "Unsupported hosting service. Pull requests can only be created on Bitbucket and Github"
5 changes: 5 additions & 0 deletions features/step_definitions/remote_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@

run "git remote add upstream #{upstream_remote_repository_path}"
end


Given(/^my remote origin is on (Github|Bitbucket) through (HTTPS|SSH)$/) do |domain, protocol|
run "git remote set-url origin #{remote_url domain, protocol}"
end
6 changes: 6 additions & 0 deletions features/step_definitions/run_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,9 @@
)
end


Then(/I see a browser open to a new pull request on (Github|Bitbucket) for the "(.+)" branch/) do |domain, branch_name|
url = remote_pull_request_url domain, branch_name
expect(@last_run_result.out).to eql "open called with: #{url}\n"
end
1 change: 1 addition & 0 deletions features/support/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'open4'
require 'rspec'

SHELL_OVERRIDE_DIRECTORY = "#{File.dirname(__FILE__)}/shell_overrides"

# The files to ignore when checking files
IGNORED_FILES = %w(tags)
Expand Down
30 changes: 28 additions & 2 deletions features/support/remote_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
def remote_url name
output_of "git remote -v | grep '#{name}.*fetch' | awk '{print $2}'"
# Returns the remote URL for a new pull request for the given domain and branch
def remote_pull_request_url domain, branch_name
if domain == 'Bitbucket'
sha = recent_commit_shas(1).join('')[0, 12]
"https://bitbucket.org/Originate/git-town/pull-request/new?source=Originate%2Fgit-town%3A#{sha}%3A#{branch_name}"
elsif domain == 'Github'
"https://github.com/Originate/git-town/compare/#{branch_name}?expand=1"
else
fail "Unknown domain: #{domain}"
end
end


# Returns the remote URL for domain / protocol
def remote_url domain, protocol
"#{remote_url_prefix domain, protocol}Originate/git-town.git"
end


# Returns the remote URL prefix for the given domain and protocol
def remote_url_prefix domain, protocol
case [domain, protocol]
when %w(Bitbucket HTTPS) then 'https://username@bitbucket.org/'
when %w(Bitbucket SSH) then 'git@bitbucket.org:'
when %w(Github HTTPS) then 'https://github.com/'
when %w(Github SSH) then 'git@github.com:'
else fail "Unknown domain/protocol pairing: #{domain}/#{protocol}"
end
end
9 changes: 6 additions & 3 deletions features/support/run_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def integer_output_of command


def git_town_command? command
%w(extract hack prune-branches ship sync-fork sync kill).any? do |subcommand|
%w(extract hack pr prune-branches ship sync-fork sync kill).any? do |subcommand|
command.starts_with? "git #{subcommand}"
end
end
Expand All @@ -35,14 +35,17 @@ def run command, allow_failures: false, debug: false, input: nil
print_result(result) if should_error || should_print_command_output?(command, debug)
fail 'Command not successful!' if should_error

@last_run_result = result
@last_run_result = result if git_town_command?(command)

result
end


def run_shell_command command, input
result = OpenStruct.new(command: command, location: Dir.pwd.split(/[_\/]/).last)
command = "PATH=#{SHELL_OVERRIDE_DIRECTORY}:$PATH; #{command} 2>&1"

status = Open4.popen4("#{command} 2>&1") do |_pid, stdin, stdout, _stderr|
status = Open4.popen4(command) do |_pid, stdin, stdout, _stderr|
stdin.puts input if input
stdin.close
result.out = stdout.read
Expand Down
3 changes: 3 additions & 0 deletions features/support/shell_overrides/open
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

echo "open called with: $@"
26 changes: 26 additions & 0 deletions git-pr
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash
source "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/helpers/helpers.sh"


# Returns the source for a new Bitbucket pull request
function bitbucket_source {
sha=$(git log --format="%H" -1 | cut -c-12)
echo "$repository:$sha:$branch" | sed 's/\//%2F/g' | sed 's/\:/%3A/g'
}


branch=$initial_branch_name
domain=$(remote_domain)
repository=$(remote_repository_name)

if [ "$domain" == github.com ]; then
pr_path="compare/$branch?expand=1"
elif [ "$domain" == bitbucket.org ]; then
pr_path="pull-request/new?source=$(bitbucket_source)"
else
echo_error_header
echo_usage "Unsupported hosting service. Pull requests can only be created on Bitbucket and Github"
exit_with_error
fi

open "https://$domain/$repository/$pr_path"
17 changes: 15 additions & 2 deletions helpers/git_helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -392,12 +392,25 @@ function remote_merged_branches {
}


# Returns the url for the remote with the specified name
# Returns the url for the remote
function remote_url {
git remote -v | grep "$1.*fetch" | awk '{print $2}'
git remote -v | grep "origin.*fetch" | awk '{print $2}'
}


# Returns the domain of the remote repository
function remote_domain {
remote_url | sed -E "s#(https?://([^@]*@)?|git@)([^/:]+).*#\3#"
}


# Returns the USER/REPO for the remote repository
function remote_repository_name {
remote_url | sed "s#.*[:/]\([^/]*/[^/]*\)\.git#\1#"
}



# Resets the current branch to the commit described by the given SHA
function reset_to_sha {
local sha=$1
Expand Down
12 changes: 12 additions & 0 deletions man/man1/git-pr.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.TH "GIT-PRUNE-BRANCHES" "1" "11/13/2014" "Git Town 0\&.4\&.0" "Git Town Manual"

.SH "NAME"
git-pr \- create a new pull request

.SH "SYNOPSIS"
\fIgit pr\fR

.SH "DESCRIPTION"
Opens a browser to a new pull request for the current branch.
.br
Supported only for repositories hosted on Github and Bitbucket.
4 changes: 4 additions & 0 deletions man/man1/git-town.1
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ cut a new feature branch off the main branch
.B git kill
remove an obsolete feature branch

.TP 4
.B git pr
create a new pull request

.TP 4
.B git prune-branches
delete merged branches
Expand Down

0 comments on commit 40d22eb

Please sign in to comment.