debian-mirror-gitlab/lib/gitlab/git/blame.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

102 lines
2.8 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
module Gitlab
module Git
class Blame
2017-09-10 17:25:29 +05:30
include Gitlab::EncodingHelper
2017-08-17 22:00:37 +05:30
2022-06-21 17:19:12 +05:30
attr_reader :lines, :blames, :range
2017-08-17 22:00:37 +05:30
2022-06-21 17:19:12 +05:30
def initialize(repository, sha, path, range: nil)
2017-08-17 22:00:37 +05:30
@repo = repository
@sha = sha
@path = path
2022-06-21 17:19:12 +05:30
@range = range
2017-08-17 22:00:37 +05:30
@lines = []
@blames = load_blame
end
def each
@blames.each do |blame|
2022-06-21 17:19:12 +05:30
yield(blame.commit, blame.line, blame.previous_path)
2017-08-17 22:00:37 +05:30
end
end
private
2022-06-21 17:19:12 +05:30
def range_spec
"#{range.first},#{range.last}" if range
end
2017-08-17 22:00:37 +05:30
def load_blame
2022-06-21 17:19:12 +05:30
output = encode_utf8(
@repo.gitaly_commit_client.raw_blame(@sha, @path, range: range_spec)
)
2017-09-10 17:25:29 +05:30
2018-11-08 19:23:39 +05:30
process_raw_blame(output)
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
def process_raw_blame(output)
2022-06-21 17:19:12 +05:30
start_line = nil
2021-04-29 21:17:54 +05:30
lines = []
final = []
info = {}
commits = {}
2022-06-21 17:19:12 +05:30
commit_id = nil
previous_paths = {}
2017-08-17 22:00:37 +05:30
# process the output
output.split("\n").each do |line|
if line[0, 1] == "\t"
lines << line[1, line.size]
elsif m = /^(\w{40}) (\d+) (\d+)/.match(line)
2021-04-17 20:07:23 +05:30
# Removed these instantiations for performance but keeping them for reference:
# commit_id, old_lineno, lineno = m[1], m[2].to_i, m[3].to_i
commit_id = m[1]
2017-08-17 22:00:37 +05:30
commits[commit_id] = nil unless commits.key?(commit_id)
2021-04-17 20:07:23 +05:30
info[m[3].to_i] = [commit_id, m[2].to_i]
2022-06-21 17:19:12 +05:30
# 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..])
2017-08-17 22:00:37 +05:30
end
end
2018-11-08 19:23:39 +05:30
Gitlab::Git::Commit.batch_by_oid(@repo, commits.keys).each do |commit|
commits[commit.sha] = commit
2017-08-17 22:00:37 +05:30
end
# get it together
info.sort.each do |lineno, (commit_id, old_lineno)|
2022-06-21 17:19:12 +05:30
final << BlameLine.new(
lineno,
old_lineno,
commits[commit_id],
lines[lineno - start_line],
previous_paths[commit_id]
)
2017-08-17 22:00:37 +05:30
end
@lines = final
end
end
class BlameLine
2022-06-21 17:19:12 +05:30
attr_accessor :lineno, :oldlineno, :commit, :line, :previous_path
2022-05-07 20:08:51 +05:30
2022-06-21 17:19:12 +05:30
def initialize(lineno, oldlineno, commit, line, previous_path)
2017-08-17 22:00:37 +05:30
@lineno = lineno
@oldlineno = oldlineno
@commit = commit
@line = line
2022-06-21 17:19:12 +05:30
@previous_path = previous_path
2017-08-17 22:00:37 +05:30
end
end
end
end