class Gitx::Cli::CleanupCommand

Public Instance Methods

cleanup() click to toggle source
# File lib/gitx/cli/cleanup_command.rb, line 9
def cleanup
  update_base_branch
  say 'Deleting local and remote branches that have been merged into '
  say config.base_branch, :green
  filtered_merged_branches(:remote).each do |branch|
    run_git_cmd 'push', 'origin', '--delete', branch
  end
  filtered_merged_branches(:local).each do |branch|
    run_git_cmd 'branch', '--delete', branch
  end
end

Private Instance Methods

base_branch_merge_target() click to toggle source
# File lib/gitx/cli/cleanup_command.rb, line 57
def base_branch_merge_target
  repo.head.target
end
deletable_branch?(branch) click to toggle source
# File lib/gitx/cli/cleanup_command.rb, line 37
def deletable_branch?(branch)
  return false if config.reserved_branches.include?(branch)
  return false if config.aggregate_branch?(branch)
  return false if config.base_branch == branch

  true
end
filtered_merged_branches(source) click to toggle source

@return list of branches that have been merged filter out reserved and aggregate branches

# File lib/gitx/cli/cleanup_command.rb, line 31
def filtered_merged_branches(source)
  merged_branches(source).select do |branch|
    deletable_branch?(branch)
  end
end
merged_branches(source) click to toggle source

@return list of branches that have been merged see stackoverflow.com/questions/26804024/git-branch-merged-sha-via-rugged-libgit2-bindings

# File lib/gitx/cli/cleanup_command.rb, line 47
def merged_branches(source)
  merged_branches = repo.branches.each(source).select do |branch|
    target = branch.resolve.target
    repo.merge_base(base_branch_merge_target, target) == target.oid
  end
  merged_branches.map do |branch|
    branch.name.gsub('origin/', '')
  end
end
update_base_branch() click to toggle source
# File lib/gitx/cli/cleanup_command.rb, line 23
def update_base_branch
  checkout_branch config.base_branch
  run_git_cmd 'pull'
  run_git_cmd 'remote', 'prune', 'origin'
end