Skip to content

Commit 3bbf612

Browse files
kwstannardperlun
authored andcommitted
Check if branch contains commit (ruby-git#174)
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')`
1 parent 27715e9 commit 3bbf612

File tree

4 files changed

+16
-0
lines changed

4 files changed

+16
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ And here are the operations that will need to write to your git repository.
202202
g.branch('new_branch').checkout
203203
g.branch('new_branch').delete
204204
g.branch('existing_branch').checkout
205+
g.branch('master').contains?('existing_branch')
205206

206207
g.checkout('new_branch')
207208
g.checkout(g.branch('new_branch'))

lib/git/branch.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ def current
6060
determine_current
6161
end
6262

63+
def contains?(commit)
64+
!@base.lib.branch_contains(commit, self.name).empty?
65+
end
66+
6367
def merge(branch = nil, message = nil)
6468
if branch
6569
in_branch do

lib/git/lib.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,9 @@ def branch_current
318318
branches_all.select { |b| b[1] }.first[0] rescue nil
319319
end
320320

321+
def branch_contains(commit, branch_name="")
322+
command("branch", [branch_name, "--contains", commit])
323+
end
321324

322325
# returns hash
323326
# [tree-ish] = [[line_no, match], [line_no, match2]]

tests/units/test_branch.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ def test_branches_single
4444
end
4545
end
4646

47+
def test_true_branch_contains?
48+
assert(@git.branch('git_grep').contains?('master'))
49+
end
50+
51+
def test_false_branch_contains?
52+
assert(!@git.branch('master').contains?('git_grep'))
53+
end
54+
4755
def test_branch_commit
4856
assert_equal(270, @git.branches[:test_branches].gcommit.size)
4957
end

0 commit comments

Comments
 (0)