Class: Bonchi::CLI

Inherits:
Thor
  • Object
show all
Includes:
Colors
Defined in:
lib/bonchi/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


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

def self.exit_on_failure?
  true
end

Instance Method Details

#create(branch, base = nil) ⇒ Object



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

def create(branch, base = nil)
  invoke :switch, [branch], c: true, base: base, setup: options[:setup], upto: options[:upto]
end

#initObject



116
117
118
119
120
121
122
123
124
# File 'lib/bonchi/cli.rb', line 116

def init
  path = File.join(Dir.pwd, ".worktree.yml")
  if File.exist?(path)
    abort "Error: .worktree.yml already exists"
  end

  File.write(path, WORKTREE_YML_TEMPLATE)
  puts "Created #{path}"
end

#listObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/bonchi/cli.rb', line 139

def list
  lines = Git.worktree_list
  base = Git.default_base_branch
  home = Dir.home

  lines.each do |line|
    branch = line[/\[([^\]]+)\]/, 1]
    path = line.split(/\s+/).first
    line = line.sub(home, "~")

    unless branch
      puts line
      next
    end

    if branch == base
      puts line
      next
    end

    merged = Git.merged?(branch, into: base)
    clean = Git.clean?(path)
    tags = []
    tags << "#{color(:yellow)}dirty#{reset}" unless clean
    tags << "#{color(:green)}merged#{reset}" if merged

    if tags.any?
      puts "#{line}  #{tags.join(" ")}"
    else
      puts line
    end
  end
end

#pr(input) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/bonchi/cli.rb', line 86

def pr(input)
  pr_number = extract_pr_number(input)
  branch = "pr-#{pr_number}"
  path = Git.worktree_dir(branch)

  existing = Git.worktree_path_for(branch)
  if existing
    puts "Worktree already exists: #{existing}"
    signal_cd(existing)
    return
  end

  Git.fetch_pr(pr_number)
  Git.worktree_add(path, branch)
  puts "PR ##{pr_number} checked out at: #{path}"

  signal_cd(path)

  if options[:setup] && Config.from_main_worktree
    puts ""
    Setup.new(worktree: path).run(upto: options[:upto])
  end
end

#pruneObject



206
207
208
209
# File 'lib/bonchi/cli.rb', line 206

def prune
  Git.worktree_prune
  puts "Pruned stale worktree administrative files"
end

#remove(branch) ⇒ Object



184
185
186
# File 'lib/bonchi/cli.rb', line 184

def remove(branch)
  remove_worktree(branch, force: options[:force], delete_branch: :merged)
end

#rmf(branch) ⇒ Object



189
190
191
# File 'lib/bonchi/cli.rb', line 189

def rmf(branch)
  remove_worktree(branch, force: true, delete_branch: :merged)
end

#rmrf(branch) ⇒ Object



194
195
196
# File 'lib/bonchi/cli.rb', line 194

def rmrf(branch)
  remove_worktree(branch, force: true, delete_branch: :always)
end

#setup(*args) ⇒ Object



128
129
130
# File 'lib/bonchi/cli.rb', line 128

def setup(*args)
  Setup.new.run(args, upto: options[:upto])
end

#shellenvObject



212
213
214
# File 'lib/bonchi/cli.rb', line 212

def shellenv
  puts SHELL_ENV
end

#switch(branch) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/bonchi/cli.rb', line 35

def switch(branch)
  abort "Error: --base requires -c flag" if options[:base] && !options[:c]

  existing = Git.worktree_path_for(branch)
  if existing
    puts "Worktree already exists: #{existing}"
    signal_cd(existing)
    return
  end

  path = Git.worktree_dir(branch)

  if options[:c] && !Git.branch_exists?(branch)
    base = options[:base] || Git.default_base_branch
    Git.worktree_add_new_branch(path, branch, base)
  elsif options[:c] || Git.branch_exists?(branch)
    Git.worktree_add(path, branch)
  else
    abort "Error: Branch '#{branch}' does not exist\nUse 'bonchi switch -c #{branch}' to create a new branch"
  end

  puts "Worktree created at: #{path}"
  signal_cd(path)

  if options[:setup] && Config.from_main_worktree
    puts ""
    Setup.new(worktree: path).run(upto: options[:upto])
  end
end

#versionObject



12
13
14
# File 'lib/bonchi/cli.rb', line 12

def version
  puts "bonchi #{VERSION}"
end