diff --git a/lib/git-up.rb b/lib/git-up.rb index d26d041..5d22b3d 100644 --- a/lib/git-up.rb +++ b/lib/git-up.rb @@ -1,12 +1,11 @@ require 'colored' require 'grit' - require 'git-up/version' class GitUp def run(argv) @fetch = true - + process_args(argv) if @fetch @@ -18,13 +17,13 @@ def run(argv) system(*command) raise GitError, "`git fetch` failed" unless $? == 0 end - + @remote_map = nil # flush cache after fetch Grit::Git.with_timeout(0) do with_stash do returning_to_current_branch do - rebase_all_branches + rebase_all_branches(argv) end end end @@ -65,7 +64,7 @@ def process_args(argv) man_path = File.expand_path('../../man/git-up.1', __FILE__) case argv - when [] + when [], ["-d"], ["-D"] return when ["-v"], ["--version"] $stdout.puts "git-up #{GitUp::VERSION}" @@ -99,13 +98,11 @@ def process_args(argv) end end - def rebase_all_branches - col_width = branches.map { |b| b.name.length }.max + 1 - + def rebase_all_branches(argv) branches.each do |branch| remote = remote_map[branch.name] - curbranch = branch.name.ljust(col_width) + curbranch = branch.name.ljust(max_width) if branch.name == repo.head.name print curbranch.bold else @@ -137,6 +134,8 @@ def rebase_all_branches checkout(branch.name) rebase(remote) end + + delete_branches(argv) if argv.any? end def repo @@ -351,5 +350,46 @@ def version_array(version_string) def git_version `git --version`[/\d+(\.\d+)+/] end + + def delete_branches(argv) + @repo || repo + branches = only_in_local_git_branches + + return puts "No branch to remove".on_black if branches.empty? + + branches.each do |branch| + command = "git branch #{argv.first} #{branch} --quiet 2> /dev/null" + print_deleted(branch) if system(command) + end + end + + def print_deleted(branch) + print "#{branch.ljust(max_width)}" + puts "deleted".on_black + end + + def remote_branches + repo.remotes.map(&:name) + .collect {|b| b.sub(/^origin\//, '') } + .reject {|b| b =~ /(HEAD|master)/ } + end + + def local_branches + repo.branches.map(&:name) + .reject {|b| b=~ /master/ } + end + + def only_in_local_git_branches + local_branches - remote_branches + end + + def max_width + @col_width ||= 1 + (branch_mapping).max + end + + def branch_mapping + branches.map { |b| b.name.length } + + only_in_local_git_branches.map {|b| b.length } + end end