From de848e460c41380c3ffe40498519407a40a7ecfd Mon Sep 17 00:00:00 2001 From: Kelly Stannard Date: Fri, 29 Aug 2014 10:17:10 -0400 Subject: [PATCH] Check if branch contains commit This allows the user to see if an arbitrary commit is contained within a branch. For example you can check to see if a branch was merged into master with `git.branch('master').contains?('feature')`. Or you can see if your feature branch has the latest master commit with `git.branch('feature').contains?('master')` --- README.md | 1 + lib/git/branch.rb | 4 ++++ lib/git/lib.rb | 3 +++ tests/units/test_branch.rb | 8 ++++++++ 4 files changed, 16 insertions(+) diff --git a/README.md b/README.md index 6758d5ee..8d7ffc9e 100644 --- a/README.md +++ b/README.md @@ -203,6 +203,7 @@ And here are the operations that will need to write to your git repository. g.branch('new_branch').checkout g.branch('new_branch').delete g.branch('existing_branch').checkout + g.branch('master').contains?('existing_branch') g.checkout('new_branch') g.checkout(g.branch('new_branch')) diff --git a/lib/git/branch.rb b/lib/git/branch.rb index 4f69e0cd..17573af6 100644 --- a/lib/git/branch.rb +++ b/lib/git/branch.rb @@ -60,6 +60,10 @@ def current determine_current end + def contains?(commit) + !@base.lib.branch_contains(commit, self.name).empty? + end + def merge(branch = nil, message = nil) if branch in_branch do diff --git a/lib/git/lib.rb b/lib/git/lib.rb index 00cc0e72..8374425b 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -317,6 +317,9 @@ def branch_current branches_all.select { |b| b[1] }.first[0] rescue nil end + def branch_contains(commit, branch_name="") + command("branch", [branch_name, "--contains", commit]) + end # returns hash # [tree-ish] = [[line_no, match], [line_no, match2]] diff --git a/tests/units/test_branch.rb b/tests/units/test_branch.rb index fee70abc..8f83a6d9 100644 --- a/tests/units/test_branch.rb +++ b/tests/units/test_branch.rb @@ -44,6 +44,14 @@ def test_branches_single end end + def test_true_branch_contains? + assert(@git.branch('git_grep').contains?('master')) + end + + def test_false_branch_contains? + assert(!@git.branch('master').contains?('git_grep')) + end + def test_branch_commit assert_equal(270, @git.branches[:test_branches].gcommit.size) end