Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactor the tag fetcher

  • Loading branch information...
commit 4356f026878fb976e8181776eebb2683fda3acee 1 parent 8393152
@thomasdziedzic authored
Showing with 104 additions and 75 deletions.
  1. +6 −3 bin/tagurit
  2. +98 −72 lib/tag_fetcher.rb
View
9 bin/tagurit
@@ -38,12 +38,15 @@ end
new_cache = {}
vcs_urls.each do |vcs_type, urls|
if vcs_type == :git
- new_cache.merge!(TagFetcher.git(urls))
+ git = GitTagFetcher.new urls
+ new_cache.merge! git.fetch
elsif vcs_type == :svn
- new_cache.merge!(TagFetcher.svn(urls))
+ svn = SvnTagFetcher.new urls
+ new_cache.merge! svn.fetch
elsif vcs_type == :hg
hg_path = File.join base_dir, "hg"
- new_cache.merge!(TagFetcher.hg(urls, hg_path))
+ hg = HgTagFetcher.new urls, hg_path
+ new_cache.merge! hg.fetch
end
end
View
170 lib/tag_fetcher.rb
@@ -1,84 +1,110 @@
+class TagFetchError < StandardError
+end
+
class TagFetcher
- class << self
- # tag fetchers get a list of urls
- # return a dictionary with {"<url>": ["<tags>"]}
- def git urls
- new_cache = {}
-
- urls.each do |url|
- raw_tags = `git ls-remote --tags #{url}`
- if $?.success?
- # cleanup tags by removing extra data before the tag name
- processed_tags = raw_tags.split("\n").map {|raw_tag| raw_tag.split("/")[-1]}
-
- # cleanup tags which end with ^{}
- processed_tags.reject! {|tag| tag[-3, 3] == "^{}"}
- else
- puts "failed to retrieve git repo tags from : #{url}"
- processed_tags = [:failed]
- end
-
- new_cache[url] = processed_tags
- end
+ def initialize urls
+ @urls = urls
+ end
- new_cache
+ # process a list of urls
+ def fetch
+ new_cache = {}
+
+ @urls.each do |url|
+ # remove whitespace before and after the string to clean it up
+ url.strip!
+
+ begin
+ raw_tags = fetch_tags url
+ rescue TagFetchError
+ puts "Failed to retrieve repo tags from : #{url}"
+ new_cache[url] = [:failed]
+ else
+ new_cache[url] = clean_tags raw_tags
+ end
end
- def svn urls
- new_cache = {}
+ # return a dictionary with {"<url>": ["<tags>"]}
+ # set "<tags>" to :failed in case of failure to retrieve tags
+ new_cache
+ end
- urls.each do |url|
- raw_tags = `svn ls #{url}`
- if $?.success?
- # only remove the following "/" from the tag directory
- processed_tags = raw_tags.split("\n").map {|raw_tag| raw_tag.split("/")[0]}
- else
- puts "failed to retrieve svn repo tags from: #{url}"
- processed_tags = [:failed]
- end
+ private
- new_cache[url] = processed_tags
- end
+ def fetch_tags url
+ raise NotImplementedError.new "You need to implement fetch_tags yourself."
+ end
- new_cache
- end
+ def clean_tags raw_tags
+ raise NotImplementedError.new "You need to implement clean_tags yourself."
+ end
+end
- def hg urls, hg_path
- new_cache = {}
-
- # create a ~/.tagurit/hg/ directory to hold mercurial repositories.
- # You can only fetch tags from a repository if they are local
- Dir.mkdir hg_path unless File.directory? hg_path
-
- urls.each do |url|
- # remove whitespace before and after the string to clean it up
- url.strip!
-
- # extract the directory it will fetch into
- local_directory = url.split("/")[-1]
- local_path = File.join hg_path, local_directory
-
- # if the directory doesn't exist, clone it first, otherwise pull upstream changes
- if File.directory? local_path
- `hg pull -u -R #{local_path} 2> /dev/null`
- else
- `hg clone #{url} #{local_path} 2> /dev/null`
- end
- if $?.success?
- raw_tags = `hg tags -R #{local_path}`
-
- # only remove the following "/" from the tag directory
- processed_tags = raw_tags.split("\n").map {|raw_tag| raw_tag.split[0]}
- else
- puts "failed to retrieve hg repo: #{url}"
- processed_tags = [:failed]
- end
-
-
- new_cache[url] = processed_tags
- end
+class GitTagFetcher < TagFetcher
+ private
+
+ def fetch_tags url
+ raw_tags = `git ls-remote --tags #{url}`
+ raise TagFetchError unless $?.success?
+ raw_tags
+ end
+
+ def clean_tags raw_tags
+ # cleanup tags by removing extra data before the tag name
+ processed_tags = raw_tags.split("\n").map {|raw_tag| raw_tag.split("/")[-1]}
+ # cleanup tags which end with ^{}
+ processed_tags.reject! {|tag| tag[-3, 3] == "^{}"}
+ processed_tags
+ end
+end
+
+class SvnTagFetcher < TagFetcher
+ private
+
+ def fetch_tags url
+ raw_tags = `svn ls #{url}`
+ raise TagFetchError unless $?.success?
+ raw_tags
+ end
+
+ def clean_tags raw_tags
+ # only remove the following "/" from the tag directory
+ processed_tags = raw_tags.split("\n").map {|raw_tag| raw_tag.split("/")[0]}
+ processed_tags
+ end
+end
- new_cache
+class HgTagFetcher < TagFetcher
+ def initialize urls, hg_path
+ # create a ~/.tagurit/hg/ directory to hold mercurial repositories.
+ # You can only fetch tags from a repository if they are local
+ Dir.mkdir hg_path unless File.directory? hg_path
+
+ @hg_path = hg_path
+
+ super urls
+ end
+
+ private
+
+ def fetch_tags url
+ # extract the directory it will fetch into
+ local_directory = url.split("/")[-1]
+ local_path = File.join @hg_path, local_directory
+
+ # if the directory doesn't exist, clone it first, otherwise pull upstream changes
+ if File.directory? local_path
+ `hg pull -u -R #{local_path} 2> /dev/null`
+ else
+ `hg clone #{url} #{local_path} 2> /dev/null`
end
+ raise TagFetchError unless $?.success?
+ raw_tags = `hg tags -R #{local_path}`
+ raw_tags
+ end
+
+ def clean_tags raw_tags
+ processed_tags = raw_tags.split("\n").map {|raw_tag| raw_tag.split[0]}
+ processed_tags
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.