Sha256: f836c820917b3c9e20246f4eeb24c8b358c0bf814bc39a4e5987b0cc3995ee18

Contents?: true

Size: 1.45 KB

Versions: 4

Compression:

Stored size: 1.45 KB

Contents

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

module Thegarage
  module Gitx
    module Cli
      class CleanupCommand < BaseCommand
        desc 'cleanup', 'Cleanup branches that have been merged into master from the repo'
        def cleanup
          checkout_branch Thegarage::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 Thegarage::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
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
thegarage-gitx-2.14.0 lib/thegarage/gitx/cli/cleanup_command.rb
thegarage-gitx-2.13.1 lib/thegarage/gitx/cli/cleanup_command.rb
thegarage-gitx-2.13.0 lib/thegarage/gitx/cli/cleanup_command.rb
thegarage-gitx-2.12.0 lib/thegarage/gitx/cli/cleanup_command.rb