Module: Bonchi::Git

Defined in:
lib/bonchi/git.rb

Class Method Summary collapse

Class Method Details

.branch_exists?(branch) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.clean?(worktree) ⇒ Boolean

Returns:

  • (Boolean)


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

def clean?(worktree)
  `git -C #{worktree.shellescape} status --porcelain`.strip.empty?
end

.current_branch(worktree = nil) ⇒ Object



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

def current_branch(worktree = nil)
  dir = worktree || Dir.pwd
  `git -C #{dir.shellescape} rev-parse --abbrev-ref HEAD`.strip
end

.default_base_branchObject



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

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

.delete_branch(branch, force: false) ⇒ Object



81
82
83
84
# File 'lib/bonchi/git.rb', line 81

def delete_branch(branch, force: false)
  flag = (force || !locally_merged?(branch)) ? "-D" : "-d"
  system("git", "branch", flag, branch) || abort("Failed to delete branch: #{branch}")
end

.fetch_pr(pr_number) ⇒ Object



86
87
88
# File 'lib/bonchi/git.rb', line 86

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

.locally_merged?(branch, into: default_base_branch) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/bonchi/git.rb', line 73

def locally_merged?(branch, into: default_base_branch)
  system("git", "merge-base", "--is-ancestor", branch, into)
end

.main_worktreeObject



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

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

.merged?(branch, into: default_base_branch) ⇒ Boolean

Returns:

  • (Boolean)


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

def merged?(branch, into: default_base_branch)
  locally_merged?(branch, into: into) ||
    remotely_merged?(branch, into: into)
end

.remotely_merged?(branch, into: default_base_branch) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/bonchi/git.rb', line 77

def remotely_merged?(branch, into: default_base_branch)
  system("git", "merge-base", "--is-ancestor", branch, "origin/#{into}")
end

.repo_nameObject



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

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



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

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



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

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

.worktree_branchesObject



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

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

.worktree_dir(branch) ⇒ Object



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

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

.worktree_listObject



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

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

.worktree_path_for(branch) ⇒ Object



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

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

.worktree_pruneObject



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

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

.worktree_remove(path, force: false) ⇒ Object



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

def worktree_remove(path, force: false)
  cmd = ["git", "worktree", "remove"]
  cmd << "--force" if force
  cmd << path
  system(*cmd) || abort("Failed to remove worktree")
end