Sha256: 3321932b7318c7aa00edb5415028846dfe9b32981587f931d45968bdf5aa48d7

Contents?: true

Size: 1.85 KB

Versions: 11

Compression:

Stored size: 1.85 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
        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

      def update_base_branch
        checkout_branch config.base_branch
        run_git_cmd 'pull'
        run_git_cmd 'remote', 'prune', 'origin'
      end

      # @return list of branches that have been merged
      # filter out reserved and aggregate branches
      def filtered_merged_branches(source)
        merged_branches(source).select do |branch|
          deletable_branch?(branch)
        end
      end

      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

      # @return list of branches that have been merged
      # see http://stackoverflow.com/questions/26804024/git-branch-merged-sha-via-rugged-libgit2-bindings
      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

      def base_branch_merge_target
        repo.head.target
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
gitx-3.1.0.ci.216.1 lib/gitx/cli/cleanup_command.rb
gitx-3.1.0 lib/gitx/cli/cleanup_command.rb
gitx-3.0.2.ci.213.1 lib/gitx/cli/cleanup_command.rb
gitx-3.0.2.ci.207.1 lib/gitx/cli/cleanup_command.rb
gitx-3.0.2 lib/gitx/cli/cleanup_command.rb
gitx-3.0.1.ci.200.1 lib/gitx/cli/cleanup_command.rb
gitx-3.0.1 lib/gitx/cli/cleanup_command.rb
gitx-3.0.0.ci.188.1 lib/gitx/cli/cleanup_command.rb
gitx-3.0.0 lib/gitx/cli/cleanup_command.rb
gitx-2.23.2.ci.182.1 lib/gitx/cli/cleanup_command.rb
gitx-2.23.2 lib/gitx/cli/cleanup_command.rb