Commit 134bf7fe authored by Sean McGivern's avatar Sean McGivern

Merge branch 'gitaly-more-annotations' into 'master'

Branch and tag refactors for Gitaly

See merge request !12872
parents 787e2058 8ca394b8
...@@ -3,39 +3,8 @@ ...@@ -3,39 +3,8 @@
module Gitlab module Gitlab
module Git module Git
class Branch < Ref class Branch < Ref
def initialize(repository, name, target) def initialize(repository, name, target, target_commit)
if target.is_a?(Gitaly::FindLocalBranchResponse) super(repository, name, target, target_commit)
target = target_from_gitaly_local_branches_response(target)
end
super(repository, name, target)
end
def target_from_gitaly_local_branches_response(response)
# Git messages have no encoding enforcements. However, in the UI we only
# handle UTF-8, so basically we cross our fingers that the message force
# encoded to UTF-8 is readable.
message = response.commit_subject.dup.force_encoding('UTF-8')
# NOTE: For ease of parsing in Gitaly, we have only the subject of
# the commit and not the full message. This is ok, since all the
# code that uses `local_branches` only cares at most about the
# commit message.
# TODO: Once gitaly "takes over" Rugged consider separating the
# subject from the message to make it clearer when there's one
# available but not the other.
hash = {
id: response.commit_id,
message: message,
authored_date: Time.at(response.commit_author.date.seconds),
author_name: response.commit_author.name,
author_email: response.commit_author.email,
committed_date: Time.at(response.commit_committer.date.seconds),
committer_name: response.commit_committer.name,
committer_email: response.commit_committer.email
}
Gitlab::Git::Commit.decorate(hash)
end end
end end
end end
......
...@@ -33,10 +33,9 @@ module Gitlab ...@@ -33,10 +33,9 @@ module Gitlab
object object
end end
def initialize(repository, name, target) def initialize(repository, name, target, derefenced_target)
encode! name @name = Gitlab::Git.ref_name(name)
@name = name.gsub(/\Arefs\/(tags|heads)\//, '') @dereferenced_target = derefenced_target
@dereferenced_target = Gitlab::Git::Commit.find(repository, target)
@target = if target.respond_to?(:oid) @target = if target.respond_to?(:oid)
target.oid target.oid
elsif target.respond_to?(:name) elsif target.respond_to?(:name)
......
...@@ -80,16 +80,10 @@ module Gitlab ...@@ -80,16 +80,10 @@ module Gitlab
end end
# Returns an Array of Branches # Returns an Array of Branches
def branches(filter: nil, sort_by: nil) #
branches = rugged.branches.each(filter).map do |rugged_ref| # Gitaly migration: https://gitlab.com/gitlab-org/gitaly/issues/389
begin def branches(sort_by: nil)
Gitlab::Git::Branch.new(self, rugged_ref.name, rugged_ref.target) branches_filter(sort_by: sort_by)
rescue Rugged::ReferenceError
# Omit invalid branch
end
end.compact
sort_branches(branches, sort_by)
end end
def reload_rugged def reload_rugged
...@@ -107,7 +101,10 @@ module Gitlab ...@@ -107,7 +101,10 @@ module Gitlab
reload_rugged if force_reload reload_rugged if force_reload
rugged_ref = rugged.branches[name] rugged_ref = rugged.branches[name]
Gitlab::Git::Branch.new(self, rugged_ref.name, rugged_ref.target) if rugged_ref if rugged_ref
target_commit = Gitlab::Git::Commit.find(self, rugged_ref.target)
Gitlab::Git::Branch.new(self, rugged_ref.name, rugged_ref.target, target_commit)
end
end end
def local_branches(sort_by: nil) def local_branches(sort_by: nil)
...@@ -115,7 +112,7 @@ module Gitlab ...@@ -115,7 +112,7 @@ module Gitlab
if is_enabled if is_enabled
gitaly_ref_client.local_branches(sort_by: sort_by) gitaly_ref_client.local_branches(sort_by: sort_by)
else else
branches(filter: :local, sort_by: sort_by) branches_filter(filter: :local, sort_by: sort_by)
end end
end end
end end
...@@ -162,6 +159,8 @@ module Gitlab ...@@ -162,6 +159,8 @@ module Gitlab
end end
# Returns an Array of Tags # Returns an Array of Tags
#
# Gitaly migration: https://gitlab.com/gitlab-org/gitaly/issues/390
def tags def tags
rugged.references.each("refs/tags/*").map do |ref| rugged.references.each("refs/tags/*").map do |ref|
message = nil message = nil
...@@ -174,7 +173,8 @@ module Gitlab ...@@ -174,7 +173,8 @@ module Gitlab
end end
end end
Gitlab::Git::Tag.new(self, ref.name, ref.target, message) target_commit = Gitlab::Git::Commit.find(self, ref.target)
Gitlab::Git::Tag.new(self, ref.name, ref.target, target_commit, message)
end.sort_by(&:name) end.sort_by(&:name)
end end
...@@ -204,13 +204,6 @@ module Gitlab ...@@ -204,13 +204,6 @@ module Gitlab
branch_names + tag_names branch_names + tag_names
end end
# Deprecated. Will be removed in 5.2
def heads
rugged.references.each("refs/heads/*").map do |head|
Gitlab::Git::Ref.new(self, head.name, head.target)
end.sort_by(&:name)
end
def has_commits? def has_commits?
!empty? !empty?
end end
...@@ -707,7 +700,8 @@ module Gitlab ...@@ -707,7 +700,8 @@ module Gitlab
# create_branch("other-feature", "master") # create_branch("other-feature", "master")
def create_branch(ref, start_point = "HEAD") def create_branch(ref, start_point = "HEAD")
rugged_ref = rugged.branches.create(ref, start_point) rugged_ref = rugged.branches.create(ref, start_point)
Gitlab::Git::Branch.new(self, rugged_ref.name, rugged_ref.target) target_commit = Gitlab::Git::Commit.find(self, rugged_ref.target)
Gitlab::Git::Branch.new(self, rugged_ref.name, rugged_ref.target, target_commit)
rescue Rugged::ReferenceError => e rescue Rugged::ReferenceError => e
raise InvalidRef.new("Branch #{ref} already exists") if e.to_s =~ /'refs\/heads\/#{ref}'/ raise InvalidRef.new("Branch #{ref} already exists") if e.to_s =~ /'refs\/heads\/#{ref}'/
raise InvalidRef.new("Invalid reference #{start_point}") raise InvalidRef.new("Invalid reference #{start_point}")
...@@ -837,6 +831,20 @@ module Gitlab ...@@ -837,6 +831,20 @@ module Gitlab
private private
# Gitaly note: JV: Trying to get rid of the 'filter' option so we can implement this with 'git'.
def branches_filter(filter: nil, sort_by: nil)
branches = rugged.branches.each(filter).map do |rugged_ref|
begin
target_commit = Gitlab::Git::Commit.find(self, rugged_ref.target)
Gitlab::Git::Branch.new(self, rugged_ref.name, rugged_ref.target, target_commit)
rescue Rugged::ReferenceError
# Omit invalid branch
end
end.compact
sort_branches(branches, sort_by)
end
def raw_log(options) def raw_log(options)
default_options = { default_options = {
limit: 10, limit: 10,
......
...@@ -5,8 +5,8 @@ module Gitlab ...@@ -5,8 +5,8 @@ module Gitlab
class Tag < Ref class Tag < Ref
attr_reader :object_sha attr_reader :object_sha
def initialize(repository, name, target, message = nil) def initialize(repository, name, target, target_commit, message = nil)
super(repository, name, target) super(repository, name, target, target_commit)
@message = message @message = message
end end
......
...@@ -72,11 +72,39 @@ module Gitlab ...@@ -72,11 +72,39 @@ module Gitlab
Gitlab::Git::Branch.new( Gitlab::Git::Branch.new(
@repository, @repository,
encode!(gitaly_branch.name.dup), encode!(gitaly_branch.name.dup),
gitaly_branch.commit_id gitaly_branch.commit_id,
commit_from_local_branches_response(gitaly_branch)
) )
end end
end end
end end
def commit_from_local_branches_response(response)
# Git messages have no encoding enforcements. However, in the UI we only
# handle UTF-8, so basically we cross our fingers that the message force
# encoded to UTF-8 is readable.
message = response.commit_subject.dup.force_encoding('UTF-8')
# NOTE: For ease of parsing in Gitaly, we have only the subject of
# the commit and not the full message. This is ok, since all the
# code that uses `local_branches` only cares at most about the
# commit message.
# TODO: Once gitaly "takes over" Rugged consider separating the
# subject from the message to make it clearer when there's one
# available but not the other.
hash = {
id: response.commit_id,
message: message,
authored_date: Time.at(response.commit_author.date.seconds),
author_name: response.commit_author.name,
author_email: response.commit_author.email,
committed_date: Time.at(response.commit_committer.date.seconds),
committer_name: response.commit_committer.name,
committer_email: response.commit_committer.email
}
Gitlab::Git::Commit.decorate(hash)
end
end end
end end
end end
...@@ -7,51 +7,6 @@ describe Gitlab::Git::Branch, seed_helper: true do ...@@ -7,51 +7,6 @@ describe Gitlab::Git::Branch, seed_helper: true do
it { is_expected.to be_kind_of Array } it { is_expected.to be_kind_of Array }
describe 'initialize' do
let(:commit_id) { 'f00' }
let(:commit_subject) { "My commit".force_encoding('ASCII-8BIT') }
let(:committer) do
Gitaly::FindLocalBranchCommitAuthor.new(
name: generate(:name),
email: generate(:email),
date: Google::Protobuf::Timestamp.new(seconds: 123)
)
end
let(:author) do
Gitaly::FindLocalBranchCommitAuthor.new(
name: generate(:name),
email: generate(:email),
date: Google::Protobuf::Timestamp.new(seconds: 456)
)
end
let(:gitaly_branch) do
Gitaly::FindLocalBranchResponse.new(
name: 'foo', commit_id: commit_id, commit_subject: commit_subject,
commit_author: author, commit_committer: committer
)
end
let(:attributes) do
{
id: commit_id,
message: commit_subject,
authored_date: Time.at(author.date.seconds),
author_name: author.name,
author_email: author.email,
committed_date: Time.at(committer.date.seconds),
committer_name: committer.name,
committer_email: committer.email
}
end
let(:branch) { described_class.new(repository, 'foo', gitaly_branch) }
it 'parses Gitaly::FindLocalBranchResponse correctly' do
expect(Gitlab::Git::Commit).to receive(:decorate)
.with(hash_including(attributes)).and_call_original
expect(branch.dereferenced_target.message).to be_utf8
end
end
describe '#size' do describe '#size' do
subject { super().size } subject { super().size }
it { is_expected.to eq(SeedRepo::Repo::BRANCHES.size) } it { is_expected.to eq(SeedRepo::Repo::BRANCHES.size) }
......
...@@ -234,33 +234,6 @@ describe Gitlab::Git::Repository, seed_helper: true do ...@@ -234,33 +234,6 @@ describe Gitlab::Git::Repository, seed_helper: true do
it { expect(repository.bare?).to be_truthy } it { expect(repository.bare?).to be_truthy }
end end
describe '#heads' do
let(:heads) { repository.heads }
subject { heads }
it { is_expected.to be_kind_of Array }
describe '#size' do
subject { super().size }
it { is_expected.to eq(SeedRepo::Repo::BRANCHES.size) }
end
context :head do
subject { heads.first }
describe '#name' do
subject { super().name }
it { is_expected.to eq("feature") }
end
context :commit do
subject { heads.first.dereferenced_target.sha }
it { is_expected.to eq("0b4bc9a49b562e85de7cc9e834518ea6828729b9") }
end
end
end
describe '#ref_names' do describe '#ref_names' do
let(:ref_names) { repository.ref_names } let(:ref_names) { repository.ref_names }
subject { ref_names } subject { ref_names }
...@@ -521,7 +494,7 @@ describe Gitlab::Git::Repository, seed_helper: true do ...@@ -521,7 +494,7 @@ describe Gitlab::Git::Repository, seed_helper: true do
end end
it "should refresh the repo's #heads collection" do it "should refresh the repo's #heads collection" do
head_names = @normal_repo.heads.map { |h| h.name } head_names = @normal_repo.branches.map { |h| h.name }
expect(head_names).to include(new_branch) expect(head_names).to include(new_branch)
end end
...@@ -542,7 +515,7 @@ describe Gitlab::Git::Repository, seed_helper: true do ...@@ -542,7 +515,7 @@ describe Gitlab::Git::Repository, seed_helper: true do
eq(normal_repo.rugged.branches["master"].target.oid) eq(normal_repo.rugged.branches["master"].target.oid)
) )
head_names = normal_repo.heads.map { |h| h.name } head_names = normal_repo.branches.map { |h| h.name }
expect(head_names).not_to include(new_branch) expect(head_names).not_to include(new_branch)
end end
...@@ -589,10 +562,6 @@ describe Gitlab::Git::Repository, seed_helper: true do ...@@ -589,10 +562,6 @@ describe Gitlab::Git::Repository, seed_helper: true do
expect(@repo.rugged.branches["feature"]).to be_nil expect(@repo.rugged.branches["feature"]).to be_nil
end end
it "should update the repo's #heads collection" do
expect(@repo.heads).not_to include("feature")
end
after(:all) do after(:all) do
FileUtils.rm_rf(TEST_MUTABLE_REPO_PATH) FileUtils.rm_rf(TEST_MUTABLE_REPO_PATH)
ensure_seeds ensure_seeds
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment