Class: GitCleanup

Inherits:
Object
  • Object
show all
Defined in:
lib/git-cleanup.rb,
lib/git-cleanup/branch.rb,
lib/git-cleanup/helper.rb

Overview

Grit.debug = true

Defined Under Namespace

Modules: Helper Classes: Branch

Class Method Summary collapse

Class Method Details

.prune(repo) ⇒ Object

Prunes branches that have already been removed on origin



67
68
69
70
71
72
73
74
# File 'lib/git-cleanup.rb', line 67

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

.runObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/git-cleanup.rb', line 7

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.reverse.each_with_index do |branch, index|
    next if branch.name == 'master'

    # Diff of commit in branch which is not in master
    diff = branch.diff(master)
    commits = branch.commits(master)

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

    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