diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-04-20 13:00:54 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-04-20 13:00:54 +0300 |
commit | 3cccd102ba543e02725d247893729e5c73b38295 (patch) | |
tree | f36a04ec38517f5deaaacb5acc7d949688d1e187 /lib/gitlab/git/blame.rb | |
parent | 205943281328046ef7b4528031b90fbda70c75ac (diff) |
Add latest changes from gitlab-org/gitlab@14-10-stable-eev14.10.0-rc42
Diffstat (limited to 'lib/gitlab/git/blame.rb')
-rw-r--r-- | lib/gitlab/git/blame.rb | 40 |
1 files changed, 33 insertions, 7 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 |