lib/r10k/git/repository.rb in r10k-1.2.0rc1 vs lib/r10k/git/repository.rb in r10k-1.2.0rc2
- old
+ new
@@ -28,32 +28,72 @@
#
# @param [String] pattern
#
# @return [String] The dereferenced hash of `pattern`
def resolve_ref(pattern)
- commit = nil
- begin
- all_commits = git ['show-ref', '-s', pattern], :git_dir => git_dir
- commit = all_commits.lines.first
- rescue R10K::Util::Subprocess::SubprocessError
- end
+ commit = resolve_tag(pattern)
+ commit ||= resolve_remote_head(pattern)
+ commit ||= resolve_head(pattern)
+ commit ||= resolve_commit(pattern)
- if commit.nil?
- begin
- commit = git ['rev-parse', "#{ref}^{commit}"], :git_dir => git_dir
- rescue R10K::Util::Subprocess::SubprocessError
- end
- end
-
if commit
commit.chomp
else
raise R10K::Git::UnresolvableRefError.new(:ref => pattern, :git_dir => git_dir)
end
end
alias rev_parse resolve_ref
+ def resolve_tag(pattern)
+ output = git ['show-ref', '--tags', '-s', pattern], :git_dir => git_dir, :raise_on_fail => false
+
+ if output.success?
+ output.stdout.lines.first
+ end
+ end
+
+ def resolve_head(pattern)
+ output = git ['show-ref', '--heads', '-s', pattern], :git_dir => git_dir, :raise_on_fail => false
+
+ if output.success?
+ output.stdout.lines.first
+ end
+ end
+
+ def resolve_remote_head(pattern, remote = 'origin')
+ pattern = "refs/remotes/#{remote}/#{pattern}"
+ output = git ['show-ref', '-s', pattern], :git_dir => git_dir, :raise_on_fail => false
+
+ if output.success?
+ output.stdout.lines.first
+ end
+ end
+
+ # Define the same interface for resolving refs.
+ def resolve_commit(pattern)
+ output = git ['rev-parse', "#{pattern}^{commit}"], :git_dir => git_dir, :raise_on_fail => false
+
+ if output.success?
+ output.stdout.chomp
+ end
+ end
+
+ # @return [Hash<String, String>] A hash of remote names and fetch URLs
+ # @api private
+ def remotes
+ output = git ['remote', '-v'], :git_dir => git_dir
+
+ ret = {}
+ output.stdout.each_line do |line|
+ next if line.match /\(push\)/
+ name, url, _ = line.split(/\s+/)
+ ret[name] = url
+ end
+
+ ret
+ end
+
private
# Fetch objects and refs from the given git remote
#
# @param remote [#to_s] The remote name to fetch from
@@ -67,16 +107,19 @@
# @param opts [Hash] opts
#
# @option opts [String] :path
# @option opts [String] :git_dir
# @option opts [String] :work_tree
+ # @option opts [String] :raise_on_fail
#
# @raise [R10K::ExecutionFailure] If the executed command exited with a
# nonzero exit code.
#
# @return [String] The git command output
def git(cmd, opts = {})
+ raise_on_fail = opts.fetch(:raise_on_fail, true)
+
argv = %w{git}
if opts[:path]
argv << "--git-dir" << File.join(opts[:path], '.git')
argv << "--work-tree" << opts[:path]
@@ -90,14 +133,13 @@
end
argv.concat(cmd)
subproc = R10K::Util::Subprocess.new(argv)
- subproc.raise_on_fail = true
+ subproc.raise_on_fail = raise_on_fail
subproc.logger = self.logger
result = subproc.execute
- # todo ensure that logging always occurs even if the command fails to run
- result.stdout
+ result
end
end