Class: Worktree::Command::Remove

Inherits:
Object
  • Object
show all
Defined in:
lib/worktree/command/remove.rb

Constant Summary collapse

NotMergedError =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(branch, project_dir: nil, drop_db: false, drop_remote_branch: false, check_merged: false) ⇒ Remove

Returns a new instance of Remove.



8
9
10
11
12
13
# File 'lib/worktree/command/remove.rb', line 8

def initialize(branch, project_dir: nil, drop_db: false, drop_remote_branch: false, check_merged: false)
  @branch = branch
  @project_dir = File.expand_path project_dir || Project.resolve(branch).root
  @drop_db = drop_db
  @check_merged = check_merged
end

Instance Method Details

#do!Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/worktree/command/remove.rb', line 15

def do!
  if @check_merged && !git.branch('master').contains?(@branch)
    raise NotMergedError.new("#{@branch} is not merged into master")
  end

  drop_db! if @drop_db

  # remove stale worktree
  Worktree.run_command "git worktree remove #{@project_dir}/#{@branch} --force", chdir: git.dir

  # if remote branch exists then remove it also
  if @drop_remote_branch && Git.ls_remote(git.dir)['remotes'].keys.include?("origin/#{@branch}")
    git.push('origin', @branch, delete: true)
    Worktree.logger.info { "Remote branch #{@branch} was deleted successfully." }
  end

  # remove local branch
  git.branch(@branch).delete
rescue NotMergedError => e
  Worktree.logger.error { e }
end