Skip to content

Commit

Permalink
Merge pull request #55 from betesh/implement-porcelain-describe-and-grep
Browse files Browse the repository at this point in the history
Implement porcelain describe
  • Loading branch information
bartkamphorst authored May 7, 2020
2 parents d718812 + 7c0a62b commit 0589b42
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lib/rjgit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@ def self.version

module Porcelain

import 'java.io.IOException'
import 'org.eclipse.jgit.lib.Constants'
import 'org.eclipse.jgit.api.AddCommand'
import 'org.eclipse.jgit.api.CommitCommand'
import 'org.eclipse.jgit.api.BlameCommand'
import 'org.eclipse.jgit.api.errors.RefNotFoundException'
import 'org.eclipse.jgit.blame.BlameGenerator'
import 'org.eclipse.jgit.blame.BlameResult'
import 'org.eclipse.jgit.errors.IncorrectObjectTypeException'
import 'org.eclipse.jgit.errors.InvalidPatternException'
import 'org.eclipse.jgit.errors.MissingObjectException'
import 'org.eclipse.jgit.treewalk.CanonicalTreeParser'
import 'org.eclipse.jgit.diff.DiffFormatter'

Expand Down Expand Up @@ -163,6 +168,28 @@ def self.diff(repository, options = {})
RJGit.convert_diff_entries(diff_entries)
end

def self.describe(repository, ref, options = {})
options = {:always => false, :long => false, :tags => false, :match => []}.merge(options)
repo = RJGit.repository_type(repository)
git = RubyGit.new(repo).jgit
command = git.describe.
set_always(options[:always]).
set_long(options[:long]).
set_tags(options[:tags])
begin
command = command.set_target(ref)
rescue IncorrectObjectTypeException, IOException, MissingObjectException, RefNotFoundException
return nil
end
options[:match].each do |match|
begin
command = command.set_match(match)
rescue InvalidPatternException
return nil
end
end
command.call
end
end

module Plumbing
Expand Down
22 changes: 22 additions & 0 deletions spec/rjgit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,28 @@
end
end

describe 'git-describe' do
it "mimics git-describe" do
id = Porcelain.commit(@repo, "Initial commit").id
expect(Porcelain.describe(@repo, id)).to match(/\Av0\.0-2-g[0-9a-f]{7}\z/)
end

it "matches the right tag when match is used" do
@repo.git.tag('v0.1', 'Tag v0.1', Porcelain.commit(@repo, "Previous commit").id)
id = Porcelain.commit(@repo, "Initial commit").id
expect(Porcelain.describe(@repo, id, match: ['v0.0'])).to match(/\Av0\.0-4-g[0-9a-f]{7}\z/)
expect(Porcelain.describe(@repo, id, match: ['v0.1'])).to match(/\Av0\.1-1-g[0-9a-f]{7}\z/)
expect(Porcelain.describe(@repo, id, match: ['v0.0', 'v0.1'])).to match(/\Av0\.1-1-g[0-9a-f]{7}\z/)
expect(Porcelain.describe(@repo, id, match: ['v0.*'])).to match(/\Av0\.1-1-g[0-9a-f]{7}\z/)
expect(Porcelain.describe(@repo, id, match: ['v99'])).to be_nil
expect(Porcelain.describe(@repo, id, match: ['*[a'])).to be_nil # Swallows NoClosingBracketException, a type of InvalidPatternException
end

it "returns nil when the ref doesn't exist" do
expect(Porcelain.describe(@repo, 'abcdef123456')).to be_nil # Swallows RefNotFoundException
end
end

after(:all) do
@repo = nil
remove_temp_repo(@temp_repo_path)
Expand Down

0 comments on commit 0589b42

Please sign in to comment.