Class: Hiiro::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/hiiro/git.rb,
lib/hiiro/git/pr.rb,
lib/hiiro/git/branch.rb,
lib/hiiro/git/remote.rb,
lib/hiiro/git/branches.rb,
lib/hiiro/git/worktree.rb,
lib/hiiro/git/worktrees.rb

Defined Under Namespace

Classes: Branch, Branches, Pr, Remote, Worktree, Worktrees

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hiiro = nil, pwd = Dir.pwd, executor: Hiiro::Effects::Executor.new) ⇒ Git

Returns a new instance of Git.



33
34
35
36
37
# File 'lib/hiiro/git.rb', line 33

def initialize(hiiro = nil, pwd = Dir.pwd, executor: Hiiro::Effects::Executor.new)
  @hiiro    = hiiro
  @pwd      = pwd
  @executor = executor
end

Instance Attribute Details

#hiiroObject (readonly)

Returns the value of attribute hiiro.



31
32
33
# File 'lib/hiiro/git.rb', line 31

def hiiro
  @hiiro
end

#pwdObject (readonly)

Returns the value of attribute pwd.



31
32
33
# File 'lib/hiiro/git.rb', line 31

def pwd
  @pwd
end

Class Method Details

.add_resolvers(hiiro) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/hiiro/git.rb', line 14

def self.add_resolvers(hiiro)
  hiiro.add_resolver(:branch, -> { hiiro.fuzzyfind(Branches.local.names) }) do |name|
    Branches.local.find_by_name(name)&.name || name
  end

  hiiro.add_resolver(:worktree,
    -> {
      wts = Worktrees.fetch.without_bare
      map = wts.each_with_object({}) { |wt, h| h["#{wt.name}  #{wt.path}"] = wt.path }
      hiiro.fuzzyfind_from_map(map)
    }
  ) do |ref|
    wts = Worktrees.fetch
    (wts.find_by_path(ref) || wts.find_by_name(ref) || wts.find_by_branch(ref))&.path || ref
  end
end

.rootObject



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

def self.root
  `git rev-parse --show-toplevel 2>/dev/null`.strip
end

Instance Method Details

#add_worktree(path, branch: nil, detach: false) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/hiiro/git.rb', line 121

def add_worktree(path, branch: nil, detach: false)
  args = ['worktree', 'add']
  args << '--detach' if detach
  args << path
  args << branch if branch && !detach
  run_system(*args)
end

#add_worktree_detached(path, repo_path: nil) ⇒ Object



129
130
131
132
133
134
135
# File 'lib/hiiro/git.rb', line 129

def add_worktree_detached(path, repo_path: nil)
  if repo_path
    run_system('-C', repo_path, 'worktree', 'add', '--detach', path)
  else
    run_system('worktree', 'add', '--detach', path)
  end
end

#branchObject

Branch convenience methods



60
61
62
# File 'lib/hiiro/git.rb', line 60

def branch
  current_branch_name
end

#branch_currentObject



64
65
66
# File 'lib/hiiro/git.rb', line 64

def branch_current
  run_safe('branch', '--show-current')
end

#branch_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

def branch_exists?(name)
  run_success?('show-ref', '--verify', '--quiet', "refs/heads/#{name}")
end

#branches(sort_by: nil, ignore_case: false) ⇒ Object



76
77
78
# File 'lib/hiiro/git.rb', line 76

def branches(sort_by: nil, ignore_case: false)
  Branches.fetch(sort_by: sort_by, ignore_case: ignore_case).names
end

#branches_collection(sort_by: nil, ignore_case: false) ⇒ Object



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

def branches_collection(sort_by: nil, ignore_case: false)
  Branches.fetch(sort_by: sort_by, ignore_case: ignore_case)
end

#checkout(ref) ⇒ Object



94
95
96
# File 'lib/hiiro/git.rb', line 94

def checkout(ref)
  run_system('checkout', ref)
end

#commit(ref = 'HEAD', short: false) ⇒ Object



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

def commit(ref = 'HEAD', short: false)
  args = short ? ['rev-parse', '--short', ref] : ['rev-parse', ref]
  run_safe(*args)
end

#create_branch(name, start_point = nil) ⇒ Object



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

def create_branch(name, start_point = nil)
  args = ['checkout', '-b', name]
  args << start_point if start_point
  run_system(*args)
end

#create_pr(title:, body: nil, base: nil, draft: false) ⇒ Object



214
215
216
# File 'lib/hiiro/git.rb', line 214

def create_pr(title:, body: nil, base: nil, draft: false)
  Pr.create(title: title, body: body, base: base, draft: draft)
end

#current_branchObject



68
69
70
# File 'lib/hiiro/git.rb', line 68

def current_branch
  Branch.current
end

#current_branch_nameObject



72
73
74
# File 'lib/hiiro/git.rb', line 72

def current_branch_name
  run_safe('rev-parse', '--abbrev-ref', 'HEAD')
end

#current_prObject

PR convenience methods



206
207
208
# File 'lib/hiiro/git.rb', line 206

def current_pr
  Pr.current
end

#delete_branch(name, force: false) ⇒ Object



98
99
100
101
# File 'lib/hiiro/git.rb', line 98

def delete_branch(name, force: false)
  flag = force ? '-D' : '-d'
  run_system('branch', flag, name)
end

#detached?Boolean

Returns:

  • (Boolean)


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

def detached?
  current_branch_name == 'HEAD'
end

#disable_sparse_checkout(path) ⇒ Object



168
169
170
# File 'lib/hiiro/git.rb', line 168

def disable_sparse_checkout(path)
  run_system('-C', path, 'sparse-checkout', 'disable')
end

#fetch_remote(remote: 'origin') ⇒ Object



200
201
202
# File 'lib/hiiro/git.rb', line 200

def fetch_remote(remote: 'origin')
  Remote.new(name: remote).fetch_remote
end

#in_repo?Boolean

Query methods

Returns:

  • (Boolean)


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

def in_repo?
  !root.nil?
end

#list_prs(state: 'open', limit: 30) ⇒ Object



210
211
212
# File 'lib/hiiro/git.rb', line 210

def list_prs(state: 'open', limit: 30)
  Pr.list(state: state, limit: limit)
end

#lock_worktree(path) ⇒ Object



152
153
154
# File 'lib/hiiro/git.rb', line 152

def lock_worktree(path)
  run_system('worktree', 'lock', path)
end

#move_worktree(from, to, repo_path: nil) ⇒ Object



137
138
139
140
141
142
143
# File 'lib/hiiro/git.rb', line 137

def move_worktree(from, to, repo_path: nil)
  if repo_path
    run_system('-C', repo_path, 'worktree', 'move', from, to)
  else
    run_system('worktree', 'move', from, to)
  end
end

#originObject



188
189
190
# File 'lib/hiiro/git.rb', line 188

def origin
  Remote.origin
end

#prune_worktreesObject



160
161
162
# File 'lib/hiiro/git.rb', line 160

def prune_worktrees
  run_system('worktree', 'prune')
end

#pull(remote: 'origin', branch: nil) ⇒ Object



196
197
198
# File 'lib/hiiro/git.rb', line 196

def pull(remote: 'origin', branch: nil)
  Remote.new(name: remote).pull(branch)
end

#push(remote: 'origin', branch: nil, force: false) ⇒ Object



192
193
194
# File 'lib/hiiro/git.rb', line 192

def push(remote: 'origin', branch: nil, force: false)
  Remote.new(name: remote).push(branch, force: force)
end

#remotesObject

Remote convenience methods



184
185
186
# File 'lib/hiiro/git.rb', line 184

def remotes
  Remote.all
end

#remove_worktree(path, force: false) ⇒ Object



145
146
147
148
149
150
# File 'lib/hiiro/git.rb', line 145

def remove_worktree(path, force: false)
  args = ['worktree', 'remove']
  args << '--force' if force
  args << path
  run_system(*args)
end

#repair_worktreesObject



164
165
166
# File 'lib/hiiro/git.rb', line 164

def repair_worktrees
  run_system('worktree', 'repair')
end

#rootObject



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

def root
  @root ||= run_safe('rev-parse', '--show-toplevel')
end

#sparse_checkout(path, dirs, cone: true) ⇒ Object



172
173
174
175
176
177
178
179
180
# File 'lib/hiiro/git.rb', line 172

def sparse_checkout(path, dirs, cone: true)
  if cone
    run_system('-C', path, 'sparse-checkout', 'init', '--cone')
    dirs = dirs.map { |d| d.sub(%r{\A/}, '') }
  else
    run_system('-C', path, 'sparse-checkout', 'init')
  end
  run_system('-C', path, 'sparse-checkout', 'set', '--skip-checks', *dirs)
end

#unlock_worktree(path) ⇒ Object



156
157
158
# File 'lib/hiiro/git.rb', line 156

def unlock_worktree(path)
  run_system('worktree', 'unlock', path)
end

#worktrees(repo_path: nil) ⇒ Object

Worktree convenience methods



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

def worktrees(repo_path: nil)
  worktrees_collection(repo_path: repo_path).to_a
end

#worktrees_collection(repo_path: nil) ⇒ Object



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

def worktrees_collection(repo_path: nil)
  Worktrees.fetch(repo_path: repo_path)
end

#worktrees_porcelain(repo_path: nil) ⇒ Object



113
114
115
116
117
118
119
# File 'lib/hiiro/git.rb', line 113

def worktrees_porcelain(repo_path: nil)
  if repo_path
    run_safe('-C', repo_path, 'worktree', 'list', '--porcelain')
  else
    run_safe('worktree', 'list', '--porcelain')
  end
end