Class: Worktree::Command::RemoveStale

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

Instance Method Summary collapse

Constructor Details

#initialize(project_dir:) ⇒ RemoveStale

Returns a new instance of RemoveStale.



6
7
8
# File 'lib/worktree/command/remove_stale.rb', line 6

def initialize(project_dir:)
  @project_dir = File.expand_path project_dir || Dir.pwd
end

Instance Method Details

#do!Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/worktree/command/remove_stale.rb', line 10

def do!
  # update refs
  git.remotes.each { |remote| git.fetch(remote, prune: true) }
  git.pull('upstream', 'master')

  branches = Dir.entries(@project_dir).
             select { |f| File.directory?("#{@project_dir}/#{f}}") }.
             reject { |f| ['master', '.', '..'].include?(f) }


  stale_branches = branches.select do |branch|
    git.branch('master').contains?(branch)
  end

  Worktree.logger.info { "You have #{stale_branches.size} stale branches!" }

  stale_branches.each_with_index do |stale_branch, index|
    Worktree.logger.info { "#{index + 1} of #{stale_branches.size}" }
    Remove.new(stale_branch,
               project_dir: @project_dir,
               drop_db: true,
               drop_remote_branch: true,
               check_merged: true).do!
  end
end