Skip to content

Commit de848e4

Browse files
Kelly StannardKelly Wolf Stannard
authored andcommitted
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')`
1 parent 667de5a commit de848e4

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
@@ -203,6 +203,7 @@ And here are the operations that will need to write to your git repository.
203203
g.branch('new_branch').checkout
204204
g.branch('new_branch').delete
205205
g.branch('existing_branch').checkout
206+
g.branch('master').contains?('existing_branch')
206207

207208
g.checkout('new_branch')
208209
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
@@ -317,6 +317,9 @@ def branch_current
317317
branches_all.select { |b| b[1] }.first[0] rescue nil
318318
end
319319

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

321324
# returns hash
322325
# [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)