Module: GT::Git

Defined in:
lib/gt/git.rb

Class Method Summary collapse

Class Method Details

.add_allObject



22
23
24
# File 'lib/gt/git.rb', line 22

def add_all
  run("git add -A")
end

.add_file(path) ⇒ Object



30
31
32
# File 'lib/gt/git.rb', line 30

def add_file(path)
  run("git add -- #{Shellwords.escape(path)}")
end

.add_patchObject



41
42
43
# File 'lib/gt/git.rb', line 41

def add_patch
  system("git add --patch")
end

.add_trackedObject



26
27
28
# File 'lib/gt/git.rb', line 26

def add_tracked
  run("git add -u")
end

.all_branchesObject



134
135
136
137
# File 'lib/gt/git.rb', line 134

def all_branches
  out, _, _ = Open3.capture3("git", "branch")
  out.strip.split("\n").map { |b| b.delete_prefix("* ").strip }
end

.amend(message: nil) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/gt/git.rb', line 126

def amend(message: nil)
  if message
    run("git commit --amend -m #{Shellwords.escape(message)}")
  else
    run("git commit --amend --no-edit")
  end
end

.ancestor?(branch, parent) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
# File 'lib/gt/git.rb', line 62

def ancestor?(branch, parent)
  _, _, status = Open3.capture3("git merge-base --is-ancestor #{branch} #{parent}")
  status.success?
end

.checkout(branch, new_branch: false) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/gt/git.rb', line 14

def checkout(branch, new_branch: false)
  if new_branch
    run("git checkout -b #{branch}")
  else
    run("git checkout #{branch}")
  end
end

.commit(message) ⇒ Object



45
46
47
# File 'lib/gt/git.rb', line 45

def commit(message)
  run("git commit -m #{Shellwords.escape(message)}")
end

.config_get(key) ⇒ Object



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

def config_get(key)
  out, _, status = Open3.capture3("git config #{key}")
  status.success? ? out.strip : nil
end

.config_set(key, value) ⇒ Object



84
85
86
# File 'lib/gt/git.rb', line 84

def config_set(key, value)
  run("git config #{key} #{Shellwords.escape(value)}")
end

.config_unset(key) ⇒ Object



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

def config_unset(key)
  _out, _err, _status = Open3.capture3("git config --unset #{key}")
end

.current_branchObject



10
11
12
# File 'lib/gt/git.rb', line 10

def current_branch
  run("git rev-parse --abbrev-ref HEAD").strip
end

.git_dirObject



139
140
141
# File 'lib/gt/git.rb', line 139

def git_dir
  run("git rev-parse --git-dir").strip
end

.gt_fork_point(branch) ⇒ Object



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

def gt_fork_point(branch)
  config_get("branch.#{branch}.gt-fork-point")
end

.gt_parent(branch) ⇒ Object



97
98
99
# File 'lib/gt/git.rb', line 97

def gt_parent(branch)
  config_get("branch.#{branch}.gt-parent")
end

.main_branchObject



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

def main_branch
  config_get("gt.main-branch") || "main"
end

.merge_base(a, b) ⇒ Object



58
59
60
# File 'lib/gt/git.rb', line 58

def merge_base(a, b)
  run("git merge-base #{a} #{b}").strip
end

.pullObject



122
123
124
# File 'lib/gt/git.rb', line 122

def pull
  run("git pull")
end

.push(branch, force: false) ⇒ Object



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

def push(branch, force: false)
  flag = force ? "--force-with-lease" : ""
  run("git push #{flag} origin #{branch}".strip)
end

.rebase_abortObject



75
76
77
# File 'lib/gt/git.rb', line 75

def rebase_abort
  run("git rebase --abort")
end

.rebase_continueObject



71
72
73
# File 'lib/gt/git.rb', line 71

def rebase_continue
  run("GIT_EDITOR=true git rebase --continue")
end

.rebase_in_progress?Boolean

Returns:

  • (Boolean)


79
80
81
82
# File 'lib/gt/git.rb', line 79

def rebase_in_progress?
  File.exist?(File.join(git_dir, "rebase-merge")) ||
    File.exist?(File.join(git_dir, "rebase-apply"))
end

.rebase_onto(new_base, fork_point, branch) ⇒ Object



67
68
69
# File 'lib/gt/git.rb', line 67

def rebase_onto(new_base, fork_point, branch)
  run("git rebase --onto #{new_base} #{fork_point} #{branch}")
end

.rev_parse(ref) ⇒ Object



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

def rev_parse(ref)
  run("git rev-parse #{ref}").strip
end

.run(cmd) ⇒ Object

Raises:



143
144
145
146
147
148
# File 'lib/gt/git.rb', line 143

def run(cmd)
  out, err, status = Open3.capture3(cmd)
  raise GT::GitError, "#{cmd}\n#{err.strip}" unless status.success?

  out
end

.set_gt_fork_point(branch, sha) ⇒ Object



109
110
111
# File 'lib/gt/git.rb', line 109

def set_gt_fork_point(branch, sha)
  run("git config branch.#{branch}.gt-fork-point #{Shellwords.escape(sha)}")
end

.set_gt_parent(branch, parent) ⇒ Object



105
106
107
# File 'lib/gt/git.rb', line 105

def set_gt_parent(branch, parent)
  run("git config branch.#{branch}.gt-parent #{Shellwords.escape(parent)}")
end

.unset_gt_meta(branch) ⇒ Object



113
114
115
116
# File 'lib/gt/git.rb', line 113

def unset_gt_meta(branch)
  config_unset("branch.#{branch}.gt-parent")
  config_unset("branch.#{branch}.gt-fork-point")
end

.untracked_filesObject



34
35
36
37
38
39
# File 'lib/gt/git.rb', line 34

def untracked_files
  out, _, status = Open3.capture3("git ls-files --others --exclude-standard")
  return [] unless status.success?

  out.strip.split("\n").reject(&:empty?)
end