]>
Commit | Line | Data |
---|---|---|
d3e9398d TM |
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 | ||
af010386 TM |
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 | ||
d3e9398d | 28 | #include "svn.h" |
af010386 TM |
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 | ||
d3e9398d TM |
48 | #include "repository.h" |
49 | ||
af010386 TM |
50 | #undef SVN_ERR |
51 | #define SVN_ERR(expr) SVN_INT_ERR(expr) | |
52 | ||
d3e9398d TM |
53 | typedef QList<Rules::Match> MatchRuleList; |
54 | typedef QHash<QString, Repository *> RepositoryHash; | |
af010386 TM |
55 | typedef QHash<QByteArray, QByteArray> IdentityHash; |
56 | ||
57 | class AprAutoPool | |
58 | { | |
59 | apr_pool_t *pool; | |
b6ba9639 TM |
60 | AprAutoPool(const AprAutoPool &); |
61 | AprAutoPool &operator=(const AprAutoPool &); | |
af010386 TM |
62 | public: |
63 | inline AprAutoPool(apr_pool_t *parent = NULL) | |
64 | { pool = svn_pool_create(parent); } | |
65 | inline ~AprAutoPool() | |
66 | { svn_pool_destroy(pool); } | |
67 | ||
b6ba9639 | 68 | inline void clear() { svn_pool_clear(pool); } |
af010386 TM |
69 | inline apr_pool_t *data() const { return pool; } |
70 | inline operator apr_pool_t *() const { return pool; } | |
71 | }; | |
d3e9398d TM |
72 | |
73 | class SvnPrivate | |
74 | { | |
75 | public: | |
af010386 TM |
76 | MatchRuleList matchRules; |
77 | RepositoryHash repositories; | |
78 | IdentityHash identities; | |
79 | ||
d3e9398d TM |
80 | SvnPrivate(const QString &pathToRepository); |
81 | ~SvnPrivate(); | |
82 | int youngestRevision(); | |
af010386 | 83 | int exportRevision(int revnum); |
d3e9398d | 84 | |
af010386 TM |
85 | int openRepository(const QString &pathToRepository); |
86 | ||
87 | private: | |
88 | AprAutoPool global_pool; | |
89 | svn_fs_t *fs; | |
90 | svn_revnum_t youngest_rev; | |
d3e9398d TM |
91 | }; |
92 | ||
93 | void Svn::initialize() | |
94 | { | |
af010386 TM |
95 | // initialize APR or exit |
96 | if (apr_initialize() != APR_SUCCESS) { | |
97 | fprintf(stderr, "You lose at apr_initialize().\n"); | |
98 | exit(1); | |
99 | } | |
100 | ||
101 | // static destructor | |
102 | static struct Destructor { ~Destructor() { apr_terminate(); } } destructor; | |
d3e9398d TM |
103 | } |
104 | ||
105 | Svn::Svn(const QString &pathToRepository) | |
106 | : d(new SvnPrivate(pathToRepository)) | |
107 | { | |
108 | } | |
109 | ||
110 | Svn::~Svn() | |
111 | { | |
112 | delete d; | |
113 | } | |
114 | ||
115 | void Svn::setMatchRules(const MatchRuleList &matchRules) | |
116 | { | |
117 | d->matchRules = matchRules; | |
118 | } | |
119 | ||
120 | void Svn::setRepositories(const RepositoryHash &repositories) | |
121 | { | |
122 | d->repositories = repositories; | |
123 | } | |
124 | ||
125 | int Svn::youngestRevision() | |
126 | { | |
127 | return d->youngestRevision(); | |
128 | } | |
129 | ||
af010386 TM |
130 | bool Svn::exportRevision(int revnum) |
131 | { | |
132 | return d->exportRevision(revnum) == EXIT_SUCCESS; | |
133 | } | |
134 | ||
135 | SvnPrivate::SvnPrivate(const QString &pathToRepository) | |
136 | : global_pool(NULL) | |
137 | { | |
138 | openRepository(pathToRepository); | |
139 | ||
140 | // get the youngest revision | |
141 | svn_fs_youngest_rev(&youngest_rev, fs, global_pool); | |
142 | } | |
143 | ||
144 | SvnPrivate::~SvnPrivate() | |
145 | { | |
146 | svn_pool_destroy(global_pool); | |
147 | } | |
148 | ||
149 | int SvnPrivate::youngestRevision() | |
150 | { | |
151 | return youngest_rev; | |
152 | } | |
153 | ||
154 | int SvnPrivate::openRepository(const QString &pathToRepository) | |
155 | { | |
156 | svn_repos_t *repos; | |
157 | SVN_ERR(svn_repos_open(&repos, QFile::encodeName(pathToRepository), global_pool)); | |
158 | fs = svn_repos_fs(repos); | |
159 | ||
160 | return EXIT_SUCCESS; | |
161 | } | |
162 | ||
392c009a | 163 | enum RuleType { AnyRule = 0, NoIgnoreRule = 0x01, NoRecurseRule = 0x02 }; |
97b52f78 | 164 | |
f97a8dff | 165 | static MatchRuleList::ConstIterator |
97b52f78 TM |
166 | findMatchRule(const MatchRuleList &matchRules, int revnum, const QString ¤t, |
167 | int ruleMask = AnyRule) | |
f97a8dff TM |
168 | { |
169 | MatchRuleList::ConstIterator it = matchRules.constBegin(), | |
170 | end = matchRules.constEnd(); | |
171 | for ( ; it != end; ++it) { | |
172 | if (it->minRevision > revnum) | |
173 | continue; | |
174 | if (it->maxRevision != -1 && it->maxRevision < revnum) | |
175 | continue; | |
97b52f78 TM |
176 | if (it->action == Rules::Match::Ignore && ruleMask & NoIgnoreRule) |
177 | continue; | |
392c009a TM |
178 | if (it->action == Rules::Match::Recurse && ruleMask & NoRecurseRule) |
179 | continue; | |
f97a8dff TM |
180 | if (it->rx.indexIn(current) == 0) |
181 | return it; | |
182 | } | |
183 | ||
184 | // no match | |
185 | return end; | |
186 | } | |
187 | ||
c338ae37 TM |
188 | static void splitPathName(const Rules::Match &rule, const QString &pathName, QString *svnprefix_p, |
189 | QString *repository_p, QString *branch_p, QString *path_p) | |
190 | { | |
191 | QString svnprefix = pathName; | |
192 | svnprefix.truncate(rule.rx.matchedLength()); | |
193 | if (svnprefix_p) | |
194 | *svnprefix_p = svnprefix; | |
195 | ||
196 | if (repository_p) { | |
197 | *repository_p = svnprefix; | |
198 | repository_p->replace(rule.rx, rule.repository); | |
199 | } | |
200 | ||
201 | if (branch_p) { | |
202 | *branch_p = svnprefix; | |
203 | branch_p->replace(rule.rx, rule.branch); | |
204 | } | |
205 | ||
206 | if (path_p) | |
207 | *path_p = pathName.mid(svnprefix.length()); | |
208 | } | |
f97a8dff | 209 | |
af010386 TM |
210 | static int pathMode(svn_fs_root_t *fs_root, const char *pathname, apr_pool_t *pool) |
211 | { | |
212 | svn_string_t *propvalue; | |
213 | SVN_ERR(svn_fs_node_prop(&propvalue, fs_root, pathname, "svn:executable", pool)); | |
214 | int mode = 0100644; | |
215 | if (propvalue) | |
216 | mode = 0100755; | |
217 | ||
218 | // maybe it's a symlink? | |
219 | SVN_ERR(svn_fs_node_prop(&propvalue, fs_root, pathname, "svn:special", pool)); | |
b6ba9639 | 220 | if (propvalue && strcmp(propvalue->data, "symlink") == 0) |
af010386 TM |
221 | mode = 0120000; |
222 | ||
223 | return mode; | |
224 | } | |
225 | ||
226 | svn_error_t *QIODevice_write(void *baton, const char *data, apr_size_t *len) | |
d3e9398d | 227 | { |
af010386 TM |
228 | QIODevice *device = reinterpret_cast<QIODevice *>(baton); |
229 | device->write(data, *len); | |
9c31646c | 230 | |
50cd32a4 TM |
231 | while (device->bytesToWrite() > 32*1024) { |
232 | if (!device->waitForBytesWritten(-1)) { | |
25f3205a TM |
233 | qFatal("Failed to write to process: %s", qPrintable(device->errorString())); |
234 | return svn_error_createf(APR_EOF, SVN_NO_ERROR, "Failed to write to process: %s", | |
235 | qPrintable(device->errorString())); | |
236 | } | |
237 | } | |
af010386 | 238 | return SVN_NO_ERROR; |
d3e9398d TM |
239 | } |
240 | ||
af010386 TM |
241 | static svn_stream_t *streamForDevice(QIODevice *device, apr_pool_t *pool) |
242 | { | |
243 | svn_stream_t *stream = svn_stream_create(device, pool); | |
244 | svn_stream_set_write(stream, QIODevice_write); | |
245 | ||
246 | return stream; | |
247 | } | |
248 | ||
249 | static int dumpBlob(Repository::Transaction *txn, svn_fs_root_t *fs_root, | |
b6ba9639 | 250 | const char *pathname, const QString &finalPathName, apr_pool_t *pool) |
af010386 | 251 | { |
898704e4 | 252 | AprAutoPool dumppool(pool); |
af010386 | 253 | // what type is it? |
898704e4 | 254 | int mode = pathMode(fs_root, pathname, dumppool); |
af010386 | 255 | |
af010386 TM |
256 | svn_filesize_t stream_length; |
257 | ||
898704e4 | 258 | SVN_ERR(svn_fs_file_length(&stream_length, fs_root, pathname, dumppool)); |
b6ba9639 | 259 | QIODevice *io = txn->addFile(finalPathName, mode, stream_length); |
af010386 | 260 | |
688d69ec | 261 | #ifndef DRY_RUN |
af010386 | 262 | // open the file |
688d69ec | 263 | svn_stream_t *in_stream, *out_stream; |
898704e4 | 264 | SVN_ERR(svn_fs_file_contents(&in_stream, fs_root, pathname, dumppool)); |
af010386 TM |
265 | |
266 | // open a generic svn_stream_t for the QIODevice | |
898704e4 TM |
267 | out_stream = streamForDevice(io, dumppool); |
268 | SVN_ERR(svn_stream_copy(in_stream, out_stream, dumppool)); | |
af010386 | 269 | |
9c31646c TM |
270 | // print an ending newline |
271 | io->putChar('\n'); | |
688d69ec | 272 | #endif |
9c31646c | 273 | |
af010386 TM |
274 | return EXIT_SUCCESS; |
275 | } | |
276 | ||
14ddd2a5 TM |
277 | static int recursiveDumpDir(Repository::Transaction *txn, svn_fs_root_t *fs_root, |
278 | const QByteArray &pathname, const QString &finalPathName, | |
279 | apr_pool_t *pool) | |
280 | { | |
281 | // get the dir listing | |
282 | apr_hash_t *entries; | |
283 | SVN_ERR(svn_fs_dir_entries(&entries, fs_root, pathname, pool)); | |
284 | AprAutoPool dirpool(pool); | |
285 | ||
286 | for (apr_hash_index_t *i = apr_hash_first(pool, entries); i; i = apr_hash_next(i)) { | |
287 | dirpool.clear(); | |
288 | const void *vkey; | |
289 | void *value; | |
290 | apr_hash_this(i, &vkey, NULL, &value); | |
291 | ||
292 | svn_fs_dirent_t *dirent = reinterpret_cast<svn_fs_dirent_t *>(value); | |
293 | QByteArray entryName = pathname + '/' + dirent->name; | |
5871af71 | 294 | QString entryFinalName = finalPathName + dirent->name; |
14ddd2a5 TM |
295 | |
296 | if (dirent->kind == svn_node_dir) { | |
5871af71 | 297 | entryFinalName += '/'; |
14ddd2a5 TM |
298 | if (recursiveDumpDir(txn, fs_root, entryName, entryFinalName, dirpool) == EXIT_FAILURE) |
299 | return EXIT_FAILURE; | |
300 | } else if (dirent->kind == svn_node_file) { | |
c338ae37 TM |
301 | printf("+"); |
302 | fflush(stdout); | |
14ddd2a5 TM |
303 | if (dumpBlob(txn, fs_root, entryName, entryFinalName, dirpool) == EXIT_FAILURE) |
304 | return EXIT_FAILURE; | |
305 | } | |
306 | } | |
898704e4 TM |
307 | |
308 | return EXIT_SUCCESS; | |
14ddd2a5 TM |
309 | } |
310 | ||
5539acb4 TM |
311 | static bool wasDir(svn_fs_t *fs, int revnum, const char *pathname, apr_pool_t *pool) |
312 | { | |
313 | AprAutoPool subpool(pool); | |
314 | svn_fs_root_t *fs_root; | |
315 | if (svn_fs_revision_root(&fs_root, fs, revnum, subpool) != SVN_NO_ERROR) | |
316 | return false; | |
317 | ||
318 | svn_boolean_t is_dir; | |
319 | if (svn_fs_is_dir(&is_dir, fs_root, pathname, subpool) != SVN_NO_ERROR) | |
320 | return false; | |
321 | ||
322 | return is_dir; | |
323 | } | |
324 | ||
af010386 TM |
325 | time_t get_epoch(char *svn_date) |
326 | { | |
327 | struct tm tm; | |
328 | memset(&tm, 0, sizeof tm); | |
329 | QByteArray date(svn_date, strlen(svn_date) - 8); | |
330 | strptime(date, "%Y-%m-%dT%H:%M:%S", &tm); | |
331 | return mktime(&tm); | |
332 | } | |
333 | ||
898704e4 | 334 | class SvnRevision |
af010386 | 335 | { |
898704e4 TM |
336 | public: |
337 | AprAutoPool pool; | |
f97a8dff | 338 | QHash<QString, Repository::Transaction *> transactions; |
898704e4 TM |
339 | MatchRuleList matchRules; |
340 | RepositoryHash repositories; | |
341 | IdentityHash identities; | |
342 | ||
343 | svn_fs_t *fs; | |
344 | svn_fs_root_t *fs_root; | |
345 | int revnum; | |
346 | ||
347 | SvnRevision(int revision, svn_fs_t *f, apr_pool_t *parent_pool) | |
348 | : pool(parent_pool), fs(f), fs_root(0), revnum(revision) | |
349 | { | |
350 | } | |
351 | ||
352 | int open() | |
353 | { | |
354 | SVN_ERR(svn_fs_revision_root(&fs_root, fs, revnum, pool)); | |
355 | return EXIT_SUCCESS; | |
356 | } | |
357 | ||
358 | int prepareTransactions(); | |
359 | int commit(); | |
360 | ||
8ca4b631 | 361 | int exportEntry(const char *path, const svn_fs_path_change_t *change, apr_hash_t *changes); |
b4cb5188 TM |
362 | int exportDispatch(const char *path, const svn_fs_path_change_t *change, |
363 | const char *path_from, svn_revnum_t rev_from, | |
364 | apr_hash_t *changes, const QString ¤t, const Rules::Match &rule, | |
365 | apr_pool_t *pool); | |
898704e4 TM |
366 | int exportInternal(const char *path, const svn_fs_path_change_t *change, |
367 | const char *path_from, svn_revnum_t rev_from, | |
368 | const QString ¤t, const Rules::Match &rule); | |
369 | int recurse(const char *path, const svn_fs_path_change_t *change, | |
8ca4b631 TM |
370 | const char *path_from, svn_revnum_t rev_from, |
371 | apr_hash_t *changes, apr_pool_t *pool); | |
898704e4 TM |
372 | }; |
373 | ||
374 | int SvnPrivate::exportRevision(int revnum) | |
375 | { | |
376 | SvnRevision rev(revnum, fs, global_pool); | |
377 | rev.matchRules = matchRules; | |
378 | rev.repositories = repositories; | |
379 | rev.identities = identities; | |
af010386 TM |
380 | |
381 | // open this revision: | |
dcbaff44 TM |
382 | printf("Exporting revision %d ", revnum); |
383 | fflush(stdout); | |
af010386 | 384 | |
898704e4 TM |
385 | if (rev.open() == EXIT_FAILURE) |
386 | return EXIT_FAILURE; | |
387 | ||
388 | if (rev.prepareTransactions() == EXIT_FAILURE) | |
389 | return EXIT_FAILURE; | |
390 | ||
391 | if (rev.transactions.isEmpty()) { | |
392 | printf(" nothing to do\n"); | |
393 | return EXIT_SUCCESS; // no changes? | |
394 | } | |
395 | ||
396 | if (rev.commit() == EXIT_FAILURE) | |
397 | return EXIT_FAILURE; | |
398 | ||
399 | printf(" done\n"); | |
400 | return EXIT_SUCCESS; | |
401 | } | |
402 | ||
403 | int SvnRevision::prepareTransactions() | |
404 | { | |
af010386 | 405 | // find out what was changed in this revision: |
af010386 TM |
406 | apr_hash_t *changes; |
407 | SVN_ERR(svn_fs_paths_changed(&changes, fs_root, pool)); | |
af010386 | 408 | for (apr_hash_index_t *i = apr_hash_first(pool, changes); i; i = apr_hash_next(i)) { |
af010386 TM |
409 | const void *vkey; |
410 | void *value; | |
411 | apr_hash_this(i, &vkey, NULL, &value); | |
412 | const char *key = reinterpret_cast<const char *>(vkey); | |
7a6160e7 | 413 | svn_fs_path_change_t *change = reinterpret_cast<svn_fs_path_change_t *>(value); |
af010386 | 414 | |
8ca4b631 | 415 | if (exportEntry(key, change, changes) == EXIT_FAILURE) |
c338ae37 | 416 | return EXIT_FAILURE; |
af010386 | 417 | } |
af010386 | 418 | |
898704e4 TM |
419 | return EXIT_SUCCESS; |
420 | } | |
af010386 | 421 | |
898704e4 TM |
422 | int SvnRevision::commit() |
423 | { | |
af010386 TM |
424 | // now create the commit |
425 | apr_hash_t *revprops; | |
426 | SVN_ERR(svn_fs_revision_proplist(&revprops, fs, revnum, pool)); | |
427 | svn_string_t *svnauthor = (svn_string_t*)apr_hash_get(revprops, "svn:author", APR_HASH_KEY_STRING); | |
428 | svn_string_t *svndate = (svn_string_t*)apr_hash_get(revprops, "svn:date", APR_HASH_KEY_STRING); | |
429 | svn_string_t *svnlog = (svn_string_t*)apr_hash_get(revprops, "svn:log", APR_HASH_KEY_STRING); | |
430 | ||
431 | QByteArray log = (char *)svnlog->data; | |
453ea6b6 | 432 | QByteArray authorident = svnauthor ? identities.value((char *)svnauthor->data) : QByteArray(); |
af010386 TM |
433 | time_t epoch = get_epoch((char*)svndate->data); |
434 | if (authorident.isEmpty()) { | |
435 | if (!svnauthor || svn_string_isempty(svnauthor)) | |
436 | authorident = "nobody <nobody@localhost>"; | |
437 | else | |
438 | authorident = svnauthor->data + QByteArray(" <") + | |
439 | svnauthor->data + QByteArray("@localhost>"); | |
440 | } | |
441 | ||
442 | foreach (Repository::Transaction *txn, transactions) { | |
443 | txn->setAuthor(authorident); | |
444 | txn->setDateTime(epoch); | |
445 | txn->setLog(log); | |
446 | ||
447 | txn->commit(); | |
448 | delete txn; | |
449 | } | |
450 | ||
451 | return EXIT_SUCCESS; | |
452 | } | |
898704e4 | 453 | |
8ca4b631 TM |
454 | int SvnRevision::exportEntry(const char *key, const svn_fs_path_change_t *change, |
455 | apr_hash_t *changes) | |
898704e4 TM |
456 | { |
457 | AprAutoPool revpool(pool.data()); | |
458 | QString current = QString::fromUtf8(key); | |
459 | ||
460 | // was this copied from somewhere? | |
461 | svn_revnum_t rev_from; | |
462 | const char *path_from; | |
463 | SVN_ERR(svn_fs_copied_from(&rev_from, &path_from, fs_root, key, revpool)); | |
464 | ||
465 | // is this a directory? | |
466 | svn_boolean_t is_dir; | |
295db4b2 | 467 | SVN_ERR(svn_fs_is_dir(&is_dir, fs_root, key, revpool)); |
898704e4 TM |
468 | if (is_dir) { |
469 | if (path_from == NULL) { | |
470 | // no, it's a new directory being added | |
471 | // Git doesn't handle directories, so we don't either | |
472 | //qDebug() << " mkdir ignored:" << key; | |
473 | return EXIT_SUCCESS; | |
474 | } | |
475 | ||
476 | current += '/'; | |
477 | qDebug() << " " << key << "was copied from" << path_from; | |
478 | } | |
479 | ||
480 | // find the first rule that matches this pathname | |
481 | MatchRuleList::ConstIterator match = findMatchRule(matchRules, revnum, current); | |
482 | if (match != matchRules.constEnd()) { | |
483 | const Rules::Match &rule = *match; | |
b4cb5188 | 484 | return exportDispatch(key, change, path_from, rev_from, changes, current, rule, revpool); |
898704e4 TM |
485 | } |
486 | ||
6b450d1d TM |
487 | if (is_dir && path_from != NULL) { |
488 | qDebug() << current << "is a copy-with-history, auto-recursing"; | |
8ca4b631 | 489 | return recurse(key, change, path_from, rev_from, changes, revpool); |
6b450d1d | 490 | } else if (wasDir(fs, revnum - 1, key, revpool)) { |
898704e4 TM |
491 | qDebug() << current << "was a directory; ignoring"; |
492 | } else if (change->change_kind == svn_fs_path_change_delete) { | |
493 | qDebug() << current << "is being deleted but I don't know anything about it; ignoring"; | |
494 | } else { | |
495 | qCritical() << current << "did not match any rules; cannot continue"; | |
496 | return EXIT_FAILURE; | |
497 | } | |
498 | ||
499 | return EXIT_SUCCESS; | |
500 | } | |
501 | ||
b4cb5188 | 502 | int SvnRevision::exportDispatch(const char *key, const svn_fs_path_change_t *change, |
898704e4 | 503 | const char *path_from, svn_revnum_t rev_from, |
b4cb5188 TM |
504 | apr_hash_t *changes, const QString ¤t, |
505 | const Rules::Match &rule, apr_pool_t *pool) | |
898704e4 | 506 | { |
b4cb5188 TM |
507 | switch (rule.action) { |
508 | case Rules::Match::Ignore: | |
16024419 TM |
509 | // ignore rule |
510 | qDebug() << " " << qPrintable(current) << "rev" << revnum | |
511 | << "-> ignored (rule" << rule << ")"; | |
512 | return EXIT_SUCCESS; | |
b4cb5188 TM |
513 | |
514 | case Rules::Match::Recurse: | |
515 | return recurse(key, change, path_from, rev_from, changes, pool); | |
516 | ||
517 | case Rules::Match::Export: | |
518 | return exportInternal(key, change, path_from, rev_from, current, rule); | |
16024419 | 519 | } |
25f3205a TM |
520 | |
521 | // never reached | |
522 | return EXIT_FAILURE; | |
b4cb5188 | 523 | } |
16024419 | 524 | |
b4cb5188 TM |
525 | int SvnRevision::exportInternal(const char *key, const svn_fs_path_change_t *change, |
526 | const char *path_from, svn_revnum_t rev_from, | |
527 | const QString ¤t, const Rules::Match &rule) | |
528 | { | |
898704e4 TM |
529 | QString svnprefix, repository, branch, path; |
530 | splitPathName(rule, current, &svnprefix, &repository, &branch, &path); | |
531 | ||
532 | printf("."); | |
533 | fflush(stdout); | |
534 | // qDebug() << " " << qPrintable(current) << "rev" << revnum << "->" | |
535 | // << qPrintable(repository) << qPrintable(branch) << qPrintable(path); | |
536 | ||
537 | if (path.isEmpty() && path_from != NULL) { | |
538 | QString previous = QString::fromUtf8(path_from) + '/'; | |
539 | MatchRuleList::ConstIterator prevmatch = | |
16024419 | 540 | findMatchRule(matchRules, rev_from, previous, NoIgnoreRule); |
898704e4 TM |
541 | if (prevmatch != matchRules.constEnd()) { |
542 | QString prevsvnprefix, prevrepository, prevbranch, prevpath; | |
543 | splitPathName(*prevmatch, previous, &prevsvnprefix, &prevrepository, | |
544 | &prevbranch, &prevpath); | |
545 | ||
546 | if (!prevpath.isEmpty()) { | |
547 | qDebug() << qPrintable(current) << "is a partial branch of repository" | |
548 | << qPrintable(prevrepository) << "branch" | |
549 | << qPrintable(prevbranch) << "subdir" | |
550 | << qPrintable(prevpath); | |
551 | } else if (prevrepository != repository) { | |
552 | qWarning() << qPrintable(current) << "rev" << revnum | |
553 | << "is a cross-repository copy (from repository" | |
554 | << qPrintable(prevrepository) << "branch" | |
555 | << qPrintable(prevbranch) << "path" | |
556 | << qPrintable(prevpath) << "rev" << rev_from << ")"; | |
557 | } else if (prevbranch == branch) { | |
558 | // same branch and same repository | |
559 | qDebug() << qPrintable(current) << "rev" << revnum | |
560 | << "is an SVN rename from" | |
561 | << qPrintable(previous) << "rev" << rev_from; | |
562 | return EXIT_SUCCESS; | |
563 | } else { | |
564 | // same repository but not same branch | |
565 | // this means this is a plain branch | |
566 | qDebug() << qPrintable(repository) << ": branch" | |
567 | << qPrintable(branch) << "is branching from" | |
568 | << qPrintable(prevbranch); | |
569 | ||
570 | Repository *repo = repositories.value(repository, 0); | |
571 | if (!repo) { | |
2ddd1758 | 572 | qCritical() << "Rule" << rule |
898704e4 TM |
573 | << "references unknown repository" << repository; |
574 | return EXIT_FAILURE; | |
575 | } | |
576 | ||
577 | repo->createBranch(branch, revnum, prevbranch, rev_from); | |
844c7a90 | 578 | return EXIT_SUCCESS; |
898704e4 TM |
579 | } |
580 | } | |
581 | } | |
582 | ||
b8549b90 | 583 | Repository::Transaction *txn = transactions.value(repository + branch, 0); |
898704e4 TM |
584 | if (!txn) { |
585 | Repository *repo = repositories.value(repository, 0); | |
586 | if (!repo) { | |
2ddd1758 | 587 | qCritical() << "Rule" << rule |
898704e4 TM |
588 | << "references unknown repository" << repository; |
589 | return EXIT_FAILURE; | |
590 | } | |
591 | ||
592 | txn = repo->newTransaction(branch, svnprefix, revnum); | |
593 | if (!txn) | |
594 | return EXIT_FAILURE; | |
595 | ||
b8549b90 | 596 | transactions.insert(repository + branch, txn); |
898704e4 TM |
597 | } |
598 | ||
6b450d1d | 599 | if (change->change_kind == svn_fs_path_change_delete) { |
898704e4 | 600 | txn->deleteFile(path); |
6b450d1d | 601 | } else if (!current.endsWith('/')) { |
898704e4 | 602 | dumpBlob(txn, fs_root, key, path, pool); |
6b450d1d | 603 | } else { |
5871af71 TM |
604 | QString pathNoSlash = path; |
605 | pathNoSlash.chop(1); | |
606 | txn->deleteFile(pathNoSlash); | |
898704e4 | 607 | recursiveDumpDir(txn, fs_root, key, path, pool); |
6b450d1d | 608 | } |
898704e4 TM |
609 | |
610 | return EXIT_SUCCESS; | |
611 | } | |
612 | ||
613 | int SvnRevision::recurse(const char *path, const svn_fs_path_change_t *change, | |
614 | const char *path_from, svn_revnum_t rev_from, | |
8ca4b631 | 615 | apr_hash_t *changes, apr_pool_t *pool) |
898704e4 TM |
616 | { |
617 | // get the dir listing | |
618 | apr_hash_t *entries; | |
619 | SVN_ERR(svn_fs_dir_entries(&entries, fs_root, path, pool)); | |
620 | ||
621 | AprAutoPool dirpool(pool); | |
622 | for (apr_hash_index_t *i = apr_hash_first(pool, entries); i; i = apr_hash_next(i)) { | |
623 | dirpool.clear(); | |
624 | const void *vkey; | |
625 | void *value; | |
626 | apr_hash_this(i, &vkey, NULL, &value); | |
627 | ||
628 | svn_fs_dirent_t *dirent = reinterpret_cast<svn_fs_dirent_t *>(value); | |
629 | QByteArray entry = path + QByteArray("/") + dirent->name; | |
6b450d1d TM |
630 | QByteArray entryFrom; |
631 | if (path_from) | |
632 | entryFrom = path_from + QByteArray("/") + dirent->name; | |
898704e4 | 633 | |
8ca4b631 TM |
634 | // check if this entry is in the changelist for this revision already |
635 | if (apr_hash_get(changes, entry.constData(), APR_HASH_KEY_STRING)) { | |
636 | qDebug() << entry << "rev" << revnum | |
637 | << "is in the change-list, deferring to that one"; | |
638 | continue; | |
639 | } | |
640 | ||
898704e4 TM |
641 | QString current = QString::fromUtf8(entry); |
642 | if (dirent->kind == svn_node_dir) | |
643 | current += '/'; | |
644 | ||
645 | // find the first rule that matches this pathname | |
646 | MatchRuleList::ConstIterator match = findMatchRule(matchRules, revnum, current); | |
647 | if (match != matchRules.constEnd()) { | |
b4cb5188 TM |
648 | if (exportDispatch(entry, change, entryFrom.isNull() ? 0 : entryFrom.constData(), |
649 | rev_from, changes, current, *match, dirpool) == EXIT_FAILURE) | |
898704e4 TM |
650 | return EXIT_FAILURE; |
651 | } else { | |
d4c5541d TM |
652 | qCritical() << current << "rev" << revnum |
653 | << "did not match any rules; cannot continue"; | |
898704e4 TM |
654 | return EXIT_FAILURE; |
655 | } | |
656 | } | |
1c5ce394 TM |
657 | |
658 | return EXIT_SUCCESS; | |
898704e4 | 659 | } |