Sha256: a51e1985e20391748897b311572e193a902f08ebfb4e5c83972be5b986331b54

Contents?: true

Size: 1.31 KB

Versions: 37

Compression:

Stored size: 1.31 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 Gitx::BASE_BRANCH
        run_cmd 'git pull'
        run_cmd 'git remote prune origin'

        say 'Deleting local and remote branches that have been merged into '
        say Gitx::BASE_BRANCH, :green
        merged_branches(remote: true).each do |branch|
          run_cmd "git push origin --delete #{branch}"
        end
        merged_branches(remote: false).each do |branch|
          run_cmd "git branch -d #{branch}"
        end
      end

      private

      # @return list of branches that have been merged
      def merged_branches(options = {})
        args = []
        args << '-r' if options[:remote]
        args << '--merged'
        output = run_cmd("git branch #{args.join(' ')}").split("\n")
        branches = output.map do |branch|
          branch = branch.gsub(/\*/, '').strip.split(' ').first
          branch = branch.split('/').last 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

37 entries across 37 versions & 1 rubygems

Version Path
gitx-2.20.0 lib/gitx/cli/cleanup_command.rb
gitx-2.20.0.ci.117.1 lib/gitx/cli/cleanup_command.rb
gitx-2.19.0.ci.114.1 lib/gitx/cli/cleanup_command.rb
gitx-2.19.0.ci.112.1 lib/gitx/cli/cleanup_command.rb
gitx-2.19.0 lib/gitx/cli/cleanup_command.rb
gitx-2.18.0.ci.106.1 lib/gitx/cli/cleanup_command.rb
gitx-2.18.0 lib/gitx/cli/cleanup_command.rb
gitx-2.18.0.pre.ci.104.1 lib/gitx/cli/cleanup_command.rb
gitx-2.18.0.pre lib/gitx/cli/cleanup_command.rb
gitx-2.17.0.pre.ci.101.1 lib/gitx/cli/cleanup_command.rb
gitx-2.17.0.pre lib/gitx/cli/cleanup_command.rb
gitx-2.17.0.pre.ci.98.1 lib/gitx/cli/cleanup_command.rb
gitx-2.16.0.ci.91.1 lib/gitx/cli/cleanup_command.rb
gitx-2.16.0 lib/gitx/cli/cleanup_command.rb
gitx-2.16.0.ci.89.1 lib/gitx/cli/cleanup_command.rb
gitx-2.16.0.pre.ci.88.1 lib/gitx/cli/cleanup_command.rb
gitx-2.16.0.pre.ci.85.1 lib/gitx/cli/cleanup_command.rb
gitx-2.16.0.pre lib/gitx/cli/cleanup_command.rb
gitx-2.16.0.pre.ci.81.1 lib/gitx/cli/cleanup_command.rb
gitx-2.15.0 lib/gitx/cli/cleanup_command.rb