Skip to content
Snippets Groups Projects
Commit 33560233 authored by Douwe Maan's avatar Douwe Maan
Browse files

Merge branch '22-preserve-target-references-for-tags-and-braches' into 'master'

Refactor Refs to preserve their target objects instead of just a string representation

Notice that in `spec/tag_spec.rb` I changed the sha because previously they
referenced the tag annotation commit. With the refactor, the sha must be the one
of the commit that the tag actually points at.

Closes #22

See merge request !103
parents 5ab3a57c d481bf4e
No related branches found
No related tags found
1 merge request!103Refactor Refs to preserve their target objects instead of just a string representation
Pipeline #
v 10.4
- Move implementation of `local_branches` to `Gitlab::Git::Repository` from gitlab-ce
- Refactor Refs to preserve their target objects instead of just a string representation
v 10.3.2
- Handle collapsable DiffCollection
 
Loading
Loading
Loading
Loading
@@ -55,7 +55,12 @@ module Gitlab
def find(repo, commit_id = "HEAD")
return decorate(commit_id) if commit_id.is_a?(Rugged::Commit)
 
obj = repo.rev_parse_target(commit_id)
obj = if commit_id.is_a?(String)
repo.rev_parse_target(commit_id)
else
Ref.dereference_object(commit_id)
end
return nil unless obj.is_a?(Rugged::Commit)
 
decorate(obj)
Loading
Loading
Loading
Loading
@@ -2,7 +2,7 @@ module Gitlab
module Git
class Ref
include EncodingHelper
# Branch or tag name
# without "refs/tags|heads" prefix
attr_reader :name
Loading
Loading
@@ -20,18 +20,17 @@ module Gitlab
str.gsub(/\Arefs\/heads\//, '')
end
 
def initialize(name, target)
def self.dereference_object(object)
object = object.target while object.is_a?(Rugged::Tag::Annotation)
object
end
def initialize(repository, name, target)
encode! name
@name = name.gsub(/\Arefs\/(tags|heads)\//, '')
@target = if target.respond_to?(:oid)
target.oid
elsif target.respond_to?(:name)
target.name
elsif target.is_a? String
target
else
nil
end
@target = Commit.find(repository, target)
end
end
end
Loading
Loading
Loading
Loading
@@ -61,13 +61,19 @@ module Gitlab
def branches
rugged.branches.map do |rugged_ref|
begin
Branch.new(rugged_ref.name, rugged_ref.target)
Branch.new(self, rugged_ref.name, rugged_ref.target)
rescue Rugged::ReferenceError
# Omit invalid branch
end
end.compact.sort_by(&:name)
end
 
def local_branches
rugged.branches.each(:local).map do |branch|
Branch.new(self, branch.name, branch.target)
end
end
# Returns the number of valid branches
def branch_count
rugged.branches.count do |ref|
Loading
Loading
@@ -99,7 +105,7 @@ module Gitlab
end
end
 
Tag.new(ref.name, ref.target, message)
Tag.new(self, ref.name, ref.target, message)
end.sort_by(&:name)
end
 
Loading
Loading
@@ -132,7 +138,7 @@ module Gitlab
# Deprecated. Will be removed in 5.2
def heads
rugged.references.each("refs/heads/*").map do |head|
Gitlab::Git::Ref.new(head.name, head.target)
Gitlab::Git::Ref.new(self, head.name, head.target)
end.sort_by(&:name)
end
 
Loading
Loading
@@ -337,8 +343,7 @@ module Gitlab
# annotated tag, then return the tag's target instead.
def rev_parse_target(revspec)
obj = rugged.rev_parse(revspec)
obj = obj.target while obj.is_a?(Rugged::Tag::Annotation)
obj
Ref.dereference_object(obj)
end
 
# Return a collection of Rugged::Commits between the two revspec arguments.
Loading
Loading
@@ -745,7 +750,7 @@ module Gitlab
# create_branch("other-feature", "master")
def create_branch(ref, start_point = "HEAD")
rugged_ref = rugged.branches.create(ref, start_point)
Branch.new(rugged_ref.name, rugged_ref.target)
Branch.new(self, rugged_ref.name, rugged_ref.target)
rescue Rugged::ReferenceError => e
raise InvalidRef.new("Branch #{ref} already exists") if e.to_s =~ /'refs\/heads\/#{ref}'/
raise InvalidRef.new("Invalid reference #{start_point}")
Loading
Loading
module Gitlab
module Git
class Tag < Ref
def initialize(name, target, message = nil)
super(name, target)
def initialize(repository, name, target, message = nil)
super(repository, name, target)
@message = message
end
 
Loading
Loading
Loading
Loading
@@ -16,14 +16,14 @@ describe Gitlab::Git::Branch do
let(:branch) { repository.branches.first }
 
it { expect(branch.name).to eq(SeedRepo::Repo::BRANCHES.first) }
it { expect(branch.target).to eq("0b4bc9a49b562e85de7cc9e834518ea6828729b9") }
it { expect(branch.target.sha).to eq("0b4bc9a49b562e85de7cc9e834518ea6828729b9") }
end
 
describe 'last branch' do
let(:branch) { repository.branches.last }
 
it { expect(branch.name).to eq(SeedRepo::Repo::BRANCHES.last) }
it { expect(branch.target).to eq(SeedRepo::LastCommit::ID) }
it { expect(branch.target.sha).to eq(SeedRepo::LastCommit::ID) }
end
 
it { expect(repository.branches.size).to eq(SeedRepo::Repo::BRANCHES.size) }
Loading
Loading
Loading
Loading
@@ -140,7 +140,7 @@ describe Gitlab::Git::Repository do
end
 
context :commit do
subject { heads.first.target }
subject { heads.first.target.sha }
 
it { is_expected.to eq("0b4bc9a49b562e85de7cc9e834518ea6828729b9") }
end
Loading
Loading
@@ -1090,4 +1090,20 @@ index 0000000..e69de29
expect(repository.branch_exists?('.bla')).to eq(false)
end
end
describe '#local_branches' do
it 'returns the local branches' do
create_remote_branch('joe', 'remote_branch', 'master')
repository.create_branch('local_branch', 'master')
expect(repository.local_branches.any? { |branch| branch.name == 'remote_branch' }).to eq(false)
expect(repository.local_branches.any? { |branch| branch.name == 'local_branch' }).to eq(true)
end
end
def create_remote_branch(remote_name, branch_name, source_branch_name)
source_branch = repository.branches.find { |branch| branch.name == source_branch_name }
rugged = repository.rugged
rugged.references.create("refs/remotes/#{remote_name}/#{branch_name}", source_branch.target.sha)
end
end
Loading
Loading
@@ -7,7 +7,7 @@ describe Gitlab::Git::Tag do
let(:tag) { repository.tags.first }
 
it { expect(tag.name).to eq("v1.0.0") }
it { expect(tag.target).to eq("f4e6814c3e4e7a0de82a9e7cd20c626cc963a2f8") }
it { expect(tag.target.sha).to eq("6f6d7e7ed97bb5f0054f2b1df789b39ca89b6ff9") }
it { expect(tag.message).to eq("Release") }
end
 
Loading
Loading
@@ -15,7 +15,7 @@ describe Gitlab::Git::Tag do
let(:tag) { repository.tags.last }
 
it { expect(tag.name).to eq("v1.2.1") }
it { expect(tag.target).to eq("2ac1f24e253e08135507d0830508febaaccf02ee") }
it { expect(tag.target.sha).to eq("fa1b1e6c004a68b7d8763b86455da9e6b23e36d6") }
it { expect(tag.message).to eq("Version 1.2.1") }
end
 
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment