2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
require_dependency 'gitlab/encoding_helper'
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
module Gitlab
|
|
|
|
module Git
|
2018-10-15 14:42:47 +05:30
|
|
|
# The ID of empty tree.
|
|
|
|
# https://github.com/git/git/blob/3ad8b5bf26362ac67c9020bf8c30eee54a84f56d/cache.h#L1011-L1012
|
2019-12-04 20:38:33 +05:30
|
|
|
EMPTY_TREE_ID = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'
|
2016-04-02 18:10:28 +05:30
|
|
|
BLANK_SHA = ('0' * 40).freeze
|
2019-10-12 21:52:04 +05:30
|
|
|
COMMIT_ID = /\A[0-9a-f]{40}\z/.freeze
|
2019-12-04 20:38:33 +05:30
|
|
|
TAG_REF_PREFIX = "refs/tags/"
|
|
|
|
BRANCH_REF_PREFIX = "refs/heads/"
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
CommandError = Class.new(BaseError)
|
|
|
|
CommitError = Class.new(BaseError)
|
|
|
|
OSError = Class.new(BaseError)
|
|
|
|
UnknownRef = Class.new(BaseError)
|
2021-02-22 17:27:13 +05:30
|
|
|
CommandTimedOut = Class.new(CommandError)
|
2021-12-11 22:18:48 +05:30
|
|
|
InvalidPageToken = Class.new(BaseError)
|
2022-08-13 15:12:31 +05:30
|
|
|
InvalidRefFormatError = Class.new(BaseError)
|
|
|
|
ReferencesLockedError = Class.new(BaseError)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
class << self
|
2017-09-10 17:25:29 +05:30
|
|
|
include Gitlab::EncodingHelper
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
def ref_name(ref)
|
2022-10-11 01:57:18 +05:30
|
|
|
encode_utf8_with_escaping!(ref).sub(%r{\Arefs/(tags|heads|remotes)/}, '')
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def branch_name(ref)
|
|
|
|
ref = ref.to_s
|
|
|
|
if self.branch_ref?(ref)
|
|
|
|
self.ref_name(ref)
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def tag_name(ref)
|
|
|
|
ref = ref.to_s
|
|
|
|
if self.tag_ref?(ref)
|
|
|
|
self.ref_name(ref)
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
def tag_ref?(ref)
|
2022-08-27 11:52:29 +05:30
|
|
|
ref =~ /^#{TAG_REF_PREFIX}.+/o
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def branch_ref?(ref)
|
2022-08-27 11:52:29 +05:30
|
|
|
ref =~ /^#{BRANCH_REF_PREFIX}.+/o
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def blank_ref?(ref)
|
|
|
|
ref == BLANK_SHA
|
|
|
|
end
|
2016-01-14 18:37:52 +05:30
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
def commit_id?(ref)
|
|
|
|
COMMIT_ID.match?(ref)
|
|
|
|
end
|
|
|
|
|
2016-01-14 18:37:52 +05:30
|
|
|
def version
|
2018-11-08 19:23:39 +05:30
|
|
|
Gitlab::Git::Version.git_version
|
2016-01-14 18:37:52 +05:30
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
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)
|
2020-03-13 15:44:24 +05:30
|
|
|
return true if sha1.nil? && sha2.nil?
|
2018-03-17 18:26:18 +05:30
|
|
|
return false if sha1.nil? || sha2.nil?
|
2021-09-30 23:02:18 +05:30
|
|
|
return false unless sha1.instance_of?(sha2.class)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
# Optimization: prevent unnecessary substring creation
|
|
|
|
if sha1.length == sha2.length
|
|
|
|
sha1 == sha2
|
|
|
|
else
|
|
|
|
sha1[0, length] == sha2[0, length]
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|