From 7898f2aae964aecdda865ba527a8cef608bffddb Mon Sep 17 00:00:00 2001 From: Jader Correa Date: Mon, 20 Jul 2015 21:23:30 -0300 Subject: [PATCH 1/2] Add -d and -D options to git up command --- lib/git-up.rb | 53 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/lib/git-up.rb b/lib/git-up.rb index d26d041..400e698 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,8 +98,8 @@ 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) + col_width = max_width branches.each do |branch| remote = remote_map[branch.name] @@ -137,6 +136,8 @@ def rebase_all_branches checkout(branch.name) rebase(remote) end + + delete_branches(argv) if argv.any? end def repo @@ -351,5 +352,43 @@ 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 + 1 + (branches.map { |b| b.name.length } + + only_in_local_git_branches.map {|b| b.length } + ).max + end end From 22296eddbfacad4ecbb8ddc8c4805a724b2a5dba Mon Sep 17 00:00:00 2001 From: Jader Correa Date: Wed, 9 Sep 2015 15:04:44 -0300 Subject: [PATCH 2/2] Fix aligment when deleting --- lib/git-up.rb | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/git-up.rb b/lib/git-up.rb index 400e698..5d22b3d 100644 --- a/lib/git-up.rb +++ b/lib/git-up.rb @@ -99,12 +99,10 @@ def process_args(argv) end def rebase_all_branches(argv) - col_width = max_width - 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 @@ -386,9 +384,12 @@ def only_in_local_git_branches end def max_width - 1 + (branches.map { |b| b.name.length } + - only_in_local_git_branches.map {|b| b.length } - ).max + @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