Class: MultiRepo::Service::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/multi_repo/service/git.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, clone_source:, dry_run: false) ⇒ Git

Returns a new instance of Git.



35
36
37
38
39
40
# File 'lib/multi_repo/service/git.rb', line 35

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

#clientObject (readonly)

Returns the value of attribute client.



33
34
35
# File 'lib/multi_repo/service/git.rb', line 33

def client
  @client
end

#dry_runObject (readonly)

Returns the value of attribute dry_run.



33
34
35
# File 'lib/multi_repo/service/git.rb', line 33

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.message.include?("does not seem to exist")

  clone(clone_source: clone_source, path: path)
  retried = true
  retry
end

.clone(clone_source:, path:) ⇒ Object

Raises:

  • (MiniGit::GitError)


21
22
23
24
25
26
27
28
29
30
31
# File 'lib/multi_repo/service/git.rb', line 21

def self.clone(clone_source:, path:)
  require "minigit"
  require "shellwords"

  args = ["clone", clone_source, path]
  command = Shellwords.join(["git", *args])
  command << " &>/dev/null" unless 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?

Returns:

  • (Boolean)


69
70
71
72
73
74
75
# File 'lib/multi_repo/service/git.rb', line 69

def branch?(branch)
  client.capturing.rev_parse("--verify", branch)
rescue MiniGit::GitError
  false
else
  true
end

#destroy_tag(tag, output: false) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/multi_repo/service/git.rb', line 56

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



42
43
44
45
46
# File 'lib/multi_repo/service/git.rb', line 42

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



48
49
50
51
52
53
54
# File 'lib/multi_repo/service/git.rb', line 48

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

#remote?(remote) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
84
# File 'lib/multi_repo/service/git.rb', line 78

def remote?(remote)
  client.capturing.remote("show", remote)
rescue MiniGit::GitError => e
  false
else
  true
end

#remote_branch?(remote, branch) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/multi_repo/service/git.rb', line 86

def remote_branch?(remote, branch)
  client.capturing.ls_remote(remote, branch).present?
end