Skip to content
This repository has been archived by the owner on Oct 5, 2023. It is now read-only.

Commit

Permalink
Merge pull request #92 from socialcast/show-unmerged-branches
Browse files Browse the repository at this point in the history
expose branchdiff functionality via the cli
  • Loading branch information
seanwalbran committed Feb 17, 2016
2 parents 7c130ab + 4596bd9 commit b64305b
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ruby-2.2.2
ruby-2.2.4
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: ruby
rvm:
- 1.9.3
- 2.0.0
- 2.1.1
- 2.2.4
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ integrate the current feature branch into an aggregate branch (ex: prototype, st

Find pull requests on github including a given commit

## git branchdiff <branch> <base_branch (optional, default: master)>

List branches merged into remote origin/`branch` and not also merged into origin/`base_branch`

## git reviewrequest

create a pull request on github for peer review of the current branch.
Expand Down
4 changes: 4 additions & 0 deletions bin/git-branchdiff
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env ruby

require File.join(File.dirname(__FILE__), '..', 'lib', 'socialcast-git-extensions', 'cli.rb')
Socialcast::Gitx::CLI.start (['branchdiff'] + ARGV)
12 changes: 12 additions & 0 deletions lib/socialcast-git-extensions/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,18 @@ def nuke(bad_branch)
post message.strip
end

desc 'branchdiff', 'show branches merged into one remote branch but not merged into another (default = master)'
def branchdiff(branch = nil, other_branch = 'master')
branch ||= ask "What remote branch would you like to compare against '#{other_branch}' (ex: staging)?"
run_cmd "git fetch origin"
results = branch_difference(branch, other_branch)
if results.any?
say "\nBranches in origin/#{branch} and not in origin/#{other_branch}:\n\n#{results.join("\n")}\n\n"
else
say "\nNo branches found in origin/#{branch} that are not also in origin/#{other_branch}\n\n"
end
end

desc 'release', 'release the current branch to production'
def release
branch = current_branch
Expand Down
11 changes: 8 additions & 3 deletions lib/socialcast-git-extensions/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ def branches(options = {})
branches.uniq
end

# retrieve a list of branches merged into one remote branch but not merged into another
def branch_difference(branch, other_branch)
branches(:remote => true, :merged => "origin/#{branch}") - branches(:remote => true, :merged => "origin/#{other_branch}")
end

# reset the specified branch to the same set of commits as the destination branch
# reverts commits on aggregate branches back to a known good state
# returns list of branches that were removed
Expand All @@ -87,7 +92,7 @@ def nuke_branch(branch, head_branch)

run_cmd "git checkout #{base_branch}"
refresh_branch_from_remote head_branch
removed_branches = branches(:remote => true, :merged => "origin/#{branch}") - branches(:remote => true, :merged => "origin/#{head_branch}")
removed_branches = branch_difference(branch, head_branch)
run_cmd "git branch -D #{branch}" rescue nil
run_cmd "git push origin --delete #{branch}" rescue nil
run_cmd "git checkout -b #{branch}"
Expand Down Expand Up @@ -151,8 +156,8 @@ def changelog_summary(branch)
dir
end
dir_counts = Hash.new(0)
dirs.each {|dir| dir_counts[dir] += 1 }
changes = dir_counts.to_a.sort_by {|k,v| v}.reverse.first(5).map {|k,v| "#{k} (#{v} file#{'s' if v > 1})"}
dirs.each { |dir| dir_counts[dir] += 1 }
changes = dir_counts.to_a.sort_by { |k, v| [-v, k] }.first(5).map { |k, v| "#{k} (#{v} file#{'s' if v > 1})" }
else
changes = changes.map do |line|
added, removed, filename = line.split
Expand Down
41 changes: 41 additions & 0 deletions spec/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,47 @@ def stub_message(message_body, params = {})
end
end

describe '#branchdiff' do
subject(:branchdiff) { Socialcast::Gitx::CLI.start(['branchdiff'] + args) }
let(:said_messages) { [] }
before do
expect_any_instance_of(Socialcast::Gitx::CLI).to receive(:run_cmd).with('git fetch origin')
allow_any_instance_of(Socialcast::Gitx::CLI).to receive(:say) do |_instance, msg|
said_messages << msg
end
end
context 'with one branch-name argument' do
let(:args) { ['my-branch'] }
before do
expect_any_instance_of(Socialcast::Gitx::CLI).to receive(:branch_difference).with('my-branch', 'master').and_return(['dummy_branch'])
branchdiff
end
it do
expect(said_messages).to eq ["\nBranches in origin/my-branch and not in origin/master:\n\ndummy_branch\n\n"]
end
end
context 'with two branch-name arguments' do
let(:args) { ['my-branch', 'other-branch'] }
before do
expect_any_instance_of(Socialcast::Gitx::CLI).to receive(:branch_difference).with('my-branch', 'other-branch').and_return(['dummy_branch'])
branchdiff
end
it do
expect(said_messages).to eq ["\nBranches in origin/my-branch and not in origin/other-branch:\n\ndummy_branch\n\n"]
end
end
context 'when no results are found' do
let(:args) { ['my-branch'] }
before do
expect_any_instance_of(Socialcast::Gitx::CLI).to receive(:branch_difference).with('my-branch', 'master').and_return([])
branchdiff
end
it do
expect(said_messages).to eq ["\nNo branches found in origin/my-branch that are not also in origin/master\n\n"]
end
end
end

describe '#nuke' do
before { allow_any_instance_of(Socialcast::Gitx::CLI).to receive(:branches).and_return([]) }
context 'when target branch == staging and --destination == last_known_good_staging' do
Expand Down
20 changes: 17 additions & 3 deletions spec/git_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@
let(:test_instance) { TestClass.new }
subject { test_instance }

describe '#branch_difference' do
subject { test_instance.send(:branch_difference, branch, other_branch) }
let(:other_branch) { 'master' }
let(:branch) { 'my-branch' }
before do
allow(test_instance).to receive(:branches) do |options|
expect(options[:remote]).to be_truthy
next %w(branch_a branch_b branch_c branch_z) if options[:merged] == "origin/#{branch}"
%w(branch_b branch_d branch_e branch_z) if options[:merged] == "origin/#{other_branch}"
end
end
it { is_expected.to eq %w(branch_a branch_c) }
end

describe '#changelog_summary' do
subject { test_instance.send(:changelog_summary, branch) }
let(:base_branch) { 'master' }
Expand Down Expand Up @@ -64,13 +78,13 @@
1 0 doc/images.md
EOS
end
it 'summarizes the changes by directory' do
it 'summarizes the changes by directory, sorting by count desc then alpha asc' do
is_expected.to eq <<-EOS.strip_heredoc
engines/shoelaces/spec/models (2 files)
lib/tasks (1 file)
script (1 file)
doc (1 file)
engines/shoelaces/app/models (1 file)
lib/tasks (1 file)
script (1 file)
6 files changed, 35 insertions(+), 129 deletions(-)
EOS
end
Expand Down

0 comments on commit b64305b

Please sign in to comment.