Skip to content
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

Speed up tags lookup #44

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 11 additions & 4 deletions lib/thor-scmversion/git_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@

module ThorSCMVersion
class GitVersion < ScmVersion
class << self
def all_from_path(path)
class << self
def tags_from_path(path)
Dir.chdir(path) do
tags = Open3.popen3("git tag") { |stdin, stdout, stderr| stdout.read }.split(/\n/)
tags.select { |tag| tag.match(ScmVersion::VERSION_FORMAT) }
.collect { |tag| from_tag(tag) }
.select { |tag| contained_in_current_branch?(tag) }.sort.reverse
end
end

def all_from_path(path)
tags_from_path(path).select { |tag| contained_in_current_branch?(tag) }.sort.reverse
end

def latest_from_path(path)
tags_from_path(path).sort.reverse.find { |tag| contained_in_current_branch?(tag) }
end

def contained_in_current_branch?(tag)
ShellUtils.sh("git branch --contains #{tag}") =~ /\*/
end
Expand All @@ -20,7 +27,7 @@ def retrieve_tags
ShellUtils.sh("git fetch --all")
end
end

def tag
begin
ShellUtils.sh "git tag -a -m \"Version #{self}\" #{self}"
Expand Down
26 changes: 15 additions & 11 deletions lib/thor-scmversion/p4_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@ class MissingP4ConfigException < StandardError
def initialize(config_option)
@config_option = config_option
end

def message
"#{@config_option} is not set in your environment."
end
end

module Perforce
class << self
def check_environment
["P4PORT","P4USER", "P4PASSWD", "P4CLIENT"].each {|config|
raise MissingP4ConfigException.new(config) if ENV[config].nil? or ENV[config].empty?
}
end

def set
ShellUtils.sh "p4 set"
end

def parse_and_set_p4_set
p4_set = set
parsed_p4_config = p4_set.split("\n").inject({}) do |p4_config, line|
Expand All @@ -35,10 +35,10 @@ def parse_and_set_p4_set
p4_config[key] = value
p4_config
end

parsed_p4_config.each {|key,value| ENV[key] = value}
end

def connection
parse_and_set_p4_set
check_environment
Expand All @@ -63,13 +63,17 @@ def all_from_path(path)

if current_versions.empty?
first_instance = new(0, 0, 0)
end
end

current_versions << first_instance if current_versions.empty?
current_versions
end
end

def latest_from_path(path)
all_from_path(path).first
end

def depot_path(path)
path = File.expand_path(path)
path = path.gsub(File::Separator, File::ALT_SEPARATOR) if ThorSCMVersion.windows?
Expand All @@ -85,7 +89,7 @@ def parse_label(label, p4_module_name)
end

def get_thor_scmversion_labels(labels, p4_module_name)
labels.select{|label| label.split(" ")[1].gsub("#{p4_module_name}-", "").match(ScmVersion::VERSION_FORMAT)}
labels.select{|label| label.split(" ")[1].gsub("#{p4_module_name}-", "").match(ScmVersion::VERSION_FORMAT)}
end
end

Expand All @@ -94,7 +98,7 @@ def initialize(major = 0, minor = 0, patch = 0, prerelease = nil, build = 1)
self.p4_module_name = self.class.module_name('.')
super
end

attr_accessor :version_file_path
attr_accessor :p4_depot_path
attr_accessor :p4_module_name
Expand All @@ -103,7 +107,7 @@ def retrieve_tags
# noop
# p4 always has labels available, you just have to ask the server for them.
end

def tag
if ThorSCMVersion.windows?
`type "#{File.expand_path(get_p4_label_file).gsub(File::Separator, File::ALT_SEPARATOR)}" | p4 label -i`
Expand All @@ -120,7 +124,7 @@ def auto_bump(options)
private

def get_label_name
"#{p4_module_name}-#{self}"
"#{p4_module_name}-#{self}"
end

def get_p4_label_template
Expand Down
18 changes: 9 additions & 9 deletions lib/thor-scmversion/scm_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def versioner
# author Josiah Kiehl <[email protected]>
class ScmVersion
include Comparable

# Tags not matching this format will not show up in the tags list
#
# Examples:
Expand All @@ -35,7 +35,7 @@ class << self
# @return [Array<ScmVersion>]
def from_path(path = '.')
retrieve_tags
all_from_path(path).first || new(0,0,1)
latest_from_path(path) || new(0,0,1)
end

# Create an ScmVersion object from a tag
Expand Down Expand Up @@ -69,7 +69,7 @@ def initialize(major = 0, minor = 0, patch = 0, prerelease = nil, build = 1)
end

# Bumps the version in place
#
#
# @param [Symbol] type Type of bump to be performed
# @param [String] prerelease_type Type of prerelease to bump to when doing a :prerelease bump
# @return [ScmVersion]
Expand All @@ -78,7 +78,7 @@ def bump!(type, options = {})
when :auto
self.auto_bump(options)
when :major
self.major += 1
self.major += 1
when :minor
self.minor += 1
when :patch
Expand Down Expand Up @@ -114,12 +114,12 @@ def reset_for(type)
self.minor = 0
}],
[:minor, Proc.new {
self.patch = 0
self.patch = 0
}],
[:patch, Proc.new {
self.prerelease = nil
}],
[:prerelease, Proc.new {
[:prerelease, Proc.new {
self.build = 1
}]].each do |matcher, reset_proc|
next unless matched or type.to_sym == matcher
Expand All @@ -134,14 +134,14 @@ def reset_for(type)
# @param [Array<String>] files List of files to write
def write_version(files = [ScmVersion::VERSION_FILENAME])
files.each do |ver_file|
File.open(ver_file, 'w+') do |f|
File.open(ver_file, 'w+') do |f|
f.write self.to_s
end
end
self
end

# Create the tag in the SCM corresponding to the version contained in self.
# Create the tag in the SCM corresponding to the version contained in self.
# Abstract method. Must be implemented by subclasses.
def tag
raise NotImplementedError
Expand All @@ -164,7 +164,7 @@ def to_s
def <=>(other)
return unless other.is_a?(self.class)
return 0 if self.version == other.version

[:major, :minor, :patch, :prerelease, :build].each do |segment|
next if self.send(segment) == other.send(segment)
return 1 if self.send(segment) > other.send(segment)
Expand Down
19 changes: 19 additions & 0 deletions spec/lib/thor-scmversion/git_version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,24 @@ module ThorSCMVersion
OUT
expect(GitVersion.contained_in_current_branch?('0.0.1')).to be_true
end

describe '::latest_from_path' do
it 'returns the latest tag' do
Open3.stub(:popen3).and_yield(nil, StringIO.new("0.0.1\n0.0.2"), nil)
ShellUtils.stub(:sh).and_return(<<OUT)
* constrain_bump_to_branch
master
OUT
GitVersion.stub(:contained_in_current_branch?).and_return(true)

expect(GitVersion.latest_from_path('.').to_s).to eq('0.0.2')
end

it 'only shells out once if multiple tags are included in the branch' do
Open3.stub(:popen3).and_yield(nil, StringIO.new("0.0.1\n0.0.2"), nil)
expect(ShellUtils).to receive(:sh).once.and_return('*')
GitVersion.latest_from_path('.')
end
end
end
end