Refactor and add the ability to recurse into certain subdirs
[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 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 static MatchRuleList::ConstIterator
164 findMatchRule(const MatchRuleList &matchRules, int revnum, const QString &current)
165 {
166     MatchRuleList::ConstIterator it = matchRules.constBegin(),
167                                 end = matchRules.constEnd();
168     for ( ; it != end; ++it) {
169         if (it->minRevision > revnum)
170             continue;
171         if (it->maxRevision != -1 && it->maxRevision < revnum)
172             continue;
173         if (it->rx.indexIn(current) == 0)
174             return it;
175     }
176
177     // no match
178     return end;
179 }
180
181 static void splitPathName(const Rules::Match &rule, const QString &pathName, QString *svnprefix_p,
182                           QString *repository_p, QString *branch_p, QString *path_p)
183 {
184     QString svnprefix = pathName;
185     svnprefix.truncate(rule.rx.matchedLength());
186     if (svnprefix_p)
187         *svnprefix_p = svnprefix;
188
189     if (repository_p) {
190         *repository_p = svnprefix;
191         repository_p->replace(rule.rx, rule.repository);
192     }
193
194     if (branch_p) {
195         *branch_p = svnprefix;
196         branch_p->replace(rule.rx, rule.branch);
197     }
198
199     if (path_p)
200         *path_p = pathName.mid(svnprefix.length());
201 }
202
203 static int pathMode(svn_fs_root_t *fs_root, const char *pathname, apr_pool_t *pool)
204 {
205     svn_string_t *propvalue;
206     SVN_ERR(svn_fs_node_prop(&propvalue, fs_root, pathname, "svn:executable", pool));
207     int mode = 0100644;
208     if (propvalue)
209         mode = 0100755;
210
211     // maybe it's a symlink?
212     SVN_ERR(svn_fs_node_prop(&propvalue, fs_root, pathname, "svn:special", pool));
213     if (propvalue && strcmp(propvalue->data, "symlink") == 0)
214         mode = 0120000;
215
216     return mode;
217 }
218
219 svn_error_t *QIODevice_write(void *baton, const char *data, apr_size_t *len)
220 {
221     QIODevice *device = reinterpret_cast<QIODevice *>(baton);
222     device->write(data, *len);
223
224     if (device->bytesToWrite() > 16384)
225         device->waitForBytesWritten(0);
226     return SVN_NO_ERROR;
227 }
228
229 static svn_stream_t *streamForDevice(QIODevice *device, apr_pool_t *pool)
230 {
231     svn_stream_t *stream = svn_stream_create(device, pool);
232     svn_stream_set_write(stream, QIODevice_write);
233
234     return stream;
235 }
236
237 static int dumpBlob(Repository::Transaction *txn, svn_fs_root_t *fs_root,
238                     const char *pathname, const QString &finalPathName, apr_pool_t *pool)
239 {
240     AprAutoPool dumppool(pool);
241     // what type is it?
242     int mode = pathMode(fs_root, pathname, dumppool);
243
244     svn_filesize_t stream_length;
245
246     SVN_ERR(svn_fs_file_length(&stream_length, fs_root, pathname, dumppool));
247     QIODevice *io = txn->addFile(finalPathName, mode, stream_length);
248
249 #ifndef DRY_RUN
250     // open the file
251     svn_stream_t *in_stream, *out_stream;
252     SVN_ERR(svn_fs_file_contents(&in_stream, fs_root, pathname, dumppool));
253
254     // open a generic svn_stream_t for the QIODevice
255     out_stream = streamForDevice(io, dumppool);
256     SVN_ERR(svn_stream_copy(in_stream, out_stream, dumppool));
257
258     // print an ending newline
259     io->putChar('\n');
260 #endif
261
262     return EXIT_SUCCESS;
263 }
264
265 static int recursiveDumpDir(Repository::Transaction *txn, svn_fs_root_t *fs_root,
266                             const QByteArray &pathname, const QString &finalPathName,
267                             apr_pool_t *pool)
268 {
269     // get the dir listing
270     apr_hash_t *entries;
271     SVN_ERR(svn_fs_dir_entries(&entries, fs_root, pathname, pool));
272     AprAutoPool dirpool(pool);
273
274     for (apr_hash_index_t *i = apr_hash_first(pool, entries); i; i = apr_hash_next(i)) {
275         dirpool.clear();
276         const void *vkey;
277         void *value;
278         apr_hash_this(i, &vkey, NULL, &value);
279
280         svn_fs_dirent_t *dirent = reinterpret_cast<svn_fs_dirent_t *>(value);
281         QByteArray entryName = pathname + '/' + dirent->name;
282         QString entryFinalName;
283         if (finalPathName.isEmpty())
284             entryFinalName = dirent->name;
285         else
286             entryFinalName = finalPathName + '/' + dirent->name;
287
288         if (dirent->kind == svn_node_dir) {
289             if (recursiveDumpDir(txn, fs_root, entryName, entryFinalName, dirpool) == EXIT_FAILURE)
290                 return EXIT_FAILURE;
291         } else if (dirent->kind == svn_node_file) {
292             printf("+");
293             fflush(stdout);
294             if (dumpBlob(txn, fs_root, entryName, entryFinalName, dirpool) == EXIT_FAILURE)
295                 return EXIT_FAILURE;
296         }
297     }
298
299     return EXIT_SUCCESS;
300 }
301
302 static bool wasDir(svn_fs_t *fs, int revnum, const char *pathname, apr_pool_t *pool)
303 {
304     AprAutoPool subpool(pool);
305     svn_fs_root_t *fs_root;
306     if (svn_fs_revision_root(&fs_root, fs, revnum, subpool) != SVN_NO_ERROR)
307         return false;
308
309     svn_boolean_t is_dir;
310     if (svn_fs_is_dir(&is_dir, fs_root, pathname, subpool) != SVN_NO_ERROR)
311         return false;
312
313     return is_dir;
314 }
315
316 time_t get_epoch(char *svn_date)
317 {
318     struct tm tm;
319     memset(&tm, 0, sizeof tm);
320     QByteArray date(svn_date, strlen(svn_date) - 8);
321     strptime(date, "%Y-%m-%dT%H:%M:%S", &tm);
322     return mktime(&tm);
323 }
324
325 class SvnRevision
326 {
327 public:
328     AprAutoPool pool;
329     QHash<QString, Repository::Transaction *> transactions;
330     MatchRuleList matchRules;
331     RepositoryHash repositories;
332     IdentityHash identities;
333
334     svn_fs_t *fs;
335     svn_fs_root_t *fs_root;
336     int revnum;
337
338     SvnRevision(int revision, svn_fs_t *f, apr_pool_t *parent_pool)
339         : pool(parent_pool), fs(f), fs_root(0), revnum(revision)
340     {
341     }
342
343     int open()
344     {
345         SVN_ERR(svn_fs_revision_root(&fs_root, fs, revnum, pool));
346         return EXIT_SUCCESS;
347     }
348
349     int prepareTransactions();
350     int commit();
351
352     int exportEntry(const char *path, const svn_fs_path_change_t *change);
353     int exportInternal(const char *path, const svn_fs_path_change_t *change,
354                        const char *path_from, svn_revnum_t rev_from,
355                        const QString &current, const Rules::Match &rule);
356     int recurse(const char *path, const svn_fs_path_change_t *change,
357                 const char *path_from, svn_revnum_t rev_from, apr_pool_t *pool);
358 };
359
360 int SvnPrivate::exportRevision(int revnum)
361 {
362     SvnRevision rev(revnum, fs, global_pool);
363     rev.matchRules = matchRules;
364     rev.repositories = repositories;
365     rev.identities = identities;
366
367     // open this revision:
368     printf("Exporting revision %d ", revnum);
369     fflush(stdout);
370
371     if (rev.open() == EXIT_FAILURE)
372         return EXIT_FAILURE;
373
374     if (rev.prepareTransactions() == EXIT_FAILURE)
375         return EXIT_FAILURE;
376
377     if (rev.transactions.isEmpty()) {
378         printf(" nothing to do\n");
379         return EXIT_SUCCESS;    // no changes?
380     }
381
382     if (rev.commit() == EXIT_FAILURE)
383         return EXIT_FAILURE;
384
385     printf(" done\n");
386     return EXIT_SUCCESS;
387 }
388
389 int SvnRevision::prepareTransactions()
390 {
391     // find out what was changed in this revision:
392     apr_hash_t *changes;
393     SVN_ERR(svn_fs_paths_changed(&changes, fs_root, pool));
394     for (apr_hash_index_t *i = apr_hash_first(pool, changes); i; i = apr_hash_next(i)) {
395         const void *vkey;
396         void *value;
397         apr_hash_this(i, &vkey, NULL, &value);
398         const char *key = reinterpret_cast<const char *>(vkey);
399         svn_fs_path_change_t *change = reinterpret_cast<svn_fs_path_change_t *>(value);
400
401         if (exportEntry(key, change) == EXIT_FAILURE)
402             return EXIT_FAILURE;
403     }
404
405     return EXIT_SUCCESS;
406 }
407
408 int SvnRevision::commit()
409 {
410     // now create the commit
411     apr_hash_t *revprops;
412     SVN_ERR(svn_fs_revision_proplist(&revprops, fs, revnum, pool));
413     svn_string_t *svnauthor = (svn_string_t*)apr_hash_get(revprops, "svn:author", APR_HASH_KEY_STRING);
414     svn_string_t *svndate = (svn_string_t*)apr_hash_get(revprops, "svn:date", APR_HASH_KEY_STRING);
415     svn_string_t *svnlog = (svn_string_t*)apr_hash_get(revprops, "svn:log", APR_HASH_KEY_STRING);
416
417     QByteArray log = (char *)svnlog->data;
418     QByteArray authorident = svnauthor ? identities.value((char *)svnauthor->data) : QByteArray();
419     time_t epoch = get_epoch((char*)svndate->data);
420     if (authorident.isEmpty()) {
421         if (!svnauthor || svn_string_isempty(svnauthor))
422             authorident = "nobody <nobody@localhost>";
423         else
424             authorident = svnauthor->data + QByteArray(" <") +
425                           svnauthor->data + QByteArray("@localhost>");
426     }
427
428     foreach (Repository::Transaction *txn, transactions) {
429         txn->setAuthor(authorident);
430         txn->setDateTime(epoch);
431         txn->setLog(log);
432
433         txn->commit();
434         delete txn;
435     }
436
437     return EXIT_SUCCESS;
438 }
439
440 int SvnRevision::exportEntry(const char *key, const svn_fs_path_change_t *change)
441 {
442     AprAutoPool revpool(pool.data());
443     QString current = QString::fromUtf8(key);
444
445     // was this copied from somewhere?
446     svn_revnum_t rev_from;
447     const char *path_from;
448     SVN_ERR(svn_fs_copied_from(&rev_from, &path_from, fs_root, key, revpool));
449
450     // is this a directory?
451     svn_boolean_t is_dir;
452     SVN_ERR(svn_fs_is_dir(&is_dir, fs_root, key, pool));
453     if (is_dir) {
454         if (path_from == NULL) {
455             // no, it's a new directory being added
456             // Git doesn't handle directories, so we don't either
457             //qDebug() << "   mkdir ignored:" << key;
458             return EXIT_SUCCESS;
459         }
460
461         current += '/';
462         qDebug() << "   " << key << "was copied from" << path_from;
463     }
464
465     // find the first rule that matches this pathname
466     MatchRuleList::ConstIterator match = findMatchRule(matchRules, revnum, current);
467     if (match != matchRules.constEnd()) {
468         const Rules::Match &rule = *match;
469         switch (rule.action) {
470         case Rules::Match::Ignore:
471             // ignore rule
472             qDebug() << "   " << qPrintable(current) << "rev" << revnum
473                      << "-> ignored (rule line" << rule.lineNumber << ")";
474             return EXIT_SUCCESS;
475
476         case Rules::Match::Recurse:
477             // recurse rule
478             if (is_dir)
479                 return recurse(key, change, path_from, rev_from, revpool);
480             if (change->change_kind != svn_fs_path_change_delete)
481                 qWarning() << "   recurse rule " << rule.rx.pattern() << "line" << rule.lineNumber
482                            << "applied to non-directory:" << qPrintable(current);
483             return EXIT_SUCCESS;
484
485         case Rules::Match::Export:
486             return exportInternal(key, change, path_from, rev_from, current, rule);
487         }
488     }
489
490     if (wasDir(fs, revnum - 1, key, pool)) {
491         qDebug() << current << "was a directory; ignoring";
492     } else if (change->change_kind == svn_fs_path_change_delete) {
493         qDebug() << current << "is being deleted but I don't know anything about it; ignoring";
494     } else {
495         qCritical() << current << "did not match any rules; cannot continue";
496         return EXIT_FAILURE;
497     }
498
499     return EXIT_SUCCESS;
500 }
501
502 int SvnRevision::exportInternal(const char *key, const svn_fs_path_change_t *change,
503                                 const char *path_from, svn_revnum_t rev_from,
504                                 const QString &current, const Rules::Match &rule)
505 {
506     QString svnprefix, repository, branch, path;
507     splitPathName(rule, current, &svnprefix, &repository, &branch, &path);
508
509     printf(".");
510     fflush(stdout);
511 //                qDebug() << "   " << qPrintable(current) << "rev" << revnum << "->"
512 //                         << qPrintable(repository) << qPrintable(branch) << qPrintable(path);
513
514     if (path.isEmpty() && path_from != NULL) {
515         QString previous = QString::fromUtf8(path_from) + '/';
516         MatchRuleList::ConstIterator prevmatch =
517             findMatchRule(matchRules, rev_from, previous);
518         if (prevmatch != matchRules.constEnd()) {
519             QString prevsvnprefix, prevrepository, prevbranch, prevpath;
520             splitPathName(*prevmatch, previous, &prevsvnprefix, &prevrepository,
521                           &prevbranch, &prevpath);
522
523             if (!prevpath.isEmpty()) {
524                 qDebug() << qPrintable(current) << "is a partial branch of repository"
525                          << qPrintable(prevrepository) << "branch"
526                          << qPrintable(prevbranch) << "subdir"
527                          << qPrintable(prevpath);
528             } else if (prevrepository != repository) {
529                 qWarning() << qPrintable(current) << "rev" << revnum
530                            << "is a cross-repository copy (from repository"
531                            << qPrintable(prevrepository) << "branch"
532                            << qPrintable(prevbranch) << "path"
533                            << qPrintable(prevpath) << "rev" << rev_from << ")";
534             } else if (prevbranch == branch) {
535                 // same branch and same repository
536                 qDebug() << qPrintable(current) << "rev" << revnum
537                          << "is an SVN rename from"
538                          << qPrintable(previous) << "rev" << rev_from;
539                 return EXIT_SUCCESS;
540             } else {
541                 // same repository but not same branch
542                 // this means this is a plain branch
543                 qDebug() << qPrintable(repository) << ": branch"
544                          << qPrintable(branch) << "is branching from"
545                          << qPrintable(prevbranch);
546
547                 Repository *repo = repositories.value(repository, 0);
548                 if (!repo) {
549                     qCritical() << "Rule" << rule.rx.pattern() << "line" << rule.lineNumber
550                                 << "references unknown repository" << repository;
551                     return EXIT_FAILURE;
552                 }
553
554                 repo->createBranch(branch, revnum, prevbranch, rev_from);
555             }
556         }
557     }
558
559     Repository::Transaction *txn = transactions.value(repository, 0);
560     if (!txn) {
561         Repository *repo = repositories.value(repository, 0);
562         if (!repo) {
563             qCritical() << "Rule" << rule.rx.pattern() << "line" << rule.lineNumber
564                         << "references unknown repository" << repository;
565             return EXIT_FAILURE;
566         }
567
568         txn = repo->newTransaction(branch, svnprefix, revnum);
569         if (!txn)
570             return EXIT_FAILURE;
571
572         transactions.insert(repository, txn);
573     }
574
575     if (change->change_kind == svn_fs_path_change_delete)
576         txn->deleteFile(path);
577     else if (!current.endsWith('/'))
578         dumpBlob(txn, fs_root, key, path, pool);
579     else
580         recursiveDumpDir(txn, fs_root, key, path, pool);
581
582     return EXIT_SUCCESS;
583 }
584
585 int SvnRevision::recurse(const char *path, const svn_fs_path_change_t *change,
586                          const char *path_from, svn_revnum_t rev_from,
587                          apr_pool_t *pool)
588 {
589     // get the dir listing
590     apr_hash_t *entries;
591     SVN_ERR(svn_fs_dir_entries(&entries, fs_root, path, pool));
592
593     AprAutoPool dirpool(pool);
594     for (apr_hash_index_t *i = apr_hash_first(pool, entries); i; i = apr_hash_next(i)) {
595         dirpool.clear();
596         const void *vkey;
597         void *value;
598         apr_hash_this(i, &vkey, NULL, &value);
599
600         svn_fs_dirent_t *dirent = reinterpret_cast<svn_fs_dirent_t *>(value);
601         QByteArray entry = path + QByteArray("/") + dirent->name;
602         QByteArray entryFrom = path_from + QByteArray("/") + dirent->name;
603
604         QString current = QString::fromUtf8(entry);
605         if (dirent->kind == svn_node_dir)
606             current += '/';
607
608         // find the first rule that matches this pathname
609         MatchRuleList::ConstIterator match = findMatchRule(matchRules, revnum, current);
610         if (match != matchRules.constEnd()) {
611             if (exportInternal(entry, change, entryFrom, rev_from, current, *match) == EXIT_FAILURE)
612                 return EXIT_FAILURE;
613         } else {
614             qCritical() << current << "did not match any rules; cannot continue";
615             return EXIT_FAILURE;
616         }
617     }
618 }