Class: MultiRepo::Service::Git
- Inherits:
-
Object
- Object
- MultiRepo::Service::Git
- Defined in:
- lib/multi_repo/service/git.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#dry_run ⇒ Object
readonly
Returns the value of attribute dry_run.
Class Method Summary collapse
- .client(path:, clone_source:) ⇒ Object
- .clone(clone_source:, path:) ⇒ Object
- .raw(*args, quiet: false) ⇒ Object
Instance Method Summary collapse
- #branch?(branch) ⇒ Boolean (also: #tag?)
- #destroy_remote(remote) ⇒ Object
- #destroy_tag(tag, output: false) ⇒ Object
- #fetch(output: false) ⇒ Object
- #hard_checkout(branch, source = "origin/#{branch}", output: false) ⇒ Object
-
#initialize(path:, clone_source:, dry_run: false) ⇒ Git
constructor
A new instance of Git.
- #raw(*args) ⇒ Object
- #remote?(remote) ⇒ Boolean
- #remote_branch?(remote, branch) ⇒ Boolean
Constructor Details
#initialize(path:, clone_source:, dry_run: false) ⇒ Git
Returns a new instance of Git.
38 39 40 41 42 43 |
# File 'lib/multi_repo/service/git.rb', line 38 def initialize(path:, clone_source:, dry_run: false) require "minigit" @dry_run = dry_run @client = self.class.client(path: path, clone_source: clone_source) end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
36 37 38 |
# File 'lib/multi_repo/service/git.rb', line 36 def client @client end |
#dry_run ⇒ Object (readonly)
Returns the value of attribute dry_run.
36 37 38 |
# File 'lib/multi_repo/service/git.rb', line 36 def dry_run @dry_run end |
Class Method Details
.client(path:, clone_source:) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/multi_repo/service/git.rb', line 5 def self.client(path:, clone_source:) require "minigit" require_relative "git/minigit_capturing_patch" retried = false MiniGit.debug = true if ENV["GIT_DEBUG"] MiniGit.new(path) rescue ArgumentError => err raise if retried raise unless err..include?("does not seem to exist") clone(clone_source: clone_source, path: path) retried = true retry end |
.clone(clone_source:, path:) ⇒ Object
32 33 34 |
# File 'lib/multi_repo/service/git.rb', line 32 def self.clone(clone_source:, path:) raw("clone", clone_source, path, quiet: true) end |
.raw(*args, quiet: false) ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/multi_repo/service/git.rb', line 21 def self.raw(*args, quiet: false) require "minigit" require "shellwords" command = Shellwords.join(["git", *args]) command << " &>/dev/null" if quiet && !ENV["GIT_DEBUG"] puts "+ #{command}" if ENV["GIT_DEBUG"] # Matches the output of MiniGit raise MiniGit::GitError.new(args, $?) unless system(command) end |
Instance Method Details
#branch?(branch) ⇒ Boolean Also known as: tag?
76 77 78 79 80 81 82 |
# File 'lib/multi_repo/service/git.rb', line 76 def branch?(branch) client.capturing.rev_parse("--verify", branch) rescue MiniGit::GitError false else true end |
#destroy_remote(remote) ⇒ Object
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/multi_repo/service/git.rb', line 93 def destroy_remote(remote) if dry_run puts "** dry-run: git remote rm #{remote}".light_black else client.remote("rm", remote) end rescue MiniGit::GitError # Ignore missing remotes because we want them destroyed anyway nil end |
#destroy_tag(tag, output: false) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/multi_repo/service/git.rb', line 63 def destroy_tag(tag, output: false) client = output ? self.client : self.client.capturing if dry_run puts "** dry-run: git tag --delete #{tag}".light_black else client.tag({:delete => true}, tag) end rescue MiniGit::GitError # Ignore missing tags because we want them destroyed anyway nil end |
#fetch(output: false) ⇒ Object
49 50 51 52 53 |
# File 'lib/multi_repo/service/git.rb', line 49 def fetch(output: false) client = output ? self.client : self.client.capturing client.fetch(:all => true, :tags => true) end |
#hard_checkout(branch, source = "origin/#{branch}", output: false) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/multi_repo/service/git.rb', line 55 def hard_checkout(branch, source = "origin/#{branch}", output: false) client = output ? self.client : self.client.capturing client.reset(:hard => true) client.clean("-xdf") client.checkout("-B", branch, source) end |
#raw(*args) ⇒ Object
45 46 47 |
# File 'lib/multi_repo/service/git.rb', line 45 def raw(*args) Dir.chdir(client.git_dir) { self.class.raw(*args) } end |
#remote?(remote) ⇒ Boolean
85 86 87 88 89 90 91 |
# File 'lib/multi_repo/service/git.rb', line 85 def remote?(remote) client.capturing.remote("show", remote) rescue MiniGit::GitError => e false else true end |
#remote_branch?(remote, branch) ⇒ Boolean
104 105 106 |
# File 'lib/multi_repo/service/git.rb', line 104 def remote_branch?(remote, branch) client.capturing.ls_remote(remote, branch).present? end |