Module: Spoom::Context::Git

Extended by:
T::Helpers
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

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



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

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

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

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



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

def git_checkout!(ref: "main")
  git("checkout #{ref}")
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



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

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

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



73
74
75
76
77
78
79
80
# File 'lib/spoom/context/git.rb', line 73

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

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



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

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



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

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



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

def git_init!(branch: nil)
  if branch
    git("init -b #{branch}")
  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?



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

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



110
111
112
# File 'lib/spoom/context/git.rb', line 110

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



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

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

#git_show(*arg) ⇒ Object

: (*String arg) -> ExecResult



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

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)


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

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