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



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

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

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

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



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

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



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

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



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

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?



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

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



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

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



44
45
46
47
48
49
50
# File 'lib/spoom/context/git.rb', line 44

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?



96
97
98
99
100
101
102
103
104
# File 'lib/spoom/context/git.rb', line 96

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



107
108
109
# File 'lib/spoom/context/git.rb', line 107

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



113
114
115
# File 'lib/spoom/context/git.rb', line 113

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

#git_show(*arg) ⇒ Object

: (*String arg) -> ExecResult



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

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)


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

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