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



29
30
31
# File 'lib/spoom/git.rb', line 29

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

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



72
73
74
75
76
# File 'lib/spoom/git.rb', line 72

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



64
65
66
67
68
# File 'lib/spoom/git.rb', line 64

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

.current_branch(path: ".") ⇒ Object



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

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

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



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

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

.epoch_to_time(timestamp) ⇒ Object



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

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
# File 'lib/spoom/git.rb', line 13

def self.exec(command, *arg, path: '.')
  return "", "Error: `#{path}` is not a directory.", false unless File.directory?(path)
  opts = {}
  opts[:chdir] = path
  i, o, e, s = Open3.popen3(*T.unsafe([command, *T.unsafe(arg), opts]))
  out = o.read.to_s
  o.close
  err = e.read.to_s
  e.close
  i.close
  [out, err, T.cast(s.value, Process::Status).success?]
end

.last_commit(path: ".") ⇒ Object



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

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

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



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

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

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



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

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

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



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

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

.sorbet_intro_commit(path: ".") ⇒ Object



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

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

.sorbet_removal_commit(path: ".") ⇒ Object



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

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

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

Returns:

  • (Boolean)


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

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