-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgooglecode_svn2_git.rb
executable file
·199 lines (172 loc) · 4.19 KB
/
googlecode_svn2_git.rb
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
#!/usr/bin/env ruby
########################################################################
# Convert a googlecode svn project to git and also can push to github
# - It's a wrapper around svn2git
# - uses ssh to push to github
# requires:
# svn2git and git-svn
# [email protected] 2013-12-29 15:07:04 -0500 - first cut
########################################################################
require 'logger'
require 'date'
require 'fileutils'
require 'pp'
me = __FILE__
while File.symlink?(me)
me = File.expand_path(File.readlink(me), File.dirname(me))
end
MYDIR = File.expand_path(File.dirname(me))
$: << MYDIR
require 'loadgems.rb'
require 'trollop'
class GoogleCode2Git
STDERRLOGGER = Logger.new(STDERR)
ME = File.basename($0)
VERSION = "1.01"
def initialize
@topts = nil
@svn_url = nil
end
def log(msg)
STDERRLOGGER.info "#{msg}"
end
def directory_exists?(dir)
return false if !dir
return File.exists?(dir) && File.directory?(dir)
return false
end
def make_dir(dir)
if !directory_exists?(dir)
pwd = Dir.pwd
begin
FileUtils.mkdir_p(dir)
rescue => e
log "Error: Could not create directory #{dir} #{e}"
exit 1
end
end
end
def convert
gc_project = @topts[:gproject]
if !gc_project
puts <<-EOF
Error: specify googlecode project namae with --gproject
EOF
exit 1
end
if !@topts[:dir]
puts <<-EOF
Error: specify the path of a non existent directory to convert to with --dir
EOF
exit
end
dir = @topts[:dir]
if directory_exists?(dir)
log "Error: directory #{dir} already exists. exiting.."
exit 1
end
pwd = Dir.pwd
make_dir(dir)
Dir.chdir(dir)
log "Working directory: #{Dir.pwd}"
@svn_url = ""
@svn_url << "http://" + gc_project + ".googlecode.com/svn"
log "SVN url: #{@svn_url}"
cmd = ""
cmd << "svn2git"
cmd << " "
cmd << "\"#{@svn_url}\""
if @topts[:authors]
cmd << " "
cmd << "--authors"
cmd << " "
cmd << @topts[:authors]
end
cmd << " "
cmd << "--verbose"
log "run: #{cmd}"
system(cmd)
Dir.chdir(pwd)
end
def push_to_github
username = @topts[:gituser]
gitproject = @topts[:gitproject]
if !username
puts <<-EOF
Error: sepcify the github user name with --gituser
EOF
exit 1
end
if !gitproject
puts <<-EOF
Error: sepcify the github project name with --gitproject
EOF
exit 1
end
cmd = ""
cmd << "git"
cmd << " "
cmd << "remote add origin [email protected]"
cmd << ":"
cmd << username
cmd << "/"
cmd << gitproject
cmd << ".git"
log "run: #{cmd}"
system(cmd)
cmd = "git push -u origin master"
log "run #{cmd}"
system(cmd)
end
def show_examples
puts <<-EOF
For help, type:
#{ME} --help
- To convert a googlecode project to git:
#{ME} --convert --gproject <project name> --dir <directory>
The directory must not exist, it will be created
Example:
#{ME} --convert --gproject mailsend /tmp/mailend
- To push the converted project to github, first create a rspository
for the project in github. Then do the following:
#{ME} --push --gituser <user> --gitproject <project name>
Example:
#{ME} -push \\
--gituser muquit --gitproject mailsend
EOF
end
def doit
parse_args
if @topts[:push]
push_to_github
exit 0
elsif @topts[:convert]
convert
exit 0
else
show_examples
exit 0
end
end
def parse_args
@topts = Trollop::options do
version "#{ME} v#{VERSION}"
banner <<-EOS
#{version}
A script to convert svn googlecode to git
Usage: #{ME} options
Where the options are:
EOS
opt :convert, "Convert googlecode project to git"
opt :dir, "The directory to convert the project to", :type => :string
opt :gproject, "Name of google project", :type => :string
opt :authors, "Path of authors.txt file", :type => :string
opt :gituser, "github username", :type => :string
opt :gitproject, "Name of the github project", :type => :string
opt :push, "push git project to github"
end
end
end
if __FILE__ == $0
GoogleCode2Git.new.doit
end