Module: Spoom::Context::Git

Extended by:
T::Helpers, T::Sig
Included in:
Spoom::Context
Defined in:
lib/spoom/context/git.rb

Overview

Git features for a context

Instance Method Summary collapse

Instance Method Details

#git(command) ⇒ Object



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

def git(command)
  exec("git #{command}")
end

#git_checkout!(ref: "main") ⇒ Object



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

def git_checkout!(ref: "main")
  git("checkout #{ref}")
end

#git_checkout_new_branch!(branch_name, ref: nil) ⇒ Object



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

def git_checkout_new_branch!(branch_name, ref: nil)
  if ref
    git("checkout -b #{branch_name} #{ref}")
  else
    git("checkout -b #{branch_name}")
  end
end

#git_commit!(message: "message", time: Time.now.utc, allow_empty: false) ⇒ Object



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

def git_commit!(message: "message", time: Time.now.utc, allow_empty: false)
  git("add --all")

  args = ["-m '#{message}'", "--date '#{time}'"]
  args << "--allow-empty" if allow_empty

  exec("GIT_COMMITTER_DATE=\"#{time}\" git -c commit.gpgsign=false commit #{args.join(" ")}")
end

#git_current_branchObject



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

def git_current_branch
  res = git("branch --show-current")
  return nil unless res.status

  res.out.strip
end

#git_diff(*arg) ⇒ Object



98
99
100
# File 'lib/spoom/context/git.rb', line 98

def git_diff(*arg)
  git("diff #{arg.join(" ")}")
end

#git_init!(branch: nil) ⇒ Object



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

def git_init!(branch: nil)
  if branch
    git("init -b #{branch}")
  else
    git("init")
  end
end

#git_last_commit(short_sha: true) ⇒ Object



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

def git_last_commit(short_sha: true)
  res = git_log("HEAD --format='%#{short_sha ? "h" : "H"} %at' -1")
  return nil unless res.status

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

  Spoom::Git::Commit.parse_line(out)
end

#git_log(*arg) ⇒ Object



115
116
117
# File 'lib/spoom/context/git.rb', line 115

def git_log(*arg)
  git("log #{arg.join(" ")}")
end

#git_push!(remote, ref, force: false) ⇒ Object



121
122
123
# File 'lib/spoom/context/git.rb', line 121

def git_push!(remote, ref, force: false)
  git("push #{force ? "-f" : ""} #{remote} #{ref}")
end

#git_show(*arg) ⇒ Object



126
127
128
# File 'lib/spoom/context/git.rb', line 126

def git_show(*arg)
  git("show #{arg.join(" ")}")
end

#git_workdir_clean?(path: ".") ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/spoom/context/git.rb', line 132

def git_workdir_clean?(path: ".")
  git_diff("HEAD").out.empty?
end