Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -d and -D options to git up command #98

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 49 additions & 9 deletions lib/git-up.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
require 'colored'
require 'grit'

require 'git-up/version'

class GitUp
def run(argv)
@fetch = true

process_args(argv)

if @fetch
Expand All @@ -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
Expand Down Expand Up @@ -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}"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -137,6 +134,8 @@ def rebase_all_branches
checkout(branch.name)
rebase(remote)
end

delete_branches(argv) if argv.any?
end

def repo
Expand Down Expand Up @@ -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