Sha256: 6706dd65cb74dd8371c97d1a3d7b6a8af457d44f9ed41b290c82cb7c18aa778f

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 KB

Contents

require 'thor'
require 'gitx'
require 'gitx/cli/base_command'

module Gitx
  module Cli
    class CleanupCommand < BaseCommand
      desc 'cleanup', 'Cleanup branches that have been merged into master from the repo'
      def cleanup
        checkout_branch config.base_branch
        run_git_cmd 'pull'
        run_git_cmd 'remote', 'prune', 'origin'

        say 'Deleting local and remote branches that have been merged into '
        say config.base_branch, :green
        merged_branches(remote: true).each do |branch|
          run_git_cmd 'push', 'origin', '--delete', branch
        end
        merged_branches(remote: false).each do |branch|
          run_git_cmd 'branch', '--delete', branch
        end
      end

      private

      # @return list of branches that have been merged
      def merged_branches(options = {})
        args = []
        args << '--remote' if options[:remote]
        args << '--merged'
        output = run_git_cmd('branch', *args).split("\n")
        branches = output.map do |branch|
          branch = branch.gsub(/\*/, '').strip.split(' ').first
          branch = branch.gsub('origin/', '') if options[:remote]
          branch
        end
        branches.uniq!
        branches -= config.reserved_branches
        branches.reject! { |b| config.aggregate_branch?(b) }

        branches
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gitx-2.23.0.ci.162.1 lib/gitx/cli/cleanup_command.rb
gitx-2.23.0 lib/gitx/cli/cleanup_command.rb