Skip to content

Commit

Permalink
fetch tags to local, if different to remote
Browse files Browse the repository at this point in the history
  • Loading branch information
tmu-sprd committed Jan 22, 2024
1 parent ea0aa7a commit d6ae45d
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 4 deletions.
24 changes: 22 additions & 2 deletions lib/r10k/git/rugged/working_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ def fetch(remote_name = 'origin')
remote = remotes[remote_name]
proxy = R10K::Git.get_proxy_for_remote(remote)

options = {:credentials => credentials, :proxy_url => proxy}
refspecs = ["+refs/heads/*:refs/remotes/#{remote_name}/*"]
options = { :credentials => credentials, :proxy_url => proxy, :prune => true }
refspecs = ["+refs/heads/*:refs/remotes/#{remote_name}/*", '+refs/tags/*:refs/tags/*']

results = nil

Expand Down Expand Up @@ -136,6 +136,26 @@ def dirty?(exclude_spec=true)
end
end

def updatedtags?
with_repo do |repo|
localtags = repo.tags.each_name.to_a

options = { :credentials => credentials }
remote = repo.remotes['origin']
remotetags = []
remote.ls(**options) do |hash|
if hash[:name].start_with?('refs/tags/') && !hash[:name].include?('^{}')
remotetags << hash[:name].split('/').last
end
end

return false unless remotetags.sort != localtags.sort

logger.debug(_("Found different tags in local and remote in %{file_path}" % {file_path: @path}))
return true
end
end

private

def with_repo
Expand Down
2 changes: 1 addition & 1 deletion lib/r10k/git/shellgit/thin_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def clone(remote, opts = {})

# Fetch refs from the backing bare Git repository.
def fetch(remote = 'cache')
git ['fetch', remote], :path => @path.to_s
git ['fetch', remote, '--prune', '--tags', '--prune-tags'], :path => @path.to_s
end

# @return [String] The origin remote URL
Expand Down
13 changes: 12 additions & 1 deletion lib/r10k/git/shellgit/working_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def fetch(remote_name='origin')
proxy = R10K::Git.get_proxy_for_remote(remote)

R10K::Git.with_proxy(proxy) do
git ['fetch', remote_name, '--prune'], :path => @path.to_s
git ['fetch', remote_name, '--prune', '--tags', '--prune-tags'], :path => @path.to_s
end
end

Expand Down Expand Up @@ -109,4 +109,15 @@ def dirty?(exclude_spec=true)
return false
end
end

def updatedtags?
result = git(['ls-remote', '--tags', '--refs', 'origin'], :path => @path.to_s, :raise_on_fail => false)
remotetags = result.stdout.scan(/refs\/tags\/(\S+)$/).flatten
result = git(['tag'], :path => @path.to_s, :raise_on_fail => false)
localtags = result.stdout.scan(/(\S+)$/).flatten
return false unless remotetags.sort != localtags.sort

logger.debug(_("Found different tags in local and remote in %{file_path}" % {file_path: @path}))
return true
end
end
5 changes: 5 additions & 0 deletions lib/r10k/git/stateful_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ def sync(ref, force=true, exclude_spec=true)
logger.warn(_("Skipping %{repo_path} due to local modifications") % {repo_path: @repo.path})
updated = false
end
when :updatedtags
logger.debug(_("Updating tags in %{repo_path}") % {repo_path: @repo.path})
@repo.fetch
else
logger.debug(_("%{repo_path} is already at Git ref %{ref}") % {repo_path: @repo.path, ref: ref })
updated = false
Expand All @@ -94,6 +97,8 @@ def status(ref, exclude_spec=true)
:outdated
elsif @cache.ref_type(ref) == :branch && !@cache.synced?
:outdated
elsif @repo.updatedtags?
:updatedtags
else
:insync
end
Expand Down
39 changes: 39 additions & 0 deletions spec/shared-examples/git/working_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,43 @@
end
end
end

shared_examples "unequal tags" do
it "reports tags as unequal" do
expect(subject.logger).to receive(:debug).with(/found different tags in local and remote in/i)
expect(subject.updatedtags?).to be true
end
end

describe "checking if tags are different" do
let(:tag_090) { subject.git_dir + 'refs' + 'tags' + '0.9.0' }
let(:packed_refs) { subject.git_dir + 'packed-refs' }

before(:each) do
subject.clone(remote)
end

context "with equal tags local and remote" do
it "reports tags as equal" do
expect(subject.updatedtags?).to be false
end
end

context "with missing local tag" do
before do
tag_090.delete if tag_090.exist?
packed_refs.delete if packed_refs.exist?
end

it_behaves_like "unequal tags"
end

context "with additional local tag" do
before(:each) do
File.open(File.join(subject.git_dir, 'packed-refs'), 'a') { |f| f.write('157011a4eaa27f1202a9d94335ee4876b26d377e refs/tags/1.0.2') }
end

it_behaves_like "unequal tags"
end
end
end

0 comments on commit d6ae45d

Please sign in to comment.