]> andersk Git - svn-all-fast-export.git/blame - src/svn.cpp
Support for pathless rules
[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
181
af010386
TM
182static int pathMode(svn_fs_root_t *fs_root, const char *pathname, apr_pool_t *pool)
183{
184 svn_string_t *propvalue;
185 SVN_ERR(svn_fs_node_prop(&propvalue, fs_root, pathname, "svn:executable", pool));
186 int mode = 0100644;
187 if (propvalue)
188 mode = 0100755;
189
190 // maybe it's a symlink?
191 SVN_ERR(svn_fs_node_prop(&propvalue, fs_root, pathname, "svn:special", pool));
b6ba9639 192 if (propvalue && strcmp(propvalue->data, "symlink") == 0)
af010386
TM
193 mode = 0120000;
194
195 return mode;
196}
197
198svn_error_t *QIODevice_write(void *baton, const char *data, apr_size_t *len)
d3e9398d 199{
af010386
TM
200 QIODevice *device = reinterpret_cast<QIODevice *>(baton);
201 device->write(data, *len);
9c31646c
TM
202
203 if (device->bytesToWrite() > 16384)
204 device->waitForBytesWritten(0);
af010386 205 return SVN_NO_ERROR;
d3e9398d
TM
206}
207
af010386
TM
208static svn_stream_t *streamForDevice(QIODevice *device, apr_pool_t *pool)
209{
210 svn_stream_t *stream = svn_stream_create(device, pool);
211 svn_stream_set_write(stream, QIODevice_write);
212
213 return stream;
214}
215
216static int dumpBlob(Repository::Transaction *txn, svn_fs_root_t *fs_root,
b6ba9639 217 const char *pathname, const QString &finalPathName, apr_pool_t *pool)
af010386
TM
218{
219 // what type is it?
220 int mode = pathMode(fs_root, pathname, pool);
221
af010386
TM
222 svn_filesize_t stream_length;
223
224 SVN_ERR(svn_fs_file_length(&stream_length, fs_root, pathname, pool));
b6ba9639 225 QIODevice *io = txn->addFile(finalPathName, mode, stream_length);
af010386 226
688d69ec 227#ifndef DRY_RUN
af010386 228 // open the file
688d69ec 229 svn_stream_t *in_stream, *out_stream;
af010386
TM
230 SVN_ERR(svn_fs_file_contents(&in_stream, fs_root, pathname, pool));
231
232 // open a generic svn_stream_t for the QIODevice
233 out_stream = streamForDevice(io, pool);
234 SVN_ERR(svn_stream_copy(in_stream, out_stream, pool));
235
9c31646c
TM
236 // print an ending newline
237 io->putChar('\n');
688d69ec 238#endif
9c31646c 239
af010386
TM
240 return EXIT_SUCCESS;
241}
242
14ddd2a5
TM
243static int recursiveDumpDir(Repository::Transaction *txn, svn_fs_root_t *fs_root,
244 const QByteArray &pathname, const QString &finalPathName,
245 apr_pool_t *pool)
246{
247 // get the dir listing
248 apr_hash_t *entries;
249 SVN_ERR(svn_fs_dir_entries(&entries, fs_root, pathname, pool));
250 AprAutoPool dirpool(pool);
251
252 for (apr_hash_index_t *i = apr_hash_first(pool, entries); i; i = apr_hash_next(i)) {
253 dirpool.clear();
254 const void *vkey;
255 void *value;
256 apr_hash_this(i, &vkey, NULL, &value);
257
258 svn_fs_dirent_t *dirent = reinterpret_cast<svn_fs_dirent_t *>(value);
259 QByteArray entryName = pathname + '/' + dirent->name;
260 QString entryFinalName = finalPathName + '/' + dirent->name;
261
262 if (dirent->kind == svn_node_dir) {
263 if (recursiveDumpDir(txn, fs_root, entryName, entryFinalName, dirpool) == EXIT_FAILURE)
264 return EXIT_FAILURE;
265 } else if (dirent->kind == svn_node_file) {
266 if (dumpBlob(txn, fs_root, entryName, entryFinalName, dirpool) == EXIT_FAILURE)
267 return EXIT_FAILURE;
268 }
269 }
270}
271
5539acb4
TM
272static bool wasDir(svn_fs_t *fs, int revnum, const char *pathname, apr_pool_t *pool)
273{
274 AprAutoPool subpool(pool);
275 svn_fs_root_t *fs_root;
276 if (svn_fs_revision_root(&fs_root, fs, revnum, subpool) != SVN_NO_ERROR)
277 return false;
278
279 svn_boolean_t is_dir;
280 if (svn_fs_is_dir(&is_dir, fs_root, pathname, subpool) != SVN_NO_ERROR)
281 return false;
282
283 return is_dir;
284}
285
af010386
TM
286time_t get_epoch(char *svn_date)
287{
288 struct tm tm;
289 memset(&tm, 0, sizeof tm);
290 QByteArray date(svn_date, strlen(svn_date) - 8);
291 strptime(date, "%Y-%m-%dT%H:%M:%S", &tm);
292 return mktime(&tm);
293}
294
295int SvnPrivate::exportRevision(int revnum)
296{
b6ba9639 297 AprAutoPool pool(global_pool.data());
f97a8dff 298 QHash<QString, Repository::Transaction *> transactions;
af010386
TM
299
300 // open this revision:
b6ba9639 301 qDebug() << "Exporting revision" << revnum;
af010386
TM
302 svn_fs_root_t *fs_root;
303 SVN_ERR(svn_fs_revision_root(&fs_root, fs, revnum, pool));
af010386
TM
304
305 // find out what was changed in this revision:
af010386
TM
306 apr_hash_t *changes;
307 SVN_ERR(svn_fs_paths_changed(&changes, fs_root, pool));
b6ba9639 308 AprAutoPool revpool(pool.data());
af010386 309 for (apr_hash_index_t *i = apr_hash_first(pool, changes); i; i = apr_hash_next(i)) {
b6ba9639 310 revpool.clear();
14ddd2a5 311
af010386
TM
312 const void *vkey;
313 void *value;
314 apr_hash_this(i, &vkey, NULL, &value);
315 const char *key = reinterpret_cast<const char *>(vkey);
316
f97a8dff
TM
317 // was this copied from somewhere?
318 svn_revnum_t rev_from;
319 const char *path_from;
320 SVN_ERR(svn_fs_copied_from(&rev_from, &path_from, fs_root, key, revpool));
321
af010386
TM
322 // is this a directory?
323 svn_boolean_t is_dir;
324 SVN_ERR(svn_fs_is_dir(&is_dir, fs_root, key, revpool));
14ddd2a5 325 if (is_dir) {
f97a8dff 326 if (path_from == NULL) {
14ddd2a5
TM
327 // no, it's a new directory being added
328 // Git doesn't handle directories, so we don't either
f97a8dff 329 qDebug() << " mkdir ignored:" << key;
14ddd2a5 330 continue;
f97a8dff 331 }
14ddd2a5 332
f97a8dff 333 qDebug() << " " << key << "was copied from" << path_from;
14ddd2a5 334 }
af010386
TM
335
336 QString current = QString::fromUtf8(key);
337
338 // find the first rule that matches this pathname
f97a8dff
TM
339 MatchRuleList::ConstIterator match = findMatchRule(matchRules, revnum, current);
340 if (match != matchRules.constEnd()) {
341 const Rules::Match &rule = *match;
342 if (rule.repository.isEmpty()) {
343 // ignore rule
344 qDebug() << " " << qPrintable(current) << "rev" << revnum
345 << "-> ignored (rule line" << rule.lineNumber << ")";
346 break;
347 } else {
348 QString svnprefix = current;
349 svnprefix.truncate(rule.rx.matchedLength());
5539acb4 350
f97a8dff
TM
351 QString repository = svnprefix;
352 QString branch = svnprefix;
353 QString path = current.mid(rule.rx.matchedLength());
af010386
TM
354
355 // do the replacement
356 repository.replace(rule.rx, rule.repository);
357 branch.replace(rule.rx, rule.branch);
af010386 358
f97a8dff
TM
359 printf(".");
360 fflush(stdout);
361// qDebug() << " " << qPrintable(current) << "rev" << revnum << "->"
362// << qPrintable(repository) << qPrintable(branch) << qPrintable(path);
af010386
TM
363
364 Repository::Transaction *txn = transactions.value(repository, 0);
365 if (!txn) {
366 Repository *repo = repositories.value(repository, 0);
367 if (!repo) {
107fa167 368 qCritical() << "Rule" << rule.rx.pattern() << "line" << rule.lineNumber
af010386
TM
369 << "references unknown repository" << repository;
370 return EXIT_FAILURE;
371 }
372
af010386
TM
373 txn = repo->newTransaction(branch, svnprefix, revnum);
374 if (!txn)
375 return EXIT_FAILURE;
376
377 transactions.insert(repository, txn);
378 }
379
380 svn_fs_path_change_t *change = reinterpret_cast<svn_fs_path_change_t *>(value);
381 if (change->change_kind == svn_fs_path_change_delete)
382 txn->deleteFile(path);
14ddd2a5 383 else if (!is_dir)
b6ba9639 384 dumpBlob(txn, fs_root, key, path, revpool);
14ddd2a5
TM
385 else
386 recursiveDumpDir(txn, fs_root, key, path, revpool);
af010386
TM
387
388 break;
389 }
f97a8dff
TM
390 } else if (is_dir && path_from != NULL) {
391 qDebug() << current << "is probably a new branch";
b6ba9639 392 }
af010386 393
f97a8dff 394 if (match == matchRules.constEnd()) {
266afd58 395 if (is_dir) {
f97a8dff 396 qDebug() << current << "is a new directory; ignoring";
266afd58 397 } else if (wasDir(fs, revnum - 1, key, pool)) {
5539acb4
TM
398 qDebug() << current << "was a directory; ignoring";
399 } else {
400 qCritical() << current << "did not match any rules; cannot continue";
401 return EXIT_FAILURE;
402 }
af010386
TM
403 }
404 }
b6ba9639 405 revpool.clear();
af010386
TM
406
407 if (transactions.isEmpty())
ab162d73 408 return EXIT_SUCCESS; // no changes?
af010386
TM
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;
453ea6b6 418 QByteArray authorident = svnauthor ? identities.value((char *)svnauthor->data) : QByteArray();
af010386
TM
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
f97a8dff 437 printf("\n");
af010386
TM
438 return EXIT_SUCCESS;
439}
This page took 1.935283 seconds and 5 git commands to generate.