Skip to content
Browse files

Merge pull request #2 from vivienschilis/master

Badass update from rockstar Vivien
  • Loading branch information...
2 parents 386a750 + d19483a commit bf134b31f3b759d9704be06a26098c0df4653b9f @mloughran committed
Showing with 78 additions and 26 deletions.
  1. +29 −1 bin/git-cleanup
  2. +1 −0 git-cleanup.gemspec
  3. +36 −18 lib/git-cleanup.rb
  4. +6 −6 lib/git-cleanup/branch.rb
  5. +6 −1 lib/git-cleanup/helper.rb
View
30 bin/git-cleanup
@@ -1,3 +1,31 @@
require 'git-cleanup'
+require 'optparse'
-GitCleanup.run
+trap("INT") {
+ Formatador.redisplay("[bold][yellow]No more cleanup[/]\n")
+ exit(1)
+}
+
+conf = {
+ :skip_unmerged => false
+}
+
+opts = OptionParser.new do |opts|
+ opts.banner = "Usage: git-cleanup [OPTIONS] -- ... commands"
+ opts.on("-s", "--skip-unmerged", "Unmerge branches will be skipped") do |u|
+ conf[:skip_unmerged] = true
+ end
+ opts.on("-h", "--help", "Shows this help") do
+ puts opts
+ exit
+ end
+end
+
+begin
+ opts.parse!(ARGV)
+rescue OptionParser::ParseError => ex
+ $stderr.puts ex
+ exit 1
+end
+
+GitCleanup.run(conf)
View
1 git-cleanup.gemspec
@@ -12,6 +12,7 @@ Gem::Specification.new do |s|
s.description = %q{Command line tool for interactively cleaning up old git branches (remotely and locally)}
s.add_dependency 'grit', '~> 2.2.0'
+ s.add_dependency 'formatador', '~> 0.2.1'
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
View
54 lib/git-cleanup.rb
@@ -1,15 +1,22 @@
require 'grit'
require 'tempfile'
-
+require 'formatador'
# Grit.debug = true
+# No default indent
+class Formatador
+ def initialize
+ @indent = 0
+ end
+end
+
class GitCleanup
- def self.run
+ def self.run(options = {})
repo = Grit::Repo.new(Dir.pwd)
master = repo.heads.find { |h| h.name == 'master' }
- # self.prune(repo)
+ self.prune(repo)
local_branches = repo.branches.map { |b| b.name }
@@ -23,28 +30,39 @@ def self.run
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
+ Formatador.display_line
+
+ Formatador.display_line("[bold][green]#{msg}[/]")
+ Formatador.display_line "[bold][green]" + '-' * msg.size + "[/]"
if diff.empty?
+ Formatador.display_line "Branch merged. Last commit on branch:"
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
+ 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 'All commits merged. Do you want the branch deleted?' do
+ Helper.boolean '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
+ if options[:skip_unmerged]
+ Helper.info "Branch not merged. Skipped"
+ next
+ end
+
+ Formatador.display_line "Branch not merged. Commits on branch:"
+
+ Formatador.indent {
+ Formatador.display_lines commits.split("\n")
+ }
+
+ Helper.boolean "Do you want to see a diff?" do
Tempfile.open('diff') do |tempfile|
tempfile << diff
tempfile.flush
@@ -52,7 +70,7 @@ def self.run
if ENV["GIT_EDITOR"]
`#{ENV["GIT_EDITOR"]} #{tempfile.path}`
else
- puts diff
+ Formatador.display_line diff
end
end
Helper.boolean "Do you want the branch deleted?" do
@@ -66,7 +84,7 @@ def self.run
# Prunes branches that have already been removed on origin
def self.prune(repo)
list = repo.git.native(:remote, {}, 'prune', '-n', "origin")
- if list.any?
+ if !list.empty?
Helper.boolean "Planning to prune the following. Ok?\n#{list}" do
repo.git.native(:remote, {}, 'prune', "origin")
end
View
12 lib/git-cleanup/branch.rb
@@ -11,7 +11,7 @@ def self.remote(repo)
def initialize(repo, ref)
@repo = repo
@ref = ref
- @remote, @name = ref.name.split('/')
+ @remote, @name = ref.name.split('/', 2)
end
def to_s
@@ -31,15 +31,15 @@ def commit
end
def delete(local_branches = nil)
- puts "Deleting..."
+ Formatador.display('[red]Deleting...[/]')
@repo.git.native(:push, {}, @remote, ":#{@name}")
- puts "Done"
+ Formatador.display_line('[red]done[/]')
if local_branches && local_branches.include?(name)
Helper.boolean "There is also a local branch called #{name}. Would you like to delete that too?" do
- puts "Deleting..."
- @repo.git.native(:branch, {}, '-d', name)
- puts "Done"
+ Formatador.display('[red]Deleting...[/]')
+ @repo.git.native(:branch, {}, '-d', name)
+ Formatador.display_line('[red]done[/]')
end
end
end
View
7 lib/git-cleanup/helper.rb
@@ -1,7 +1,8 @@
class GitCleanup
module Helper
def self.boolean(question, &block)
- puts "#{question} (y/n)"
+ Formatador.display_line ""
+ Formatador.display("[bold][blue][QUESTION][/] #{question} (y/n)[/] ")
answer = STDIN.gets.chomp
if answer == 'y'
yield
@@ -11,5 +12,9 @@ def self.boolean(question, &block)
boolean(question, &block)
end
end
+
+ def self.info(info)
+ Formatador.display_line "[bold][yellow][INFO][/] #{info}[/]"
+ end
end
end

0 comments on commit bf134b3

Please sign in to comment.
Something went wrong with that request. Please try again.