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 20, 2016
1 parent 74a9d4a commit 9984f90
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 10 deletions.
12 changes: 12 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_location and then merges your base_branch into your feature_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 -r <remote_location> -b <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 merge into your feature_branch. The remote_location defaults to "origin" and the base_branch defaults to "master". This command also takes in remote and branch name as flag options.

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

Expand All @@ -112,6 +122,8 @@ We assume you know what you're doing, so if you need something different, do it

After making commits to your branch, run +review+. Didn't push it up? We don't care, we'll do it for you.

git reflow review -t <title> -m <message>

If you do not pass the title or message options to the review command, you will be given an editor to write your PR request in, similar to `git commit`. The first line is the title, the rest is the body.

$ git reflow review
Expand Down
27 changes: 27 additions & 0 deletions lib/git_reflow/commands/refresh.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
desc 'Updates and synchronizes your base branch and feature 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 <remote_location> <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.desc 'updates base_branch based on remote and merges the base with your feature_branch'
c.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)'
c.flag [:r,:remote], default_value: 'origin'
c.flag [:b,:branch], default_value: 'master'
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
refresh_options = {
:remote => options[:remote] || 'origin',
:branch => options[:branch] || 'master',
}

GitReflow.update_feature_branch(refresh_options)
end
end
6 changes: 3 additions & 3 deletions lib/git_reflow/commands/review.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
c.flag [:t, :title], default_value: 'last commit message'
c.flag [:m, :message], default_value: 'title'
c.action do |global_options,options,args|
if global_options[:title] || global_options[:message]
if options[:title] || options[:message]
review_options = {
'base' => args[0],
'title' => global_options[:title],
'body' => global_options[:message]
'title' => options[:title],
'body' => options[:message]
}
else
review_options = { 'base' => args[0] }
Expand Down
28 changes: 21 additions & 7 deletions lib/git_reflow/git_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,40 @@ def get_first_commit_message
run('git log --pretty=format:"%s" --no-merges -n 1', loud: false).strip
end

def push_current_branch
run_command_with_label "git push origin #{current_branch}"
def push_current_branch(options = {})
remote = options[:remote] || "origin"
run_command_with_label "git push #{remote} #{current_branch}"
end

def update_current_branch
run_command_with_label "git pull origin #{current_branch}"
push_current_branch
def update_current_branch(options = {})
remote = options[:remote] || "origin"
run_command_with_label "git pull #{remote} #{current_branch}"
push_current_branch(options)
end

def fetch_destination(destination_branch)
run_command_with_label "git fetch origin #{destination_branch}"
end

def update_destination(destination_branch)
def update_destination(destination_branch, remote = 'origin')
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 pull #{remote} #{destination_branch}"
run_command_with_label "git checkout #{origin_branch}"
end

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

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

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

def merge_feature_branch(feature_branch_name, options = {})
options[:destination_branch] ||= 'master'

Expand Down
17 changes: 17 additions & 0 deletions spec/lib/git_reflow/git_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,23 @@ module Gitacular
end
end

describe ".update_feature_branch" do
options = {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 remote feature",
"git push remote 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 9984f90

Please sign in to comment.