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 = 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
# 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).
    reject { |d| d == '.' || d == '..' || d == 'master' }.
    select { |f| File.directory?(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, update_refs: false).do!
  end
end