Module: Legion::Extensions::Exec::Runners::Git

Extended by:
Definitions
Defined in:
lib/legion/extensions/exec/runners/git.rb

Overview

rubocop:disable Legion/Extension/RunnerIncludeHelpers

Constant Summary

Constants included from Definitions

Definitions::DEFAULTS

Class Method Summary collapse

Methods included from Definitions

definition, definition_for, definitions

Class Method Details

.add(path:, files: '.') ⇒ Object



40
41
42
43
# File 'lib/legion/extensions/exec/runners/git.rb', line 40

def add(path:, files: '.', **)
  cmd = files == '.' ? 'git add -A' : "git add #{shellwords_join(Array(files))}"
  Runners::Shell.execute(command: cmd, cwd: path)
end

.branch(path:, all: false) ⇒ Object



125
126
127
128
# File 'lib/legion/extensions/exec/runners/git.rb', line 125

def branch(path:, all: false, **)
  command = all ? 'git branch --all' : 'git branch'
  Runners::Shell.execute(command: command, cwd: path)
end

.checkout(path:, ref:, create: false) ⇒ Object



232
233
234
235
# File 'lib/legion/extensions/exec/runners/git.rb', line 232

def checkout(path:, ref:, create: false, **)
  flag = create ? ' -b' : ''
  Runners::Shell.execute(command: "git checkout#{flag} #{Shellwords.shellescape(ref)}", cwd: path)
end

.clone(url:, path:, depth: nil, branch: nil) ⇒ Object



195
196
197
198
199
200
201
202
203
# File 'lib/legion/extensions/exec/runners/git.rb', line 195

def clone(url:, path:, depth: nil, branch: nil, **)
  resolved_depth = depth
  resolved_depth ||= Legion::Settings.dig(:fleet, :git, :depth) if defined?(Legion::Settings)
  args = ['git clone']
  args << "--depth #{resolved_depth}" if resolved_depth
  args << "--branch #{Shellwords.shellescape(branch)}" if branch
  args << Shellwords.shellescape(url) << Shellwords.shellescape(path)
  Runners::Shell.execute(command: args.join(' '), cwd: Dir.pwd)
end

.commit(path:, message:) ⇒ Object



55
56
57
# File 'lib/legion/extensions/exec/runners/git.rb', line 55

def commit(path:, message:, **)
  Runners::Shell.execute(command: "git commit -m #{Shellwords.shellescape(message)}", cwd: path)
end

.create_repo(name:, org: 'LegionIO', description: '', public: true) ⇒ Object



175
176
177
178
179
180
181
# File 'lib/legion/extensions/exec/runners/git.rb', line 175

def create_repo(name:, org: 'LegionIO', description: '', public: true, **)
  visibility = public ? '--public' : '--private'
  Runners::Shell.execute(
    command: "gh repo create #{Shellwords.shellescape("#{org}/#{name}")} #{visibility} --description #{Shellwords.shellescape(description)} --clone",
    cwd:     Dir.pwd
  )
end

.diff(path:, staged: false, ref: nil, files: nil) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/legion/extensions/exec/runners/git.rb', line 105

def diff(path:, staged: false, ref: nil, files: nil, **)
  args = ['git diff']
  args << '--cached' if staged
  args << Shellwords.shellescape(ref) if ref
  args << '--' if files
  args << shellwords_join(Array(files)) if files
  Runners::Shell.execute(command: args.join(' '), cwd: path)
end

.fetch(path:, remote: nil) ⇒ Object



216
217
218
219
# File 'lib/legion/extensions/exec/runners/git.rb', line 216

def fetch(path:, remote: nil, **)
  cmd = remote ? "git fetch #{Shellwords.shellescape(remote)} --prune" : 'git fetch --all --prune'
  Runners::Shell.execute(command: cmd, cwd: path)
end

.init(path:) ⇒ Object



26
27
28
# File 'lib/legion/extensions/exec/runners/git.rb', line 26

def init(path:, **)
  Runners::Shell.execute(command: 'git init', cwd: path)
end

.log(path:, max_count: 20, oneline: true) ⇒ Object



142
143
144
145
146
# File 'lib/legion/extensions/exec/runners/git.rb', line 142

def log(path:, max_count: 20, oneline: true, **)
  args = ['git log', "--max-count=#{Integer(max_count)}"]
  args << '--oneline' if oneline
  Runners::Shell.execute(command: args.join(' '), cwd: path)
end

.push(path:, remote: nil, branch: nil, set_upstream: false) ⇒ Object



71
72
73
# File 'lib/legion/extensions/exec/runners/git.rb', line 71

def push(path:, remote: nil, branch: nil, set_upstream: false, **)
  Runners::Shell.execute(command: push_command(remote: remote, branch: branch, set_upstream: set_upstream), cwd: path)
end

.push_command(remote:, branch:, set_upstream:) ⇒ Object



241
242
243
244
245
246
247
# File 'lib/legion/extensions/exec/runners/git.rb', line 241

def push_command(remote:, branch:, set_upstream:)
  args = ['git push']
  args << '-u' if set_upstream
  args << Shellwords.shellescape(remote) if remote
  args << Shellwords.shellescape(branch) if branch
  args.join(' ')
end

.shellwords_join(values) ⇒ Object



237
238
239
# File 'lib/legion/extensions/exec/runners/git.rb', line 237

def shellwords_join(values)
  values.map { |value| Shellwords.shellescape(value.to_s) }.join(' ')
end

.show(path:, ref: 'HEAD') ⇒ Object



159
160
161
# File 'lib/legion/extensions/exec/runners/git.rb', line 159

def show(path:, ref: 'HEAD', **)
  Runners::Shell.execute(command: "git show #{Shellwords.shellescape(ref)}", cwd: path)
end

.status(path:) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/legion/extensions/exec/runners/git.rb', line 84

def status(path:, **)
  result = Runners::Shell.execute(command: 'git status --porcelain', cwd: path)
  return result unless result[:success] # rubocop:disable Legion/Extension/RunnerReturnHash

  parsed = Helpers::ResultParser.parse_git_status(result[:stdout] || '')
  result.merge(parsed: parsed)
end

.trigger_wordsObject



14
15
16
# File 'lib/legion/extensions/exec/runners/git.rb', line 14

def self.trigger_words
  %w[git repo repository status diff add commit branch checkout clone fetch push]
end