git-up 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/git-up +1 -1
- data/lib/git-up.rb +37 -22
- data/lib/git-up/version.rb +1 -1
- metadata +3 -3
data/bin/git-up
CHANGED
data/lib/git-up.rb
CHANGED
@@ -2,21 +2,16 @@ require 'colored'
|
|
2
2
|
require 'grit'
|
3
3
|
|
4
4
|
class GitUp
|
5
|
-
def initialize(args)
|
6
|
-
end
|
7
|
-
|
8
5
|
def run
|
9
|
-
|
10
|
-
|
11
|
-
system "git fetch"
|
6
|
+
system "git", "fetch", "--all"
|
12
7
|
raise GitError, "`git fetch` failed" unless $? == 0
|
13
8
|
|
14
9
|
with_stash do
|
15
10
|
returning_to_current_branch do
|
16
|
-
col_width =
|
11
|
+
col_width = branches.map { |b| b.name.length }.max + 1
|
17
12
|
|
18
|
-
|
19
|
-
|
13
|
+
branches.each do |branch|
|
14
|
+
remote = remote_map[branch.name]
|
20
15
|
|
21
16
|
print branch.name.ljust(col_width)
|
22
17
|
|
@@ -50,6 +45,10 @@ class GitUp
|
|
50
45
|
exit 1
|
51
46
|
end
|
52
47
|
|
48
|
+
def repo
|
49
|
+
@repo ||= get_repo
|
50
|
+
end
|
51
|
+
|
53
52
|
def get_repo
|
54
53
|
git_dir = `git rev-parse --git-dir`
|
55
54
|
|
@@ -60,20 +59,36 @@ class GitUp
|
|
60
59
|
end
|
61
60
|
end
|
62
61
|
|
62
|
+
def branches
|
63
|
+
@branches ||= repo.branches.select { |b| remote_map.has_key?(b.name) }
|
64
|
+
end
|
65
|
+
|
66
|
+
def remote_map
|
67
|
+
@remote_map ||= repo.branches.inject({}) { |map, branch|
|
68
|
+
if remote = remote_for_branch(branch)
|
69
|
+
map[branch.name] = remote
|
70
|
+
end
|
71
|
+
|
72
|
+
map
|
73
|
+
}
|
74
|
+
end
|
75
|
+
|
63
76
|
def remote_for_branch(branch)
|
64
|
-
remote_name
|
65
|
-
|
77
|
+
remote_name = repo.config["branch.#{branch.name}.remote"] || "origin"
|
78
|
+
remote_branch = repo.config["branch.#{branch.name}.merge"] || branch.name
|
79
|
+
remote_branch.sub!(%r{^refs/heads/}, '')
|
80
|
+
repo.remotes.find { |r| r.name == "#{remote_name}/#{remote_branch}" }
|
66
81
|
end
|
67
82
|
|
68
83
|
def with_stash
|
69
84
|
stashed = false
|
70
85
|
|
71
|
-
status =
|
86
|
+
status = repo.status
|
72
87
|
change_count = status.added.length + status.changed.length + status.deleted.length
|
73
88
|
|
74
89
|
if change_count > 0
|
75
90
|
puts "stashing #{change_count} changes".magenta
|
76
|
-
|
91
|
+
repo.git.stash
|
77
92
|
stashed = true
|
78
93
|
end
|
79
94
|
|
@@ -81,17 +96,17 @@ class GitUp
|
|
81
96
|
|
82
97
|
if stashed
|
83
98
|
puts "unstashing".magenta
|
84
|
-
|
99
|
+
repo.git.stash({}, "pop")
|
85
100
|
end
|
86
101
|
end
|
87
102
|
|
88
103
|
def returning_to_current_branch
|
89
|
-
unless
|
104
|
+
unless repo.head.respond_to?(:name)
|
90
105
|
puts "You're not currently on a branch. I'm exiting in case you're in the middle of something.".red
|
91
106
|
return
|
92
107
|
end
|
93
108
|
|
94
|
-
branch_name =
|
109
|
+
branch_name = repo.head.name
|
95
110
|
|
96
111
|
yield
|
97
112
|
|
@@ -102,7 +117,7 @@ class GitUp
|
|
102
117
|
end
|
103
118
|
|
104
119
|
def checkout(branch_name)
|
105
|
-
output =
|
120
|
+
output = repo.git.checkout({}, branch_name)
|
106
121
|
|
107
122
|
unless on_branch?(branch_name)
|
108
123
|
raise GitError.new("Failed to checkout #{branch_name}", output)
|
@@ -110,9 +125,9 @@ class GitUp
|
|
110
125
|
end
|
111
126
|
|
112
127
|
def rebase(target_branch)
|
113
|
-
current_branch =
|
128
|
+
current_branch = repo.head
|
114
129
|
|
115
|
-
output, err =
|
130
|
+
output, err = repo.git.sh("#{Grit::Git.git_binary} rebase #{target_branch.name}")
|
116
131
|
|
117
132
|
unless on_branch?(current_branch.name) and is_fast_forward?(current_branch, target_branch)
|
118
133
|
raise GitError.new("Failed to rebase #{current_branch.name} onto #{target_branch.name}", output+err)
|
@@ -126,7 +141,7 @@ class GitUp
|
|
126
141
|
require 'bundler'
|
127
142
|
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('Gemfile')
|
128
143
|
Bundler.setup
|
129
|
-
rescue Bundler::GemNotFound
|
144
|
+
rescue Bundler::GemNotFound, Bundler::GitError
|
130
145
|
puts 'Gems are missing. You should `bundle install`.'.yellow
|
131
146
|
end
|
132
147
|
end
|
@@ -136,11 +151,11 @@ class GitUp
|
|
136
151
|
end
|
137
152
|
|
138
153
|
def merge_base(a, b)
|
139
|
-
|
154
|
+
repo.git.send("merge-base", {}, a, b).strip
|
140
155
|
end
|
141
156
|
|
142
157
|
def on_branch?(branch_name=nil)
|
143
|
-
|
158
|
+
repo.head.respond_to?(:name) and repo.head.name == branch_name
|
144
159
|
end
|
145
160
|
|
146
161
|
class GitError < StandardError
|
data/lib/git-up/version.rb
CHANGED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 4
|
8
|
-
-
|
9
|
-
version: 0.4.
|
8
|
+
- 2
|
9
|
+
version: 0.4.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Aanand Prasad
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2011-01-27 00:00:00 +00:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|