Skip to content

Commit 6e61da2

Browse files
committed
fetch tags to local, if different to remote
1 parent 8dd238f commit 6e61da2

File tree

5 files changed

+79
-4
lines changed

5 files changed

+79
-4
lines changed

lib/r10k/git/rugged/working_repository.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ def fetch(remote_name = 'origin')
8989
remote = remotes[remote_name]
9090
proxy = R10K::Git.get_proxy_for_remote(remote)
9191

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

9595
results = nil
9696

@@ -136,6 +136,26 @@ def dirty?(exclude_spec=true)
136136
end
137137
end
138138

139+
def updatedtags?
140+
with_repo do |repo|
141+
localtags = repo.tags.each_name.to_a
142+
143+
options = { :credentials => credentials }
144+
remote = repo.remotes['origin']
145+
remotetags = []
146+
remote.ls(**options) do |hash|
147+
if hash[:name].start_with?('refs/tags/') && !hash[:name].include?('^{}')
148+
remotetags << hash[:name].split('/').last
149+
end
150+
end
151+
152+
return false unless remotetags.sort != localtags.sort
153+
154+
logger.debug(_("Found different tags in local and remote in %{file_path}" % {file_path: @path}))
155+
return true
156+
end
157+
end
158+
139159
private
140160

141161
def with_repo

lib/r10k/git/shellgit/thin_repository.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def clone(remote, opts = {})
3131

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

3737
# @return [String] The origin remote URL

lib/r10k/git/shellgit/working_repository.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def fetch(remote_name='origin')
6464
proxy = R10K::Git.get_proxy_for_remote(remote)
6565

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

@@ -109,4 +109,15 @@ def dirty?(exclude_spec=true)
109109
return false
110110
end
111111
end
112+
113+
def updatedtags?
114+
result = git(['ls-remote', '--tags', '--refs', 'origin'], :path => @path.to_s, :raise_on_fail => false)
115+
remotetags = result.stdout.scan(/refs\/tags\/(\S+)$/).flatten
116+
result = git(['tag'], :path => @path.to_s, :raise_on_fail => false)
117+
localtags = result.stdout.scan(/(\S+)$/).flatten
118+
return false unless remotetags.sort != localtags.sort
119+
120+
logger.debug(_("Found different tags in local and remote in %{file_path}" % {file_path: @path}))
121+
return true
122+
end
112123
end

lib/r10k/git/stateful_repository.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ def sync(ref, force=true, exclude_spec=true)
6868
logger.warn(_("Skipping %{repo_path} due to local modifications") % {repo_path: @repo.path})
6969
updated = false
7070
end
71+
when :updatedtags
72+
logger.debug(_("Updating tags in %{repo_path}") % {repo_path: @repo.path})
73+
@repo.fetch
7174
else
7275
logger.debug(_("%{repo_path} is already at Git ref %{ref}") % {repo_path: @repo.path, ref: ref })
7376
updated = false
@@ -94,6 +97,8 @@ def status(ref, exclude_spec=true)
9497
:outdated
9598
elsif @cache.ref_type(ref) == :branch && !@cache.synced?
9699
:outdated
100+
elsif @repo.updatedtags?
101+
:updatedtags
97102
else
98103
:insync
99104
end

spec/shared-examples/git/working_repository.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,4 +206,43 @@
206206
end
207207
end
208208
end
209+
210+
shared_examples "unequal tags" do
211+
it "reports tags as unequal" do
212+
expect(subject.logger).to receive(:debug).with(/found different tags in local and remote in/i)
213+
expect(subject.updatedtags?).to be true
214+
end
215+
end
216+
217+
describe "checking if tags are different" do
218+
let(:tag_090) { subject.git_dir + 'refs' + 'tags' + '0.9.0' }
219+
let(:packed_refs) { subject.git_dir + 'packed-refs' }
220+
221+
before(:each) do
222+
subject.clone(remote)
223+
end
224+
225+
context "with equal tags local and remote" do
226+
it "reports tags as equal" do
227+
expect(subject.updatedtags?).to be false
228+
end
229+
end
230+
231+
context "with missing local tag" do
232+
before do
233+
tag_090.delete if tag_090.exist?
234+
packed_refs.delete if packed_refs.exist?
235+
end
236+
237+
it_behaves_like "unequal tags"
238+
end
239+
240+
context "with additional local tag" do
241+
before(:each) do
242+
File.open(File.join(subject.git_dir, 'packed-refs'), 'a') { |f| f.write('157011a4eaa27f1202a9d94335ee4876b26d377e refs/tags/1.0.2') }
243+
end
244+
245+
it_behaves_like "unequal tags"
246+
end
247+
end
209248
end

0 commit comments

Comments
 (0)