]>
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 | ||
bb416d33 TM |
125 | void Svn::setIdentityMap(const IdentityHash &identityMap) |
126 | { | |
127 | d->identities = identityMap; | |
128 | } | |
129 | ||
d3e9398d TM |
130 | int Svn::youngestRevision() |
131 | { | |
132 | return d->youngestRevision(); | |
133 | } | |
134 | ||
af010386 TM |
135 | bool Svn::exportRevision(int revnum) |
136 | { | |
137 | return d->exportRevision(revnum) == EXIT_SUCCESS; | |
138 | } | |
139 | ||
140 | SvnPrivate::SvnPrivate(const QString &pathToRepository) | |
141 | : global_pool(NULL) | |
142 | { | |
143 | openRepository(pathToRepository); | |
144 | ||
145 | // get the youngest revision | |
146 | svn_fs_youngest_rev(&youngest_rev, fs, global_pool); | |
147 | } | |
148 | ||
149 | SvnPrivate::~SvnPrivate() | |
150 | { | |
151 | svn_pool_destroy(global_pool); | |
152 | } | |
153 | ||
154 | int SvnPrivate::youngestRevision() | |
155 | { | |
156 | return youngest_rev; | |
157 | } | |
158 | ||
159 | int SvnPrivate::openRepository(const QString &pathToRepository) | |
160 | { | |
161 | svn_repos_t *repos; | |
162 | SVN_ERR(svn_repos_open(&repos, QFile::encodeName(pathToRepository), global_pool)); | |
163 | fs = svn_repos_fs(repos); | |
164 | ||
165 | return EXIT_SUCCESS; | |
166 | } | |
167 | ||
392c009a | 168 | enum RuleType { AnyRule = 0, NoIgnoreRule = 0x01, NoRecurseRule = 0x02 }; |
97b52f78 | 169 | |
f97a8dff | 170 | static MatchRuleList::ConstIterator |
97b52f78 TM |
171 | findMatchRule(const MatchRuleList &matchRules, int revnum, const QString ¤t, |
172 | int ruleMask = AnyRule) | |
f97a8dff TM |
173 | { |
174 | MatchRuleList::ConstIterator it = matchRules.constBegin(), | |
175 | end = matchRules.constEnd(); | |
176 | for ( ; it != end; ++it) { | |
177 | if (it->minRevision > revnum) | |
178 | continue; | |
179 | if (it->maxRevision != -1 && it->maxRevision < revnum) | |
180 | continue; | |
97b52f78 TM |
181 | if (it->action == Rules::Match::Ignore && ruleMask & NoIgnoreRule) |
182 | continue; | |
392c009a TM |
183 | if (it->action == Rules::Match::Recurse && ruleMask & NoRecurseRule) |
184 | continue; | |
f97a8dff TM |
185 | if (it->rx.indexIn(current) == 0) |
186 | return it; | |
187 | } | |
188 | ||
189 | // no match | |
190 | return end; | |
191 | } | |
192 | ||
c338ae37 TM |
193 | static void splitPathName(const Rules::Match &rule, const QString &pathName, QString *svnprefix_p, |
194 | QString *repository_p, QString *branch_p, QString *path_p) | |
195 | { | |
196 | QString svnprefix = pathName; | |
197 | svnprefix.truncate(rule.rx.matchedLength()); | |
198 | if (svnprefix_p) | |
199 | *svnprefix_p = svnprefix; | |
200 | ||
201 | if (repository_p) { | |
202 | *repository_p = svnprefix; | |
203 | repository_p->replace(rule.rx, rule.repository); | |
204 | } | |
205 | ||
206 | if (branch_p) { | |
207 | *branch_p = svnprefix; | |
208 | branch_p->replace(rule.rx, rule.branch); | |
209 | } | |
210 | ||
211 | if (path_p) | |
212 | *path_p = pathName.mid(svnprefix.length()); | |
213 | } | |
f97a8dff | 214 | |
af010386 TM |
215 | static int pathMode(svn_fs_root_t *fs_root, const char *pathname, apr_pool_t *pool) |
216 | { | |
217 | svn_string_t *propvalue; | |
218 | SVN_ERR(svn_fs_node_prop(&propvalue, fs_root, pathname, "svn:executable", pool)); | |
219 | int mode = 0100644; | |
220 | if (propvalue) | |
221 | mode = 0100755; | |
222 | ||
af010386 TM |
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)); |
af010386 | 259 | |
688d69ec | 260 | #ifndef DRY_RUN |
af010386 | 261 | // open the file |
688d69ec | 262 | svn_stream_t *in_stream, *out_stream; |
898704e4 | 263 | SVN_ERR(svn_fs_file_contents(&in_stream, fs_root, pathname, dumppool)); |
4c4a6cf1 | 264 | #endif |
af010386 | 265 | |
4c4a6cf1 AK |
266 | // maybe it's a symlink? |
267 | svn_string_t *propvalue; | |
268 | SVN_ERR(svn_fs_node_prop(&propvalue, fs_root, pathname, "svn:special", dumppool)); | |
269 | if (propvalue) { | |
270 | apr_size_t len = strlen("link "); | |
271 | #ifndef DRY_RUN | |
272 | QByteArray buf; | |
273 | buf.reserve(len); | |
274 | SVN_ERR(svn_stream_read(in_stream, buf.data(), &len)); | |
275 | if (len != strlen("link ") || strncmp(buf, "link ", len) != 0) | |
276 | qFatal("file %s is svn:special but not a symlink", pathname); | |
277 | #endif | |
278 | mode = 0120000; | |
279 | stream_length -= len; | |
280 | } | |
281 | ||
282 | QIODevice *io = txn->addFile(finalPathName, mode, stream_length); | |
283 | ||
284 | #ifndef DRY_RUN | |
af010386 | 285 | // open a generic svn_stream_t for the QIODevice |
898704e4 TM |
286 | out_stream = streamForDevice(io, dumppool); |
287 | SVN_ERR(svn_stream_copy(in_stream, out_stream, dumppool)); | |
b9ea3501 AK |
288 | svn_stream_close(out_stream); |
289 | svn_stream_close(in_stream); | |
af010386 | 290 | |
9c31646c TM |
291 | // print an ending newline |
292 | io->putChar('\n'); | |
688d69ec | 293 | #endif |
9c31646c | 294 | |
af010386 TM |
295 | return EXIT_SUCCESS; |
296 | } | |
297 | ||
14ddd2a5 TM |
298 | static int recursiveDumpDir(Repository::Transaction *txn, svn_fs_root_t *fs_root, |
299 | const QByteArray &pathname, const QString &finalPathName, | |
300 | apr_pool_t *pool) | |
301 | { | |
302 | // get the dir listing | |
303 | apr_hash_t *entries; | |
304 | SVN_ERR(svn_fs_dir_entries(&entries, fs_root, pathname, pool)); | |
305 | AprAutoPool dirpool(pool); | |
306 | ||
307 | for (apr_hash_index_t *i = apr_hash_first(pool, entries); i; i = apr_hash_next(i)) { | |
308 | dirpool.clear(); | |
309 | const void *vkey; | |
310 | void *value; | |
311 | apr_hash_this(i, &vkey, NULL, &value); | |
312 | ||
313 | svn_fs_dirent_t *dirent = reinterpret_cast<svn_fs_dirent_t *>(value); | |
314 | QByteArray entryName = pathname + '/' + dirent->name; | |
5871af71 | 315 | QString entryFinalName = finalPathName + dirent->name; |
14ddd2a5 TM |
316 | |
317 | if (dirent->kind == svn_node_dir) { | |
5871af71 | 318 | entryFinalName += '/'; |
14ddd2a5 TM |
319 | if (recursiveDumpDir(txn, fs_root, entryName, entryFinalName, dirpool) == EXIT_FAILURE) |
320 | return EXIT_FAILURE; | |
321 | } else if (dirent->kind == svn_node_file) { | |
c338ae37 TM |
322 | printf("+"); |
323 | fflush(stdout); | |
14ddd2a5 TM |
324 | if (dumpBlob(txn, fs_root, entryName, entryFinalName, dirpool) == EXIT_FAILURE) |
325 | return EXIT_FAILURE; | |
326 | } | |
327 | } | |
898704e4 TM |
328 | |
329 | return EXIT_SUCCESS; | |
14ddd2a5 TM |
330 | } |
331 | ||
5539acb4 TM |
332 | static bool wasDir(svn_fs_t *fs, int revnum, const char *pathname, apr_pool_t *pool) |
333 | { | |
334 | AprAutoPool subpool(pool); | |
335 | svn_fs_root_t *fs_root; | |
336 | if (svn_fs_revision_root(&fs_root, fs, revnum, subpool) != SVN_NO_ERROR) | |
337 | return false; | |
338 | ||
339 | svn_boolean_t is_dir; | |
340 | if (svn_fs_is_dir(&is_dir, fs_root, pathname, subpool) != SVN_NO_ERROR) | |
341 | return false; | |
342 | ||
343 | return is_dir; | |
344 | } | |
345 | ||
af010386 TM |
346 | time_t get_epoch(char *svn_date) |
347 | { | |
348 | struct tm tm; | |
349 | memset(&tm, 0, sizeof tm); | |
350 | QByteArray date(svn_date, strlen(svn_date) - 8); | |
351 | strptime(date, "%Y-%m-%dT%H:%M:%S", &tm); | |
58d2eb3e | 352 | return timegm(&tm); |
af010386 TM |
353 | } |
354 | ||
898704e4 | 355 | class SvnRevision |
af010386 | 356 | { |
898704e4 TM |
357 | public: |
358 | AprAutoPool pool; | |
f97a8dff | 359 | QHash<QString, Repository::Transaction *> transactions; |
898704e4 TM |
360 | MatchRuleList matchRules; |
361 | RepositoryHash repositories; | |
362 | IdentityHash identities; | |
363 | ||
364 | svn_fs_t *fs; | |
365 | svn_fs_root_t *fs_root; | |
366 | int revnum; | |
367 | ||
a5fd311b TM |
368 | // must call fetchRevProps first: |
369 | QByteArray authorident; | |
370 | QByteArray log; | |
371 | uint epoch; | |
372 | ||
898704e4 TM |
373 | SvnRevision(int revision, svn_fs_t *f, apr_pool_t *parent_pool) |
374 | : pool(parent_pool), fs(f), fs_root(0), revnum(revision) | |
375 | { | |
376 | } | |
377 | ||
378 | int open() | |
379 | { | |
380 | SVN_ERR(svn_fs_revision_root(&fs_root, fs, revnum, pool)); | |
381 | return EXIT_SUCCESS; | |
382 | } | |
383 | ||
384 | int prepareTransactions(); | |
a5fd311b | 385 | int fetchRevProps(); |
898704e4 TM |
386 | int commit(); |
387 | ||
8ca4b631 | 388 | int exportEntry(const char *path, const svn_fs_path_change_t *change, apr_hash_t *changes); |
b4cb5188 TM |
389 | int exportDispatch(const char *path, const svn_fs_path_change_t *change, |
390 | const char *path_from, svn_revnum_t rev_from, | |
391 | apr_hash_t *changes, const QString ¤t, const Rules::Match &rule, | |
392 | apr_pool_t *pool); | |
898704e4 TM |
393 | int exportInternal(const char *path, const svn_fs_path_change_t *change, |
394 | const char *path_from, svn_revnum_t rev_from, | |
395 | const QString ¤t, const Rules::Match &rule); | |
396 | int recurse(const char *path, const svn_fs_path_change_t *change, | |
8ca4b631 TM |
397 | const char *path_from, svn_revnum_t rev_from, |
398 | apr_hash_t *changes, apr_pool_t *pool); | |
898704e4 TM |
399 | }; |
400 | ||
401 | int SvnPrivate::exportRevision(int revnum) | |
402 | { | |
403 | SvnRevision rev(revnum, fs, global_pool); | |
404 | rev.matchRules = matchRules; | |
405 | rev.repositories = repositories; | |
406 | rev.identities = identities; | |
af010386 TM |
407 | |
408 | // open this revision: | |
dcbaff44 TM |
409 | printf("Exporting revision %d ", revnum); |
410 | fflush(stdout); | |
af010386 | 411 | |
898704e4 TM |
412 | if (rev.open() == EXIT_FAILURE) |
413 | return EXIT_FAILURE; | |
414 | ||
415 | if (rev.prepareTransactions() == EXIT_FAILURE) | |
416 | return EXIT_FAILURE; | |
417 | ||
418 | if (rev.transactions.isEmpty()) { | |
419 | printf(" nothing to do\n"); | |
420 | return EXIT_SUCCESS; // no changes? | |
421 | } | |
422 | ||
423 | if (rev.commit() == EXIT_FAILURE) | |
424 | return EXIT_FAILURE; | |
425 | ||
426 | printf(" done\n"); | |
427 | return EXIT_SUCCESS; | |
428 | } | |
429 | ||
430 | int SvnRevision::prepareTransactions() | |
431 | { | |
af010386 | 432 | // find out what was changed in this revision: |
af010386 TM |
433 | apr_hash_t *changes; |
434 | SVN_ERR(svn_fs_paths_changed(&changes, fs_root, pool)); | |
af010386 | 435 | for (apr_hash_index_t *i = apr_hash_first(pool, changes); i; i = apr_hash_next(i)) { |
af010386 TM |
436 | const void *vkey; |
437 | void *value; | |
438 | apr_hash_this(i, &vkey, NULL, &value); | |
439 | const char *key = reinterpret_cast<const char *>(vkey); | |
7a6160e7 | 440 | svn_fs_path_change_t *change = reinterpret_cast<svn_fs_path_change_t *>(value); |
af010386 | 441 | |
8ca4b631 | 442 | if (exportEntry(key, change, changes) == EXIT_FAILURE) |
c338ae37 | 443 | return EXIT_FAILURE; |
af010386 | 444 | } |
af010386 | 445 | |
898704e4 TM |
446 | return EXIT_SUCCESS; |
447 | } | |
af010386 | 448 | |
a5fd311b | 449 | int SvnRevision::fetchRevProps() |
898704e4 | 450 | { |
af010386 TM |
451 | apr_hash_t *revprops; |
452 | SVN_ERR(svn_fs_revision_proplist(&revprops, fs, revnum, pool)); | |
453 | svn_string_t *svnauthor = (svn_string_t*)apr_hash_get(revprops, "svn:author", APR_HASH_KEY_STRING); | |
454 | svn_string_t *svndate = (svn_string_t*)apr_hash_get(revprops, "svn:date", APR_HASH_KEY_STRING); | |
455 | svn_string_t *svnlog = (svn_string_t*)apr_hash_get(revprops, "svn:log", APR_HASH_KEY_STRING); | |
456 | ||
a5fd311b TM |
457 | log = (char *)svnlog->data; |
458 | authorident = svnauthor ? identities.value((char *)svnauthor->data) : QByteArray(); | |
459 | epoch = get_epoch((char*)svndate->data); | |
af010386 TM |
460 | if (authorident.isEmpty()) { |
461 | if (!svnauthor || svn_string_isempty(svnauthor)) | |
462 | authorident = "nobody <nobody@localhost>"; | |
463 | else | |
464 | authorident = svnauthor->data + QByteArray(" <") + | |
465 | svnauthor->data + QByteArray("@localhost>"); | |
466 | } | |
a5fd311b TM |
467 | return EXIT_SUCCESS; |
468 | } | |
af010386 | 469 | |
a5fd311b TM |
470 | int SvnRevision::commit() |
471 | { | |
472 | // now create the commit | |
473 | if (fetchRevProps() != EXIT_SUCCESS) | |
474 | return EXIT_FAILURE; | |
af010386 TM |
475 | foreach (Repository::Transaction *txn, transactions) { |
476 | txn->setAuthor(authorident); | |
477 | txn->setDateTime(epoch); | |
478 | txn->setLog(log); | |
479 | ||
480 | txn->commit(); | |
481 | delete txn; | |
482 | } | |
483 | ||
484 | return EXIT_SUCCESS; | |
485 | } | |
898704e4 | 486 | |
8ca4b631 TM |
487 | int SvnRevision::exportEntry(const char *key, const svn_fs_path_change_t *change, |
488 | apr_hash_t *changes) | |
898704e4 TM |
489 | { |
490 | AprAutoPool revpool(pool.data()); | |
491 | QString current = QString::fromUtf8(key); | |
492 | ||
493 | // was this copied from somewhere? | |
494 | svn_revnum_t rev_from; | |
495 | const char *path_from; | |
496 | SVN_ERR(svn_fs_copied_from(&rev_from, &path_from, fs_root, key, revpool)); | |
497 | ||
498 | // is this a directory? | |
499 | svn_boolean_t is_dir; | |
295db4b2 | 500 | SVN_ERR(svn_fs_is_dir(&is_dir, fs_root, key, revpool)); |
898704e4 TM |
501 | if (is_dir) { |
502 | if (path_from == NULL) { | |
503 | // no, it's a new directory being added | |
504 | // Git doesn't handle directories, so we don't either | |
505 | //qDebug() << " mkdir ignored:" << key; | |
506 | return EXIT_SUCCESS; | |
507 | } | |
508 | ||
509 | current += '/'; | |
25e6c28e | 510 | qDebug() << " " << key << "was copied from" << path_from << "rev" << rev_from; |
898704e4 TM |
511 | } |
512 | ||
513 | // find the first rule that matches this pathname | |
514 | MatchRuleList::ConstIterator match = findMatchRule(matchRules, revnum, current); | |
515 | if (match != matchRules.constEnd()) { | |
516 | const Rules::Match &rule = *match; | |
b4cb5188 | 517 | return exportDispatch(key, change, path_from, rev_from, changes, current, rule, revpool); |
898704e4 TM |
518 | } |
519 | ||
6b450d1d TM |
520 | if (is_dir && path_from != NULL) { |
521 | qDebug() << current << "is a copy-with-history, auto-recursing"; | |
8ca4b631 | 522 | return recurse(key, change, path_from, rev_from, changes, revpool); |
6b450d1d | 523 | } else if (wasDir(fs, revnum - 1, key, revpool)) { |
898704e4 TM |
524 | qDebug() << current << "was a directory; ignoring"; |
525 | } else if (change->change_kind == svn_fs_path_change_delete) { | |
526 | qDebug() << current << "is being deleted but I don't know anything about it; ignoring"; | |
527 | } else { | |
528 | qCritical() << current << "did not match any rules; cannot continue"; | |
529 | return EXIT_FAILURE; | |
530 | } | |
531 | ||
532 | return EXIT_SUCCESS; | |
533 | } | |
534 | ||
b4cb5188 | 535 | int SvnRevision::exportDispatch(const char *key, const svn_fs_path_change_t *change, |
898704e4 | 536 | const char *path_from, svn_revnum_t rev_from, |
b4cb5188 TM |
537 | apr_hash_t *changes, const QString ¤t, |
538 | const Rules::Match &rule, apr_pool_t *pool) | |
898704e4 | 539 | { |
b4cb5188 TM |
540 | switch (rule.action) { |
541 | case Rules::Match::Ignore: | |
16024419 | 542 | // ignore rule |
25e6c28e TM |
543 | //qDebug() << " " << qPrintable(current) << "rev" << revnum |
544 | // << "-> ignored (rule" << rule << ")"; | |
16024419 | 545 | return EXIT_SUCCESS; |
b4cb5188 TM |
546 | |
547 | case Rules::Match::Recurse: | |
548 | return recurse(key, change, path_from, rev_from, changes, pool); | |
549 | ||
550 | case Rules::Match::Export: | |
551 | return exportInternal(key, change, path_from, rev_from, current, rule); | |
16024419 | 552 | } |
25f3205a TM |
553 | |
554 | // never reached | |
555 | return EXIT_FAILURE; | |
b4cb5188 | 556 | } |
16024419 | 557 | |
b4cb5188 TM |
558 | int SvnRevision::exportInternal(const char *key, const svn_fs_path_change_t *change, |
559 | const char *path_from, svn_revnum_t rev_from, | |
560 | const QString ¤t, const Rules::Match &rule) | |
561 | { | |
898704e4 TM |
562 | QString svnprefix, repository, branch, path; |
563 | splitPathName(rule, current, &svnprefix, &repository, &branch, &path); | |
564 | ||
565 | printf("."); | |
566 | fflush(stdout); | |
567 | // qDebug() << " " << qPrintable(current) << "rev" << revnum << "->" | |
568 | // << qPrintable(repository) << qPrintable(branch) << qPrintable(path); | |
569 | ||
570 | if (path.isEmpty() && path_from != NULL) { | |
571 | QString previous = QString::fromUtf8(path_from) + '/'; | |
572 | MatchRuleList::ConstIterator prevmatch = | |
16024419 | 573 | findMatchRule(matchRules, rev_from, previous, NoIgnoreRule); |
898704e4 TM |
574 | if (prevmatch != matchRules.constEnd()) { |
575 | QString prevsvnprefix, prevrepository, prevbranch, prevpath; | |
576 | splitPathName(*prevmatch, previous, &prevsvnprefix, &prevrepository, | |
577 | &prevbranch, &prevpath); | |
578 | ||
579 | if (!prevpath.isEmpty()) { | |
580 | qDebug() << qPrintable(current) << "is a partial branch of repository" | |
581 | << qPrintable(prevrepository) << "branch" | |
582 | << qPrintable(prevbranch) << "subdir" | |
583 | << qPrintable(prevpath); | |
584 | } else if (prevrepository != repository) { | |
585 | qWarning() << qPrintable(current) << "rev" << revnum | |
586 | << "is a cross-repository copy (from repository" | |
587 | << qPrintable(prevrepository) << "branch" | |
588 | << qPrintable(prevbranch) << "path" | |
589 | << qPrintable(prevpath) << "rev" << rev_from << ")"; | |
590 | } else if (prevbranch == branch) { | |
591 | // same branch and same repository | |
592 | qDebug() << qPrintable(current) << "rev" << revnum | |
593 | << "is an SVN rename from" | |
594 | << qPrintable(previous) << "rev" << rev_from; | |
595 | return EXIT_SUCCESS; | |
596 | } else { | |
597 | // same repository but not same branch | |
598 | // this means this is a plain branch | |
599 | qDebug() << qPrintable(repository) << ": branch" | |
600 | << qPrintable(branch) << "is branching from" | |
601 | << qPrintable(prevbranch); | |
602 | ||
603 | Repository *repo = repositories.value(repository, 0); | |
604 | if (!repo) { | |
2ddd1758 | 605 | qCritical() << "Rule" << rule |
898704e4 TM |
606 | << "references unknown repository" << repository; |
607 | return EXIT_FAILURE; | |
608 | } | |
609 | ||
610 | repo->createBranch(branch, revnum, prevbranch, rev_from); | |
a5fd311b TM |
611 | if (rule.annotate) { |
612 | // create an annotated tag | |
613 | fetchRevProps(); | |
614 | repo->createAnnotatedTag(branch, svnprefix, revnum, authorident, | |
615 | epoch, log); | |
616 | } | |
844c7a90 | 617 | return EXIT_SUCCESS; |
898704e4 TM |
618 | } |
619 | } | |
620 | } | |
621 | ||
b8549b90 | 622 | Repository::Transaction *txn = transactions.value(repository + branch, 0); |
898704e4 TM |
623 | if (!txn) { |
624 | Repository *repo = repositories.value(repository, 0); | |
625 | if (!repo) { | |
2ddd1758 | 626 | qCritical() << "Rule" << rule |
898704e4 TM |
627 | << "references unknown repository" << repository; |
628 | return EXIT_FAILURE; | |
629 | } | |
630 | ||
631 | txn = repo->newTransaction(branch, svnprefix, revnum); | |
632 | if (!txn) | |
633 | return EXIT_FAILURE; | |
634 | ||
b8549b90 | 635 | transactions.insert(repository + branch, txn); |
898704e4 TM |
636 | } |
637 | ||
6b450d1d | 638 | if (change->change_kind == svn_fs_path_change_delete) { |
898704e4 | 639 | txn->deleteFile(path); |
6b450d1d | 640 | } else if (!current.endsWith('/')) { |
898704e4 | 641 | dumpBlob(txn, fs_root, key, path, pool); |
6b450d1d | 642 | } else { |
5871af71 TM |
643 | QString pathNoSlash = path; |
644 | pathNoSlash.chop(1); | |
645 | txn->deleteFile(pathNoSlash); | |
898704e4 | 646 | recursiveDumpDir(txn, fs_root, key, path, pool); |
6b450d1d | 647 | } |
898704e4 TM |
648 | |
649 | return EXIT_SUCCESS; | |
650 | } | |
651 | ||
652 | int SvnRevision::recurse(const char *path, const svn_fs_path_change_t *change, | |
653 | const char *path_from, svn_revnum_t rev_from, | |
8ca4b631 | 654 | apr_hash_t *changes, apr_pool_t *pool) |
898704e4 TM |
655 | { |
656 | // get the dir listing | |
657 | apr_hash_t *entries; | |
658 | SVN_ERR(svn_fs_dir_entries(&entries, fs_root, path, pool)); | |
659 | ||
660 | AprAutoPool dirpool(pool); | |
661 | for (apr_hash_index_t *i = apr_hash_first(pool, entries); i; i = apr_hash_next(i)) { | |
662 | dirpool.clear(); | |
663 | const void *vkey; | |
664 | void *value; | |
665 | apr_hash_this(i, &vkey, NULL, &value); | |
666 | ||
667 | svn_fs_dirent_t *dirent = reinterpret_cast<svn_fs_dirent_t *>(value); | |
d6da65de TM |
668 | if (dirent->kind != svn_node_dir) |
669 | continue; // not a directory, so can't recurse; skip | |
670 | ||
898704e4 | 671 | QByteArray entry = path + QByteArray("/") + dirent->name; |
6b450d1d TM |
672 | QByteArray entryFrom; |
673 | if (path_from) | |
674 | entryFrom = path_from + QByteArray("/") + dirent->name; | |
898704e4 | 675 | |
8ca4b631 | 676 | // check if this entry is in the changelist for this revision already |
18dfc8c4 TM |
677 | svn_fs_path_change_t *otherchange = |
678 | (svn_fs_path_change_t*)apr_hash_get(changes, entry.constData(), APR_HASH_KEY_STRING); | |
679 | if (otherchange && otherchange->change_kind == svn_fs_path_change_add) { | |
8ca4b631 TM |
680 | qDebug() << entry << "rev" << revnum |
681 | << "is in the change-list, deferring to that one"; | |
682 | continue; | |
683 | } | |
684 | ||
898704e4 TM |
685 | QString current = QString::fromUtf8(entry); |
686 | if (dirent->kind == svn_node_dir) | |
687 | current += '/'; | |
688 | ||
689 | // find the first rule that matches this pathname | |
690 | MatchRuleList::ConstIterator match = findMatchRule(matchRules, revnum, current); | |
691 | if (match != matchRules.constEnd()) { | |
b4cb5188 TM |
692 | if (exportDispatch(entry, change, entryFrom.isNull() ? 0 : entryFrom.constData(), |
693 | rev_from, changes, current, *match, dirpool) == EXIT_FAILURE) | |
898704e4 TM |
694 | return EXIT_FAILURE; |
695 | } else { | |
d4c5541d TM |
696 | qCritical() << current << "rev" << revnum |
697 | << "did not match any rules; cannot continue"; | |
898704e4 TM |
698 | return EXIT_FAILURE; |
699 | } | |
700 | } | |
1c5ce394 TM |
701 | |
702 | return EXIT_SUCCESS; | |
898704e4 | 703 | } |