From d5df53b17ab0acd8ac3f118f144a968b43ceb60e Mon Sep 17 00:00:00 2001 From: Simon Zhu Date: Mon, 18 Apr 2016 14:40:31 -0700 Subject: [PATCH] [Issue #66 + #74] Implementing "Git Reflow Refresh" --- README.rdoc | 12 +++++++++++ lib/git_reflow/commands/refresh.rb | 28 +++++++++++++++++++++++++ lib/git_reflow/commands/review.rb | 6 +++--- lib/git_reflow/git_helpers.rb | 28 ++++++++++++++++++------- spec/lib/git_reflow/git_helpers_spec.rb | 17 +++++++++++++++ 5 files changed, 81 insertions(+), 10 deletions(-) create mode 100644 lib/git_reflow/commands/refresh.rb diff --git a/README.rdoc b/README.rdoc index 633f9e0..f83e1f1 100644 --- a/README.rdoc +++ b/README.rdoc @@ -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 -b + +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 @@ -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 -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 diff --git a/lib/git_reflow/commands/refresh.rb b/lib/git_reflow/commands/refresh.rb new file mode 100644 index 0000000..e2ececa --- /dev/null +++ b/lib/git_reflow/commands/refresh.rb @@ -0,0 +1,28 @@ +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], + :branch => options[:branch] + } + + GitReflow.update_feature_branch(refresh_options) + end +end diff --git a/lib/git_reflow/commands/review.rb b/lib/git_reflow/commands/review.rb index 9d5878e..7a8c684 100644 --- a/lib/git_reflow/commands/review.rb +++ b/lib/git_reflow/commands/review.rb @@ -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] } diff --git a/lib/git_reflow/git_helpers.rb b/lib/git_reflow/git_helpers.rb index df480cb..62d23b2 100644 --- a/lib/git_reflow/git_helpers.rb +++ b/lib/git_reflow/git_helpers.rb @@ -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' diff --git a/spec/lib/git_reflow/git_helpers_spec.rb b/spec/lib/git_reflow/git_helpers_spec.rb index e33a23f..f9e14f4 100644 --- a/spec/lib/git_reflow/git_helpers_spec.rb +++ b/spec/lib/git_reflow/git_helpers_spec.rb @@ -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' }