public
Rubygem
Description: Apache Buildr
Homepage: http://incubator.apache.org/buildr
Clone URL: git://github.com/vic/buildr.git
Search Repo:
Don't print ugly stacktraces, just exit cleanly.
Refactored script into functions.

Thanks Alexis Midon for your comments on buildr-git.rb


git-svn-id: https://svn.apache.org/repos/asf/incubator/buildr/trunk@648731 
13f79535-47bb-0310-9956-ffa450edef68
vic (author)
Wed Apr 16 09:03:17 -0700 2008
commit  9d67bc6336e2990718f4b6ec2a7e2df499c0ddaf
tree    f9571fd36c1a4e2897a8d39dbd6aa9cc940bc880
parent  8d377a825b1763a3178b9d86ef9c8ff6dc9aad2f
...
9
10
11
12
13
14
15
...
37
38
39
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
...
170
171
172
 
173
174
175
176
177
178
179
180
181
182
 
 
 
 
 
 
 
 
 
183
184
185
186
187
188
189
190
191
192
 
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
 
 
 
226
227
228
229
230
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
253
254
255
256
257
 
 
 
 
 
 
 
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
 
286
287
288
289
290
291
292
293
 
294
295
296
 
 
 
 
 
 
 
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
 
 
315
316
317
318
319
 
 
 
 
 
 
 
 
 
 
 
 
320
321
322
 
 
 
 
323
324
325
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
326
327
328
 
 
329
330
 
 
 
331
332
333
334
 
...
9
10
11
 
12
13
14
...
36
37
38
 
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
...
186
187
188
189
190
 
 
 
 
 
 
 
 
 
191
192
193
194
195
196
197
198
199
200
 
 
 
 
 
 
 
 
 
201
202
203
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
204
 
205
206
207
208
 
 
 
 
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
 
247
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
 
 
 
265
266
267
268
269
270
271
272
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
273
274
 
 
 
 
275
 
 
276
277
 
 
278
279
280
281
282
283
284
285
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
286
287
288
 
 
 
 
289
290
291
292
293
294
295
296
297
298
299
300
301
 
 
302
303
304
305
306
 
 
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
 
 
377
378
379
 
380
381
382
383
 
 
 
384
0
@@ -9,7 +9,6 @@
0
 require 'optparse'
0
 require 'ostruct'
0
 
0
-
0
 # Pager from http://nex-3.com/posts/73-git-style-automatic-paging-in-ruby
0
 def run_pager
0
   return if PLATFORM =~ /win32/
0
@@ -37,7 +36,24 @@
0
   exec pager rescue exec "/bin/sh", "-c", pager
0
 end
0
 
0
-notice = <<NOTICE
0
+def header
0
+ <<HEADER
0
+
0
+Buildr official commit channel is Apache's svn repository, however some
0
+developers may prefer to use git while working on several features and
0
+merging other's changes.
0
+
0
+This script will configure a buildr-git copy on so you can commit to svn.
0
+
0
+Enter <-h> to see options, <-H> to see notes about configured aliases
0
+and recommended workflow, or any other option.
0
+
0
+Ctrl+D or an invalid option to abort
0
+HEADER
0
+end
0
+
0
+def notice
0
+ <<NOTICE
0
 ALIASES:
0
 
0
   Some git aliases have been created for developer convenience:
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
@@ -170,166 +186,200 @@
0
    http://groups.google.com/group/git-users/web/git-references
0
 
0
 NOTICE
0
+end # notice method
0
 
0
-options = OpenStruct.new
0
-opt = OptionParser.new do |opt|
0
-
0
- if `git status 2>/dev/null`.chomp.empty?
0
- options.local = File.expand_path('buildr', Dir.pwd)
0
- else
0
- puts "Current directory is a git repo: #{Dir.pwd}"
0
- options.local = Dir.pwd
0
- end
0
+def optparse(options = OpenStruct.new, argv = ARGV)
0
+ opt = OptionParser.new do |opt|
0
+
0
+ if `git status 2>/dev/null`.chomp.empty?
0
+ options.local = File.expand_path('buildr', Dir.pwd)
0
+ else
0
+ puts "Current directory is a git repo: #{Dir.pwd}"
0
+ options.local = Dir.pwd
0
+ end
0
 
0
- options.svn_branch = "apache/trunk"
0
- options.origin = "git://github.com/vic/buildr.git"
0
- options.member = false
0
-
0
- opt.banner = "Usage: buildr-git.rb [options]"
0
- opt.separator ""
0
- opt.separator "OPTIONS:"
0
-
0
- opt.on('-a', "--anonymous", "Use git://github.com/vic/buildr.git as origin") do
0
+ options.svn_branch = "apache/trunk"
0
     options.origin = "git://github.com/vic/buildr.git"
0
     options.member = false
0
- end
0
- opt.on('-A', "--authenticated", "Use git@github.com:vic/buildr.git as origin") do
0
- options.origin = "git@github.com/vic/buildr.git"
0
- options.member = true
0
- end
0
- opt.on("-o", "--origin GIT_URL", "Clone from GIT_URL origin") do |value|
0
- options.origin = value
0
- end
0
- opt.on('-l', "--local DIRECTORY", "Create local git clone on DIRECTORY") do |value|
0
- options.local = value
0
- end
0
- opt.on('-b', "--branch GIT_SVN_BRANCH",
0
- "Set the name for svn branch instead of apache/trunk") do |value|
0
- options.svn_branch = value
0
- end
0
- opt.on('-e', "--email EMAIL",
0
- "Configure git to use EMAIL for commits") do |value|
0
- options.email = value
0
- end
0
- opt.on('-h', "--help", "Show buildr-git help") do
0
- puts opt
0
- exit(0)
0
- end
0
- opt.on('-n', "--notice", "Show notice about aliases and git workflow") do
0
- run_pager
0
- puts notice
0
- exit(0)
0
- end
0
-end
0
 
0
-opt.parse!(ARGV)
0
+ opt.banner = "Usage: buildr-git.rb [options]"
0
+ opt.separator ""
0
+ opt.separator "OPTIONS:"
0
 
0
-origin = options.origin
0
-local = options.local
0
-svn_branch = options.svn_branch
0
-email = options.email
0
+ opt.on('-a', "--anonymous", "Use git://github.com/vic/buildr.git as origin") do
0
+ options.origin = "git://github.com/vic/buildr.git"
0
+ end
0
+ opt.on('-A', "--authenticated", "Use git@github.com:vic/buildr.git as origin") do
0
+ options.origin = "git@github.com/vic/buildr.git"
0
+ end
0
+ opt.on("-o", "--origin GIT_URL", "Clone from GIT_URL origin") do |value|
0
+ options.origin = value
0
+ end
0
+ opt.on('-l', "--local DIRECTORY", "Create local git clone on DIRECTORY") do |value|
0
+ options.local = value
0
+ end
0
+ opt.on('-b', "--branch GIT_SVN_BRANCH",
0
+ "Set the name for svn branch instead of apache/trunk") do |value|
0
+ options.svn_branch = value
0
+ end
0
+ opt.on('-e', "--email EMAIL",
0
+ "Configure git to use EMAIL for commits") do |value|
0
+ options.email = value
0
+ end
0
+ opt.on('-n', "--name USER_NAME",
0
+ "Configure your USER_NAME for git commits") do |value|
0
+ options.user_name = value
0
+ end
0
+ opt.on('-h', "--help", "Show buildr-git help") do
0
+ puts opt
0
+ throw :exit
0
+ end
0
+ opt.on('-H', "--notice", "Show notice about aliases and git workflow") do
0
+ run_pager
0
+ puts notice
0
+ throw :exit
0
+ end
0
+ end
0
+ opt.parse!(argv)
0
+ options
0
+end # optparse method
0
 
0
-puts <<HEADER
0
 
0
-Buildr official commit channel is Apache's svn repository, however some
0
-developers may prefer to use git while working on several features and
0
-merging other's changes.
0
-
0
-This script will configure a buildr-git copy on so you can commit to svn.
0
-Local git copy: #{local}
0
-
0
-Enter <-h> to see options, <-n> to see notes about configured aliases
0
-and recommended workflow, or any other option.
0
-
0
-Ctrl+D or an invalid option to abort
0
-HEADER
0
-print '> '
0
-input = gets
0
-input = input.split if input
0
-opt.parse! input
0
-unless input && input.empty?
0
- puts "Aborting."
0
- exit(0)
0
+def main
0
+ catch :exit do
0
+ options = optparse
0
+ puts header
0
+ puts
0
+ print '> '
0
+ if input = gets
0
+ options = optparse(options, input.split)
0
+ end
0
+ if input.nil? || (input != "\n" && input.empty?)
0
+ puts "Aborting."
0
+ return
0
+ end
0
+ perform(options)
0
+ end
0
 end
0
 
0
-`git clone #{origin} #{local} 1>&2` unless File.directory?(File.join('.git', origin))
0
-
0
-Dir.chdir(local) do
0
+def perform(options)
0
+ origin = options.origin
0
+ member = origin =~ /@/
0
+ local = options.local
0
+ svn_branch = options.svn_branch
0
+ user_email = options.email
0
+ user_name = options.user_name
0
   
0
- # Load the list of git-svn committers
0
- svn_authors_file = File.expand_path('etc/git-svn-authors', Dir.pwd)
0
- svn_authors = File.read(svn_authors_file)
0
- svn_authors.gsub!(/\s+=\s+/, ': ')
0
- svn_authors = YAML.load(svn_authors)
0
-
0
- # set the git-svn-authors file
0
-
0
- `git config svn.authorsfile "#{svn_authors_file}"`
0
-
0
- # Check that git is configured for the git developer
0
- email ||= `git config --get user.email`.chomp
0
- if email.empty?
0
- puts "Enter your email as listed on #{svn_authors_file}"
0
- print "> "
0
- email = gets.chomp
0
- end
0
-
0
- # Check user lis listed
0
- svn_user, git_contact = *svn_authors.find { |entry| /#{email}/i === entry.join(' ') }
0
- fail "You need to be a buildr commmitter listed on #{svn_authors_file}"+
0
- "\nPerhaps you need to set user.email, user.name on .git/config" unless git_contact
0
- if /\s*(.*?)\s+\<(.+)\>\s*/ === git_contact
0
- user_name, user_email = $1, $2
0
- else
0
- fail "Invalid contact string for #{svn_user}: #{git_contact.inspect}"
0
- end
0
+ `git clone #{origin} #{local} 1>&2` unless File.directory?(File.join('.git', origin))
0
   
0
- # Start the pager
0
- run_pager
0
-
0
- # Configure user name and email for git sake (and github's gravatar)
0
   puts
0
- puts "You claim to be #{user_name} <#{user_email}> with apache-svn user"
0
- puts " #{svn_user}"
0
+ puts "Entering #{local} directory"
0
   puts
0
- `git config user.name "#{user_name}"`
0
- `git config user.email "#{user_email}"`
0
+ Dir.chdir(local) do
0
+
0
+ # Load the list of git-svn committers
0
+ svn_authors_file = File.expand_path('etc/git-svn-authors', Dir.pwd)
0
+ svn_authors = File.read(svn_authors_file)
0
+ svn_authors.gsub!(/\s+=\s+/, ': ')
0
+ svn_authors = YAML.load(svn_authors)
0
 
0
- # Ok, now obtain the last svn commit from history
0
- last_svn_log = `git log -n 10 | grep git-svn-id | head -n 1`
0
- fail "No svn metadata on last 10 commits" unless last_svn_log =~ /git-svn-id/
0
- svn_repo, svn_prev = last_svn_log.split[1].split("@")
0
-
0
- # Tell git where the svn repository is.
0
- `git config svn-remote.#{svn_branch}.url #{svn_repo}`
0
- `git config svn-remote.#{svn_branch}.fetch :refs/remotes/#{svn_branch}`
0
-
0
- # Create the svn branch, do this instead of pulling the full svn history
0
- `git push --force . refs/remotes/origin/master:refs/remotes/#{svn_branch}`
0
-
0
- # Create apache aliases for developers git-workflow.
0
- `git config alias.apache-fetch "!git-svn fetch #{svn_branch}"`
0
- `git config alias.apache-merge "!git merge #{svn_branch}"`
0
- `git config alias.apache-pull "!git apache-fetch && git apache-merge"`
0
- `git config alias.apache-push "!git-svn dcommit --username #{svn_user}"`
0
+ # set the git-svn-authors file
0
+ `git config svn.authorsfile "#{svn_authors_file}"`
0
 
0
- # Create github origin aliases
0
- `git config alias.get "!git apache-fetch && git fetch origin"`
0
- `git config alias.mrg "!git apache-merge && git merge origin"`
0
- `git config alias.put "!git apache-push && git push origin"`
0
+ # Check that git is configured for the developer
0
+ user_email ||= `git config --get user.email`.chomp
0
+ if user_email.empty?
0
+ if member
0
+ puts "Enter your email as listed on #{svn_authors_file}"
0
+ print "> "
0
+ user_email = gets.chomp
0
+ else
0
+ puts "Provide an email address for git usage:"
0
+ user_email = gets.chomp
0
+ end
0
+ end
0
 
0
- # This is Victor's cronjob
0
- `git config alias.synchronize "!git get && git mrg && git put"`
0
+ # Check user is listed
0
+ unless user_email.empty?
0
+ svn_user, git_contact = *svn_authors.find { |entry| /#{user_email}/ === entry.join(' ') }
0
+ end
0
 
0
- # Final notices.
0
- notice = <<-NOTICE + notice
0
+ if member
0
+ if git_contact.nil? # if using the authenticated git, he must be listed
0
+ puts <<-NOTICE
0
+ You need to be a buildr commmitter listed on #{svn_authors_file}.
0
+ Perhaps you need to add yourself to it, commit it using SVN, and
0
+ and run this script again.
0
+ Also checks your git global values for user.email and user.name
0
+ NOTICE
0
+ return
0
+ elsif /\s*(.*?)\s+\<(.+)\>\s*/ === git_contact
0
+ user_name, user_email = $1, $2
0
+ else
0
+ puts "Invalid contact string for #{svn_user}: #{git_contact.inspect}"
0
+ return
0
+ end
0
+ elsif user_email =~ /^\s*$/
0
+ puts "User email shall not include spaces: #{user_email.inspect}"
0
+ return
0
+ end
0
+
0
+ user_name ||= `git config --get user.name`.chomp
0
+ if git_contact.nil? && user_name.empty?
0
+ puts "Provide a developer name for git usage:"
0
+ user_name = gets.chomp
0
+ end
0
+
0
+ # Start the pager
0
+ run_pager
0
+
0
+ # Configure user name and email for git sake (and github's gravatar)
0
+ puts
0
+ puts "You claim to be #{user_name.inspect} <#{user_email}> "
0
+ puts "with apache-svn user: #{svn_user}" if svn_user
0
+ puts
0
+ `git config user.name "#{user_name}"`
0
+ `git config user.email "#{user_email}"`
0
+
0
+ # Ok, now obtain the last svn commit from history
0
+ last_svn_log = `git log -n 10 | grep git-svn-id | head -n 1`
0
+ fail "No svn metadata on last 10 commits" unless last_svn_log =~ /git-svn-id/
0
+ svn_repo, svn_prev = last_svn_log.split[1].split("@")
0
+
0
+ # Tell git where the svn repository is.
0
+ `git config svn-remote.#{svn_branch}.url #{svn_repo}`
0
+ `git config svn-remote.#{svn_branch}.fetch :refs/remotes/#{svn_branch}`
0
+
0
+ # Create the svn branch, do this instead of pulling the full svn history
0
+ `git push --force . refs/remotes/origin/master:refs/remotes/#{svn_branch}`
0
+
0
+ # Create apache aliases for developers git-workflow.
0
+ `git config alias.apache-fetch "!git-svn fetch #{svn_branch}"`
0
+ `git config alias.apache-merge "!git merge #{svn_branch}"`
0
+ `git config alias.apache-pull "!git apache-fetch && git apache-merge"`
0
+ if svn_user
0
+ `git config alias.apache-push "!git-svn dcommit --username #{svn_user}"`
0
+ else
0
+ `git config alias.apache-push "!git-svn dcommit"`
0
+ end
0
+
0
+ # Create github origin aliases
0
+ `git config alias.get "!git apache-fetch && git fetch origin"`
0
+ `git config alias.mrg "!git apache-merge && git merge origin"`
0
+ `git config alias.put "!git apache-push && git push origin"`
0
+
0
+ # This is Victor's cronjob
0
+ `git config alias.synchronize "!git get && git mrg && git put"`
0
+
0
+ # Final notices.
0
+ puts <<-NOTICE + notice
0
 
0
- Your git repo #{local} has been configured, please review the
0
- #{File.join(local, '.git/config')} file.
0
+ Your git repo #{local} has been configured, please review the
0
+ #{File.join(local, '.git/config')} file.
0
 
0
- NOTICE
0
+ NOTICE
0
+ end
0
+end # perform method
0
 
0
- puts notice
0
-
0
-end
0
+main

Comments

    No one has commented yet.