]> andersk Git - svn-all-fast-export.git/blob - src/svn.cpp
Don't forget the final newline for a file's data
[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 public:
61     inline AprAutoPool(apr_pool_t *parent = NULL)
62         { pool = svn_pool_create(parent); }
63     inline ~AprAutoPool()
64         { svn_pool_destroy(pool); }
65
66     inline apr_pool_t *data() const { return pool; }
67     inline operator apr_pool_t *() const { return pool; }
68 };
69
70 class SvnPrivate
71 {
72 public:
73     MatchRuleList matchRules;
74     RepositoryHash repositories;
75     IdentityHash identities;
76
77     SvnPrivate(const QString &pathToRepository);
78     ~SvnPrivate();
79     int youngestRevision();
80     int exportRevision(int revnum);
81
82     int openRepository(const QString &pathToRepository);
83
84 private:
85     AprAutoPool global_pool;
86     svn_fs_t *fs;
87     svn_revnum_t youngest_rev;
88 };
89
90 void Svn::initialize()
91 {
92     // initialize APR or exit
93     if (apr_initialize() != APR_SUCCESS) {
94         fprintf(stderr, "You lose at apr_initialize().\n");
95         exit(1);
96     }
97
98     // static destructor
99     static struct Destructor { ~Destructor() { apr_terminate(); } } destructor;
100 }
101
102 Svn::Svn(const QString &pathToRepository)
103     : d(new SvnPrivate(pathToRepository))
104 {
105 }
106
107 Svn::~Svn()
108 {
109     delete d;
110 }
111
112 void Svn::setMatchRules(const MatchRuleList &matchRules)
113 {
114     d->matchRules = matchRules;
115 }
116
117 void Svn::setRepositories(const RepositoryHash &repositories)
118 {
119     d->repositories = repositories;
120 }
121
122 int Svn::youngestRevision()
123 {
124     return d->youngestRevision();
125 }
126
127 bool Svn::exportRevision(int revnum)
128 {
129     return d->exportRevision(revnum) == EXIT_SUCCESS;
130 }
131
132 SvnPrivate::SvnPrivate(const QString &pathToRepository)
133     : global_pool(NULL)
134 {
135     openRepository(pathToRepository);
136
137     // get the youngest revision
138     svn_fs_youngest_rev(&youngest_rev, fs, global_pool);
139 }
140
141 SvnPrivate::~SvnPrivate()
142 {
143     svn_pool_destroy(global_pool);
144 }
145
146 int SvnPrivate::youngestRevision()
147 {
148     return youngest_rev;
149 }
150
151 int SvnPrivate::openRepository(const QString &pathToRepository)
152 {
153     svn_repos_t *repos;
154     SVN_ERR(svn_repos_open(&repos, QFile::encodeName(pathToRepository), global_pool));
155     fs = svn_repos_fs(repos);
156
157     return EXIT_SUCCESS;
158 }
159
160 static int pathMode(svn_fs_root_t *fs_root, const char *pathname, apr_pool_t *pool)
161 {
162     svn_string_t *propvalue;
163     SVN_ERR(svn_fs_node_prop(&propvalue, fs_root, pathname, "svn:executable", pool));
164     int mode = 0100644;
165     if (propvalue)
166         mode = 0100755;
167
168     // maybe it's a symlink?
169     SVN_ERR(svn_fs_node_prop(&propvalue, fs_root, pathname, "svn:special", pool));
170     if (strcmp(propvalue->data, "symlink") == 0)
171         mode = 0120000;
172
173     return mode;
174 }
175
176 svn_error_t *QIODevice_write(void *baton, const char *data, apr_size_t *len)
177 {
178     QIODevice *device = reinterpret_cast<QIODevice *>(baton);
179     device->write(data, *len);
180
181     if (device->bytesToWrite() > 16384)
182         device->waitForBytesWritten(0);
183     return SVN_NO_ERROR;
184 }
185
186 static svn_stream_t *streamForDevice(QIODevice *device, apr_pool_t *pool)
187 {
188     svn_stream_t *stream = svn_stream_create(device, pool);
189     svn_stream_set_write(stream, QIODevice_write);
190
191     return stream;
192 }
193
194 static int dumpBlob(Repository::Transaction *txn, svn_fs_root_t *fs_root,
195                     const char *pathname, apr_pool_t *pool)
196 {
197     // what type is it?
198     int mode = pathMode(fs_root, pathname, pool);
199
200     svn_stream_t *in_stream, *out_stream;
201     svn_filesize_t stream_length;
202
203     SVN_ERR(svn_fs_file_length(&stream_length, fs_root, pathname, pool));
204     QIODevice *io = txn->addFile(pathname, mode, stream_length);
205
206     // open the file
207     SVN_ERR(svn_fs_file_contents(&in_stream, fs_root, pathname, pool));
208
209     // open a generic svn_stream_t for the QIODevice
210     out_stream = streamForDevice(io, pool);
211     SVN_ERR(svn_stream_copy(in_stream, out_stream, pool));
212
213     // print an ending newline
214     io->putChar('\n');
215
216     return EXIT_SUCCESS;
217 }
218
219 time_t get_epoch(char *svn_date)
220 {
221     struct tm tm;
222     memset(&tm, 0, sizeof tm);
223     QByteArray date(svn_date, strlen(svn_date) - 8);
224     strptime(date, "%Y-%m-%dT%H:%M:%S", &tm);
225     return mktime(&tm);
226 }
227
228 int SvnPrivate::exportRevision(int revnum)
229 {
230     AprAutoPool pool(global_pool);
231
232     // open this revision:
233     svn_fs_root_t *fs_root;
234     SVN_ERR(svn_fs_revision_root(&fs_root, fs, revnum, pool));
235     qDebug() << "Exporting revision" << revnum;
236
237     // find out what was changed in this revision:
238     QHash<QString, Repository::Transaction *> transactions;
239     apr_hash_t *changes;
240     SVN_ERR(svn_fs_paths_changed(&changes, fs_root, pool));
241     AprAutoPool revpool(pool);
242     for (apr_hash_index_t *i = apr_hash_first(pool, changes); i; i = apr_hash_next(i)) {
243         svn_pool_clear(revpool);
244
245         const void *vkey;
246         void *value;
247         apr_hash_this(i, &vkey, NULL, &value);
248         const char *key = reinterpret_cast<const char *>(vkey);
249
250         // is this a directory?
251         svn_boolean_t is_dir;
252         SVN_ERR(svn_fs_is_dir(&is_dir, fs_root, key, revpool));
253         if (is_dir)
254             continue;           // Git doesn't handle directories, so we don't either
255
256         QString current = QString::fromUtf8(key);
257
258         // find the first rule that matches this pathname
259         bool foundMatch = false;
260         foreach (Rules::Match rule, matchRules)
261             if (rule.rx.exactMatch(current)) {
262                 foundMatch = true;
263                 QString repository = current;
264                 QString branch = current;
265                 QString path = current;
266
267                 // do the replacement
268                 repository.replace(rule.rx, rule.repository);
269                 branch.replace(rule.rx, rule.branch);
270                 path.replace(rule.rx, rule.path);
271
272                 qDebug() << "..." << current << "->"
273                          << repository << branch << path;
274
275                 Repository::Transaction *txn = transactions.value(repository, 0);
276                 if (!txn) {
277                     Repository *repo = repositories.value(repository, 0);
278                     if (!repo) {
279                         qCritical() << "Rule" << rule.rx.pattern()
280                                     << "references unknown repository" << repository;
281                         return EXIT_FAILURE;
282                     }
283
284                     QString svnprefix = current;
285                     if (current.endsWith(path))
286                         current.chop(path.length());
287
288                     txn = repo->newTransaction(branch, svnprefix, revnum);
289                     if (!txn)
290                         return EXIT_FAILURE;
291
292                     transactions.insert(repository, txn);
293                 }
294
295                 svn_fs_path_change_t *change = reinterpret_cast<svn_fs_path_change_t *>(value);
296                 if (change->change_kind == svn_fs_path_change_delete)
297                     txn->deleteFile(path);
298                 else
299                     dumpBlob(txn, fs_root, key, revpool);
300
301                 break;
302             }
303
304         if (!foundMatch) {
305             qCritical() << current << "did not match any rules; cannot continue";
306             return EXIT_FAILURE;
307         }
308     }
309     svn_pool_clear(revpool);
310
311     if (transactions.isEmpty())
312         return true;            // no changes?
313
314     // now create the commit
315     apr_hash_t *revprops;
316     SVN_ERR(svn_fs_revision_proplist(&revprops, fs, revnum, pool));
317     svn_string_t *svnauthor = (svn_string_t*)apr_hash_get(revprops, "svn:author", APR_HASH_KEY_STRING);
318     svn_string_t *svndate = (svn_string_t*)apr_hash_get(revprops, "svn:date", APR_HASH_KEY_STRING);
319     svn_string_t *svnlog = (svn_string_t*)apr_hash_get(revprops, "svn:log", APR_HASH_KEY_STRING);
320
321     QByteArray log = (char *)svnlog->data;
322     QByteArray authorident = identities.value((char *)svnauthor->data);
323     time_t epoch = get_epoch((char*)svndate->data);
324     if (authorident.isEmpty()) {
325         if (!svnauthor || svn_string_isempty(svnauthor))
326             authorident = "nobody <nobody@localhost>";
327         else
328             authorident = svnauthor->data + QByteArray(" <") +
329                           svnauthor->data + QByteArray("@localhost>");
330     }
331
332     foreach (Repository::Transaction *txn, transactions) {
333         txn->setAuthor(authorident);
334         txn->setDateTime(epoch);
335         txn->setLog(log);
336
337         txn->commit();
338         delete txn;
339     }
340
341     return EXIT_SUCCESS;
342 }
This page took 0.055728 seconds and 5 git commands to generate.