Module: Bonchi::Git

Defined in:
lib/bonchi/git.rb

Class Method Summary collapse

Class Method Details

.branch_exists?(branch) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
# File 'lib/bonchi/git.rb', line 35

def branch_exists?(branch)
  system("git show-ref --verify --quiet refs/heads/#{branch.shellescape}") ||
    system("git show-ref --verify --quiet refs/remotes/origin/#{branch.shellescape}")
end

.default_base_branchObject



13
14
15
16
# File 'lib/bonchi/git.rb', line 13

def default_base_branch
  ref = `git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null`.strip
  ref.empty? ? "main" : ref.sub(%r{^refs/remotes/origin/}, "")
end

.fetch_pr(pr_number) ⇒ Object



56
57
58
# File 'lib/bonchi/git.rb', line 56

def fetch_pr(pr_number)
  system("git", "fetch", "origin", "pull/#{pr_number}/head:pr-#{pr_number}")
end

.main_worktreeObject



18
19
20
# File 'lib/bonchi/git.rb', line 18

def main_worktree
  `git worktree list --porcelain`.lines.first.sub("worktree ", "").strip
end

.repo_nameObject



7
8
9
10
11
# File 'lib/bonchi/git.rb', line 7

def repo_name
  url = `git remote get-url origin 2>/dev/null`.strip
  base = url.empty? ? `git rev-parse --show-toplevel`.strip : url
  File.basename(base, ".git")
end

.worktree_add(path, branch) ⇒ Object



40
41
42
# File 'lib/bonchi/git.rb', line 40

def worktree_add(path, branch)
  system("git", "worktree", "add", path, branch) || abort("Failed to add worktree")
end

.worktree_add_new_branch(path, branch, base) ⇒ Object



44
45
46
# File 'lib/bonchi/git.rb', line 44

def worktree_add_new_branch(path, branch, base)
  system("git", "worktree", "add", path, "-b", branch, base) || abort("Failed to add worktree")
end

.worktree_branchesObject



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

def worktree_branches
  worktree_list.filter_map { |line| line[/\[([^\]]+)\]/, 1] }
end

.worktree_dir(branch) ⇒ Object



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

def worktree_dir(branch)
  File.join(GlobalConfig.new.worktree_root, repo_name, branch)
end

.worktree_listObject



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

def worktree_list
  `git worktree list`.lines.map(&:strip).reject(&:empty?)
end

.worktree_path_for(branch) ⇒ Object



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

def worktree_path_for(branch)
  line = `git worktree list`.lines.find { |l| l.include?("[#{branch}]") }
  line&.split(/\s+/)&.first
end

.worktree_pruneObject



52
53
54
# File 'lib/bonchi/git.rb', line 52

def worktree_prune
  system("git", "worktree", "prune")
end

.worktree_remove(path) ⇒ Object



48
49
50
# File 'lib/bonchi/git.rb', line 48

def worktree_remove(path)
  system("git", "worktree", "remove", path) || abort("Failed to remove worktree")
end