Class: Garlic::Repo
- Inherits:
-
Object
- Object
- Garlic::Repo
- Defined in:
- lib/garlic/repo.rb
Overview
This class represents a local git repo
Instance Attribute Summary collapse
-
#local ⇒ Object
readonly
Returns the value of attribute local.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
Instance Method Summary collapse
- #check ⇒ Object
- #checkout(tree_ish) ⇒ Object
- #clone_to(clone_path) ⇒ Object
- #export_to(export_path) ⇒ Object
- #head_sha ⇒ Object
-
#initialize(options = {}) ⇒ Repo
constructor
A new instance of Repo.
- #install ⇒ Object
- #origin_url ⇒ Object
- #reset ⇒ Object
- #same_url?(url) ⇒ Boolean
- #update ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Repo
Returns a new instance of Repo.
6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/garlic/repo.rb', line 6 def initialize( = {}) if @url = [:url] @url = File.(@url) unless [:url] =~ /^\w+(:|@)/ end @path = [:path] or raise ArgumentError, "Repo requires a :path" @path = File.(@path) @local = [:local] @local = File.(@local) if @local @name = [:name] || File.basename(@path) end |
Instance Attribute Details
#local ⇒ Object (readonly)
Returns the value of attribute local.
4 5 6 |
# File 'lib/garlic/repo.rb', line 4 def local @local end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/garlic/repo.rb', line 4 def name @name end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
4 5 6 |
# File 'lib/garlic/repo.rb', line 4 def path @path end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
4 5 6 |
# File 'lib/garlic/repo.rb', line 4 def url @url end |
Class Method Details
.head_sha(path) ⇒ Object
32 33 34 |
# File 'lib/garlic/repo.rb', line 32 def head_sha(path) `cd #{path}; git log HEAD -1 --pretty=format:\"%H\"` end |
.path?(path) ⇒ Boolean
21 22 23 |
# File 'lib/garlic/repo.rb', line 21 def path?(path) File.directory?(File.join(path, '.git')) end |
.tree_ish(options) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/garlic/repo.rb', line 25 def tree_ish() [:tree_ish, :tree, :branch, :tag, :commit, :sha].each do |key| return [key] if [key] end nil end |
Instance Method Details
#check ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/garlic/repo.rb', line 70 def check if !Repo.path?(path) raise "#{name} is missing from #{path}, or is not a git repo" elsif url && !same_url?(origin_url) raise "#{name}'s url has changed from #{url} to #{origin_url}" end end |
#checkout(tree_ish) ⇒ Object
83 84 85 |
# File 'lib/garlic/repo.rb', line 83 def checkout(tree_ish) cd(path) { sh "git checkout -q #{tree_ish}", :verbose => false } end |
#clone_to(clone_path) ⇒ Object
98 99 100 101 102 103 |
# File 'lib/garlic/repo.rb', line 98 def clone_to(clone_path) mkdir_p File.dirname(clone_path) cd (File.dirname(clone_path)) do sh "git clone #{path} #{clone_path}" end end |
#export_to(export_path) ⇒ Object
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/garlic/repo.rb', line 87 def export_to(export_path) rm_rf export_path; mkdir_p export_path cd(path) do sh "git archive --format=tar HEAD > #{File.join(export_path, "#{name}.tar")}", :verbose => false end cd(export_path) do `tar -x -f #{name}.tar` rm "#{name}.tar" end end |
#head_sha ⇒ Object
117 118 119 |
# File 'lib/garlic/repo.rb', line 117 def head_sha Repo.head_sha(path) end |
#install ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/garlic/repo.rb', line 37 def install if File.exists?(path) puts "\nSkipping #{name}, as it exists" else puts "\nInstalling #{name}" sh "git clone #{url}#{" --reference #{local}" if local} #{path}" end end |
#origin_url ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/garlic/repo.rb', line 105 def origin_url unless @origin_url match = `cd #{path}; git remote show -n origin`.match(/URL: (.*)\n/) @origin_url = (match && match[1]) end @origin_url end |
#reset ⇒ Object
78 79 80 81 |
# File 'lib/garlic/repo.rb', line 78 def reset cd(path) { sh "git reset --hard master" } checkout('master') end |
#same_url?(url) ⇒ Boolean
113 114 115 |
# File 'lib/garlic/repo.rb', line 113 def same_url?(url) self.url.sub(/\/?(\.git)?$/, '') == url.sub(/\/?(\.git)?$/, '') end |
#update ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/garlic/repo.rb', line 46 def update puts "\nUpdating #{name}..." if Repo.path?(path) if url begin checkout 'master' cd(path) do sh "git fetch origin", :verbose => false sh "git pull", :verbose => false end rescue Exception => e puts "\n\nIt seems there was a problem.\nTry running rake garlic:reset_repos\n\n" raise e end else puts "No url for #{name} given, so not updating" end elsif File.exists?(path) raise "\nRepo #{name} (#{path}) is not a git repo.\nRemove it and run rake garlic:install_repos\n\n" else raise "\nRepo #{name} missing from #{path}.\nPlease run rake garlic:install_repos\n\n" end end |