Complement the recurse rule finding (or not finding)
[svn-all-fast-export.git] / src / svn.cpp
bloba32005a023e2cf1ce4f1e6e0de9ae877b66bc8ac
1 /*
2 * Copyright (C) 2007 Thiago Macieira <thiago@kde.org>
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.
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.
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/>.
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
24 #define _XOPEN_SOURCE
25 #define _LARGEFILE_SUPPORT
26 #define _LARGEFILE64_SUPPORT
28 #include "svn.h"
30 #include <unistd.h>
31 #include <string.h>
32 #include <stdio.h>
33 #include <time.h>
34 #include <unistd.h>
36 #include <apr_lib.h>
37 #include <apr_getopt.h>
38 #include <apr_general.h>
40 #include <svn_fs.h>
41 #include <svn_pools.h>
42 #include <svn_repos.h>
43 #include <svn_types.h>
45 #include <QFile>
46 #include <QDebug>
48 #include "repository.h"
50 #undef SVN_ERR
51 #define SVN_ERR(expr) SVN_INT_ERR(expr)
53 typedef QList<Rules::Match> MatchRuleList;
54 typedef QHash<QString, Repository *> RepositoryHash;
55 typedef QHash<QByteArray, QByteArray> IdentityHash;
57 class AprAutoPool
59 apr_pool_t *pool;
60 AprAutoPool(const AprAutoPool &);
61 AprAutoPool &operator=(const AprAutoPool &);
62 public:
63 inline AprAutoPool(apr_pool_t *parent = NULL)
64 { pool = svn_pool_create(parent); }
65 inline ~AprAutoPool()
66 { svn_pool_destroy(pool); }
68 inline void clear() { svn_pool_clear(pool); }
69 inline apr_pool_t *data() const { return pool; }
70 inline operator apr_pool_t *() const { return pool; }
73 class SvnPrivate
75 public:
76 MatchRuleList matchRules;
77 RepositoryHash repositories;
78 IdentityHash identities;
80 SvnPrivate(const QString &pathToRepository);
81 ~SvnPrivate();
82 int youngestRevision();
83 int exportRevision(int revnum);
85 int openRepository(const QString &pathToRepository);
87 private:
88 AprAutoPool global_pool;
89 svn_fs_t *fs;
90 svn_revnum_t youngest_rev;
93 void Svn::initialize()
95 // initialize APR or exit
96 if (apr_initialize() != APR_SUCCESS) {
97 fprintf(stderr, "You lose at apr_initialize().\n");
98 exit(1);
101 // static destructor
102 static struct Destructor { ~Destructor() { apr_terminate(); } } destructor;
105 Svn::Svn(const QString &pathToRepository)
106 : d(new SvnPrivate(pathToRepository))
110 Svn::~Svn()
112 delete d;
115 void Svn::setMatchRules(const MatchRuleList &matchRules)
117 d->matchRules = matchRules;
120 void Svn::setRepositories(const RepositoryHash &repositories)
122 d->repositories = repositories;
125 int Svn::youngestRevision()
127 return d->youngestRevision();
130 bool Svn::exportRevision(int revnum)
132 return d->exportRevision(revnum) == EXIT_SUCCESS;
135 SvnPrivate::SvnPrivate(const QString &pathToRepository)
136 : global_pool(NULL)
138 openRepository(pathToRepository);
140 // get the youngest revision
141 svn_fs_youngest_rev(&youngest_rev, fs, global_pool);
144 SvnPrivate::~SvnPrivate()
146 svn_pool_destroy(global_pool);
149 int SvnPrivate::youngestRevision()
151 return youngest_rev;
154 int SvnPrivate::openRepository(const QString &pathToRepository)
156 svn_repos_t *repos;
157 SVN_ERR(svn_repos_open(&repos, QFile::encodeName(pathToRepository), global_pool));
158 fs = svn_repos_fs(repos);
160 return EXIT_SUCCESS;
163 enum RuleType { AnyRule = 0, NoIgnoreRule = 0x01, NoRecurseRule = 0x02 };
165 static MatchRuleList::ConstIterator
166 findMatchRule(const MatchRuleList &matchRules, int revnum, const QString &current,
167 int ruleMask = AnyRule)
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;
176 if (it->action == Rules::Match::Ignore && ruleMask & NoIgnoreRule)
177 continue;
178 if (it->action == Rules::Match::Recurse && ruleMask & NoRecurseRule)
179 continue;
180 if (it->rx.indexIn(current) == 0)
181 return it;
184 // no match
185 return end;
188 static void splitPathName(const Rules::Match &rule, const QString &pathName, QString *svnprefix_p,
189 QString *repository_p, QString *branch_p, QString *path_p)
191 QString svnprefix = pathName;
192 svnprefix.truncate(rule.rx.matchedLength());
193 if (svnprefix_p)
194 *svnprefix_p = svnprefix;
196 if (repository_p) {
197 *repository_p = svnprefix;
198 repository_p->replace(rule.rx, rule.repository);
201 if (branch_p) {
202 *branch_p = svnprefix;
203 branch_p->replace(rule.rx, rule.branch);
206 if (path_p)
207 *path_p = pathName.mid(svnprefix.length());
210 static int pathMode(svn_fs_root_t *fs_root, const char *pathname, apr_pool_t *pool)
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;
218 // maybe it's a symlink?
219 SVN_ERR(svn_fs_node_prop(&propvalue, fs_root, pathname, "svn:special", pool));
220 if (propvalue && strcmp(propvalue->data, "symlink") == 0)
221 mode = 0120000;
223 return mode;
226 svn_error_t *QIODevice_write(void *baton, const char *data, apr_size_t *len)
228 QIODevice *device = reinterpret_cast<QIODevice *>(baton);
229 device->write(data, *len);
231 if (device->bytesToWrite() > 16384)
232 device->waitForBytesWritten(0);
233 return SVN_NO_ERROR;
236 static svn_stream_t *streamForDevice(QIODevice *device, apr_pool_t *pool)
238 svn_stream_t *stream = svn_stream_create(device, pool);
239 svn_stream_set_write(stream, QIODevice_write);
241 return stream;
244 static int dumpBlob(Repository::Transaction *txn, svn_fs_root_t *fs_root,
245 const char *pathname, const QString &finalPathName, apr_pool_t *pool)
247 AprAutoPool dumppool(pool);
248 // what type is it?
249 int mode = pathMode(fs_root, pathname, dumppool);
251 svn_filesize_t stream_length;
253 SVN_ERR(svn_fs_file_length(&stream_length, fs_root, pathname, dumppool));
254 QIODevice *io = txn->addFile(finalPathName, mode, stream_length);
256 #ifndef DRY_RUN
257 // open the file
258 svn_stream_t *in_stream, *out_stream;
259 SVN_ERR(svn_fs_file_contents(&in_stream, fs_root, pathname, dumppool));
261 // open a generic svn_stream_t for the QIODevice
262 out_stream = streamForDevice(io, dumppool);
263 SVN_ERR(svn_stream_copy(in_stream, out_stream, dumppool));
265 // print an ending newline
266 io->putChar('\n');
267 #endif
269 return EXIT_SUCCESS;
272 static int recursiveDumpDir(Repository::Transaction *txn, svn_fs_root_t *fs_root,
273 const QByteArray &pathname, const QString &finalPathName,
274 apr_pool_t *pool)
276 // get the dir listing
277 apr_hash_t *entries;
278 SVN_ERR(svn_fs_dir_entries(&entries, fs_root, pathname, pool));
279 AprAutoPool dirpool(pool);
281 for (apr_hash_index_t *i = apr_hash_first(pool, entries); i; i = apr_hash_next(i)) {
282 dirpool.clear();
283 const void *vkey;
284 void *value;
285 apr_hash_this(i, &vkey, NULL, &value);
287 svn_fs_dirent_t *dirent = reinterpret_cast<svn_fs_dirent_t *>(value);
288 QByteArray entryName = pathname + '/' + dirent->name;
289 QString entryFinalName = finalPathName + dirent->name;
291 if (dirent->kind == svn_node_dir) {
292 entryFinalName += '/';
293 if (recursiveDumpDir(txn, fs_root, entryName, entryFinalName, dirpool) == EXIT_FAILURE)
294 return EXIT_FAILURE;
295 } else if (dirent->kind == svn_node_file) {
296 printf("+");
297 fflush(stdout);
298 if (dumpBlob(txn, fs_root, entryName, entryFinalName, dirpool) == EXIT_FAILURE)
299 return EXIT_FAILURE;
303 return EXIT_SUCCESS;
306 static bool wasDir(svn_fs_t *fs, int revnum, const char *pathname, apr_pool_t *pool)
308 AprAutoPool subpool(pool);
309 svn_fs_root_t *fs_root;
310 if (svn_fs_revision_root(&fs_root, fs, revnum, subpool) != SVN_NO_ERROR)
311 return false;
313 svn_boolean_t is_dir;
314 if (svn_fs_is_dir(&is_dir, fs_root, pathname, subpool) != SVN_NO_ERROR)
315 return false;
317 return is_dir;
320 time_t get_epoch(char *svn_date)
322 struct tm tm;
323 memset(&tm, 0, sizeof tm);
324 QByteArray date(svn_date, strlen(svn_date) - 8);
325 strptime(date, "%Y-%m-%dT%H:%M:%S", &tm);
326 return mktime(&tm);
329 class SvnRevision
331 public:
332 AprAutoPool pool;
333 QHash<QString, Repository::Transaction *> transactions;
334 MatchRuleList matchRules;
335 RepositoryHash repositories;
336 IdentityHash identities;
338 svn_fs_t *fs;
339 svn_fs_root_t *fs_root;
340 int revnum;
342 SvnRevision(int revision, svn_fs_t *f, apr_pool_t *parent_pool)
343 : pool(parent_pool), fs(f), fs_root(0), revnum(revision)
347 int open()
349 SVN_ERR(svn_fs_revision_root(&fs_root, fs, revnum, pool));
350 return EXIT_SUCCESS;
353 int prepareTransactions();
354 int commit();
356 int exportEntry(const char *path, const svn_fs_path_change_t *change, apr_hash_t *changes);
357 int exportDispatch(const char *path, const svn_fs_path_change_t *change,
358 const char *path_from, svn_revnum_t rev_from,
359 apr_hash_t *changes, const QString &current, const Rules::Match &rule,
360 apr_pool_t *pool);
361 int exportInternal(const char *path, const svn_fs_path_change_t *change,
362 const char *path_from, svn_revnum_t rev_from,
363 const QString &current, const Rules::Match &rule);
364 int recurse(const char *path, const svn_fs_path_change_t *change,
365 const char *path_from, svn_revnum_t rev_from,
366 apr_hash_t *changes, apr_pool_t *pool);
369 int SvnPrivate::exportRevision(int revnum)
371 SvnRevision rev(revnum, fs, global_pool);
372 rev.matchRules = matchRules;
373 rev.repositories = repositories;
374 rev.identities = identities;
376 // open this revision:
377 printf("Exporting revision %d ", revnum);
378 fflush(stdout);
380 if (rev.open() == EXIT_FAILURE)
381 return EXIT_FAILURE;
383 if (rev.prepareTransactions() == EXIT_FAILURE)
384 return EXIT_FAILURE;
386 if (rev.transactions.isEmpty()) {
387 printf(" nothing to do\n");
388 return EXIT_SUCCESS; // no changes?
391 if (rev.commit() == EXIT_FAILURE)
392 return EXIT_FAILURE;
394 printf(" done\n");
395 return EXIT_SUCCESS;
398 int SvnRevision::prepareTransactions()
400 // find out what was changed in this revision:
401 apr_hash_t *changes;
402 SVN_ERR(svn_fs_paths_changed(&changes, fs_root, pool));
403 for (apr_hash_index_t *i = apr_hash_first(pool, changes); i; i = apr_hash_next(i)) {
404 const void *vkey;
405 void *value;
406 apr_hash_this(i, &vkey, NULL, &value);
407 const char *key = reinterpret_cast<const char *>(vkey);
408 svn_fs_path_change_t *change = reinterpret_cast<svn_fs_path_change_t *>(value);
410 if (exportEntry(key, change, changes) == EXIT_FAILURE)
411 return EXIT_FAILURE;
414 return EXIT_SUCCESS;
417 int SvnRevision::commit()
419 // now create the commit
420 apr_hash_t *revprops;
421 SVN_ERR(svn_fs_revision_proplist(&revprops, fs, revnum, pool));
422 svn_string_t *svnauthor = (svn_string_t*)apr_hash_get(revprops, "svn:author", APR_HASH_KEY_STRING);
423 svn_string_t *svndate = (svn_string_t*)apr_hash_get(revprops, "svn:date", APR_HASH_KEY_STRING);
424 svn_string_t *svnlog = (svn_string_t*)apr_hash_get(revprops, "svn:log", APR_HASH_KEY_STRING);
426 QByteArray log = (char *)svnlog->data;
427 QByteArray authorident = svnauthor ? identities.value((char *)svnauthor->data) : QByteArray();
428 time_t epoch = get_epoch((char*)svndate->data);
429 if (authorident.isEmpty()) {
430 if (!svnauthor || svn_string_isempty(svnauthor))
431 authorident = "nobody <nobody@localhost>";
432 else
433 authorident = svnauthor->data + QByteArray(" <") +
434 svnauthor->data + QByteArray("@localhost>");
437 foreach (Repository::Transaction *txn, transactions) {
438 txn->setAuthor(authorident);
439 txn->setDateTime(epoch);
440 txn->setLog(log);
442 txn->commit();
443 delete txn;
446 return EXIT_SUCCESS;
449 int SvnRevision::exportEntry(const char *key, const svn_fs_path_change_t *change,
450 apr_hash_t *changes)
452 AprAutoPool revpool(pool.data());
453 QString current = QString::fromUtf8(key);
455 // was this copied from somewhere?
456 svn_revnum_t rev_from;
457 const char *path_from;
458 SVN_ERR(svn_fs_copied_from(&rev_from, &path_from, fs_root, key, revpool));
460 // is this a directory?
461 svn_boolean_t is_dir;
462 SVN_ERR(svn_fs_is_dir(&is_dir, fs_root, key, revpool));
463 if (is_dir) {
464 if (path_from == NULL) {
465 // no, it's a new directory being added
466 // Git doesn't handle directories, so we don't either
467 //qDebug() << " mkdir ignored:" << key;
468 return EXIT_SUCCESS;
471 current += '/';
472 qDebug() << " " << key << "was copied from" << path_from;
475 // find the first rule that matches this pathname
476 MatchRuleList::ConstIterator match = findMatchRule(matchRules, revnum, current);
477 if (match != matchRules.constEnd()) {
478 const Rules::Match &rule = *match;
479 return exportDispatch(key, change, path_from, rev_from, changes, current, rule, revpool);
482 if (is_dir && path_from != NULL) {
483 qDebug() << current << "is a copy-with-history, auto-recursing";
484 return recurse(key, change, path_from, rev_from, changes, revpool);
485 } else if (wasDir(fs, revnum - 1, key, revpool)) {
486 qDebug() << current << "was a directory; ignoring";
487 } else if (change->change_kind == svn_fs_path_change_delete) {
488 qDebug() << current << "is being deleted but I don't know anything about it; ignoring";
489 } else {
490 qCritical() << current << "did not match any rules; cannot continue";
491 return EXIT_FAILURE;
494 return EXIT_SUCCESS;
497 int SvnRevision::exportDispatch(const char *key, const svn_fs_path_change_t *change,
498 const char *path_from, svn_revnum_t rev_from,
499 apr_hash_t *changes, const QString &current,
500 const Rules::Match &rule, apr_pool_t *pool)
502 switch (rule.action) {
503 case Rules::Match::Ignore:
504 // ignore rule
505 qDebug() << " " << qPrintable(current) << "rev" << revnum
506 << "-> ignored (rule" << rule << ")";
507 return EXIT_SUCCESS;
509 case Rules::Match::Recurse:
510 return recurse(key, change, path_from, rev_from, changes, pool);
512 case Rules::Match::Export:
513 return exportInternal(key, change, path_from, rev_from, current, rule);
517 int SvnRevision::exportInternal(const char *key, const svn_fs_path_change_t *change,
518 const char *path_from, svn_revnum_t rev_from,
519 const QString &current, const Rules::Match &rule)
521 QString svnprefix, repository, branch, path;
522 splitPathName(rule, current, &svnprefix, &repository, &branch, &path);
524 printf(".");
525 fflush(stdout);
526 // qDebug() << " " << qPrintable(current) << "rev" << revnum << "->"
527 // << qPrintable(repository) << qPrintable(branch) << qPrintable(path);
529 if (path.isEmpty() && path_from != NULL) {
530 QString previous = QString::fromUtf8(path_from) + '/';
531 MatchRuleList::ConstIterator prevmatch =
532 findMatchRule(matchRules, rev_from, previous, NoIgnoreRule);
533 if (prevmatch != matchRules.constEnd()) {
534 QString prevsvnprefix, prevrepository, prevbranch, prevpath;
535 splitPathName(*prevmatch, previous, &prevsvnprefix, &prevrepository,
536 &prevbranch, &prevpath);
538 if (!prevpath.isEmpty()) {
539 qDebug() << qPrintable(current) << "is a partial branch of repository"
540 << qPrintable(prevrepository) << "branch"
541 << qPrintable(prevbranch) << "subdir"
542 << qPrintable(prevpath);
543 } else if (prevrepository != repository) {
544 qWarning() << qPrintable(current) << "rev" << revnum
545 << "is a cross-repository copy (from repository"
546 << qPrintable(prevrepository) << "branch"
547 << qPrintable(prevbranch) << "path"
548 << qPrintable(prevpath) << "rev" << rev_from << ")";
549 } else if (prevbranch == branch) {
550 // same branch and same repository
551 qDebug() << qPrintable(current) << "rev" << revnum
552 << "is an SVN rename from"
553 << qPrintable(previous) << "rev" << rev_from;
554 return EXIT_SUCCESS;
555 } else {
556 // same repository but not same branch
557 // this means this is a plain branch
558 qDebug() << qPrintable(repository) << ": branch"
559 << qPrintable(branch) << "is branching from"
560 << qPrintable(prevbranch);
562 Repository *repo = repositories.value(repository, 0);
563 if (!repo) {
564 qCritical() << "Rule" << rule
565 << "references unknown repository" << repository;
566 return EXIT_FAILURE;
569 repo->createBranch(branch, revnum, prevbranch, rev_from);
570 return EXIT_SUCCESS;
575 Repository::Transaction *txn = transactions.value(repository + branch, 0);
576 if (!txn) {
577 Repository *repo = repositories.value(repository, 0);
578 if (!repo) {
579 qCritical() << "Rule" << rule
580 << "references unknown repository" << repository;
581 return EXIT_FAILURE;
584 txn = repo->newTransaction(branch, svnprefix, revnum);
585 if (!txn)
586 return EXIT_FAILURE;
588 transactions.insert(repository + branch, txn);
591 if (change->change_kind == svn_fs_path_change_delete) {
592 txn->deleteFile(path);
593 } else if (!current.endsWith('/')) {
594 dumpBlob(txn, fs_root, key, path, pool);
595 } else {
596 QString pathNoSlash = path;
597 pathNoSlash.chop(1);
598 txn->deleteFile(pathNoSlash);
599 recursiveDumpDir(txn, fs_root, key, path, pool);
602 return EXIT_SUCCESS;
605 int SvnRevision::recurse(const char *path, const svn_fs_path_change_t *change,
606 const char *path_from, svn_revnum_t rev_from,
607 apr_hash_t *changes, apr_pool_t *pool)
609 // get the dir listing
610 apr_hash_t *entries;
611 SVN_ERR(svn_fs_dir_entries(&entries, fs_root, path, pool));
613 AprAutoPool dirpool(pool);
614 for (apr_hash_index_t *i = apr_hash_first(pool, entries); i; i = apr_hash_next(i)) {
615 dirpool.clear();
616 const void *vkey;
617 void *value;
618 apr_hash_this(i, &vkey, NULL, &value);
620 svn_fs_dirent_t *dirent = reinterpret_cast<svn_fs_dirent_t *>(value);
621 QByteArray entry = path + QByteArray("/") + dirent->name;
622 QByteArray entryFrom;
623 if (path_from)
624 entryFrom = path_from + QByteArray("/") + dirent->name;
626 // check if this entry is in the changelist for this revision already
627 if (apr_hash_get(changes, entry.constData(), APR_HASH_KEY_STRING)) {
628 qDebug() << entry << "rev" << revnum
629 << "is in the change-list, deferring to that one";
630 continue;
633 QString current = QString::fromUtf8(entry);
634 if (dirent->kind == svn_node_dir)
635 current += '/';
637 // find the first rule that matches this pathname
638 MatchRuleList::ConstIterator match = findMatchRule(matchRules, revnum, current);
639 if (match != matchRules.constEnd()) {
640 if (exportDispatch(entry, change, entryFrom.isNull() ? 0 : entryFrom.constData(),
641 rev_from, changes, current, *match, dirpool) == EXIT_FAILURE)
642 return EXIT_FAILURE;
643 } else {
644 qCritical() << current << "rev" << revnum
645 << "did not match any rules; cannot continue";
646 return EXIT_FAILURE;
650 return EXIT_SUCCESS;