Skip to content

Commit

Permalink
resolves #188: git-ship with no shippable changes
Browse files Browse the repository at this point in the history
  • Loading branch information
charlierudolph committed Nov 30, 2014
1 parent 2ef502b commit 52fd94e
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 46 deletions.
7 changes: 0 additions & 7 deletions features/git-ship/current_branch/errors.feature
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
Feature: Git Ship: errors while shipping the current branch

Scenario: does not ship a feature branch not ahead of main
Given I am on a feature branch
When I run `git ship -m 'feature done'` while allowing errors
Then I get the error "The branch 'feature' has no commits to merge into 'main'."
And I end up on the "feature" branch


Scenario: does not ship the main branch
Given I am on the main branch
When I run `git ship -m 'feature done'` while allowing errors
Expand Down
15 changes: 15 additions & 0 deletions features/git-ship/current_branch/no_diff.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Feature: Git Ship: errors when the branch diff is empty

Background:
Given I have a feature branch named "feature"
And the following commit exists in my repository
| branch | location | file name | file content |
| main | remote | common_file | common content |
| feature | local | common_file | common content |
And I am on the "feature" branch
When I run `git ship -m 'feature done'` while allowing errors


Scenario: result
Then I get the error "The branch 'feature' has no shippable changes"
And I am still on the "feature" branch
10 changes: 0 additions & 10 deletions features/git-ship/supplied_branch/errors_with_open_changes.feature
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,6 @@ Feature: Git Ship: errors while shipping the supplied branch with open changes
And I still have an uncommitted file with name: "uncommitted" and content: "stuff"


Scenario: does not ship a feature branch that is not ahead of main
Given I have feature branches named "feature" and "other_feature"
And I am on the "other_feature" branch
And I have an uncommitted file with name: "uncommitted" and content: "stuff"
When I run `git ship feature -m 'feature done'` while allowing errors
Then I get the error "The branch 'feature' has no commits to merge into 'main'."
And I end up on the "other_feature" branch
And I still have an uncommitted file with name: "uncommitted" and content: "stuff"


Scenario: does not ship the main branch
Given I am on the "feature" branch
And I have an uncommitted file with name: "uncommitted" and content: "stuff"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ Feature: Git Ship: errors while shipping the supplied branch without open change
And I end up on the "feature" branch


Scenario: does not ship a feature branch that is not ahead of main
Given I have feature branches named "feature" and "other_feature"
And I am on the "other_feature" branch
When I run `git ship feature -m 'feature done'` while allowing errors
Then I get the error "The branch 'feature' has no commits to merge into 'main'."
And I end up on the "other_feature" branch


Scenario: does not ship the main branch
Given I am on the "feature" branch
When I run `git ship main -m 'feature done'` while allowing errors
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Feature: Git Ship: errors when the branch diff is empty with open changes

Background:
Given I have feature branches named "feature" and "other_feature"
And the following commit exists in my repository
| branch | location | file name | file content |
| main | remote | common_file | common content |
| feature | local | common_file | common content |
And I am on the "other_feature" branch
And I have an uncommitted file with name: "uncommitted" and content: "stuff"
When I run `git ship feature` while allowing errors


Scenario: result
Then I get the error "The branch 'feature' has no shippable changes"
And I am still on the "other_feature" branch
And I still have an uncommitted file with name: "uncommitted" and content: "stuff"
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Feature: Git Ship: errors when the branch diff is empty without open changes

Background:
Given I have feature branches named "feature" and "other_feature"
And the following commit exists in my repository
| branch | location | file name | file content |
| main | remote | common_file | common content |
| feature | local | common_file | common content |
And I am on the "other_feature" branch
When I run `git ship feature` while allowing errors


Scenario: result
Then I get the error "The branch 'feature' has no shippable changes"
And I am still on the "other_feature" branch
2 changes: 1 addition & 1 deletion features/support/run_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def run command, allow_failures: false, debug: false, input: nil
def run_shell_command command, input
result = OpenStruct.new(command: command, location: Dir.pwd.split(/[_\/]/).last)

status = Open4.popen4("#{command} 2>&1") do |_pid, stdin, stdout, stderr|
status = Open4.popen4("#{command} 2>&1") do |_pid, stdin, stdout, _stderr|
stdin.puts input if input
stdin.close
result.out = stdout.read
Expand Down
15 changes: 1 addition & 14 deletions git-ship
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,6 @@ source "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/helpers/helpers.sh"
export abort_script_filename=$(abort_script_filename_for_command ship)



# Exit if the current branch is not ahead of main
function ensure_ahead_of_main_branch {
local current_branch_name=$(get_current_branch_name)
if [ "$(is_ahead_of_main "$current_branch_name")" == false ]; then
return_to_initial_branch

echo_error_header
echo_error "The branch '$current_branch_name' has no commits to merge into '$main_branch_name'."
exit_with_error
fi
}

# Called when squash_merge fails due to empty commit message
function error_empty_commit {
discard_open_changes
Expand Down Expand Up @@ -95,7 +82,7 @@ function perform_ship {
checkout_branch "$target_branch_name"
pull_branch
merge_branch "$main_branch_name"
ensure_ahead_of_main_branch
ensure_has_shippable_changes
checkout_main_branch
squash_merge "$target_branch_name" "$message"
push_branch
Expand Down
25 changes: 19 additions & 6 deletions helpers/git_helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,19 @@ function ensure_has_branch {
}


# Exit if the current branch does not have shippable changes
function ensure_has_shippable_changes {
local current_branch_name=$(get_current_branch_name)
if [ "$(has_shippable_changes "$current_branch_name")" == false ]; then
return_to_initial_branch

echo_error_header
echo_error "The branch '$current_branch_name' has no shippable changes."
exit_with_error
fi
}


# Exits the application with an error message if the supplied branch is
# not a feature branch
function ensure_is_feature_branch {
Expand Down Expand Up @@ -262,21 +275,21 @@ function has_open_changes {
}


# Determines whether the given branch has a remote tracking branch.
function has_tracking_branch {
# Determines whether the given branch has shippable changes
function has_shippable_changes {
local branch_name=$1
if [ "$(git branch -r | tr -d ' ' | grep -c "^origin\/$branch_name\$")" == 0 ]; then
if [ "$(git diff --quiet "$main_branch_name..$branch_name" ; echo $?)" == 0 ]; then
echo false
else
echo true
fi
}


# Determines whether the given branch is ahead of main
function is_ahead_of_main {
# Determines whether the given branch has a remote tracking branch.
function has_tracking_branch {
local branch_name=$1
if [ "$(git log --oneline "$main_branch_name..$branch_name" | wc -l | tr -d ' ')" == 0 ]; then
if [ "$(git branch -r | tr -d ' ' | grep -c "^origin\/$branch_name\$")" == 0 ]; then
echo false
else
echo true
Expand Down

0 comments on commit 52fd94e

Please sign in to comment.