Add missing calls to svn_stream_close().
[svn-all-fast-export.git] / src / svn.cpp
blob5236579600af2babf06a558823d88e3e927d3bb4
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 void Svn::setIdentityMap(const IdentityHash &identityMap)
127 d->identities = identityMap;
130 int Svn::youngestRevision()
132 return d->youngestRevision();
135 bool Svn::exportRevision(int revnum)
137 return d->exportRevision(revnum) == EXIT_SUCCESS;
140 SvnPrivate::SvnPrivate(const QString &pathToRepository)
141 : global_pool(NULL)
143 openRepository(pathToRepository);
145 // get the youngest revision
146 svn_fs_youngest_rev(&youngest_rev, fs, global_pool);
149 SvnPrivate::~SvnPrivate()
151 svn_pool_destroy(global_pool);
154 int SvnPrivate::youngestRevision()
156 return youngest_rev;
159 int SvnPrivate::openRepository(const QString &pathToRepository)
161 svn_repos_t *repos;
162 SVN_ERR(svn_repos_open(&repos, QFile::encodeName(pathToRepository), global_pool));
163 fs = svn_repos_fs(repos);
165 return EXIT_SUCCESS;
168 enum RuleType { AnyRule = 0, NoIgnoreRule = 0x01, NoRecurseRule = 0x02 };
170 static MatchRuleList::ConstIterator
171 findMatchRule(const MatchRuleList &matchRules, int revnum, const QString &current,
172 int ruleMask = AnyRule)
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;
181 if (it->action == Rules::Match::Ignore && ruleMask & NoIgnoreRule)
182 continue;
183 if (it->action == Rules::Match::Recurse && ruleMask & NoRecurseRule)
184 continue;
185 if (it->rx.indexIn(current) == 0)
186 return it;
189 // no match
190 return end;
193 static void splitPathName(const Rules::Match &rule, const QString &pathName, QString *svnprefix_p,
194 QString *repository_p, QString *branch_p, QString *path_p)
196 QString svnprefix = pathName;
197 svnprefix.truncate(rule.rx.matchedLength());
198 if (svnprefix_p)
199 *svnprefix_p = svnprefix;
201 if (repository_p) {
202 *repository_p = svnprefix;
203 repository_p->replace(rule.rx, rule.repository);
206 if (branch_p) {
207 *branch_p = svnprefix;
208 branch_p->replace(rule.rx, rule.branch);
211 if (path_p)
212 *path_p = pathName.mid(svnprefix.length());
215 static int pathMode(svn_fs_root_t *fs_root, const char *pathname, apr_pool_t *pool)
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;
223 // maybe it's a symlink?
224 SVN_ERR(svn_fs_node_prop(&propvalue, fs_root, pathname, "svn:special", pool));
225 if (propvalue && strcmp(propvalue->data, "symlink") == 0)
226 mode = 0120000;
228 return mode;
231 svn_error_t *QIODevice_write(void *baton, const char *data, apr_size_t *len)
233 QIODevice *device = reinterpret_cast<QIODevice *>(baton);
234 device->write(data, *len);
236 while (device->bytesToWrite() > 32*1024) {
237 if (!device->waitForBytesWritten(-1)) {
238 qFatal("Failed to write to process: %s", qPrintable(device->errorString()));
239 return svn_error_createf(APR_EOF, SVN_NO_ERROR, "Failed to write to process: %s",
240 qPrintable(device->errorString()));
243 return SVN_NO_ERROR;
246 static svn_stream_t *streamForDevice(QIODevice *device, apr_pool_t *pool)
248 svn_stream_t *stream = svn_stream_create(device, pool);
249 svn_stream_set_write(stream, QIODevice_write);
251 return stream;
254 static int dumpBlob(Repository::Transaction *txn, svn_fs_root_t *fs_root,
255 const char *pathname, const QString &finalPathName, apr_pool_t *pool)
257 AprAutoPool dumppool(pool);
258 // what type is it?
259 int mode = pathMode(fs_root, pathname, dumppool);
261 svn_filesize_t stream_length;
263 SVN_ERR(svn_fs_file_length(&stream_length, fs_root, pathname, dumppool));
264 QIODevice *io = txn->addFile(finalPathName, mode, stream_length);
266 #ifndef DRY_RUN
267 // open the file
268 svn_stream_t *in_stream, *out_stream;
269 SVN_ERR(svn_fs_file_contents(&in_stream, fs_root, pathname, dumppool));
271 // open a generic svn_stream_t for the QIODevice
272 out_stream = streamForDevice(io, dumppool);
273 SVN_ERR(svn_stream_copy(in_stream, out_stream, dumppool));
274 svn_stream_close(out_stream);
275 svn_stream_close(in_stream);
277 // print an ending newline
278 io->putChar('\n');
279 #endif
281 return EXIT_SUCCESS;
284 static int recursiveDumpDir(Repository::Transaction *txn, svn_fs_root_t *fs_root,
285 const QByteArray &pathname, const QString &finalPathName,
286 apr_pool_t *pool)
288 // get the dir listing
289 apr_hash_t *entries;
290 SVN_ERR(svn_fs_dir_entries(&entries, fs_root, pathname, pool));
291 AprAutoPool dirpool(pool);
293 for (apr_hash_index_t *i = apr_hash_first(pool, entries); i; i = apr_hash_next(i)) {
294 dirpool.clear();
295 const void *vkey;
296 void *value;
297 apr_hash_this(i, &vkey, NULL, &value);
299 svn_fs_dirent_t *dirent = reinterpret_cast<svn_fs_dirent_t *>(value);
300 QByteArray entryName = pathname + '/' + dirent->name;
301 QString entryFinalName = finalPathName + dirent->name;
303 if (dirent->kind == svn_node_dir) {
304 entryFinalName += '/';
305 if (recursiveDumpDir(txn, fs_root, entryName, entryFinalName, dirpool) == EXIT_FAILURE)
306 return EXIT_FAILURE;
307 } else if (dirent->kind == svn_node_file) {
308 printf("+");
309 fflush(stdout);
310 if (dumpBlob(txn, fs_root, entryName, entryFinalName, dirpool) == EXIT_FAILURE)
311 return EXIT_FAILURE;
315 return EXIT_SUCCESS;
318 static bool wasDir(svn_fs_t *fs, int revnum, const char *pathname, apr_pool_t *pool)
320 AprAutoPool subpool(pool);
321 svn_fs_root_t *fs_root;
322 if (svn_fs_revision_root(&fs_root, fs, revnum, subpool) != SVN_NO_ERROR)
323 return false;
325 svn_boolean_t is_dir;
326 if (svn_fs_is_dir(&is_dir, fs_root, pathname, subpool) != SVN_NO_ERROR)
327 return false;
329 return is_dir;
332 time_t get_epoch(char *svn_date)
334 struct tm tm;
335 memset(&tm, 0, sizeof tm);
336 QByteArray date(svn_date, strlen(svn_date) - 8);
337 strptime(date, "%Y-%m-%dT%H:%M:%S", &tm);
338 return mktime(&tm);
341 class SvnRevision
343 public:
344 AprAutoPool pool;
345 QHash<QString, Repository::Transaction *> transactions;
346 MatchRuleList matchRules;
347 RepositoryHash repositories;
348 IdentityHash identities;
350 svn_fs_t *fs;
351 svn_fs_root_t *fs_root;
352 int revnum;
354 SvnRevision(int revision, svn_fs_t *f, apr_pool_t *parent_pool)
355 : pool(parent_pool), fs(f), fs_root(0), revnum(revision)
359 int open()
361 SVN_ERR(svn_fs_revision_root(&fs_root, fs, revnum, pool));
362 return EXIT_SUCCESS;
365 int prepareTransactions();
366 int commit();
368 int exportEntry(const char *path, const svn_fs_path_change_t *change, apr_hash_t *changes);
369 int exportDispatch(const char *path, const svn_fs_path_change_t *change,
370 const char *path_from, svn_revnum_t rev_from,
371 apr_hash_t *changes, const QString &current, const Rules::Match &rule,
372 apr_pool_t *pool);
373 int exportInternal(const char *path, const svn_fs_path_change_t *change,
374 const char *path_from, svn_revnum_t rev_from,
375 const QString &current, const Rules::Match &rule);
376 int recurse(const char *path, const svn_fs_path_change_t *change,
377 const char *path_from, svn_revnum_t rev_from,
378 apr_hash_t *changes, apr_pool_t *pool);
381 int SvnPrivate::exportRevision(int revnum)
383 SvnRevision rev(revnum, fs, global_pool);
384 rev.matchRules = matchRules;
385 rev.repositories = repositories;
386 rev.identities = identities;
388 // open this revision:
389 printf("Exporting revision %d ", revnum);
390 fflush(stdout);
392 if (rev.open() == EXIT_FAILURE)
393 return EXIT_FAILURE;
395 if (rev.prepareTransactions() == EXIT_FAILURE)
396 return EXIT_FAILURE;
398 if (rev.transactions.isEmpty()) {
399 printf(" nothing to do\n");
400 return EXIT_SUCCESS; // no changes?
403 if (rev.commit() == EXIT_FAILURE)
404 return EXIT_FAILURE;
406 printf(" done\n");
407 return EXIT_SUCCESS;
410 int SvnRevision::prepareTransactions()
412 // find out what was changed in this revision:
413 apr_hash_t *changes;
414 SVN_ERR(svn_fs_paths_changed(&changes, fs_root, pool));
415 for (apr_hash_index_t *i = apr_hash_first(pool, changes); i; i = apr_hash_next(i)) {
416 const void *vkey;
417 void *value;
418 apr_hash_this(i, &vkey, NULL, &value);
419 const char *key = reinterpret_cast<const char *>(vkey);
420 svn_fs_path_change_t *change = reinterpret_cast<svn_fs_path_change_t *>(value);
422 if (exportEntry(key, change, changes) == EXIT_FAILURE)
423 return EXIT_FAILURE;
426 return EXIT_SUCCESS;
429 int SvnRevision::commit()
431 // now create the commit
432 apr_hash_t *revprops;
433 SVN_ERR(svn_fs_revision_proplist(&revprops, fs, revnum, pool));
434 svn_string_t *svnauthor = (svn_string_t*)apr_hash_get(revprops, "svn:author", APR_HASH_KEY_STRING);
435 svn_string_t *svndate = (svn_string_t*)apr_hash_get(revprops, "svn:date", APR_HASH_KEY_STRING);
436 svn_string_t *svnlog = (svn_string_t*)apr_hash_get(revprops, "svn:log", APR_HASH_KEY_STRING);
438 QByteArray log = (char *)svnlog->data;
439 QByteArray authorident = svnauthor ? identities.value((char *)svnauthor->data) : QByteArray();
440 time_t epoch = get_epoch((char*)svndate->data);
441 if (authorident.isEmpty()) {
442 if (!svnauthor || svn_string_isempty(svnauthor))
443 authorident = "nobody <nobody@localhost>";
444 else
445 authorident = svnauthor->data + QByteArray(" <") +
446 svnauthor->data + QByteArray("@localhost>");
449 foreach (Repository::Transaction *txn, transactions) {
450 txn->setAuthor(authorident);
451 txn->setDateTime(epoch);
452 txn->setLog(log);
454 txn->commit();
455 delete txn;
458 return EXIT_SUCCESS;
461 int SvnRevision::exportEntry(const char *key, const svn_fs_path_change_t *change,
462 apr_hash_t *changes)
464 AprAutoPool revpool(pool.data());
465 QString current = QString::fromUtf8(key);
467 // was this copied from somewhere?
468 svn_revnum_t rev_from;
469 const char *path_from;
470 SVN_ERR(svn_fs_copied_from(&rev_from, &path_from, fs_root, key, revpool));
472 // is this a directory?
473 svn_boolean_t is_dir;
474 SVN_ERR(svn_fs_is_dir(&is_dir, fs_root, key, revpool));
475 if (is_dir) {
476 if (path_from == NULL) {
477 // no, it's a new directory being added
478 // Git doesn't handle directories, so we don't either
479 //qDebug() << " mkdir ignored:" << key;
480 return EXIT_SUCCESS;
483 current += '/';
484 qDebug() << " " << key << "was copied from" << path_from << "rev" << rev_from;
487 // find the first rule that matches this pathname
488 MatchRuleList::ConstIterator match = findMatchRule(matchRules, revnum, current);
489 if (match != matchRules.constEnd()) {
490 const Rules::Match &rule = *match;
491 return exportDispatch(key, change, path_from, rev_from, changes, current, rule, revpool);
494 if (is_dir && path_from != NULL) {
495 qDebug() << current << "is a copy-with-history, auto-recursing";
496 return recurse(key, change, path_from, rev_from, changes, revpool);
497 } else if (wasDir(fs, revnum - 1, key, revpool)) {
498 qDebug() << current << "was a directory; ignoring";
499 } else if (change->change_kind == svn_fs_path_change_delete) {
500 qDebug() << current << "is being deleted but I don't know anything about it; ignoring";
501 } else {
502 qCritical() << current << "did not match any rules; cannot continue";
503 return EXIT_FAILURE;
506 return EXIT_SUCCESS;
509 int SvnRevision::exportDispatch(const char *key, const svn_fs_path_change_t *change,
510 const char *path_from, svn_revnum_t rev_from,
511 apr_hash_t *changes, const QString &current,
512 const Rules::Match &rule, apr_pool_t *pool)
514 switch (rule.action) {
515 case Rules::Match::Ignore:
516 // ignore rule
517 //qDebug() << " " << qPrintable(current) << "rev" << revnum
518 // << "-> ignored (rule" << rule << ")";
519 return EXIT_SUCCESS;
521 case Rules::Match::Recurse:
522 return recurse(key, change, path_from, rev_from, changes, pool);
524 case Rules::Match::Export:
525 return exportInternal(key, change, path_from, rev_from, current, rule);
528 // never reached
529 return EXIT_FAILURE;
532 int SvnRevision::exportInternal(const char *key, const svn_fs_path_change_t *change,
533 const char *path_from, svn_revnum_t rev_from,
534 const QString &current, const Rules::Match &rule)
536 QString svnprefix, repository, branch, path;
537 splitPathName(rule, current, &svnprefix, &repository, &branch, &path);
539 printf(".");
540 fflush(stdout);
541 // qDebug() << " " << qPrintable(current) << "rev" << revnum << "->"
542 // << qPrintable(repository) << qPrintable(branch) << qPrintable(path);
544 if (path.isEmpty() && path_from != NULL) {
545 QString previous = QString::fromUtf8(path_from) + '/';
546 MatchRuleList::ConstIterator prevmatch =
547 findMatchRule(matchRules, rev_from, previous, NoIgnoreRule);
548 if (prevmatch != matchRules.constEnd()) {
549 QString prevsvnprefix, prevrepository, prevbranch, prevpath;
550 splitPathName(*prevmatch, previous, &prevsvnprefix, &prevrepository,
551 &prevbranch, &prevpath);
553 if (!prevpath.isEmpty()) {
554 qDebug() << qPrintable(current) << "is a partial branch of repository"
555 << qPrintable(prevrepository) << "branch"
556 << qPrintable(prevbranch) << "subdir"
557 << qPrintable(prevpath);
558 } else if (prevrepository != repository) {
559 qWarning() << qPrintable(current) << "rev" << revnum
560 << "is a cross-repository copy (from repository"
561 << qPrintable(prevrepository) << "branch"
562 << qPrintable(prevbranch) << "path"
563 << qPrintable(prevpath) << "rev" << rev_from << ")";
564 } else if (prevbranch == branch) {
565 // same branch and same repository
566 qDebug() << qPrintable(current) << "rev" << revnum
567 << "is an SVN rename from"
568 << qPrintable(previous) << "rev" << rev_from;
569 return EXIT_SUCCESS;
570 } else {
571 // same repository but not same branch
572 // this means this is a plain branch
573 qDebug() << qPrintable(repository) << ": branch"
574 << qPrintable(branch) << "is branching from"
575 << qPrintable(prevbranch);
577 Repository *repo = repositories.value(repository, 0);
578 if (!repo) {
579 qCritical() << "Rule" << rule
580 << "references unknown repository" << repository;
581 return EXIT_FAILURE;
584 repo->createBranch(branch, revnum, prevbranch, rev_from);
585 return EXIT_SUCCESS;
590 Repository::Transaction *txn = transactions.value(repository + branch, 0);
591 if (!txn) {
592 Repository *repo = repositories.value(repository, 0);
593 if (!repo) {
594 qCritical() << "Rule" << rule
595 << "references unknown repository" << repository;
596 return EXIT_FAILURE;
599 txn = repo->newTransaction(branch, svnprefix, revnum);
600 if (!txn)
601 return EXIT_FAILURE;
603 transactions.insert(repository + branch, txn);
606 if (change->change_kind == svn_fs_path_change_delete) {
607 txn->deleteFile(path);
608 } else if (!current.endsWith('/')) {
609 dumpBlob(txn, fs_root, key, path, pool);
610 } else {
611 QString pathNoSlash = path;
612 pathNoSlash.chop(1);
613 txn->deleteFile(pathNoSlash);
614 recursiveDumpDir(txn, fs_root, key, path, pool);
617 return EXIT_SUCCESS;
620 int SvnRevision::recurse(const char *path, const svn_fs_path_change_t *change,
621 const char *path_from, svn_revnum_t rev_from,
622 apr_hash_t *changes, apr_pool_t *pool)
624 // get the dir listing
625 apr_hash_t *entries;
626 SVN_ERR(svn_fs_dir_entries(&entries, fs_root, path, pool));
628 AprAutoPool dirpool(pool);
629 for (apr_hash_index_t *i = apr_hash_first(pool, entries); i; i = apr_hash_next(i)) {
630 dirpool.clear();
631 const void *vkey;
632 void *value;
633 apr_hash_this(i, &vkey, NULL, &value);
635 svn_fs_dirent_t *dirent = reinterpret_cast<svn_fs_dirent_t *>(value);
636 if (dirent->kind != svn_node_dir)
637 continue; // not a directory, so can't recurse; skip
639 QByteArray entry = path + QByteArray("/") + dirent->name;
640 QByteArray entryFrom;
641 if (path_from)
642 entryFrom = path_from + QByteArray("/") + dirent->name;
644 // check if this entry is in the changelist for this revision already
645 if (apr_hash_get(changes, entry.constData(), APR_HASH_KEY_STRING)) {
646 qDebug() << entry << "rev" << revnum
647 << "is in the change-list, deferring to that one";
648 continue;
651 QString current = QString::fromUtf8(entry);
652 if (dirent->kind == svn_node_dir)
653 current += '/';
655 // find the first rule that matches this pathname
656 MatchRuleList::ConstIterator match = findMatchRule(matchRules, revnum, current);
657 if (match != matchRules.constEnd()) {
658 if (exportDispatch(entry, change, entryFrom.isNull() ? 0 : entryFrom.constData(),
659 rev_from, changes, current, *match, dirpool) == EXIT_FAILURE)
660 return EXIT_FAILURE;
661 } else {
662 qCritical() << current << "rev" << revnum
663 << "did not match any rules; cannot continue";
664 return EXIT_FAILURE;
668 return EXIT_SUCCESS;