Class: GerritSeed::Git

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir:, host:, log:, name: nil, port:, user:) ⇒ Git

Returns a new instance of Git.



5
6
7
8
9
10
11
12
13
# File 'lib/gerrit_seed/git.rb', line 5

def initialize(dir:, host:, log:, name: nil, port:, user:)
  @dir = dir
  @host = host
  @log = log
  @name = name
  @port = port
  @shell = Shell.new(chdir: dir)
  @user = user
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



3
4
5
# File 'lib/gerrit_seed/git.rb', line 3

def dir
  @dir
end

#hostObject (readonly)

Returns the value of attribute host.



3
4
5
# File 'lib/gerrit_seed/git.rb', line 3

def host
  @host
end

#logObject (readonly)

Returns the value of attribute log.



3
4
5
# File 'lib/gerrit_seed/git.rb', line 3

def log
  @log
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/gerrit_seed/git.rb', line 3

def name
  @name
end

#portObject (readonly)

Returns the value of attribute port.



3
4
5
# File 'lib/gerrit_seed/git.rb', line 3

def port
  @port
end

#shellObject (readonly)

Returns the value of attribute shell.



3
4
5
# File 'lib/gerrit_seed/git.rb', line 3

def shell
  @shell
end

#userObject (readonly)

Returns the value of attribute user.



3
4
5
# File 'lib/gerrit_seed/git.rb', line 3

def user
  @user
end

Instance Method Details

#checkout(branch:, commit:) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/gerrit_seed/git.rb', line 43

def checkout(branch:, commit:)
  if available_branches.include?(branch)
    shell.('git', 'checkout', branch)
    shell.('git clean -f -d')
  else
    shell.('git', 'checkout', '-b', branch, commit)
    shell.('git clean -f -d')
  end
end

#clone(name:) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/gerrit_seed/git.rb', line 15

def clone(name:)
  unless File.exist?(File.join(dir, name))
    shell.("git", "clone", "ssh://#{user}@#{host}:#{port}/#{name}", name)
    shell.(
      'scp',
        '-p',
        '-P', port.to_s,
        "#{user}@#{host}:hooks/commit-msg",
        "#{name}/.git/hooks/"
    )
  end

  self.class.new(
    dir: File.join(dir, name),
    host: host,
    log: log,
    name: name,
    port: port,
    user: user
  )
end

#commit(author:, email:, subject:) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/gerrit_seed/git.rb', line 53

def commit(author:, email:, subject:)
  return if shell.('git', 'status', '--short').empty?

  shell.(
    'git',
      "-c", "user.name='#{author}'",
      "-c", "user.email='#{email}'",
      'commit',
        "--author='#{author} <#{email}>'",
        '-m', subject
  )
end

#push(user:, reviewers: []) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/gerrit_seed/git.rb', line 66

def push(user:, reviewers: [])
  fail "name is not set, did you forget to #clone?" if @name.nil?

  reviewers_query = if reviewers.any?
    '%' + reviewers.map { |x| "r=#{x}" }.join(',')
  else
    ''
  end

  shell.(
    'git', 'push',
    "ssh://#{user}@#{host}:#{port}/#{name}",
    'HEAD:refs/for/master' + reviewers_query
  )
end

#rm_rf(name:) ⇒ Object



37
38
39
40
41
# File 'lib/gerrit_seed/git.rb', line 37

def rm_rf(name:)
  if File.exist?(File.join(dir, name))
    FileUtils.rm_rf(File.join(dir, name))
  end
end