/
vcsdo
executable file
·374 lines (308 loc) · 9.93 KB
/
vcsdo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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
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
#!/usr/bin/env ruby
# @Author: Tom Link (micathom AT gmail com)
# @License: GPL (see http://www.gnu.org/licenses/gpl.txt)
# @Created: 2010-10-26.
# @Last Change: 2010-11-21.
# @Revision: 154
require 'optparse'
require 'rbconfig'
require 'logger'
require 'fileutils'
require 'yaml'
class VCSDo
APPNAME = 'vcsdo'
VERSION = '0.2'
VCSTYPES = ['git', 'svn', 'hg', 'bzr']
SYNONYMS = {
'import' => 'patch',
'apply' => 'patch',
'ca' => 'commitall',
'update' => 'pull',
}
CONFIGS = []
if ENV['WINDIR']
CONFIGS << File.join(File.dirname(ENV['WINDIR'].gsub(/\\/, '/')) ,'vcsdo.yml')
else
CONFIGS << '/etc/vcsdo.yml'
end
if ENV['HOME']
CONFIGS << File.join(ENV['HOME'].gsub(/\\/, '/'), '.vcsdo.yml')
if ENV['HOSTNAME']
CONFIGS << File.join(ENV['HOME'].gsub(/\\/, '/'), ".vcsdo_#{ENV['HOSTNAME']}.yml")
end
elsif ENV['USERPROFILE']
CONFIGS << File.join(ENV['USERPROFILE'].gsub(/\\/, '/'), 'vcsdo.yml')
end
CONFIGS.delete_if {|f| !File.exist?(f)}
class AppLog
def initialize(output=$stdout)
@output = output
$logger = Logger.new(output)
$logger.progname = defined?(APPNAME) ? APPNAME : File.basename(APPNAME, '.*')
$logger.datetime_format = "%H:%M:%S"
AppLog.set_level
end
def self.set_level
if $DEBUG
$logger.level = Logger::DEBUG
elsif $VERBOSE
$logger.level = Logger::INFO
else
$logger.level = Logger::WARN
end
end
end
class << self
def with_args(args)
AppLog.new
config = {:root => Dir.getwd}
CONFIGS.each do |file|
$logger.debug "Load configuration from #{file}"
config.merge!(YAML.load_file(file))
end
opts = OptionParser.new do |opts|
opts.banner = "Usage: #{File.basename(APPNAME)} [OPTIONS] [VCSTYPE] COMMAND [ARGS ...]"
opts.separator ' '
opts.separator 'vcsdo is a free software with ABSOLUTELY NO WARRANTY under'
opts.separator 'the terms of the GNU General Public License version 2 or newer.'
opts.separator ' '
opts.separator 'Where COMMAND can be one of:'
opts.separator ' clone ................ create a local copy'
opts.separator ' diff ................. show diff'
opts.separator ' ls ................... list files'
opts.separator ' patch|import|apply ... apply a patch'
opts.separator ' status ............... show status message'
opts.separator ' pull|update .......... incorporate changes'
opts.separator ' commitall|ca ......... commit all changes on disk'
opts.separator ' push (without ARGS: push origin master)'
opts.separator ' '
opts.separator 'Not every command is supported for every VCS type.'
opts.separator 'ATTENTION: The command arguments are not normalized,'
opts.separator 'i.e. they are passed to the VCS as provided.'
opts.separator ' '
opts.separator 'Supported VCS types: git, svn*, hg*'
opts.separator '* ... partial support'
opts.separator ' '
opts.separator 'Options:'
opts.on('-c', '--config YAML', String, 'Config file') do |value|
if File.exist?(value)
config.merge!(YAML.load_file(value))
else
$logger.fatal "Config file does not exist: #{value}"
exit 5
end
end
opts.on('-d', '--dir DIR', String, "Work in DIR") do |value|
config[:root] = expand_arg(config, value)
end
opts.on('-e', '--each [DIR]', String, "Run ARGS for each sub-directory in DIR (implies -d)") do |value|
config[:root] = expand_arg(config, value) unless value.nil?
config[:each] = true
end
unless CONFIGS.empty?
opts.separator ' '
opts.separator "Configuration: #{CONFIGS.join(', ')}"
end
opts.separator ' '
opts.separator 'Other Options:'
opts.on('-n', '--[no-]dry', 'Dry run') do |bool|
config[:dry] = true
end
opts.on('--debug', 'Show debug messages') do |v|
$DEBUG = true
$VERBOSE = true
AppLog.set_level
end
opts.on('-v', '--verbose', 'Run verbosely') do |v|
$VERBOSE = true
AppLog.set_level
end
opts.on_tail('-h', '--help', 'Show this message') do
puts opts
exit 1
end
end
$logger.debug "command-line arguments: #{args}"
argv = opts.parse!(args)
$logger.debug "config: #{config}"
$logger.debug "argv: #{argv}"
if argv.empty?
puts "No command given"
puts `#{APPNAME} --help`
exit 5
end
config[:root] = File.expand_path(config[:root])
if config[:each]
config[:dirs] = Dir[File.join(config[:root], '*')]
config[:dirs].delete_if {|d| !File.directory?(d)}
else
config[:dirs] = [config[:root]]
end
return VCSDo.new(config, argv)
end
def expand_arg(config, arg)
if arg =~ /^@/ and (aliases = config['aliases'])
arg1 = arg.sub(/^@/, '')
if aliases.has_key?(arg1)
aliases[arg1]
else
$logger.fatal "Unknown alias: #{arg}"
exit 5
end
else
arg
end
end
end
# config ... hash
# args ... array of strings
def initialize(config, args)
@config = config
@args = args
end
def process
argv = @args.dup
arg1 = argv.shift
if VCSTYPES.include?(arg1)
vcs0 = arg1
arg1 = argv.shift
else
vcs0 = nil
end
arg1 = SYNONYMS[arg1] || arg1
args = argv.join(' ')
@config[:dirs].each do |dir|
FileUtils.cd(dir) do
vcs = detect_vcs || vcs0
if vcs
$logger.warn "Process #{dir.inspect} (#{vcs})"
meth = "do_#{vcs}_#{arg1}"
if respond_to?(meth)
send(meth, args)
else
$logger.warn "Command #{arg1.inspect} not supported for #{vcs} repositories"
end
end
end
end
end
def detect_vcs
if File.directory?('.git')
'git'
elsif File.directory?('.svn')
'svn'
elsif File.directory?('.hg')
'hg'
elsif File.directory?('.bzr')
'bzr'
else
nil
end
end
def run(*cmds)
for cmd in cmds
if @config[:dry]
puts cmd
else
$logger.info cmd
puts `#{cmd}`
if $?.exitstatus != 0
$logger.error "Error #{$?.exitstatus} when running: #{cmd}"
return
end
end
end
end
###################################### git {{{1
def do_git_clone(args)
run "git clone #{args}"
end
def do_git_diff(args)
run "git diff #{args}"
end
def do_git_ls(args)
run "git ls-files #{args}"
end
def do_git_patch(args)
run "git apply #{args}"
end
def do_git_status(args)
run "git status #{args}"
end
def do_git_tag(args)
run "git tag #{args}"
end
def do_git_pull(args)
run "git pull #{args}"
end
def do_git_push(args)
if args.empty?
args = 'origin master'
end
run "git push #{args}"
end
def do_git_commitall(args)
run "git add #{args} *", "git commit"
end
###################################### svn {{{1
def do_svn_diff(args)
run "svn diff #{args}"
end
def do_svn_ls(args)
run "svn ls #{args}"
end
def do_svn_patch(args)
run "svn import #{args}"
end
def do_svn_status(args)
run "svn status #{args}"
end
def do_svn_pull(args)
run "svn update #{args}"
end
###################################### hg {{{1
def do_hg_diff(args)
run "hg diff #{args}"
end
def do_hg_ls(args)
run "hg locate #{args}"
end
def do_hg_patch(args)
run "hg import #{args}"
end
def do_hg_status(args)
run "hg status #{args}"
end
def do_hg_pull(args)
run "hg pull #{args}", "hg update"
end
###################################### bzr {{{1
def do_bzr_clone(args)
run "bzr checkout #{args}"
end
def do_bzr_diff(args)
run "bzr diff #{args}"
end
def do_bzr_ls(args)
run "bzr ls #{args}"
end
def do_bzr_status(args)
run "bzr status #{args}"
end
def do_bzr_tag(args)
if !args or args.empty?
run "bzr tags"
else
run "bzr tag #{args}"
end
end
def do_bzr_pull(args)
run "bzr pull #{args}"
end
end
if __FILE__ == $0
VCSDo.with_args(ARGV).process
end
# Local Variables:
# revisionRx: REVISION\s\+=\s\+\'
# End: