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



27
28
29
# File 'lib/spoom/git.rb', line 27

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

.current_branch(path: ".") ⇒ Object



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

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

  result.out.strip
end

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



32
33
34
# File 'lib/spoom/git.rb', line 32

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

.last_commit(path: ".", short_sha: true) ⇒ Object



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

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



37
38
39
# File 'lib/spoom/git.rb', line 37

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

.parse_commit(string) ⇒ Object



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

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



42
43
44
# File 'lib/spoom/git.rb', line 42

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

.sorbet_intro_commit(path: ".") ⇒ Object



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

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



88
89
90
91
92
93
94
95
96
# File 'lib/spoom/git.rb', line 88

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)


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

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