Class: GitCleanup

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

Defined Under Namespace

Modules: Helper Classes: Branch

Constant Summary collapse

VERSION =
"0.2.3"

Class Method Summary collapse

Class Method Details

.prune(repo) ⇒ Object

Prunes branches that have already been removed on origin



106
107
108
109
110
111
112
113
# File 'lib/git-cleanup.rb', line 106

def self.prune(repo)
  list = repo.git.native(:remote, {}, 'prune', '-n', "origin")
  if !list.empty?
    Helper.boolean "Planning to prune the following. Ok?\n#{list}" do
      repo.git.native(:remote, {}, 'prune', "origin")
    end
  end
end

.run(options = {}) ⇒ Object



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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/git-cleanup.rb', line 14

def self.run(options = {})
  begin
    repo = Grit::Repo.new(Dir.pwd)
  rescue Grit::InvalidGitRepositoryError
    Formatador.display_line(%Q{[bold][red] Could not find git repository in "#{Dir.pwd}".[/]})
    exit
  end

  master_branch_name = options[:master_branch] || 'master'
  
  master = repo.heads.find { |h| h.name == master_branch_name }

  unless master
    Formatador.display_line(%Q{[bold][red] Could not find branch "#{master_branch_name}". Try using "-m BRANCH" to specify the master branch.[/]})
    exit
  end

  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_branch_name
    next if options[:only_filter] and !branch.name.match(options[:only_filter])

    msg = "Branch #{branch.to_s} (#{index+1}/#{remote_branches.size})"
    Formatador.display_line

    Formatador.display_line("[bold][green]#{msg}[/]")
    Formatador.display_line "[bold][green]" + '-' * msg.size + "[/]"

    # Diff of commit in branch which is not in master
    begin
      diff = branch.diff(master)
    rescue Grit::Git::GitTimeout => e
      Formatador.display_line("[bold][red] There was an error generating the diff.[/]")
      Formatador.display_line("[red]Message: #{e.message}")
      Formatador.display_line("Skipped")
      next
    end

    if diff.empty?
      Formatador.display_line "[bold]Branch merged.[/] Last commit on branch:"
      last_commit = branch.commit
      if last_commit
        Formatador.indent {
          Formatador.display_line "Author: #{last_commit.author}"
          Formatador.display_line "Date:   #{last_commit.committed_date}"
          Formatador.display_line "SHA:    #{last_commit.sha}"
          Formatador.display_line "#{last_commit.message}"
        }
      end

      Helper.boolean '[bold]Branch merged.[/] Do you want the branch deleted?' do
        branch.delete(local_branches)
      end
    else
      if options[:skip_unmerged]
        Helper.info "[bold]Branch not merged.[/] Skipped"
        next
      end

      commits = branch.commits(master)

      Formatador.display_line "[bold]Branch not merged.[/] Commits on branch:"

      Formatador.indent {
        Formatador.display_lines commits.split("\n")
      }

      Helper.boolean "[bold]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
            Formatador.display_line diff
          end
        end
        Helper.boolean "Do you want the branch deleted?" do
          branch.delete(local_branches)
        end
      end
    end
  end
end