Skip to content

Check if branch contains commit #174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 12, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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')`
  • Loading branch information
Kelly Stannard authored and Kelly Wolf Stannard committed Apr 5, 2018
commit de848e460c41380c3ffe40498519407a40a7ecfd
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
4 changes: 4 additions & 0 deletions lib/git/branch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions lib/git/lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down
8 changes: 8 additions & 0 deletions tests/units/test_branch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down