Module: Spoom::Git

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

Overview

Execute git commands

Defined Under Namespace

Classes: Commit

Class Method Summary collapse

Class Method Details

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



48
49
50
# File 'lib/spoom/git.rb', line 48

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

.current_branch(path: ".") ⇒ Object



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

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

  result.out.strip
end

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



53
54
55
# File 'lib/spoom/git.rb', line 53

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

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



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/spoom/git.rb', line 26

def 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: ".", short_sha: true) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/spoom/git.rb', line 79

def last_commit(path: ".", short_sha: true)
  result = log("HEAD --format='%#{short_sha ? "h" : "H"} %at' -1", path: path)
  return nil unless result.status

  out = result.out.strip
  return nil if out.empty?

  parse_commit(out)
end

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



58
59
60
# File 'lib/spoom/git.rb', line 58

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

.parse_commit(string) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/spoom/git.rb', line 121

def parse_commit(string)
  sha, epoch = string.split(" ", 2)
  return nil unless sha && epoch

  time = Time.strptime(epoch, "%s")
  Commit.new(sha: sha, time: time)
end

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



63
64
65
# File 'lib/spoom/git.rb', line 63

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

.sorbet_intro_commit(path: ".") ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/spoom/git.rb', line 97

def sorbet_intro_commit(path: ".")
  result = log("--diff-filter=A --format='%h %at' -1 -- sorbet/config", path: path)
  return nil unless result.status

  out = result.out.strip
  return nil if out.empty?

  parse_commit(out)
end

.sorbet_removal_commit(path: ".") ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/spoom/git.rb', line 109

def sorbet_removal_commit(path: ".")
  result = log("--diff-filter=D --format='%h %at' -1 -- sorbet/config", path: path)
  return nil unless result.status

  out = result.out.strip
  return nil if out.empty?

  parse_commit(out)
end

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

Returns:

  • (Boolean)


91
92
93
# File 'lib/spoom/git.rb', line 91

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