Module: Gitlab::Git

Extended by:
EncodingHelper
Defined in:
lib/gitlab/git.rb,
lib/gitlab/git/ref.rb,
lib/gitlab/git/tag.rb,
lib/gitlab/git/blob.rb,
lib/gitlab/git/diff.rb,
lib/gitlab/git/push.rb,
lib/gitlab/git/tree.rb,
lib/gitlab/git/user.rb,
lib/gitlab/git/util.rb,
lib/gitlab/git/blame.rb,
lib/gitlab/git/index.rb,
lib/gitlab/git/branch.rb,
lib/gitlab/git/commit.rb,
lib/gitlab/git/changes.rb,
lib/gitlab/git/compare.rb,
lib/gitlab/git/version.rb,
lib/gitlab/git/hook_env.rb,
lib/gitlab/git/diff_tree.rb,
lib/gitlab/git/wiki_file.rb,
lib/gitlab/git/wiki_page.rb,
lib/gitlab/git/base_error.rb,
lib/gitlab/git/blame_mode.rb,
lib/gitlab/git/cross_repo.rb,
lib/gitlab/git/merge_base.rb,
lib/gitlab/git/repository.rb,
lib/gitlab/git/bundle_file.rb,
lib/gitlab/git/keep_around.rb,
lib/gitlab/git/lfs_changes.rb,
lib/gitlab/git/object_pool.rb,
lib/gitlab/git/path_helper.rb,
lib/gitlab/git/changed_path.rb,
lib/gitlab/git/commit_stats.rb,
lib/gitlab/git/conflict/file.rb,
lib/gitlab/git/patches/patch.rb,
lib/gitlab/git/remote_mirror.rb,
lib/gitlab/git/conflict/parser.rb,
lib/gitlab/git/diff_collection.rb,
lib/gitlab/git/raw_diff_change.rb,
lib/gitlab/git/blame_pagination.rb,
lib/gitlab/git/declared_license.rb,
lib/gitlab/git/lfs_pointer_file.rb,
lib/gitlab/git/attributes_parser.rb,
lib/gitlab/git/conflict/resolver.rb,
lib/gitlab/git/gitmodules_parser.rb,
lib/gitlab/git/operation_service.rb,
lib/gitlab/git/pre_receive_error.rb,
lib/gitlab/git/wiki_page_version.rb,
lib/gitlab/git/patches/collection.rb,
lib/gitlab/git/repository_cleaner.rb,
app/policies/gitlab/git/tag_policy.rb,
lib/gitlab/git/conflict/resolution.rb,
lib/gitlab/git/finders/refs_finder.rb,
lib/gitlab/git/wraps_gitaly_errors.rb,
lib/gitlab/git/repository_mirroring.rb,
lib/gitlab/git/diff_stats_collection.rb,
lib/gitlab/git/patches/commit_patches.rb,
lib/gitlab/git/reference_update_error.rb,
lib/gitlab/git/attributes_at_ref_parser.rb,
lib/gitlab/git/process_commit_worker_pool.rb

Defined Under Namespace

Modules: Conflict, Finders, Patches, RepositoryMirroring, Util, Version, WrapsGitalyErrors Classes: AttributesAtRefParser, AttributesParser, BaseError, Blame, BlameLine, BlameMode, BlamePagination, Blob, Branch, BundleFile, ChangedPath, Changes, Commit, CommitStats, Compare, CrossRepo, DeclaredLicense, Diff, DiffCollection, DiffStatsCollection, DiffTree, GitmodulesParser, HookEnv, Index, KeepAround, LfsChanges, LfsPointerFile, MergeBase, ObjectPool, OperationService, PathHelper, PreReceiveError, ProcessCommitWorkerPool, Push, RawDiffChange, Ref, ReferenceNotFoundError, ReferenceUpdateError, RemoteMirror, Repository, RepositoryCleaner, ResourceExhaustedError, Tag, TagPolicy, Tree, User, WikiFile, WikiPage, WikiPageVersion

Constant Summary collapse

SHA1_EMPTY_TREE_ID =
'4b825dc642cb6eb9a060e54bf8d69288fbee4904'
SHA256_EMPTY_TREE_ID =
'6ef19b41225c5369f1c104d45d8d85efa9b057b53b14b4b9b939dd74decc5321'
SHA1_BLANK_SHA =
('0' * 40).freeze
SHA256_BLANK_SHA =
('0' * 64).freeze
COMMIT_ID =
/\A#{Gitlab::Git::Commit::RAW_FULL_SHA_PATTERN}\z/
TAG_REF_PREFIX =
"refs/tags/"
BRANCH_REF_PREFIX =
"refs/heads/"
SHA_LIKE_REF =
%r{\A(#{TAG_REF_PREFIX}|#{BRANCH_REF_PREFIX})#{Gitlab::Git::Commit::RAW_FULL_SHA_PATTERN}\z}
ATTRIBUTE_OVERRIDES =

NOTE: We don’t use linguist anymore, but we’d still want to support it to be backward/GitHub compatible. Using ‘gitlab-*` prefixed overrides going forward would give us a better control and flexibility.

{
  generated: %w[gitlab-generated linguist-generated]
}.freeze
CommandError =
Class.new(BaseError)
CommitError =
Class.new(BaseError)
OSError =
Class.new(BaseError)
AmbiguousRef =
Class.new(BaseError)
CommandTimedOut =
Class.new(CommandError)
InvalidPageToken =
Class.new(BaseError)
InvalidRefFormatError =
Class.new(BaseError)
ReferencesLockedError =
Class.new(BaseError)
ReferenceStateMismatchError =
Class.new(BaseError)

Constants included from EncodingHelper

EncodingHelper::BOM_UTF8, EncodingHelper::ENCODING_CONFIDENCE_THRESHOLD, EncodingHelper::ESCAPED_CHARS, EncodingHelper::UNICODE_REPLACEMENT_CHARACTER

Class Method Summary collapse

Methods included from EncodingHelper

binary_io, detect_binary?, detect_encoding, detect_libgit2_binary?, encode!, encode_binary, encode_utf8, encode_utf8_no_detect, encode_utf8_with_escaping!, encode_utf8_with_replacement_character, force_encode_utf8, strip_bom, unquote_path

Class Method Details

.blank_ref?(ref) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/gitlab/git.rb', line 92

def blank_ref?(ref)
  ref == SHA1_BLANK_SHA || ref == SHA256_BLANK_SHA
end

.branch_name(ref) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/gitlab/git.rb', line 66

def branch_name(ref)
  ref = ref.to_s
  if self.branch_ref?(ref)
    self.ref_name(ref)
  else
    nil
  end
end

.branch_ref?(ref) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/gitlab/git.rb', line 88

def branch_ref?(ref)
  ref =~ /^#{BRANCH_REF_PREFIX}.+/o
end

.check_namespace!(*objects) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/gitlab/git.rb', line 104

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

.commit_id?(ref) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/gitlab/git.rb', line 96

def commit_id?(ref)
  COMMIT_ID.match?(ref)
end

.diff_line_code(file_path, new_line_position, old_line_position) ⇒ Object



113
114
115
# File 'lib/gitlab/git.rb', line 113

def diff_line_code(file_path, new_line_position, old_line_position)
  "#{Digest::SHA1.hexdigest(file_path)}_#{old_line_position}_#{new_line_position}"
end

.ref_name(ref, types: 'tags|heads|remotes') ⇒ Object



62
63
64
# File 'lib/gitlab/git.rb', line 62

def ref_name(ref, types: 'tags|heads|remotes')
  encode_utf8_with_escaping!(ref).sub(%r{\Arefs/(#{types})/}, '')
end

.shas_eql?(sha1, sha2) ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/gitlab/git.rb', line 117

def shas_eql?(sha1, sha2)
  return true if sha1.nil? && sha2.nil?
  return false if sha1.nil? || sha2.nil?
  return false unless sha1.instance_of?(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

  # Optimization: prevent unnecessary substring creation
  if sha1.length == sha2.length
    sha1 == sha2
  else
    sha1[0, length] == sha2[0, length]
  end
end

.tag_name(ref) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/gitlab/git.rb', line 75

def tag_name(ref)
  ref = ref.to_s
  if self.tag_ref?(ref)
    self.ref_name(ref)
  else
    nil
  end
end

.tag_ref?(ref) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/gitlab/git.rb', line 84

def tag_ref?(ref)
  ref =~ /^#{TAG_REF_PREFIX}.+/o
end

.versionObject



100
101
102
# File 'lib/gitlab/git.rb', line 100

def version
  Gitlab::Git::Version.git_version
end