Module: Spoom::Git

Extended by:
T::Sig
Defined in:
lib/spoom/git.rb

Overview

Execute git commands

Class Method Summary collapse

Class Method Details

.checkout(*arg, path: ".") ⇒ Object



35
36
37
# File 'lib/spoom/git.rb', line 35

def self.checkout(*arg, path: ".")
  exec("git checkout -q #{arg.join(' ')}", path: path)
end

.commit_time(sha, path: ".") ⇒ Object



78
79
80
81
82
# File 'lib/spoom/git.rb', line 78

def self.commit_time(sha, path: ".")
  timestamp = commit_timestamp(sha, path: path)
  return nil unless timestamp
  epoch_to_time(timestamp.to_s)
end

.commit_timestamp(sha, path: ".") ⇒ Object



70
71
72
73
74
# File 'lib/spoom/git.rb', line 70

def self.commit_timestamp(sha, path: ".")
  result = show("--no-notes --no-patch --pretty=%at #{sha}", path: path)
  return nil unless result.status
  result.out.strip.to_i
end

.current_branch(path: ".") ⇒ Object



60
61
62
63
64
# File 'lib/spoom/git.rb', line 60

def self.current_branch(path: ".")
  result = exec("git branch --show-current", path: path)
  return nil unless result.status
  result.out.strip
end

.diff(*arg, path: ".") ⇒ Object



40
41
42
# File 'lib/spoom/git.rb', line 40

def self.diff(*arg, path: ".")
  exec("git diff #{arg.join(' ')}", path: path)
end

.epoch_to_time(timestamp) ⇒ Object



94
95
96
# File 'lib/spoom/git.rb', line 94

def self.epoch_to_time(timestamp)
  Time.strptime(timestamp, "%s")
end

.exec(command, *arg, path: '.') ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/spoom/git.rb', line 13

def self.exec(command, *arg, path: '.')
  return ExecResult.new(
    out: "",
    err: "Error: `#{path}` is not a directory.",
    status: false,
    exit_code: 1
  ) unless File.directory?(path)

  T.unsafe(Open3).popen3(command, *arg, chdir: path) do |_, stdout, stderr, thread|
    status = T.cast(thread.value, Process::Status)
    ExecResult.new(
      out: stdout.read,
      err: stderr.read,
      status: T.must(status.success?),
      exit_code: T.must(status.exitstatus)
    )
  end
end

.last_commit(path: ".") ⇒ Object



86
87
88
89
90
# File 'lib/spoom/git.rb', line 86

def self.last_commit(path: ".")
  result = rev_parse("HEAD", path: path)
  return nil unless result.status
  result.out.strip
end

.log(*arg, path: ".") ⇒ Object



45
46
47
# File 'lib/spoom/git.rb', line 45

def self.log(*arg, path: ".")
  exec("git log #{arg.join(' ')}", path: path)
end

.rev_parse(*arg, path: ".") ⇒ Object



50
51
52
# File 'lib/spoom/git.rb', line 50

def self.rev_parse(*arg, path: ".")
  exec("git rev-parse --short #{arg.join(' ')}", path: path)
end

.show(*arg, path: ".") ⇒ Object



55
56
57
# File 'lib/spoom/git.rb', line 55

def self.show(*arg, path: ".")
  exec("git show #{arg.join(' ')}", path: path)
end

.sorbet_intro_commit(path: ".") ⇒ Object



106
107
108
109
110
111
112
# File 'lib/spoom/git.rb', line 106

def self.sorbet_intro_commit(path: ".")
  result = Spoom::Git.log("--diff-filter=A --format='%h' -1 -- sorbet/config", path: path)
  return nil unless result.status
  out = result.out.strip
  return nil if out.empty?
  out
end

.sorbet_removal_commit(path: ".") ⇒ Object



116
117
118
119
120
121
122
# File 'lib/spoom/git.rb', line 116

def self.sorbet_removal_commit(path: ".")
  result = Spoom::Git.log("--diff-filter=D --format='%h' -1 -- sorbet/config", path: path)
  return nil unless result.status
  out = result.out.strip
  return nil if out.empty?
  out
end

.workdir_clean?(path: ".") ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/spoom/git.rb', line 100

def self.workdir_clean?(path: ".")
  diff("HEAD", path: path).out.empty?
end