Sha256: 36851718aae75f0e5d9920873f5f971df7c4dda703beb08c1216a17c97b72cbe

Contents?: true

Size: 1.92 KB

Versions: 1

Compression:

Stored size: 1.92 KB

Contents

require 'grit'
require 'tempfile'

# Grit.debug = true

class GitCleanup
  def self.run
    repo = Grit::Repo.new(Dir.pwd)
    
    master = repo.heads.find { |h| h.name == 'master' }

    self.prune(repo)

    local_branches = repo.branches.map { |b| b.name }

    remote_branches = Branch.remote(repo).select { |b| b.commit.lazy_source }

    remote_branches.sort.each_with_index do |branch, index|
      # Diff of commit in branch which is not in master
      diff = branch.diff(master)

      msg = "Branch #{branch.to_s} (#{index+1}/#{remote_branches.size})"
      puts
      puts msg
      puts '-' * msg.size

      if diff.empty?
        last_commit = branch.commit
        if last_commit
          puts "Last commit:"
          puts "Author: #{last_commit.author}"
          puts "Date:   #{last_commit.committed_date}"
          puts "SHA:    #{last_commit.sha}"
          puts "#{last_commit.message}"
          puts
        end

        Helper.boolean 'All commits merged. Do you want the branch deleted?' do
          branch.delete(local_branches)
        end
      else
        Helper.boolean "Branch not merged. Do you want to see a diff?" do
          Tempfile.open('diff') do |tempfile|
            tempfile << diff
            tempfile.flush
            
            if ENV["GIT_EDITOR"]
              `#{ENV["GIT_EDITOR"]} #{tempfile.path}`
            else
              puts diff
            end
          end
          Helper.boolean "Do you want the branch deleted?" do
            branch.delete(local_branches)
          end
        end
      end
    end
  end

  # Prunes branches that have already been removed on origin
  def self.prune(repo)
    list = repo.git.native(:remote, {}, 'prune', '-n', "origin")
    if list.any?
      Helper.boolean "Planning to prune the following. Ok?\n#{list}" do
        repo.git.native(:remote, {}, 'prune', "origin")
      end
    end
  end
end

require 'git-cleanup/branch'
require 'git-cleanup/helper'

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
git-cleanup-0.1.1 lib/git-cleanup.rb