Skip to content

Commit

Permalink
[Issue reenhanced#66 + reenhanced#74] Implementing "Git Reflow Refresh"
Browse files Browse the repository at this point in the history
  • Loading branch information
simonzhu24 committed Apr 18, 2016
1 parent 74a9d4a commit 150f8f7
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 12 deletions.
10 changes: 10 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ Then for your Enterprise projects, you have to setup GitReflow for each one:
cd replace_with_your_enterprise_project_path
git reflow setup --enterprise --local

=== Refreshing your current branch based on your base branch

git reflow refresh

This command updates your feature_branch and base_branch according to the remote and then merges your feature_branch with the base_branch. This is just a handy command to keep your branches up to date at any point in time if someone else has committed to the base_branch or the remote.

git reflow refresh remote_location base_branch_name

You pass in the name of the remote to fetch from and the name of the base branch that you would like to rebase your current branch on.

=== Starting a feature branch
http://reenhanced.com/reflow/git-reflow-start.gif

Expand Down
21 changes: 21 additions & 0 deletions lib/git_reflow/commands/refresh.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
desc 'refresh will merge your current branch based on the base branch'
long_desc <<LONGTIME
Performs the following:\n
\t$ git checkout <base_branch>\n
\t$ git pull <remote_location> <base_branch>\n
\t$ git checkout <current_branch>\n
\t$ git pull origin <current_branch>\n
\t$ git push origin <current_branch>\n
\t$ git merge <base_branch>\n
LONGTIME
arg_name '[remote_location] - remote repository name to fetch updates from (origin by default), [base_branch] - branch that you want to merge with (master by default)'
command :refresh do |c|
c.action do |global_options, options, args|
# usage: git reflow refresh [remote_location - defaults to origin] [base_branch_name - defaults to master]
# defaults remote_location to origin
remote_location = args.length < 2 ? "origin" : args[1]
base_branch = args.length < 1 ? "master" : args[0]

GitReflow.update_feature_branch({base_branch: base_branch, remote: remote_location})
end
end
23 changes: 18 additions & 5 deletions lib/git_reflow/git_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,24 @@ def fetch_destination(destination_branch)
run_command_with_label "git fetch origin #{destination_branch}"
end

def update_destination(destination_branch)
origin_branch = current_branch
run_command_with_label "git checkout #{destination_branch}"
run_command_with_label "git pull origin #{destination_branch}"
run_command_with_label "git checkout #{origin_branch}"
def update_destination(options = {})
feature_branch = options[:feature_branch]
base_branch = options[:base_branch]
remote = options[:remote] || 'origin'
run_command_with_label "git checkout #{base_branch}"
run_command_with_label "git pull #{remote} #{base_branch}"
run_command_with_label "git checkout #{feature_branch}"
end

def update_feature_branch(options = {})
base_branch = options[:base_branch]
update_destination(options)

# update feature branch in case there are multiple authors and remote changes
update_current_branch

# rebase on base branch
run_command_with_label "git merge #{base_branch}"
end

def merge_feature_branch(feature_branch_name, options = {})
Expand Down
30 changes: 23 additions & 7 deletions spec/lib/git_reflow/git_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,16 @@ module Gitacular
end

describe ".update_destination(destination_branch)" do
let(:current_branch) { 'bananas' }
let(:destination_branch) { 'monkey-business' }
let(:feature_branch) { 'bananas' }
let(:base_branch) { 'monkey-business' }

before { allow(Gitacular).to receive(:current_branch).and_return(current_branch) }
subject { Gitacular.update_destination(destination_branch) }
subject { Gitacular.update_destination({feature_branch: feature_branch, base_branch: base_branch}) }

it "updates the destination branch with the latest code from the remote repo" do
expect { subject }.to have_run_commands_in_order [
"git checkout #{destination_branch}",
"git pull origin #{destination_branch}",
"git checkout #{current_branch}"
"git checkout #{base_branch}",
"git pull origin #{base_branch}",
"git checkout #{feature_branch}",
]
end
end
Expand All @@ -100,6 +99,23 @@ module Gitacular
end
end

describe ".update_feature_branch" do
options = {feature_branch: "feature", base_branch: "base", remote: "remote"}
subject { Gitacular.update_feature_branch(options) }
before { allow(Gitacular).to receive(:current_branch).and_return('feature') }

it "calls the correct methods" do
expect { subject }.to have_run_commands_in_order [
"git checkout base",
"git pull remote base",
"git checkout feature",
"git pull origin feature",
"git push origin feature",
"git merge base"
]
end
end

describe ".merge_feature_branch(options)" do
let(:destination_branch) { 'monkey-business' }
let(:feature_branch) { 'bananas' }
Expand Down

0 comments on commit 150f8f7

Please sign in to comment.