Class: Metaverse::Repo
- Inherits:
-
Object
- Object
- Metaverse::Repo
- Defined in:
- lib/metaverse/repo.rb
Instance Method Summary collapse
- #add_option_to_remote(remote, value, key) ⇒ Object
- #add_remote(name, url) ⇒ Object
- #ahead_of_develop?(branch) ⇒ Boolean
- #branches ⇒ Object
- #checkout(ref) ⇒ Object
- #clean_branch(branch) ⇒ Object
- #create_ref(ref_name, ref_target) ⇒ Object
- #create_state(prefix, state) ⇒ Object
- #current_branch ⇒ Object
- #dirty? ⇒ Boolean
- #exec(env, command) ⇒ Object
- #has_changes?(ref1, ref2) ⇒ Boolean
- #head ⇒ Object
-
#initialize(path) ⇒ Repo
constructor
Initialize a git repository path - An absolute path to the repo root folder.
- #load(path) ⇒ Object
- #load_state(prefix, state, remote = nil, should_create_branch = false) ⇒ Object
- #name ⇒ Object
- #peek_previous_branch ⇒ Object
- #pop_previous_branch ⇒ Object
- #pull(remote) ⇒ Object
- #send_state(prefix, state, remote, is_branch, should_clean = false) ⇒ Object
- #update(remote = nil) ⇒ Object
- #update_ref(ref_name, ref_target) ⇒ Object
- #workdir ⇒ Object
Constructor Details
#initialize(path) ⇒ Repo
Initialize a git repository path - An absolute path to the repo root folder
10 11 12 13 |
# File 'lib/metaverse/repo.rb', line 10 def initialize path load path @previous_branches = [] end |
Instance Method Details
#add_option_to_remote(remote, value, key) ⇒ Object
185 186 187 188 189 190 191 192 |
# File 'lib/metaverse/repo.rb', line 185 def add_option_to_remote remote, value, key config_path = "#{@repo.path}config" cfg = IniParse.parse(File.read(config_path)) if not [*cfg["remote \"#{remote}\""][key]].include? value cfg["remote \"#{remote}\""][key] = [*cfg["remote \"#{remote}\""][key], value] cfg.save config_path end end |
#add_remote(name, url) ⇒ Object
213 214 215 216 |
# File 'lib/metaverse/repo.rb', line 213 def add_remote name, url return Errors::remote_exists name if not @repo.remotes[name].nil? @repo.remotes.create name, url end |
#ahead_of_develop?(branch) ⇒ Boolean
200 201 202 |
# File 'lib/metaverse/repo.rb', line 200 def ahead_of_develop? branch has_changes? branch, @repo.branches['develop'] end |
#branches ⇒ Object
81 82 83 |
# File 'lib/metaverse/repo.rb', line 81 def branches @repo.branches end |
#checkout(ref) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/metaverse/repo.rb', line 35 def checkout ref if @repo.branches[ref].nil? is_reference = false if Rugged::Reference.valid_name?(ref) and @repo.references[ref].nil? raise Errors::ref_not_found ref end end @previous_branches.push current_branch Dir.chdir(@repo.workdir) do `git checkout #{ref}`#TODO: find out what's going on, check out a branch end end |
#clean_branch(branch) ⇒ Object
155 156 157 158 159 160 161 162 163 |
# File 'lib/metaverse/repo.rb', line 155 def clean_branch branch changed = ahead_of_develop? branch if not changed checkout 'develop' @repo.branches.delete branch end changed end |
#create_ref(ref_name, ref_target) ⇒ Object
86 87 88 |
# File 'lib/metaverse/repo.rb', line 86 def create_ref ref_name, ref_target @repo.references.create ref_name, ref_target.target_id end |
#create_state(prefix, state) ⇒ Object
96 97 98 99 100 101 102 103 104 |
# File 'lib/metaverse/repo.rb', line 96 def create_state prefix, state ref_target = @repo.head if not prefix == 'snapshot' branch_name = "#{prefix}/#{state}" ref_target = @repo.create_branch branch_name end ref_name = "refs/meta/local/#{prefix}/#{state}" create_ref ref_name, ref_target end |
#current_branch ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/metaverse/repo.rb', line 50 def current_branch branch_name = '' Dir.chdir(@repo.workdir) do branch_name = `git branch | sed -n '/\* /s///p'` end if branch_name.include? "detached" match = /[A-Za-z0-9._-]+(?:\/[A-Za-z0-9._-]+)+/.match(branch_name) if not match.nil? return match[0] end return 'HEAD' end branch_name.strip end |
#dirty? ⇒ Boolean
165 166 167 168 169 170 171 172 |
# File 'lib/metaverse/repo.rb', line 165 def dirty? is_dirty = false @repo.status { |file, data| next if data == [:ignored] is_dirty = true } is_dirty end |
#exec(env, command) ⇒ Object
218 219 220 221 222 |
# File 'lib/metaverse/repo.rb', line 218 def exec env, command Dir.chdir(@repo.workdir) do system env, command end end |
#has_changes?(ref1, ref2) ⇒ Boolean
195 196 197 |
# File 'lib/metaverse/repo.rb', line 195 def has_changes? ref1, ref2 not ref1.target_id == ref2.target_id end |
#head ⇒ Object
76 77 78 |
# File 'lib/metaverse/repo.rb', line 76 def head @repo.head end |
#load(path) ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/metaverse/repo.rb', line 16 def load path @repo = Rugged::Repository.new(path) @repo.remotes.each_name do |remote| fetch_refspec = "+refs/meta/local/*:refs/meta/remotes/#{remote}/*" push_refspec = "+refs/meta/local/*:refs/meta/local/*" add_option_to_remote remote, fetch_refspec, 'fetch' add_option_to_remote remote, push_refspec, 'push' end end |
#load_state(prefix, state, remote = nil, should_create_branch = false) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/metaverse/repo.rb', line 107 def load_state prefix, state, remote = nil, should_create_branch = false ref_name = "refs/meta" ref_name += remote.nil? ? "/local" : "/remotes/#{remote}" ref_name += "/#{prefix}/#{state}" if should_create_branch branch_name = "#{prefix}/#{state}" # TODO: Decide what to do when a branch exists previous. Should we : # - Replace it's ref by the saved state ? ( We lose the previous branch reference) # - Load it as it is ( We may get an inconsistent state since the branch could point to a different commit than the state reference ) if @repo.branches[branch_name].nil? puts "Creating branch #{branch_name} based on #{ref_name}" @repo.create_branch branch_name, ref_name end return checkout branch_name end checkout ref_name end |
#name ⇒ Object
27 28 29 |
# File 'lib/metaverse/repo.rb', line 27 def name File.basename @repo.workdir end |
#peek_previous_branch ⇒ Object
68 69 70 |
# File 'lib/metaverse/repo.rb', line 68 def peek_previous_branch @previous_branches.last end |
#pop_previous_branch ⇒ Object
72 73 74 |
# File 'lib/metaverse/repo.rb', line 72 def pop_previous_branch @previous_branches.pop end |
#pull(remote) ⇒ Object
204 205 206 207 208 209 210 211 |
# File 'lib/metaverse/repo.rb', line 204 def pull remote if @repo.remotes[remote].nil? return Errors::remote_not_found remote end Dir.chdir(@repo.workdir) do `git pull #{remote} #{current_branch}` end end |
#send_state(prefix, state, remote, is_branch, should_clean = false) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/metaverse/repo.rb', line 126 def send_state prefix, state, remote, is_branch, should_clean = false refs = ["refs/meta/local/#{prefix}/#{state}"] branch_name = "#{prefix}/#{state}" branch = @repo.branches[branch_name] develop = @repo.branches['develop'] if branch.nil? Errors::ref_not_found branch_name return false end if develop.nil? Errors::ref_not_found 'develop' return false end if (not prefix == 'snapshot') and is_branch and ahead_of_develop? branch refs << branch_name update_ref refs[0], @repo.branches[branch_name] end clean_branch branch if should_clean Dir.chdir(@repo.workdir) do `git push #{remote} #{refs.join(' ')}` end end |
#update(remote = nil) ⇒ Object
175 176 177 178 179 180 181 182 |
# File 'lib/metaverse/repo.rb', line 175 def update remote = nil if @repo.remotes[remote].nil? return Errors::remote_not_found remote end Dir.chdir(@repo.workdir) do `git remote update #{remote}` end end |
#update_ref(ref_name, ref_target) ⇒ Object
91 92 93 |
# File 'lib/metaverse/repo.rb', line 91 def update_ref ref_name, ref_target @repo.references.update ref_name, ref_target.target_id end |
#workdir ⇒ Object
31 32 33 |
# File 'lib/metaverse/repo.rb', line 31 def workdir @repo.workdir end |