gitlab-org--gitlab-foss/lib/gitlab/git.rb

97 lines
2.5 KiB
Ruby
Raw Normal View History

require_dependency 'gitlab/encoding_helper'
module Gitlab
module Git
2018-04-11 23:05:07 -04:00
# The ID of empty tree.
# See http://stackoverflow.com/a/40884093/1856239 and
# https://github.com/git/git/blob/3ad8b5bf26362ac67c9020bf8c30eee54a84f56d/cache.h#L1011-L1012
EMPTY_TREE_ID = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'.freeze
BLANK_SHA = ('0' * 40).freeze
2015-11-02 13:02:08 -05:00
TAG_REF_PREFIX = "refs/tags/".freeze
BRANCH_REF_PREFIX = "refs/heads/".freeze
BaseError = Class.new(StandardError)
CommandError = Class.new(BaseError)
CommitError = Class.new(BaseError)
OSError = Class.new(BaseError)
UnknownRef = Class.new(BaseError)
class << self
2017-06-28 08:20:29 -04:00
include Gitlab::EncodingHelper
def ref_name(ref)
2018-01-27 00:35:53 -05:00
encode!(ref).sub(%r{\Arefs/(tags|heads|remotes)/}, '')
end
def branch_name(ref)
ref = ref.to_s
if self.branch_ref?(ref)
self.ref_name(ref)
else
nil
end
end
def committer_hash(email:, name:)
2016-09-20 12:07:52 -04:00
return if email.nil? || name.nil?
{
email: email,
name: name,
time: Time.now
}
end
def tag_name(ref)
ref = ref.to_s
if self.tag_ref?(ref)
self.ref_name(ref)
else
nil
end
end
def tag_ref?(ref)
ref.start_with?(TAG_REF_PREFIX)
end
def branch_ref?(ref)
ref.start_with?(BRANCH_REF_PREFIX)
end
def blank_ref?(ref)
ref == BLANK_SHA
end
2015-12-14 20:30:55 -05:00
def version
Gitlab::Git::Version.git_version
2015-12-14 20:30:55 -05:00
end
def check_namespace!(*objects)
expected_namespace = self.name + '::'
objects.each do |object|
unless object.class.name.start_with?(expected_namespace)
raise ArgumentError, "expected object in #{expected_namespace}, got #{object}"
end
end
end
def diff_line_code(file_path, new_line_position, old_line_position)
"#{Digest::SHA1.hexdigest(file_path)}_#{old_line_position}_#{new_line_position}"
end
def shas_eql?(sha1, sha2)
return false if sha1.nil? || sha2.nil?
return false unless sha1.class == sha2.class
# If either of the shas is below the minimum length, we cannot be sure
# that they actually refer to the same commit because of hash collision.
length = [sha1.length, sha2.length].min
return false if length < Gitlab::Git::Commit::MIN_SHA_LENGTH
sha1[0, length] == sha2[0, length]
end
end
end
end