Class: Worktree::Command::Remove

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

Instance Method Summary collapse

Constructor Details

#initialize(branch, project_dir:, update_refs: true) ⇒ Remove

Returns a new instance of Remove.



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

def initialize(branch, project_dir:, update_refs: true)
  @branch = branch
  @project_dir = project_dir || Project.resolve(branch).root
  @worktree = "#{@project_dir}/#{@branch}"
  @update_refs = update_refs
end

Instance Method Details

#do!Object



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

def do!
  return unless Dir.exist?(@worktree)
  return unless TTY::Prompt.new.yes?("Do you want to remove #{@worktree}?")

  # update refs
  git.remotes.each { |remote| git.fetch(remote, prune: true) } if @update_refs

  unless git.branch('master').contains?(@branch)
    unless TTY::Prompt.new.yes?("The branch #{@branch} was not merged to master. Would you like to remove it anyway?")
      Worktree.logger.warn { "You've skipped removing the worktree #{@worktree}" }
      return
    end
  end

  drop_db! if File.exist?("#{@worktree}/config/database.yml")

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

  # if remote branch exists then remove it also
  if Git.ls_remote(git.dir)['remotes'].keys.include?("origin/#{@branch}")
    if TTY::Prompt.new.yes?("Do you want to remove remote branch origin/#{@branch}?")
      git.push('origin', @branch, delete: true)
    end
  end

  # remove local branch
  git.branch(@branch).delete
end