summaryrefslogtreecommitdiff
path: root/lib/gitlab/git
diff options
context:
space:
mode:
Diffstat (limited to 'lib/gitlab/git')
-rw-r--r--lib/gitlab/git/blame.rb40
-rw-r--r--lib/gitlab/git/diff.rb11
-rw-r--r--lib/gitlab/git/diff_collection.rb17
-rw-r--r--lib/gitlab/git/ref.rb2
-rw-r--r--lib/gitlab/git/repository.rb4
5 files changed, 53 insertions, 21 deletions
diff --git a/lib/gitlab/git/blame.rb b/lib/gitlab/git/blame.rb
index 5669a65cbd9..30977adaea1 100644
--- a/lib/gitlab/git/blame.rb
+++ b/lib/gitlab/git/blame.rb
@@ -5,35 +5,45 @@ module Gitlab
class Blame
include Gitlab::EncodingHelper
- attr_reader :lines, :blames
+ attr_reader :lines, :blames, :range
- def initialize(repository, sha, path)
+ def initialize(repository, sha, path, range: nil)
@repo = repository
@sha = sha
@path = path
+ @range = range
@lines = []
@blames = load_blame
end
def each
@blames.each do |blame|
- yield(blame.commit, blame.line)
+ yield(blame.commit, blame.line, blame.previous_path)
end
end
private
+ def range_spec
+ "#{range.first},#{range.last}" if range
+ end
+
def load_blame
- output = encode_utf8(@repo.gitaly_commit_client.raw_blame(@sha, @path))
+ output = encode_utf8(
+ @repo.gitaly_commit_client.raw_blame(@sha, @path, range: range_spec)
+ )
process_raw_blame(output)
end
def process_raw_blame(output)
+ start_line = nil
lines = []
final = []
info = {}
commits = {}
+ commit_id = nil
+ previous_paths = {}
# process the output
output.split("\n").each do |line|
@@ -45,6 +55,15 @@ module Gitlab
commit_id = m[1]
commits[commit_id] = nil unless commits.key?(commit_id)
info[m[3].to_i] = [commit_id, m[2].to_i]
+
+ # Assumption: the first line returned by git blame is lowest-numbered
+ # This is true unless we start passing it `--incremental`.
+ start_line = m[3].to_i if start_line.nil?
+ elsif line.start_with?("previous ")
+ # previous 1485b69e7b839a21436e81be6d3aa70def5ed341 initial-commit
+ # previous 9521e52704ee6100e7d2a76896a4ef0eb53ff1b8 "\303\2511\\\303\251\\303\\251\n"
+ # ^ char index 50
+ previous_paths[commit_id] = unquote_path(line[50..])
end
end
@@ -54,7 +73,13 @@ module Gitlab
# get it together
info.sort.each do |lineno, (commit_id, old_lineno)|
- final << BlameLine.new(lineno, old_lineno, commits[commit_id], lines[lineno - 1])
+ final << BlameLine.new(
+ lineno,
+ old_lineno,
+ commits[commit_id],
+ lines[lineno - start_line],
+ previous_paths[commit_id]
+ )
end
@lines = final
@@ -62,13 +87,14 @@ module Gitlab
end
class BlameLine
- attr_accessor :lineno, :oldlineno, :commit, :line
+ attr_accessor :lineno, :oldlineno, :commit, :line, :previous_path
- def initialize(lineno, oldlineno, commit, line)
+ def initialize(lineno, oldlineno, commit, line, previous_path)
@lineno = lineno
@oldlineno = oldlineno
@commit = commit
@line = line
+ @previous_path = previous_path
end
end
end
diff --git a/lib/gitlab/git/diff.rb b/lib/gitlab/git/diff.rb
index 8325eadce2f..a66517b4ca0 100644
--- a/lib/gitlab/git/diff.rb
+++ b/lib/gitlab/git/diff.rb
@@ -140,7 +140,7 @@ module Gitlab
text.start_with?(BINARY_NOTICE_PATTERN)
end
end
- def initialize(raw_diff, expanded: true)
+ def initialize(raw_diff, expanded: true, replace_invalid_utf8_chars: true)
@expanded = expanded
case raw_diff
@@ -157,6 +157,8 @@ module Gitlab
else
raise "Invalid raw diff type: #{raw_diff.class}"
end
+
+ encode_diff_to_utf8(replace_invalid_utf8_chars)
end
def to_hash
@@ -227,6 +229,13 @@ module Gitlab
private
+ def encode_diff_to_utf8(replace_invalid_utf8_chars)
+ return unless Feature.enabled?(:convert_diff_to_utf8_with_replacement_symbol, default_enabled: :yaml)
+ return unless replace_invalid_utf8_chars && !detect_binary?(@diff)
+
+ @diff = Gitlab::EncodingHelper.encode_utf8_with_replacement_character(@diff)
+ end
+
def init_from_hash(hash)
raw_diff = hash.symbolize_keys
diff --git a/lib/gitlab/git/diff_collection.rb b/lib/gitlab/git/diff_collection.rb
index 24b67424f28..0ffe8bee953 100644
--- a/lib/gitlab/git/diff_collection.rb
+++ b/lib/gitlab/git/diff_collection.rb
@@ -9,8 +9,6 @@ module Gitlab
attr_reader :limits
- delegate :max_files, :max_lines, :max_bytes, :safe_max_files, :safe_max_lines, :safe_max_bytes, to: :limits
-
def self.default_limits
{ max_files: ::Commit.diff_safe_max_files, max_lines: ::Commit.diff_safe_max_lines }
end
@@ -26,8 +24,7 @@ module Gitlab
limits[:safe_max_lines] = [limits[:max_lines], defaults[:max_lines]].min
limits[:safe_max_bytes] = limits[:safe_max_files] * 5.kilobytes # Average 5 KB per file
limits[:max_patch_bytes] = Gitlab::Git::Diff.patch_hard_limit_bytes
-
- OpenStruct.new(limits)
+ limits
end
def initialize(iterator, options = {})
@@ -140,11 +137,11 @@ module Gitlab
end
def over_safe_limits?(files)
- if files >= safe_max_files
+ if files >= limits[:safe_max_files]
@collapsed_safe_files = true
- elsif @line_count > safe_max_lines
+ elsif @line_count > limits[:safe_max_lines]
@collapsed_safe_lines = true
- elsif @byte_count >= safe_max_bytes
+ elsif @byte_count >= limits[:safe_max_bytes]
@collapsed_safe_bytes = true
end
@@ -179,7 +176,7 @@ module Gitlab
@iterator.each_with_index do |raw, iterator_index|
@empty = false
- if @enforce_limits && i >= max_files
+ if @enforce_limits && i >= limits[:max_files]
@overflow = true
@overflow_max_files = true
break
@@ -194,7 +191,7 @@ module Gitlab
@line_count += diff.line_count
@byte_count += diff.diff.bytesize
- if @enforce_limits && @line_count >= max_lines
+ if @enforce_limits && @line_count >= limits[:max_lines]
# This last Diff instance pushes us over the lines limit. We stop and
# discard it.
@overflow = true
@@ -202,7 +199,7 @@ module Gitlab
break
end
- if @enforce_limits && @byte_count >= max_bytes
+ if @enforce_limits && @byte_count >= limits[:max_bytes]
# This last Diff instance pushes us over the lines limit. We stop and
# discard it.
@overflow = true
diff --git a/lib/gitlab/git/ref.rb b/lib/gitlab/git/ref.rb
index 47cfb483509..1d7966a11ed 100644
--- a/lib/gitlab/git/ref.rb
+++ b/lib/gitlab/git/ref.rb
@@ -24,7 +24,7 @@ module Gitlab
# Ex.
# Ref.extract_branch_name('refs/heads/master') #=> 'master'
def self.extract_branch_name(str)
- str.gsub(%r{\Arefs/heads/}, '')
+ str.delete_prefix('refs/heads/')
end
def initialize(repository, name, target, dereferenced_target)
diff --git a/lib/gitlab/git/repository.rb b/lib/gitlab/git/repository.rb
index 1492ea1ce76..ab365069adf 100644
--- a/lib/gitlab/git/repository.rb
+++ b/lib/gitlab/git/repository.rb
@@ -841,11 +841,11 @@ module Gitlab
end
end
- def import_repository(url)
+ def import_repository(url, http_authorization_header: '', mirror: false)
raise ArgumentError, "don't use disk paths with import_repository: #{url.inspect}" if url.start_with?('.', '/')
wrapped_gitaly_errors do
- gitaly_repository_client.import_repository(url)
+ gitaly_repository_client.import_repository(url, http_authorization_header: http_authorization_header, mirror: mirror)
end
end