Class: Gollum::Git::Git
- Inherits:
-
Object
- Object
- Gollum::Git::Git
- Defined in:
- lib/rugged_adapter/git_layer_rugged.rb
Instance Method Summary collapse
- #cat_file(options, sha) ⇒ Object
- #checkout(path, ref = 'HEAD', options = {}) ⇒ Object
- #commit_from_ref(ref) ⇒ Object
- #exist? ⇒ Boolean
- #grep(search_terms, options = {}, &block) ⇒ Object
-
#initialize(repo) ⇒ Git
constructor
Rugged does not have a Git class, but the Repository class should allows us to do what’s necessary.
- #log(ref = 'refs/heads/master', path = nil, options = {}) ⇒ Object
- #lookup(id) ⇒ Object
- #ls_files(query, options = {}) ⇒ Object
- #pull(remote, branches = nil, options = {}) ⇒ Object
- #push(remote, branches = nil, options = {}) ⇒ Object
- #ref_to_sha(query) ⇒ Object
- #revert_commit(sha1, sha2) ⇒ Object
- #revert_path(path, sha1, sha2) ⇒ Object
- #rm(path, options = {}) ⇒ Object
- #sha_or_commit_from_ref(ref, request_kind = nil) ⇒ Object (also: #sha_from_ref)
- #versions_for_path(path = nil, ref = nil, options = {}) ⇒ Object
Constructor Details
#initialize(repo) ⇒ Git
Rugged does not have a Git class, but the Repository class should allows us to do what’s necessary.
161 162 163 |
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 161 def initialize(repo) @repo = repo end |
Instance Method Details
#cat_file(options, sha) ⇒ Object
191 192 193 |
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 191 def cat_file(, sha) @repo.lookup(sha).read_raw end |
#checkout(path, ref = 'HEAD', options = {}) ⇒ Object
226 227 228 229 230 231 232 233 234 235 |
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 226 def checkout(path, ref = 'HEAD', = {}) path = path.nil? ? path : [path] = .merge({:paths => path, :strategy => :force}) if ref == 'HEAD' @repo.checkout_head() else ref = "refs/heads/#{ref}" unless ref =~ /^refs\/heads\// @repo.checkout_tree(sha_from_ref(ref), ) end end |
#commit_from_ref(ref) ⇒ Object
302 303 304 |
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 302 def commit_from_ref(ref) sha_or_commit_from_ref(ref, :commit) end |
#exist? ⇒ Boolean
165 166 167 |
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 165 def exist? ::File.exists?(@repo.path) end |
#grep(search_terms, options = {}, &block) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 169 def grep(search_terms, ={}, &block) ref = [:ref] ? [:ref] : "HEAD" tree = @repo.lookup(sha_from_ref(ref)).tree tree = @repo.lookup(tree[[:path]][:oid]) if [:path] enc = .fetch(:encoding, 'utf-8') results = [] tree.walk_blobs(:postorder) do |root, entry| blob = @repo.lookup(entry[:oid]) path = [:path] ? ::File.join([:path], root, entry[:name]) : "#{root}#{entry[:name]}" data = blob.binary? ? nil : blob.content.force_encoding(enc) results << yield(path, data) end results.compact end |
#log(ref = 'refs/heads/master', path = nil, options = {}) ⇒ Object
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 237 def log(ref = 'refs/heads/master', path = nil, = {}) = { :limit => [:max_count] ? [:max_count] : 10, :offset => [:skip] ? [:skip] : 0, :path => path, :follow => false, :skip_merges => false } = .merge() [:limit] ||= 0 [:offset] ||= 0 sha = sha_from_ref(ref) return [] if sha.nil? begin build_log(sha, ) rescue Rugged::OdbError, Rugged::InvalidError, Rugged::ReferenceError # Return an empty array if the ref wasn't found [] end end |
#lookup(id) ⇒ Object
275 276 277 |
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 275 def lookup(id) @repo.lookup(id) end |
#ls_files(query, options = {}) ⇒ Object
262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 262 def ls_files(query, = {}) ref = [:ref] || "refs/heads/master" tree = @repo.lookup(sha_from_ref(ref)).tree tree = @repo.lookup(tree[[:path]][:oid]) if [:path] results = [] tree.walk_blobs do |root, blob| next unless blob[:name] =~ /#{query}/ path = [:path] ? ::File.join([:path], root, blob[:name]) : "#{root}#{blob[:name]}" results << path end results end |
#pull(remote, branches = nil, options = {}) ⇒ Object
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 311 def pull(remote, branches = nil, = {}) branches = [branches].flatten.map {|branch| "refs/heads/#{branch}" unless branch =~ /^refs\/heads\//} r = @repo.remotes[remote] r.fetch(branches, ) branches.each do |branch| branch_name = branch.match(/^refs\/heads\/(.*)/)[1] remote_name = remote.match(/^(refs\/heads\/)?(.*)/)[2] remote_ref = @repo.branches["#{remote_name}/#{branch_name}"].target local_ref = @repo.branches[branch].target index = @repo.merge_commits(local_ref, remote_ref) = { author: Actor.default_actor.to_h, committer: Actor.default_actor.to_h, message: "Merged branch #{branch} of #{remote}.", parents: [local_ref, remote_ref], tree: index.write_tree(@repo), update_ref: branch } Rugged::Commit.create @repo, @repo.checkout(@repo.head.name, :strategy => :force) if !@repo. && branch == @repo.head.name end end |
#push(remote, branches = nil, options = {}) ⇒ Object
306 307 308 309 |
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 306 def push(remote, branches = nil, = {}) branches = [branches].flatten.map {|branch| "refs/heads/#{branch}" unless branch =~ /^refs\/heads\//} @repo.push(remote, branches, ) end |
#ref_to_sha(query) ⇒ Object
279 280 281 282 283 284 285 286 287 |
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 279 def ref_to_sha(query) return query if sha?(query) query = "refs/heads/#{query}" if !query.nil? && !(query =~ /^refs\/heads\//) && !(query == "HEAD") begin return @repo.rev_parse_oid(query) rescue Rugged::ReferenceError, Rugged::InvalidError return nil end end |
#revert_commit(sha1, sha2) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 209 def revert_commit(sha1, sha2) diff = @repo.diff(sha2, sha1) index = @repo.revert_commit(sha2, sha1) return false unless index paths = [] diff.each_delta do |delta| paths << delta.new_file[:path] paths << delta.old_file[:path] end paths.uniq! begin return index.write_tree(@repo), paths rescue Rugged::IndexError return false end end |
#revert_path(path, sha1, sha2) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 195 def revert_path(path, sha1, sha2) diff = @repo.diff(sha2, sha1, {:paths => [path]}).first.diff begin result = @repo.apply(diff, {:location => :index, :path => path}) rescue RuntimeError, Rugged::PathError return false end begin return @repo.index.write_tree rescue Rugged::IndexError return false end end |
#rm(path, options = {}) ⇒ Object
184 185 186 187 188 189 |
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 184 def rm(path, = {}) index = @repo.index index.write to_delete = ::File.join(@repo.workdir, path) ::File.unlink to_delete if ::File.exist?(to_delete) end |
#sha_or_commit_from_ref(ref, request_kind = nil) ⇒ Object Also known as: sha_from_ref
289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 289 def sha_or_commit_from_ref(ref, request_kind = nil) sha = ref_to_sha(ref) return nil if sha.nil? object = @repo.lookup(sha) if object.kind_of?(Rugged::Commit) then return Gollum::Git::Commit.new(object) if request_kind == :commit sha elsif object.respond_to?(:target) sha_or_commit_from_ref(object.target.oid, request_kind) end end |
#versions_for_path(path = nil, ref = nil, options = {}) ⇒ Object
258 259 260 |
# File 'lib/rugged_adapter/git_layer_rugged.rb', line 258 def versions_for_path(path = nil, ref = nil, = {}) log(ref, path, ) end |