Module: Spoom::Context::Git

Included in:
Spoom::Context
Defined in:
lib/spoom/context/git.rb

Overview

Git features for a context @requires_ancestor: Context

Instance Method Summary collapse

Instance Method Details

#git(command) ⇒ Object

Run a command prefixed by git in this context directory : (String command) -> ExecResult



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

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

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

Run git checkout in this context directory : (?ref: String) -> ExecResult



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

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

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

Run git checkout -b <branch-name> <ref> in this context directory : (String branch_name, ?ref: String?) -> ExecResult



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

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

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

Run git add . && git commit in this context directory : (?message: String, ?time: Time, ?allow_empty: bool) -> ExecResult



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

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

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

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

#git_current_branch ⇒ Object

Get the current git branch in this context directory : -> String?



92
93
94
95
96
97
# File 'lib/spoom/context/git.rb', line 92

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

  res.out.strip
end

#git_diff(*arg) ⇒ Object

Run git diff in this context directory : (*String arg) -> ExecResult



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

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

#git_init!(branch: nil) ⇒ Object

Run git init in this context directory

Warning: passing a branch will run git init -b <branch> which is only available in git 2.28+. In older versions, use git_init! followed by git("checkout -b <branch>"). : (?branch: String?) -> ExecResult



55
56
57
58
59
60
61
# File 'lib/spoom/context/git.rb', line 55

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

#git_last_commit(short_sha: true) ⇒ Object

Get the last commit in the currently checked out branch : (?short_sha: bool) -> Spoom::Git::Commit?



107
108
109
110
111
112
113
114
115
# File 'lib/spoom/context/git.rb', line 107

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

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

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

#git_log(*arg) ⇒ Object

: (*String arg) -> ExecResult



118
119
120
# File 'lib/spoom/context/git.rb', line 118

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

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

Run git push <remote> <ref> in this context directory : (String remote, String ref, ?force: bool) -> ExecResult



124
125
126
# File 'lib/spoom/context/git.rb', line 124

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

#git_show(*arg) ⇒ Object

: (*String arg) -> ExecResult



129
130
131
# File 'lib/spoom/context/git.rb', line 129

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

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

Is there uncommitted changes in this context directory? : (?path: String) -> bool

Returns:

  • (Boolean)


135
136
137
# File 'lib/spoom/context/git.rb', line 135

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