]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (C) 2007 Thiago Macieira <thiago@kde.org> | |
3 | * | |
4 | * This program is free software: you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation, either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | */ | |
17 | ||
18 | /* | |
19 | * Based on svn-fast-export by Chris Lee <clee@kde.org> | |
20 | * License: MIT <http://www.opensource.org/licenses/mit-license.php> | |
21 | * URL: git://repo.or.cz/fast-import.git http://repo.or.cz/w/fast-export.git | |
22 | */ | |
23 | ||
24 | #define _XOPEN_SOURCE | |
25 | #define _LARGEFILE_SUPPORT | |
26 | #define _LARGEFILE64_SUPPORT | |
27 | ||
28 | #include "svn.h" | |
29 | ||
30 | #include <unistd.h> | |
31 | #include <string.h> | |
32 | #include <stdio.h> | |
33 | #include <time.h> | |
34 | #include <unistd.h> | |
35 | ||
36 | #include <apr_lib.h> | |
37 | #include <apr_getopt.h> | |
38 | #include <apr_general.h> | |
39 | ||
40 | #include <svn_fs.h> | |
41 | #include <svn_pools.h> | |
42 | #include <svn_repos.h> | |
43 | #include <svn_types.h> | |
44 | ||
45 | #include <QFile> | |
46 | #include <QDebug> | |
47 | ||
48 | #include "repository.h" | |
49 | ||
50 | #undef SVN_ERR | |
51 | #define SVN_ERR(expr) SVN_INT_ERR(expr) | |
52 | ||
53 | typedef QList<Rules::Match> MatchRuleList; | |
54 | typedef QHash<QString, Repository *> RepositoryHash; | |
55 | typedef QHash<QByteArray, QByteArray> IdentityHash; | |
56 | ||
57 | class AprAutoPool | |
58 | { | |
59 | apr_pool_t *pool; | |
60 | public: | |
61 | inline AprAutoPool(apr_pool_t *parent = NULL) | |
62 | { pool = svn_pool_create(parent); } | |
63 | inline ~AprAutoPool() | |
64 | { svn_pool_destroy(pool); } | |
65 | ||
66 | inline apr_pool_t *data() const { return pool; } | |
67 | inline operator apr_pool_t *() const { return pool; } | |
68 | }; | |
69 | ||
70 | class SvnPrivate | |
71 | { | |
72 | public: | |
73 | MatchRuleList matchRules; | |
74 | RepositoryHash repositories; | |
75 | IdentityHash identities; | |
76 | ||
77 | SvnPrivate(const QString &pathToRepository); | |
78 | ~SvnPrivate(); | |
79 | int youngestRevision(); | |
80 | int exportRevision(int revnum); | |
81 | ||
82 | int openRepository(const QString &pathToRepository); | |
83 | ||
84 | private: | |
85 | AprAutoPool global_pool; | |
86 | svn_fs_t *fs; | |
87 | svn_revnum_t youngest_rev; | |
88 | }; | |
89 | ||
90 | void Svn::initialize() | |
91 | { | |
92 | // initialize APR or exit | |
93 | if (apr_initialize() != APR_SUCCESS) { | |
94 | fprintf(stderr, "You lose at apr_initialize().\n"); | |
95 | exit(1); | |
96 | } | |
97 | ||
98 | // static destructor | |
99 | static struct Destructor { ~Destructor() { apr_terminate(); } } destructor; | |
100 | } | |
101 | ||
102 | Svn::Svn(const QString &pathToRepository) | |
103 | : d(new SvnPrivate(pathToRepository)) | |
104 | { | |
105 | } | |
106 | ||
107 | Svn::~Svn() | |
108 | { | |
109 | delete d; | |
110 | } | |
111 | ||
112 | void Svn::setMatchRules(const MatchRuleList &matchRules) | |
113 | { | |
114 | d->matchRules = matchRules; | |
115 | } | |
116 | ||
117 | void Svn::setRepositories(const RepositoryHash &repositories) | |
118 | { | |
119 | d->repositories = repositories; | |
120 | } | |
121 | ||
122 | int Svn::youngestRevision() | |
123 | { | |
124 | return d->youngestRevision(); | |
125 | } | |
126 | ||
127 | bool Svn::exportRevision(int revnum) | |
128 | { | |
129 | return d->exportRevision(revnum) == EXIT_SUCCESS; | |
130 | } | |
131 | ||
132 | SvnPrivate::SvnPrivate(const QString &pathToRepository) | |
133 | : global_pool(NULL) | |
134 | { | |
135 | openRepository(pathToRepository); | |
136 | ||
137 | // get the youngest revision | |
138 | svn_fs_youngest_rev(&youngest_rev, fs, global_pool); | |
139 | } | |
140 | ||
141 | SvnPrivate::~SvnPrivate() | |
142 | { | |
143 | svn_pool_destroy(global_pool); | |
144 | } | |
145 | ||
146 | int SvnPrivate::youngestRevision() | |
147 | { | |
148 | return youngest_rev; | |
149 | } | |
150 | ||
151 | int SvnPrivate::openRepository(const QString &pathToRepository) | |
152 | { | |
153 | svn_repos_t *repos; | |
154 | SVN_ERR(svn_repos_open(&repos, QFile::encodeName(pathToRepository), global_pool)); | |
155 | fs = svn_repos_fs(repos); | |
156 | ||
157 | return EXIT_SUCCESS; | |
158 | } | |
159 | ||
160 | static int pathMode(svn_fs_root_t *fs_root, const char *pathname, apr_pool_t *pool) | |
161 | { | |
162 | svn_string_t *propvalue; | |
163 | SVN_ERR(svn_fs_node_prop(&propvalue, fs_root, pathname, "svn:executable", pool)); | |
164 | int mode = 0100644; | |
165 | if (propvalue) | |
166 | mode = 0100755; | |
167 | ||
168 | // maybe it's a symlink? | |
169 | SVN_ERR(svn_fs_node_prop(&propvalue, fs_root, pathname, "svn:special", pool)); | |
170 | if (strcmp(propvalue->data, "symlink") == 0) | |
171 | mode = 0120000; | |
172 | ||
173 | return mode; | |
174 | } | |
175 | ||
176 | svn_error_t *QIODevice_write(void *baton, const char *data, apr_size_t *len) | |
177 | { | |
178 | QIODevice *device = reinterpret_cast<QIODevice *>(baton); | |
179 | device->write(data, *len); | |
180 | return SVN_NO_ERROR; | |
181 | } | |
182 | ||
183 | static svn_stream_t *streamForDevice(QIODevice *device, apr_pool_t *pool) | |
184 | { | |
185 | svn_stream_t *stream = svn_stream_create(device, pool); | |
186 | svn_stream_set_write(stream, QIODevice_write); | |
187 | ||
188 | return stream; | |
189 | } | |
190 | ||
191 | static int dumpBlob(Repository::Transaction *txn, svn_fs_root_t *fs_root, | |
192 | const char *pathname, apr_pool_t *pool) | |
193 | { | |
194 | // what type is it? | |
195 | int mode = pathMode(fs_root, pathname, pool); | |
196 | ||
197 | svn_stream_t *in_stream, *out_stream; | |
198 | svn_filesize_t stream_length; | |
199 | ||
200 | SVN_ERR(svn_fs_file_length(&stream_length, fs_root, pathname, pool)); | |
201 | QIODevice *io = txn->addFile(pathname, mode, stream_length); | |
202 | ||
203 | // open the file | |
204 | SVN_ERR(svn_fs_file_contents(&in_stream, fs_root, pathname, pool)); | |
205 | ||
206 | // open a generic svn_stream_t for the QIODevice | |
207 | out_stream = streamForDevice(io, pool); | |
208 | SVN_ERR(svn_stream_copy(in_stream, out_stream, pool)); | |
209 | ||
210 | return EXIT_SUCCESS; | |
211 | } | |
212 | ||
213 | time_t get_epoch(char *svn_date) | |
214 | { | |
215 | struct tm tm; | |
216 | memset(&tm, 0, sizeof tm); | |
217 | QByteArray date(svn_date, strlen(svn_date) - 8); | |
218 | strptime(date, "%Y-%m-%dT%H:%M:%S", &tm); | |
219 | return mktime(&tm); | |
220 | } | |
221 | ||
222 | int SvnPrivate::exportRevision(int revnum) | |
223 | { | |
224 | AprAutoPool pool(global_pool); | |
225 | ||
226 | // open this revision: | |
227 | svn_fs_root_t *fs_root; | |
228 | SVN_ERR(svn_fs_revision_root(&fs_root, fs, revnum, pool)); | |
229 | qDebug() << "Exporting revision" << revnum; | |
230 | ||
231 | // find out what was changed in this revision: | |
232 | QHash<QString, Repository::Transaction *> transactions; | |
233 | apr_hash_t *changes; | |
234 | SVN_ERR(svn_fs_paths_changed(&changes, fs_root, pool)); | |
235 | AprAutoPool revpool(pool); | |
236 | for (apr_hash_index_t *i = apr_hash_first(pool, changes); i; i = apr_hash_next(i)) { | |
237 | svn_pool_clear(revpool); | |
238 | ||
239 | const void *vkey; | |
240 | void *value; | |
241 | apr_hash_this(i, &vkey, NULL, &value); | |
242 | const char *key = reinterpret_cast<const char *>(vkey); | |
243 | ||
244 | // is this a directory? | |
245 | svn_boolean_t is_dir; | |
246 | SVN_ERR(svn_fs_is_dir(&is_dir, fs_root, key, revpool)); | |
247 | if (is_dir) | |
248 | continue; // Git doesn't handle directories, so we don't either | |
249 | ||
250 | QString current = QString::fromUtf8(key); | |
251 | ||
252 | // find the first rule that matches this pathname | |
253 | bool foundMatch = false; | |
254 | foreach (Rules::Match rule, matchRules) | |
255 | if (rule.rx.exactMatch(current)) { | |
256 | foundMatch = true; | |
257 | QString repository = current; | |
258 | QString branch = current; | |
259 | QString path = current; | |
260 | ||
261 | // do the replacement | |
262 | repository.replace(rule.rx, rule.repository); | |
263 | branch.replace(rule.rx, rule.branch); | |
264 | path.replace(rule.rx, rule.path); | |
265 | ||
266 | qDebug() << "..." << current << "->" | |
267 | << repository << branch << path; | |
268 | ||
269 | Repository::Transaction *txn = transactions.value(repository, 0); | |
270 | if (!txn) { | |
271 | Repository *repo = repositories.value(repository, 0); | |
272 | if (!repo) { | |
273 | qCritical() << "Rule" << rule.rx.pattern() | |
274 | << "references unknown repository" << repository; | |
275 | return EXIT_FAILURE; | |
276 | } | |
277 | ||
278 | QString svnprefix = current; | |
279 | if (current.endsWith(path)) | |
280 | current.chop(path.length()); | |
281 | ||
282 | txn = repo->newTransaction(branch, svnprefix, revnum); | |
283 | if (!txn) | |
284 | return EXIT_FAILURE; | |
285 | ||
286 | transactions.insert(repository, txn); | |
287 | } | |
288 | ||
289 | svn_fs_path_change_t *change = reinterpret_cast<svn_fs_path_change_t *>(value); | |
290 | if (change->change_kind == svn_fs_path_change_delete) | |
291 | txn->deleteFile(path); | |
292 | else | |
293 | dumpBlob(txn, fs_root, key, revpool); | |
294 | ||
295 | break; | |
296 | } | |
297 | ||
298 | if (!foundMatch) { | |
299 | qCritical() << current << "did not match any rules; cannot continue"; | |
300 | return EXIT_FAILURE; | |
301 | } | |
302 | } | |
303 | svn_pool_clear(revpool); | |
304 | ||
305 | if (transactions.isEmpty()) | |
306 | return true; // no changes? | |
307 | ||
308 | // now create the commit | |
309 | apr_hash_t *revprops; | |
310 | SVN_ERR(svn_fs_revision_proplist(&revprops, fs, revnum, pool)); | |
311 | svn_string_t *svnauthor = (svn_string_t*)apr_hash_get(revprops, "svn:author", APR_HASH_KEY_STRING); | |
312 | svn_string_t *svndate = (svn_string_t*)apr_hash_get(revprops, "svn:date", APR_HASH_KEY_STRING); | |
313 | svn_string_t *svnlog = (svn_string_t*)apr_hash_get(revprops, "svn:log", APR_HASH_KEY_STRING); | |
314 | ||
315 | QByteArray log = (char *)svnlog->data; | |
316 | QByteArray authorident = identities.value((char *)svnauthor->data); | |
317 | time_t epoch = get_epoch((char*)svndate->data); | |
318 | if (authorident.isEmpty()) { | |
319 | if (!svnauthor || svn_string_isempty(svnauthor)) | |
320 | authorident = "nobody <nobody@localhost>"; | |
321 | else | |
322 | authorident = svnauthor->data + QByteArray(" <") + | |
323 | svnauthor->data + QByteArray("@localhost>"); | |
324 | } | |
325 | ||
326 | foreach (Repository::Transaction *txn, transactions) { | |
327 | txn->setAuthor(authorident); | |
328 | txn->setDateTime(epoch); | |
329 | txn->setLog(log); | |
330 | ||
331 | txn->commit(); | |
332 | delete txn; | |
333 | } | |
334 | ||
335 | return EXIT_SUCCESS; | |
336 | } |