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
25 #define _LARGEFILE_SUPPORT
26 #define _LARGEFILE64_SUPPORT
37 #include <apr_getopt.h>
38 #include <apr_general.h>
41 #include <svn_pools.h>
42 #include <svn_repos.h>
43 #include <svn_types.h>
48 #include "repository.h"
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;
60 AprAutoPool(const AprAutoPool &);
61 AprAutoPool &operator=(const AprAutoPool &);
63 inline AprAutoPool(apr_pool_t *parent = NULL)
64 { pool = svn_pool_create(parent); }
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; }
76 MatchRuleList matchRules;
77 RepositoryHash repositories;
78 IdentityHash identities;
80 SvnPrivate(const QString &pathToRepository);
82 int youngestRevision();
83 int exportRevision(int revnum);
85 int openRepository(const QString &pathToRepository);
88 AprAutoPool global_pool;
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");
102 static struct Destructor { ~Destructor() { apr_terminate(); } } destructor;
105 Svn::Svn(const QString &pathToRepository)
106 : d(new SvnPrivate(pathToRepository))
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)
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()
154 int SvnPrivate::openRepository(const QString &pathToRepository)
157 SVN_ERR(svn_repos_open(&repos, QFile::encodeName(pathToRepository), global_pool));
158 fs = svn_repos_fs(repos);
163 enum RuleType { AnyRule = 0, NoIgnoreRule = 0x01 };
165 static MatchRuleList::ConstIterator
166 findMatchRule(const MatchRuleList &matchRules, int revnum, const QString ¤t,
167 int ruleMask = AnyRule)
169 MatchRuleList::ConstIterator it = matchRules.constBegin(),
170 end = matchRules.constEnd();
171 for ( ; it != end; ++it) {
172 if (it->minRevision > revnum)
174 if (it->maxRevision != -1 && it->maxRevision < revnum)
176 if (it->action == Rules::Match::Ignore && ruleMask & NoIgnoreRule)
178 if (it->rx.indexIn(current) == 0)
186 static void splitPathName(const Rules::Match &rule, const QString &pathName, QString *svnprefix_p,
187 QString *repository_p, QString *branch_p, QString *path_p)
189 QString svnprefix = pathName;
190 svnprefix.truncate(rule.rx.matchedLength());
192 *svnprefix_p = svnprefix;
195 *repository_p = svnprefix;
196 repository_p->replace(rule.rx, rule.repository);
200 *branch_p = svnprefix;
201 branch_p->replace(rule.rx, rule.branch);
205 *path_p = pathName.mid(svnprefix.length());
208 static int pathMode(svn_fs_root_t *fs_root, const char *pathname, apr_pool_t *pool)
210 svn_string_t *propvalue;
211 SVN_ERR(svn_fs_node_prop(&propvalue, fs_root, pathname, "svn:executable", pool));
216 // maybe it's a symlink?
217 SVN_ERR(svn_fs_node_prop(&propvalue, fs_root, pathname, "svn:special", pool));
218 if (propvalue && strcmp(propvalue->data, "symlink") == 0)
224 svn_error_t *QIODevice_write(void *baton, const char *data, apr_size_t *len)
226 QIODevice *device = reinterpret_cast<QIODevice *>(baton);
227 device->write(data, *len);
229 if (device->bytesToWrite() > 16384)
230 device->waitForBytesWritten(0);
234 static svn_stream_t *streamForDevice(QIODevice *device, apr_pool_t *pool)
236 svn_stream_t *stream = svn_stream_create(device, pool);
237 svn_stream_set_write(stream, QIODevice_write);
242 static int dumpBlob(Repository::Transaction *txn, svn_fs_root_t *fs_root,
243 const char *pathname, const QString &finalPathName, apr_pool_t *pool)
245 AprAutoPool dumppool(pool);
247 int mode = pathMode(fs_root, pathname, dumppool);
249 svn_filesize_t stream_length;
251 SVN_ERR(svn_fs_file_length(&stream_length, fs_root, pathname, dumppool));
252 QIODevice *io = txn->addFile(finalPathName, mode, stream_length);
256 svn_stream_t *in_stream, *out_stream;
257 SVN_ERR(svn_fs_file_contents(&in_stream, fs_root, pathname, dumppool));
259 // open a generic svn_stream_t for the QIODevice
260 out_stream = streamForDevice(io, dumppool);
261 SVN_ERR(svn_stream_copy(in_stream, out_stream, dumppool));
263 // print an ending newline
270 static int recursiveDumpDir(Repository::Transaction *txn, svn_fs_root_t *fs_root,
271 const QByteArray &pathname, const QString &finalPathName,
274 // get the dir listing
276 SVN_ERR(svn_fs_dir_entries(&entries, fs_root, pathname, pool));
277 AprAutoPool dirpool(pool);
279 for (apr_hash_index_t *i = apr_hash_first(pool, entries); i; i = apr_hash_next(i)) {
283 apr_hash_this(i, &vkey, NULL, &value);
285 svn_fs_dirent_t *dirent = reinterpret_cast<svn_fs_dirent_t *>(value);
286 QByteArray entryName = pathname + '/' + dirent->name;
287 QString entryFinalName;
288 if (finalPathName.isEmpty())
289 entryFinalName = dirent->name;
291 entryFinalName = finalPathName + '/' + dirent->name;
293 if (dirent->kind == svn_node_dir) {
294 if (recursiveDumpDir(txn, fs_root, entryName, entryFinalName, dirpool) == EXIT_FAILURE)
296 } else if (dirent->kind == svn_node_file) {
299 if (dumpBlob(txn, fs_root, entryName, entryFinalName, dirpool) == EXIT_FAILURE)
307 static bool wasDir(svn_fs_t *fs, int revnum, const char *pathname, apr_pool_t *pool)
309 AprAutoPool subpool(pool);
310 svn_fs_root_t *fs_root;
311 if (svn_fs_revision_root(&fs_root, fs, revnum, subpool) != SVN_NO_ERROR)
314 svn_boolean_t is_dir;
315 if (svn_fs_is_dir(&is_dir, fs_root, pathname, subpool) != SVN_NO_ERROR)
321 time_t get_epoch(char *svn_date)
324 memset(&tm, 0, sizeof tm);
325 QByteArray date(svn_date, strlen(svn_date) - 8);
326 strptime(date, "%Y-%m-%dT%H:%M:%S", &tm);
334 QHash<QString, Repository::Transaction *> transactions;
335 MatchRuleList matchRules;
336 RepositoryHash repositories;
337 IdentityHash identities;
340 svn_fs_root_t *fs_root;
343 SvnRevision(int revision, svn_fs_t *f, apr_pool_t *parent_pool)
344 : pool(parent_pool), fs(f), fs_root(0), revnum(revision)
350 SVN_ERR(svn_fs_revision_root(&fs_root, fs, revnum, pool));
354 int prepareTransactions();
357 int exportEntry(const char *path, const svn_fs_path_change_t *change);
358 int exportInternal(const char *path, const svn_fs_path_change_t *change,
359 const char *path_from, svn_revnum_t rev_from,
360 const QString ¤t, const Rules::Match &rule);
361 int recurse(const char *path, const svn_fs_path_change_t *change,
362 const char *path_from, svn_revnum_t rev_from, apr_pool_t *pool);
365 int SvnPrivate::exportRevision(int revnum)
367 SvnRevision rev(revnum, fs, global_pool);
368 rev.matchRules = matchRules;
369 rev.repositories = repositories;
370 rev.identities = identities;
372 // open this revision:
373 printf("Exporting revision %d ", revnum);
376 if (rev.open() == EXIT_FAILURE)
379 if (rev.prepareTransactions() == EXIT_FAILURE)
382 if (rev.transactions.isEmpty()) {
383 printf(" nothing to do\n");
384 return EXIT_SUCCESS; // no changes?
387 if (rev.commit() == EXIT_FAILURE)
394 int SvnRevision::prepareTransactions()
396 // find out what was changed in this revision:
398 SVN_ERR(svn_fs_paths_changed(&changes, fs_root, pool));
399 for (apr_hash_index_t *i = apr_hash_first(pool, changes); i; i = apr_hash_next(i)) {
402 apr_hash_this(i, &vkey, NULL, &value);
403 const char *key = reinterpret_cast<const char *>(vkey);
404 svn_fs_path_change_t *change = reinterpret_cast<svn_fs_path_change_t *>(value);
406 if (exportEntry(key, change) == EXIT_FAILURE)
413 int SvnRevision::commit()
415 // now create the commit
416 apr_hash_t *revprops;
417 SVN_ERR(svn_fs_revision_proplist(&revprops, fs, revnum, pool));
418 svn_string_t *svnauthor = (svn_string_t*)apr_hash_get(revprops, "svn:author", APR_HASH_KEY_STRING);
419 svn_string_t *svndate = (svn_string_t*)apr_hash_get(revprops, "svn:date", APR_HASH_KEY_STRING);
420 svn_string_t *svnlog = (svn_string_t*)apr_hash_get(revprops, "svn:log", APR_HASH_KEY_STRING);
422 QByteArray log = (char *)svnlog->data;
423 QByteArray authorident = svnauthor ? identities.value((char *)svnauthor->data) : QByteArray();
424 time_t epoch = get_epoch((char*)svndate->data);
425 if (authorident.isEmpty()) {
426 if (!svnauthor || svn_string_isempty(svnauthor))
427 authorident = "nobody <nobody@localhost>";
429 authorident = svnauthor->data + QByteArray(" <") +
430 svnauthor->data + QByteArray("@localhost>");
433 foreach (Repository::Transaction *txn, transactions) {
434 txn->setAuthor(authorident);
435 txn->setDateTime(epoch);
445 int SvnRevision::exportEntry(const char *key, const svn_fs_path_change_t *change)
447 AprAutoPool revpool(pool.data());
448 QString current = QString::fromUtf8(key);
450 // was this copied from somewhere?
451 svn_revnum_t rev_from;
452 const char *path_from;
453 SVN_ERR(svn_fs_copied_from(&rev_from, &path_from, fs_root, key, revpool));
455 // is this a directory?
456 svn_boolean_t is_dir;
457 SVN_ERR(svn_fs_is_dir(&is_dir, fs_root, key, revpool));
459 if (path_from == NULL) {
460 // no, it's a new directory being added
461 // Git doesn't handle directories, so we don't either
462 //qDebug() << " mkdir ignored:" << key;
467 qDebug() << " " << key << "was copied from" << path_from;
470 // find the first rule that matches this pathname
471 MatchRuleList::ConstIterator match = findMatchRule(matchRules, revnum, current);
472 if (match != matchRules.constEnd()) {
473 const Rules::Match &rule = *match;
474 return exportInternal(key, change, path_from, rev_from, current, rule);
477 if (is_dir && path_from != NULL) {
478 qDebug() << current << "is a copy-with-history, auto-recursing";
479 return recurse(key, change, path_from, rev_from, revpool);
480 } else if (wasDir(fs, revnum - 1, key, revpool)) {
481 qDebug() << current << "was a directory; ignoring";
482 } else if (change->change_kind == svn_fs_path_change_delete) {
483 qDebug() << current << "is being deleted but I don't know anything about it; ignoring";
485 qCritical() << current << "did not match any rules; cannot continue";
492 int SvnRevision::exportInternal(const char *key, const svn_fs_path_change_t *change,
493 const char *path_from, svn_revnum_t rev_from,
494 const QString ¤t, const Rules::Match &rule)
496 if (rule.action == Rules::Match::Ignore) {
498 qDebug() << " " << qPrintable(current) << "rev" << revnum
499 << "-> ignored (rule" << rule << ")";
503 QString svnprefix, repository, branch, path;
504 splitPathName(rule, current, &svnprefix, &repository, &branch, &path);
508 // qDebug() << " " << qPrintable(current) << "rev" << revnum << "->"
509 // << qPrintable(repository) << qPrintable(branch) << qPrintable(path);
511 if (path.isEmpty() && path_from != NULL) {
512 QString previous = QString::fromUtf8(path_from) + '/';
513 MatchRuleList::ConstIterator prevmatch =
514 findMatchRule(matchRules, rev_from, previous, NoIgnoreRule);
515 if (prevmatch != matchRules.constEnd()) {
516 QString prevsvnprefix, prevrepository, prevbranch, prevpath;
517 splitPathName(*prevmatch, previous, &prevsvnprefix, &prevrepository,
518 &prevbranch, &prevpath);
520 if (!prevpath.isEmpty()) {
521 qDebug() << qPrintable(current) << "is a partial branch of repository"
522 << qPrintable(prevrepository) << "branch"
523 << qPrintable(prevbranch) << "subdir"
524 << qPrintable(prevpath);
525 } else if (prevrepository != repository) {
526 qWarning() << qPrintable(current) << "rev" << revnum
527 << "is a cross-repository copy (from repository"
528 << qPrintable(prevrepository) << "branch"
529 << qPrintable(prevbranch) << "path"
530 << qPrintable(prevpath) << "rev" << rev_from << ")";
531 } else if (prevbranch == branch) {
532 // same branch and same repository
533 qDebug() << qPrintable(current) << "rev" << revnum
534 << "is an SVN rename from"
535 << qPrintable(previous) << "rev" << rev_from;
538 // same repository but not same branch
539 // this means this is a plain branch
540 qDebug() << qPrintable(repository) << ": branch"
541 << qPrintable(branch) << "is branching from"
542 << qPrintable(prevbranch);
544 Repository *repo = repositories.value(repository, 0);
546 qCritical() << "Rule" << rule
547 << "references unknown repository" << repository;
551 repo->createBranch(branch, revnum, prevbranch, rev_from);
556 Repository::Transaction *txn = transactions.value(repository, 0);
558 Repository *repo = repositories.value(repository, 0);
560 qCritical() << "Rule" << rule
561 << "references unknown repository" << repository;
565 txn = repo->newTransaction(branch, svnprefix, revnum);
569 transactions.insert(repository, txn);
572 if (change->change_kind == svn_fs_path_change_delete) {
573 txn->deleteFile(path);
574 } else if (!current.endsWith('/')) {
575 dumpBlob(txn, fs_root, key, path, pool);
577 txn->deleteFile(path);
578 recursiveDumpDir(txn, fs_root, key, path, pool);
584 int SvnRevision::recurse(const char *path, const svn_fs_path_change_t *change,
585 const char *path_from, svn_revnum_t rev_from,
588 // get the dir listing
590 SVN_ERR(svn_fs_dir_entries(&entries, fs_root, path, pool));
592 AprAutoPool dirpool(pool);
593 for (apr_hash_index_t *i = apr_hash_first(pool, entries); i; i = apr_hash_next(i)) {
597 apr_hash_this(i, &vkey, NULL, &value);
599 svn_fs_dirent_t *dirent = reinterpret_cast<svn_fs_dirent_t *>(value);
600 QByteArray entry = path + QByteArray("/") + dirent->name;
601 QByteArray entryFrom;
603 entryFrom = path_from + QByteArray("/") + dirent->name;
605 QString current = QString::fromUtf8(entry);
606 if (dirent->kind == svn_node_dir)
609 // find the first rule that matches this pathname
610 MatchRuleList::ConstIterator match = findMatchRule(matchRules, revnum, current);
611 if (match != matchRules.constEnd()) {
612 if (exportInternal(entry, change, entryFrom.isNull() ? 0 : entryFrom.constData(),
613 rev_from, current, *match) == EXIT_FAILURE)
616 qCritical() << current << "did not match any rules; cannot continue";