]> andersk Git - svn-all-fast-export.git/blob - src/svn.cpp
d97a0e533b7a668d6722cec10f7eb84c16836106
[svn-all-fast-export.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 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
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
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
48 #include "repository.h"
49
50 #undef SVN_ERR
51 #define SVN_ERR(expr) SVN_INT_ERR(expr)
52
53 typedef QList<Rules::Match> MatchRuleList;
54 typedef QHash<QString, Repository *> RepositoryHash;
55 typedef QHash<QByteArray, QByteArray> IdentityHash;
56
57 class AprAutoPool
58 {
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); }
67
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; }
71 };
72
73 class SvnPrivate
74 {
75 public:
76     MatchRuleList matchRules;
77     RepositoryHash repositories;
78     IdentityHash identities;
79
80     SvnPrivate(const QString &pathToRepository);
81     ~SvnPrivate();
82     int youngestRevision();
83     int exportRevision(int revnum);
84
85     int openRepository(const QString &pathToRepository);
86
87 private:
88     AprAutoPool global_pool;
89     svn_fs_t *fs;
90     svn_revnum_t youngest_rev;
91 };
92
93 void Svn::initialize()
94 {
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;
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
125 int Svn::youngestRevision()
126 {
127     return d->youngestRevision();
128 }
129
130 bool Svn::exportRevision(int revnum)
131 {
132     return d->exportRevision(revnum) == EXIT_SUCCESS;
133 }
134
135 SvnPrivate::SvnPrivate(const QString &pathToRepository)
136     : global_pool(NULL)
137 {
138     openRepository(pathToRepository);
139
140     // get the youngest revision
141     svn_fs_youngest_rev(&youngest_rev, fs, global_pool);
142 }
143
144 SvnPrivate::~SvnPrivate()
145 {
146     svn_pool_destroy(global_pool);
147 }
148
149 int SvnPrivate::youngestRevision()
150 {
151     return youngest_rev;
152 }
153
154 int SvnPrivate::openRepository(const QString &pathToRepository)
155 {
156     svn_repos_t *repos;
157     SVN_ERR(svn_repos_open(&repos, QFile::encodeName(pathToRepository), global_pool));
158     fs = svn_repos_fs(repos);
159
160     return EXIT_SUCCESS;
161 }
162
163 enum RuleType { AnyRule = 0, NoIgnoreRule = 0x01 };
164
165 static MatchRuleList::ConstIterator
166 findMatchRule(const MatchRuleList &matchRules, int revnum, const QString &current,
167               int ruleMask = AnyRule)
168 {
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->rx.indexIn(current) == 0)
179             return it;
180     }
181
182     // no match
183     return end;
184 }
185
186 static void splitPathName(const Rules::Match &rule, const QString &pathName, QString *svnprefix_p,
187                           QString *repository_p, QString *branch_p, QString *path_p)
188 {
189     QString svnprefix = pathName;
190     svnprefix.truncate(rule.rx.matchedLength());
191     if (svnprefix_p)
192         *svnprefix_p = svnprefix;
193
194     if (repository_p) {
195         *repository_p = svnprefix;
196         repository_p->replace(rule.rx, rule.repository);
197     }
198
199     if (branch_p) {
200         *branch_p = svnprefix;
201         branch_p->replace(rule.rx, rule.branch);
202     }
203
204     if (path_p)
205         *path_p = pathName.mid(svnprefix.length());
206 }
207
208 static int pathMode(svn_fs_root_t *fs_root, const char *pathname, apr_pool_t *pool)
209 {
210     svn_string_t *propvalue;
211     SVN_ERR(svn_fs_node_prop(&propvalue, fs_root, pathname, "svn:executable", pool));
212     int mode = 0100644;
213     if (propvalue)
214         mode = 0100755;
215
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)
219         mode = 0120000;
220
221     return mode;
222 }
223
224 svn_error_t *QIODevice_write(void *baton, const char *data, apr_size_t *len)
225 {
226     QIODevice *device = reinterpret_cast<QIODevice *>(baton);
227     device->write(data, *len);
228
229     if (device->bytesToWrite() > 16384)
230         device->waitForBytesWritten(0);
231     return SVN_NO_ERROR;
232 }
233
234 static svn_stream_t *streamForDevice(QIODevice *device, apr_pool_t *pool)
235 {
236     svn_stream_t *stream = svn_stream_create(device, pool);
237     svn_stream_set_write(stream, QIODevice_write);
238
239     return stream;
240 }
241
242 static int dumpBlob(Repository::Transaction *txn, svn_fs_root_t *fs_root,
243                     const char *pathname, const QString &finalPathName, apr_pool_t *pool)
244 {
245     AprAutoPool dumppool(pool);
246     // what type is it?
247     int mode = pathMode(fs_root, pathname, dumppool);
248
249     svn_filesize_t stream_length;
250
251     SVN_ERR(svn_fs_file_length(&stream_length, fs_root, pathname, dumppool));
252     QIODevice *io = txn->addFile(finalPathName, mode, stream_length);
253
254 #ifndef DRY_RUN
255     // open the file
256     svn_stream_t *in_stream, *out_stream;
257     SVN_ERR(svn_fs_file_contents(&in_stream, fs_root, pathname, dumppool));
258
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));
262
263     // print an ending newline
264     io->putChar('\n');
265 #endif
266
267     return EXIT_SUCCESS;
268 }
269
270 static int recursiveDumpDir(Repository::Transaction *txn, svn_fs_root_t *fs_root,
271                             const QByteArray &pathname, const QString &finalPathName,
272                             apr_pool_t *pool)
273 {
274     // get the dir listing
275     apr_hash_t *entries;
276     SVN_ERR(svn_fs_dir_entries(&entries, fs_root, pathname, pool));
277     AprAutoPool dirpool(pool);
278
279     for (apr_hash_index_t *i = apr_hash_first(pool, entries); i; i = apr_hash_next(i)) {
280         dirpool.clear();
281         const void *vkey;
282         void *value;
283         apr_hash_this(i, &vkey, NULL, &value);
284
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;
290         else
291             entryFinalName = finalPathName + '/' + dirent->name;
292
293         if (dirent->kind == svn_node_dir) {
294             if (recursiveDumpDir(txn, fs_root, entryName, entryFinalName, dirpool) == EXIT_FAILURE)
295                 return EXIT_FAILURE;
296         } else if (dirent->kind == svn_node_file) {
297             printf("+");
298             fflush(stdout);
299             if (dumpBlob(txn, fs_root, entryName, entryFinalName, dirpool) == EXIT_FAILURE)
300                 return EXIT_FAILURE;
301         }
302     }
303
304     return EXIT_SUCCESS;
305 }
306
307 static bool wasDir(svn_fs_t *fs, int revnum, const char *pathname, apr_pool_t *pool)
308 {
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)
312         return false;
313
314     svn_boolean_t is_dir;
315     if (svn_fs_is_dir(&is_dir, fs_root, pathname, subpool) != SVN_NO_ERROR)
316         return false;
317
318     return is_dir;
319 }
320
321 time_t get_epoch(char *svn_date)
322 {
323     struct tm tm;
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);
327     return mktime(&tm);
328 }
329
330 class SvnRevision
331 {
332 public:
333     AprAutoPool pool;
334     QHash<QString, Repository::Transaction *> transactions;
335     MatchRuleList matchRules;
336     RepositoryHash repositories;
337     IdentityHash identities;
338
339     svn_fs_t *fs;
340     svn_fs_root_t *fs_root;
341     int revnum;
342
343     SvnRevision(int revision, svn_fs_t *f, apr_pool_t *parent_pool)
344         : pool(parent_pool), fs(f), fs_root(0), revnum(revision)
345     {
346     }
347
348     int open()
349     {
350         SVN_ERR(svn_fs_revision_root(&fs_root, fs, revnum, pool));
351         return EXIT_SUCCESS;
352     }
353
354     int prepareTransactions();
355     int commit();
356
357     int exportEntry(const char *path, const svn_fs_path_change_t *change, apr_hash_t *changes);
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 &current, 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,
363                 apr_hash_t *changes, apr_pool_t *pool);
364 };
365
366 int SvnPrivate::exportRevision(int revnum)
367 {
368     SvnRevision rev(revnum, fs, global_pool);
369     rev.matchRules = matchRules;
370     rev.repositories = repositories;
371     rev.identities = identities;
372
373     // open this revision:
374     printf("Exporting revision %d ", revnum);
375     fflush(stdout);
376
377     if (rev.open() == EXIT_FAILURE)
378         return EXIT_FAILURE;
379
380     if (rev.prepareTransactions() == EXIT_FAILURE)
381         return EXIT_FAILURE;
382
383     if (rev.transactions.isEmpty()) {
384         printf(" nothing to do\n");
385         return EXIT_SUCCESS;    // no changes?
386     }
387
388     if (rev.commit() == EXIT_FAILURE)
389         return EXIT_FAILURE;
390
391     printf(" done\n");
392     return EXIT_SUCCESS;
393 }
394
395 int SvnRevision::prepareTransactions()
396 {
397     // find out what was changed in this revision:
398     apr_hash_t *changes;
399     SVN_ERR(svn_fs_paths_changed(&changes, fs_root, pool));
400     for (apr_hash_index_t *i = apr_hash_first(pool, changes); i; i = apr_hash_next(i)) {
401         const void *vkey;
402         void *value;
403         apr_hash_this(i, &vkey, NULL, &value);
404         const char *key = reinterpret_cast<const char *>(vkey);
405         svn_fs_path_change_t *change = reinterpret_cast<svn_fs_path_change_t *>(value);
406
407         if (exportEntry(key, change, changes) == EXIT_FAILURE)
408             return EXIT_FAILURE;
409     }
410
411     return EXIT_SUCCESS;
412 }
413
414 int SvnRevision::commit()
415 {
416     // now create the commit
417     apr_hash_t *revprops;
418     SVN_ERR(svn_fs_revision_proplist(&revprops, fs, revnum, pool));
419     svn_string_t *svnauthor = (svn_string_t*)apr_hash_get(revprops, "svn:author", APR_HASH_KEY_STRING);
420     svn_string_t *svndate = (svn_string_t*)apr_hash_get(revprops, "svn:date", APR_HASH_KEY_STRING);
421     svn_string_t *svnlog = (svn_string_t*)apr_hash_get(revprops, "svn:log", APR_HASH_KEY_STRING);
422
423     QByteArray log = (char *)svnlog->data;
424     QByteArray authorident = svnauthor ? identities.value((char *)svnauthor->data) : QByteArray();
425     time_t epoch = get_epoch((char*)svndate->data);
426     if (authorident.isEmpty()) {
427         if (!svnauthor || svn_string_isempty(svnauthor))
428             authorident = "nobody <nobody@localhost>";
429         else
430             authorident = svnauthor->data + QByteArray(" <") +
431                           svnauthor->data + QByteArray("@localhost>");
432     }
433
434     foreach (Repository::Transaction *txn, transactions) {
435         txn->setAuthor(authorident);
436         txn->setDateTime(epoch);
437         txn->setLog(log);
438
439         txn->commit();
440         delete txn;
441     }
442
443     return EXIT_SUCCESS;
444 }
445
446 int SvnRevision::exportEntry(const char *key, const svn_fs_path_change_t *change,
447                              apr_hash_t *changes)
448 {
449     AprAutoPool revpool(pool.data());
450     QString current = QString::fromUtf8(key);
451
452     // was this copied from somewhere?
453     svn_revnum_t rev_from;
454     const char *path_from;
455     SVN_ERR(svn_fs_copied_from(&rev_from, &path_from, fs_root, key, revpool));
456
457     // is this a directory?
458     svn_boolean_t is_dir;
459     SVN_ERR(svn_fs_is_dir(&is_dir, fs_root, key, revpool));
460     if (is_dir) {
461         if (path_from == NULL) {
462             // no, it's a new directory being added
463             // Git doesn't handle directories, so we don't either
464             //qDebug() << "   mkdir ignored:" << key;
465             return EXIT_SUCCESS;
466         }
467
468         current += '/';
469         qDebug() << "   " << key << "was copied from" << path_from;
470     }
471
472     // find the first rule that matches this pathname
473     MatchRuleList::ConstIterator match = findMatchRule(matchRules, revnum, current);
474     if (match != matchRules.constEnd()) {
475         const Rules::Match &rule = *match;
476         return exportInternal(key, change, path_from, rev_from, current, rule);
477     }
478
479     if (is_dir && path_from != NULL) {
480         qDebug() << current << "is a copy-with-history, auto-recursing";
481         return recurse(key, change, path_from, rev_from, changes, revpool);
482     } else if (wasDir(fs, revnum - 1, key, revpool)) {
483         qDebug() << current << "was a directory; ignoring";
484     } else if (change->change_kind == svn_fs_path_change_delete) {
485         qDebug() << current << "is being deleted but I don't know anything about it; ignoring";
486     } else {
487         qCritical() << current << "did not match any rules; cannot continue";
488         return EXIT_FAILURE;
489     }
490
491     return EXIT_SUCCESS;
492 }
493
494 int SvnRevision::exportInternal(const char *key, const svn_fs_path_change_t *change,
495                                 const char *path_from, svn_revnum_t rev_from,
496                                 const QString &current, const Rules::Match &rule)
497 {
498     if (rule.action == Rules::Match::Ignore) {
499         // ignore rule
500         qDebug() << "   " << qPrintable(current) << "rev" << revnum
501                  << "-> ignored (rule" << rule << ")";
502         return EXIT_SUCCESS;
503     }
504
505     QString svnprefix, repository, branch, path;
506     splitPathName(rule, current, &svnprefix, &repository, &branch, &path);
507
508     printf(".");
509     fflush(stdout);
510 //                qDebug() << "   " << qPrintable(current) << "rev" << revnum << "->"
511 //                         << qPrintable(repository) << qPrintable(branch) << qPrintable(path);
512
513     if (path.isEmpty() && path_from != NULL) {
514         QString previous = QString::fromUtf8(path_from) + '/';
515         MatchRuleList::ConstIterator prevmatch =
516             findMatchRule(matchRules, rev_from, previous, NoIgnoreRule);
517         if (prevmatch != matchRules.constEnd()) {
518             QString prevsvnprefix, prevrepository, prevbranch, prevpath;
519             splitPathName(*prevmatch, previous, &prevsvnprefix, &prevrepository,
520                           &prevbranch, &prevpath);
521
522             if (!prevpath.isEmpty()) {
523                 qDebug() << qPrintable(current) << "is a partial branch of repository"
524                          << qPrintable(prevrepository) << "branch"
525                          << qPrintable(prevbranch) << "subdir"
526                          << qPrintable(prevpath);
527             } else if (prevrepository != repository) {
528                 qWarning() << qPrintable(current) << "rev" << revnum
529                            << "is a cross-repository copy (from repository"
530                            << qPrintable(prevrepository) << "branch"
531                            << qPrintable(prevbranch) << "path"
532                            << qPrintable(prevpath) << "rev" << rev_from << ")";
533             } else if (prevbranch == branch) {
534                 // same branch and same repository
535                 qDebug() << qPrintable(current) << "rev" << revnum
536                          << "is an SVN rename from"
537                          << qPrintable(previous) << "rev" << rev_from;
538                 return EXIT_SUCCESS;
539             } else {
540                 // same repository but not same branch
541                 // this means this is a plain branch
542                 qDebug() << qPrintable(repository) << ": branch"
543                          << qPrintable(branch) << "is branching from"
544                          << qPrintable(prevbranch);
545
546                 Repository *repo = repositories.value(repository, 0);
547                 if (!repo) {
548                     qCritical() << "Rule" << rule
549                                 << "references unknown repository" << repository;
550                     return EXIT_FAILURE;
551                 }
552
553                 repo->createBranch(branch, revnum, prevbranch, rev_from);
554             }
555         }
556     }
557
558     Repository::Transaction *txn = transactions.value(repository, 0);
559     if (!txn) {
560         Repository *repo = repositories.value(repository, 0);
561         if (!repo) {
562             qCritical() << "Rule" << rule
563                         << "references unknown repository" << repository;
564             return EXIT_FAILURE;
565         }
566
567         txn = repo->newTransaction(branch, svnprefix, revnum);
568         if (!txn)
569             return EXIT_FAILURE;
570
571         transactions.insert(repository, txn);
572     }
573
574     if (change->change_kind == svn_fs_path_change_delete) {
575         txn->deleteFile(path);
576     } else if (!current.endsWith('/')) {
577         dumpBlob(txn, fs_root, key, path, pool);
578     } else {
579         txn->deleteFile(path);
580         recursiveDumpDir(txn, fs_root, key, path, pool);
581     }
582
583     return EXIT_SUCCESS;
584 }
585
586 int SvnRevision::recurse(const char *path, const svn_fs_path_change_t *change,
587                          const char *path_from, svn_revnum_t rev_from,
588                          apr_hash_t *changes, apr_pool_t *pool)
589 {
590     // get the dir listing
591     apr_hash_t *entries;
592     SVN_ERR(svn_fs_dir_entries(&entries, fs_root, path, pool));
593
594     AprAutoPool dirpool(pool);
595     for (apr_hash_index_t *i = apr_hash_first(pool, entries); i; i = apr_hash_next(i)) {
596         dirpool.clear();
597         const void *vkey;
598         void *value;
599         apr_hash_this(i, &vkey, NULL, &value);
600
601         svn_fs_dirent_t *dirent = reinterpret_cast<svn_fs_dirent_t *>(value);
602         QByteArray entry = path + QByteArray("/") + dirent->name;
603         QByteArray entryFrom;
604         if (path_from)
605             entryFrom = path_from + QByteArray("/") + dirent->name;
606
607         // check if this entry is in the changelist for this revision already
608         if (apr_hash_get(changes, entry.constData(), APR_HASH_KEY_STRING)) {
609             qDebug() << entry << "rev" << revnum
610                      << "is in the change-list, deferring to that one";
611             continue;
612         }
613
614         QString current = QString::fromUtf8(entry);
615         if (dirent->kind == svn_node_dir)
616             current += '/';
617
618         // find the first rule that matches this pathname
619         MatchRuleList::ConstIterator match = findMatchRule(matchRules, revnum, current);
620         if (match != matchRules.constEnd()) {
621             if (exportInternal(entry, change, entryFrom.isNull() ? 0 : entryFrom.constData(),
622                                rev_from, current, *match) == EXIT_FAILURE)
623                 return EXIT_FAILURE;
624         } else {
625             qCritical() << current << "did not match any rules; cannot continue";
626             return EXIT_FAILURE;
627         }
628     }
629 }
This page took 0.595959 seconds and 3 git commands to generate.