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