]> andersk Git - svn-all-fast-export.git/blame - src/svn.cpp
more information at the end of the revision export
[svn-all-fast-export.git] / src / svn.cpp
CommitLineData
d3e9398d
TM
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
af010386
TM
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
d3e9398d 28#include "svn.h"
af010386
TM
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
d3e9398d
TM
48#include "repository.h"
49
af010386
TM
50#undef SVN_ERR
51#define SVN_ERR(expr) SVN_INT_ERR(expr)
52
d3e9398d
TM
53typedef QList<Rules::Match> MatchRuleList;
54typedef QHash<QString, Repository *> RepositoryHash;
af010386
TM
55typedef QHash<QByteArray, QByteArray> IdentityHash;
56
57class AprAutoPool
58{
59 apr_pool_t *pool;
b6ba9639
TM
60 AprAutoPool(const AprAutoPool &);
61 AprAutoPool &operator=(const AprAutoPool &);
af010386
TM
62public:
63 inline AprAutoPool(apr_pool_t *parent = NULL)
64 { pool = svn_pool_create(parent); }
65 inline ~AprAutoPool()
66 { svn_pool_destroy(pool); }
67
b6ba9639 68 inline void clear() { svn_pool_clear(pool); }
af010386
TM
69 inline apr_pool_t *data() const { return pool; }
70 inline operator apr_pool_t *() const { return pool; }
71};
d3e9398d
TM
72
73class SvnPrivate
74{
75public:
af010386
TM
76 MatchRuleList matchRules;
77 RepositoryHash repositories;
78 IdentityHash identities;
79
d3e9398d
TM
80 SvnPrivate(const QString &pathToRepository);
81 ~SvnPrivate();
82 int youngestRevision();
af010386 83 int exportRevision(int revnum);
d3e9398d 84
af010386
TM
85 int openRepository(const QString &pathToRepository);
86
87private:
88 AprAutoPool global_pool;
89 svn_fs_t *fs;
90 svn_revnum_t youngest_rev;
d3e9398d
TM
91};
92
93void Svn::initialize()
94{
af010386
TM
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;
d3e9398d
TM
103}
104
105Svn::Svn(const QString &pathToRepository)
106 : d(new SvnPrivate(pathToRepository))
107{
108}
109
110Svn::~Svn()
111{
112 delete d;
113}
114
115void Svn::setMatchRules(const MatchRuleList &matchRules)
116{
117 d->matchRules = matchRules;
118}
119
120void Svn::setRepositories(const RepositoryHash &repositories)
121{
122 d->repositories = repositories;
123}
124
125int Svn::youngestRevision()
126{
127 return d->youngestRevision();
128}
129
af010386
TM
130bool Svn::exportRevision(int revnum)
131{
132 return d->exportRevision(revnum) == EXIT_SUCCESS;
133}
134
135SvnPrivate::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
144SvnPrivate::~SvnPrivate()
145{
146 svn_pool_destroy(global_pool);
147}
148
149int SvnPrivate::youngestRevision()
150{
151 return youngest_rev;
152}
153
154int 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
f97a8dff
TM
163static MatchRuleList::ConstIterator
164findMatchRule(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
c338ae37
TM
181static 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}
f97a8dff 202
af010386
TM
203static 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));
b6ba9639 213 if (propvalue && strcmp(propvalue->data, "symlink") == 0)
af010386
TM
214 mode = 0120000;
215
216 return mode;
217}
218
219svn_error_t *QIODevice_write(void *baton, const char *data, apr_size_t *len)
d3e9398d 220{
af010386
TM
221 QIODevice *device = reinterpret_cast<QIODevice *>(baton);
222 device->write(data, *len);
9c31646c
TM
223
224 if (device->bytesToWrite() > 16384)
225 device->waitForBytesWritten(0);
af010386 226 return SVN_NO_ERROR;
d3e9398d
TM
227}
228
af010386
TM
229static 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
237static int dumpBlob(Repository::Transaction *txn, svn_fs_root_t *fs_root,
b6ba9639 238 const char *pathname, const QString &finalPathName, apr_pool_t *pool)
af010386
TM
239{
240 // what type is it?
241 int mode = pathMode(fs_root, pathname, pool);
242
af010386
TM
243 svn_filesize_t stream_length;
244
245 SVN_ERR(svn_fs_file_length(&stream_length, fs_root, pathname, pool));
b6ba9639 246 QIODevice *io = txn->addFile(finalPathName, mode, stream_length);
af010386 247
688d69ec 248#ifndef DRY_RUN
af010386 249 // open the file
688d69ec 250 svn_stream_t *in_stream, *out_stream;
af010386
TM
251 SVN_ERR(svn_fs_file_contents(&in_stream, fs_root, pathname, pool));
252
253 // open a generic svn_stream_t for the QIODevice
254 out_stream = streamForDevice(io, pool);
255 SVN_ERR(svn_stream_copy(in_stream, out_stream, pool));
256
9c31646c
TM
257 // print an ending newline
258 io->putChar('\n');
688d69ec 259#endif
9c31646c 260
af010386
TM
261 return EXIT_SUCCESS;
262}
263
14ddd2a5
TM
264static int recursiveDumpDir(Repository::Transaction *txn, svn_fs_root_t *fs_root,
265 const QByteArray &pathname, const QString &finalPathName,
266 apr_pool_t *pool)
267{
268 // get the dir listing
269 apr_hash_t *entries;
270 SVN_ERR(svn_fs_dir_entries(&entries, fs_root, pathname, pool));
271 AprAutoPool dirpool(pool);
272
273 for (apr_hash_index_t *i = apr_hash_first(pool, entries); i; i = apr_hash_next(i)) {
274 dirpool.clear();
275 const void *vkey;
276 void *value;
277 apr_hash_this(i, &vkey, NULL, &value);
278
279 svn_fs_dirent_t *dirent = reinterpret_cast<svn_fs_dirent_t *>(value);
280 QByteArray entryName = pathname + '/' + dirent->name;
8a1468fd
TM
281 QString entryFinalName;
282 if (finalPathName.isEmpty())
283 entryFinalName = dirent->name;
284 else
285 entryFinalName = finalPathName + '/' + dirent->name;
14ddd2a5
TM
286
287 if (dirent->kind == svn_node_dir) {
288 if (recursiveDumpDir(txn, fs_root, entryName, entryFinalName, dirpool) == EXIT_FAILURE)
289 return EXIT_FAILURE;
290 } else if (dirent->kind == svn_node_file) {
c338ae37
TM
291 printf("+");
292 fflush(stdout);
14ddd2a5
TM
293 if (dumpBlob(txn, fs_root, entryName, entryFinalName, dirpool) == EXIT_FAILURE)
294 return EXIT_FAILURE;
295 }
296 }
297}
298
5539acb4
TM
299static bool wasDir(svn_fs_t *fs, int revnum, const char *pathname, apr_pool_t *pool)
300{
301 AprAutoPool subpool(pool);
302 svn_fs_root_t *fs_root;
303 if (svn_fs_revision_root(&fs_root, fs, revnum, subpool) != SVN_NO_ERROR)
304 return false;
305
306 svn_boolean_t is_dir;
307 if (svn_fs_is_dir(&is_dir, fs_root, pathname, subpool) != SVN_NO_ERROR)
308 return false;
309
310 return is_dir;
311}
312
af010386
TM
313time_t get_epoch(char *svn_date)
314{
315 struct tm tm;
316 memset(&tm, 0, sizeof tm);
317 QByteArray date(svn_date, strlen(svn_date) - 8);
318 strptime(date, "%Y-%m-%dT%H:%M:%S", &tm);
319 return mktime(&tm);
320}
321
322int SvnPrivate::exportRevision(int revnum)
323{
b6ba9639 324 AprAutoPool pool(global_pool.data());
f97a8dff 325 QHash<QString, Repository::Transaction *> transactions;
af010386
TM
326
327 // open this revision:
dcbaff44
TM
328 printf("Exporting revision %d ", revnum);
329 fflush(stdout);
af010386
TM
330 svn_fs_root_t *fs_root;
331 SVN_ERR(svn_fs_revision_root(&fs_root, fs, revnum, pool));
af010386
TM
332
333 // find out what was changed in this revision:
af010386
TM
334 apr_hash_t *changes;
335 SVN_ERR(svn_fs_paths_changed(&changes, fs_root, pool));
b6ba9639 336 AprAutoPool revpool(pool.data());
af010386 337 for (apr_hash_index_t *i = apr_hash_first(pool, changes); i; i = apr_hash_next(i)) {
b6ba9639 338 revpool.clear();
14ddd2a5 339
af010386
TM
340 const void *vkey;
341 void *value;
342 apr_hash_this(i, &vkey, NULL, &value);
343 const char *key = reinterpret_cast<const char *>(vkey);
3ac8628a 344 QString current = QString::fromUtf8(key);
af010386 345
f97a8dff
TM
346 // was this copied from somewhere?
347 svn_revnum_t rev_from;
348 const char *path_from;
349 SVN_ERR(svn_fs_copied_from(&rev_from, &path_from, fs_root, key, revpool));
350
af010386
TM
351 // is this a directory?
352 svn_boolean_t is_dir;
353 SVN_ERR(svn_fs_is_dir(&is_dir, fs_root, key, revpool));
14ddd2a5 354 if (is_dir) {
f97a8dff 355 if (path_from == NULL) {
14ddd2a5
TM
356 // no, it's a new directory being added
357 // Git doesn't handle directories, so we don't either
973fa95b 358 //qDebug() << " mkdir ignored:" << key;
14ddd2a5 359 continue;
f97a8dff 360 }
14ddd2a5 361
3ac8628a 362 current += '/';
f97a8dff 363 qDebug() << " " << key << "was copied from" << path_from;
14ddd2a5 364 }
af010386 365
af010386 366 // find the first rule that matches this pathname
f97a8dff
TM
367 MatchRuleList::ConstIterator match = findMatchRule(matchRules, revnum, current);
368 if (match != matchRules.constEnd()) {
369 const Rules::Match &rule = *match;
370 if (rule.repository.isEmpty()) {
371 // ignore rule
372 qDebug() << " " << qPrintable(current) << "rev" << revnum
373 << "-> ignored (rule line" << rule.lineNumber << ")";
c338ae37 374 continue;
f97a8dff 375 } else {
c338ae37
TM
376 QString svnprefix, repository, branch, path;
377 splitPathName(rule, current, &svnprefix, &repository, &branch, &path);
378
379 if (path.isEmpty() && path_from != NULL) {
380 QString previous = QString::fromUtf8(path_from) + '/';
381 MatchRuleList::ConstIterator prevmatch =
382 findMatchRule(matchRules, rev_from, previous);
383 if (prevmatch != matchRules.constEnd()) {
384 QString prevsvnprefix, prevrepository, prevbranch, prevpath;
385 splitPathName(*prevmatch, previous, &prevsvnprefix, &prevrepository,
386 &prevbranch, &prevpath);
387
388 if (!prevpath.isEmpty()) {
389 qDebug() << qPrintable(current) << "is a partial branch of repository"
390 << qPrintable(prevrepository) << "branch"
391 << qPrintable(prevbranch) << "subdir"
392 << qPrintable(prevpath);
393 } else if (prevrepository != repository) {
394 qWarning() << qPrintable(current) << "rev" << revnum
395 << "is a cross-repository copy (from repository"
396 << qPrintable(prevrepository) << "branch"
397 << qPrintable(prevbranch) << "path"
398 << qPrintable(prevpath) << "rev" << rev_from << ")";
399 } else if (prevbranch == branch) {
400 // same branch and same repository
401 qDebug() << qPrintable(current) << "rev" << revnum
402 << "is an SVN rename from"
403 << qPrintable(previous) << "rev" << rev_from;
404 continue;
405 } else {
406 // same repository but not same branch
407 // this means this is a plain branch
408 qDebug() << qPrintable(repository) << ": branch"
409 << qPrintable(branch) << "is branching from"
410 << qPrintable(prevbranch);
411
412 Repository *repo = repositories.value(repository, 0);
413 if (!repo) {
414 qCritical() << "Rule" << rule.rx.pattern() << "line" << rule.lineNumber
415 << "references unknown repository" << repository;
416 return EXIT_FAILURE;
417 }
418
419 repo->createBranch(branch, revnum, prevbranch, rev_from);
420 }
421 }
422 }
af010386 423
f97a8dff
TM
424 printf(".");
425 fflush(stdout);
426// qDebug() << " " << qPrintable(current) << "rev" << revnum << "->"
427// << qPrintable(repository) << qPrintable(branch) << qPrintable(path);
af010386
TM
428
429 Repository::Transaction *txn = transactions.value(repository, 0);
430 if (!txn) {
431 Repository *repo = repositories.value(repository, 0);
432 if (!repo) {
107fa167 433 qCritical() << "Rule" << rule.rx.pattern() << "line" << rule.lineNumber
af010386
TM
434 << "references unknown repository" << repository;
435 return EXIT_FAILURE;
436 }
437
af010386
TM
438 txn = repo->newTransaction(branch, svnprefix, revnum);
439 if (!txn)
440 return EXIT_FAILURE;
441
442 transactions.insert(repository, txn);
443 }
444
445 svn_fs_path_change_t *change = reinterpret_cast<svn_fs_path_change_t *>(value);
446 if (change->change_kind == svn_fs_path_change_delete)
447 txn->deleteFile(path);
14ddd2a5 448 else if (!is_dir)
b6ba9639 449 dumpBlob(txn, fs_root, key, path, revpool);
14ddd2a5
TM
450 else
451 recursiveDumpDir(txn, fs_root, key, path, revpool);
af010386 452
c338ae37 453 continue;
af010386 454 }
b6ba9639 455 }
af010386 456
c338ae37
TM
457 if (is_dir) {
458 qDebug() << current << "is a new directory; ignoring";
459 } else if (wasDir(fs, revnum - 1, key, pool)) {
460 qDebug() << current << "was a directory; ignoring";
461 } else {
462 qCritical() << current << "did not match any rules; cannot continue";
463 return EXIT_FAILURE;
af010386
TM
464 }
465 }
b6ba9639 466 revpool.clear();
af010386 467
3ac8628a
TM
468 if (transactions.isEmpty()) {
469 printf("nothing to do\n");
ab162d73 470 return EXIT_SUCCESS; // no changes?
3ac8628a 471 }
af010386
TM
472
473 // now create the commit
474 apr_hash_t *revprops;
475 SVN_ERR(svn_fs_revision_proplist(&revprops, fs, revnum, pool));
476 svn_string_t *svnauthor = (svn_string_t*)apr_hash_get(revprops, "svn:author", APR_HASH_KEY_STRING);
477 svn_string_t *svndate = (svn_string_t*)apr_hash_get(revprops, "svn:date", APR_HASH_KEY_STRING);
478 svn_string_t *svnlog = (svn_string_t*)apr_hash_get(revprops, "svn:log", APR_HASH_KEY_STRING);
479
480 QByteArray log = (char *)svnlog->data;
453ea6b6 481 QByteArray authorident = svnauthor ? identities.value((char *)svnauthor->data) : QByteArray();
af010386
TM
482 time_t epoch = get_epoch((char*)svndate->data);
483 if (authorident.isEmpty()) {
484 if (!svnauthor || svn_string_isempty(svnauthor))
485 authorident = "nobody <nobody@localhost>";
486 else
487 authorident = svnauthor->data + QByteArray(" <") +
488 svnauthor->data + QByteArray("@localhost>");
489 }
490
491 foreach (Repository::Transaction *txn, transactions) {
492 txn->setAuthor(authorident);
493 txn->setDateTime(epoch);
494 txn->setLog(log);
495
496 txn->commit();
497 delete txn;
498 }
499
3ac8628a 500 printf("done\n");
af010386
TM
501 return EXIT_SUCCESS;
502}
This page took 0.1188 seconds and 5 git commands to generate.