1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
|
/*
* Based on svn-fast-export.c
*
* Walk through each revision of a local Subversion repository and export it
* in a stream that git-fast-import can consume.
*
* Author: Chris Lee <[email protected]>
* Jan Holesovsky <[email protected]>
* License: MIT <http://www.opensource.org/licenses/mit-license.php>
*/
#define _XOPEN_SOURCE
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <ostream>
#include "committers.hxx"
#include "error.hxx"
#include "filter.hxx"
#include "repository.hxx"
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
#include <apr_lib.h>
#include <apr_getopt.h>
#include <apr_general.h>
#include <svn_fs.h>
#include <svn_repos.h>
#include <svn_pools.h>
#include <svn_types.h>
#undef SVN_ERR
#define SVN_ERR(expr) SVN_INT_ERR(expr)
#define apr_sane_push(arr, contents) *(char **)apr_array_push(arr) = contents
using namespace std;
static string trunk_base = "/trunk";
static string trunk = trunk_base + "/";
static string branches = "/branches/";
static string tags = "/tags/";
static bool split_into_branch_filename( const char* path_, string& branch_, string& fname_ );
static Time get_epoch( const svn_string_t* svndate )
{
struct tm tm = {0};
if ( svndate )
{
const char* svn_date = static_cast<const char *>( svndate->data );
char date[(strlen(svn_date) * sizeof(char *))];
strncpy(date, svn_date, strlen(svn_date) - 8);
strptime(date, "%Y-%m-%dT%H:%M:%S", &tm);
}
return Time( mktime(&tm) );
}
static int dump_blob( svn_fs_root_t *root, char *full_path, const string &target_name, apr_pool_t *pool )
{
// create an own pool to avoid overflow of open streams
apr_pool_t *subpool = svn_pool_create( pool );
// prepare the stream
svn_string_t *propvalue;
SVN_ERR( svn_fs_node_prop( &propvalue, root, full_path, "svn:executable", subpool ) );
const char* mode = "644";
if ( propvalue )
mode = "755";
SVN_ERR( svn_fs_node_prop( &propvalue, root, full_path, "svn:special", subpool ) );
if ( propvalue )
Error::report( "Got a symlink; we cannot handle symlinks now." );
Filter filter( target_name );
FilePermission perm = filter.getPermission();
switch ( perm )
{
case PERMISSION_EXEC: mode = "755"; break;
case PERMISSION_NOEXEC: mode = "644"; break;
default: break;
}
ostream& out = Repositories::modifyFile( target_name, mode );
// dump the content of the file
svn_stream_t *stream;
SVN_ERR( svn_fs_file_contents( &stream, root, full_path, subpool ) );
const size_t buffer_size = 8192;
char buffer[buffer_size];
apr_size_t len;
do {
len = buffer_size;
SVN_ERR( svn_stream_read( stream, buffer, &len ) );
filter.addData( buffer, len );
} while ( len > 0 );
filter.write( out );
svn_pool_destroy( subpool );
return 0;
}
static int delete_hierarchy( svn_fs_root_t *fs_root, char *path, apr_pool_t *pool )
{
// we have to crawl the hierarchy and delete the files one by one because
// the regexp deciding to what repository does the file belong can be just
// anything
svn_boolean_t is_dir;
SVN_ERR( svn_fs_is_dir( &is_dir, fs_root, path, pool ) );
if ( is_dir )
{
apr_hash_t *entries;
SVN_ERR( svn_fs_dir_entries( &entries, fs_root, path, pool ) );
for ( apr_hash_index_t *i = apr_hash_first( pool, entries ); i; i = apr_hash_next( i ) )
{
const void *key;
void *val;
apr_hash_this( i, &key, NULL, &val );
delete_hierarchy( fs_root, (char *)( string( path ) + '/' + (char *)key ).c_str(), pool );
}
}
else
{
string this_branch, fname;
// we don't have to care about the branch name, it cannot change
if ( split_into_branch_filename( path, this_branch, fname ) )
Repositories::deleteFile( fname );
}
}
static int delete_hierarchy_rev( svn_fs_t *fs, svn_revnum_t rev, char *path, apr_pool_t *pool )
{
svn_fs_root_t *fs_root;
// rev - 1: the last rev where the deleted thing still existed
SVN_ERR( svn_fs_revision_root( &fs_root, fs, rev - 1, pool ) );
return delete_hierarchy( fs_root, path, pool );
}
static int dump_hierarchy( svn_fs_root_t *fs_root, char *path, int skip,
const string &prefix, apr_pool_t *pool )
{
svn_boolean_t is_dir;
SVN_ERR( svn_fs_is_dir( &is_dir, fs_root, path, pool ) );
if ( is_dir )
{
apr_hash_t *entries;
SVN_ERR( svn_fs_dir_entries( &entries, fs_root, path, pool ) );
for ( apr_hash_index_t *i = apr_hash_first( pool, entries ); i; i = apr_hash_next( i ) )
{
const void *key;
void *val;
apr_hash_this( i, &key, NULL, &val );
dump_hierarchy( fs_root, (char *)( string( path ) + '/' + (char *)key ).c_str(), skip, prefix, pool );
}
}
else
dump_blob( fs_root, path, prefix + string( path + skip ), pool );
return 0;
}
static int copy_hierarchy( svn_fs_t *fs, svn_revnum_t rev, char *path_from, const string &path_to, apr_pool_t *pool )
{
svn_fs_root_t *fs_root;
SVN_ERR( svn_fs_revision_root( &fs_root, fs, rev, pool ) );
return dump_hierarchy( fs_root, path_from, strlen( path_from ), path_to, pool );
}
static bool is_trunk( const char* path_ )
{
const size_t len = trunk.length();
return trunk.compare( 0, len, path_, 0, len ) == 0;
}
static bool is_branch( const char* path_ )
{
const size_t len = branches.length();
return branches.compare( 0, len, path_, 0, len ) == 0;
}
static bool is_tag( const char* path_ )
{
const size_t len = tags.length();
return tags.compare( 0, len, path_, 0, len ) == 0;
}
static bool split_into_branch_filename( const char* path_, string& branch_, string& fname_ )
{
if ( is_trunk( path_ ) )
{
branch_ = "master";
fname_ = path_ + trunk.length();
}
else if ( trunk_base == path_ )
{
branch_ = "master";
fname_ = string();
}
else
{
string tmp;
string prefix;
if ( is_branch( path_ ) )
tmp = path_ + branches.length();
else if ( is_tag( path_ ) )
{
tmp = path_ + tags.length();
prefix = TAG_TEMP_BRANCH;
}
else
return false;
size_t slash = tmp.find( '/' );
if ( slash == 0 )
return false;
else if ( slash == string::npos )
{
branch_ = prefix + tmp;
fname_ = string();
}
else
{
branch_ = prefix + tmp.substr( 0, slash );
fname_ = tmp.substr( slash + 1 );
}
}
return true;
}
int export_revision(svn_revnum_t rev, svn_fs_t *fs, apr_pool_t *pool)
{
const void *key;
void *val;
char *path, *file_change;
apr_pool_t *revpool;
apr_hash_t *changes, *props;
apr_hash_index_t *i;
svn_string_t *author, *committer, *svndate, *svnlog;
svn_boolean_t is_dir;
svn_fs_root_t *fs_root;
svn_fs_path_change_t *change;
fprintf( stderr, "Exporting revision %ld... ", rev );
if ( Repositories::ignoreRevision( rev ) )
{
fprintf( stderr, "ignored.\n" );
return 0;
}
SVN_ERR(svn_fs_revision_root(&fs_root, fs, rev, pool));
SVN_ERR(svn_fs_paths_changed(&changes, fs_root, pool));
SVN_ERR(svn_fs_revision_proplist(&props, fs, rev, pool));
revpool = svn_pool_create(pool);
author = static_cast<svn_string_t*>( apr_hash_get(props, "svn:author", APR_HASH_KEY_STRING) );
if ( !author || svn_string_isempty( author ) )
author = svn_string_create( "nobody", pool );
svndate = static_cast<svn_string_t*>( apr_hash_get(props, "svn:date", APR_HASH_KEY_STRING) );
Time epoch = get_epoch( svndate );
svnlog = static_cast<svn_string_t*>( apr_hash_get(props, "svn:log", APR_HASH_KEY_STRING) );
string branch;
bool no_changes = true;
bool debug_once = true;
bool tagged_or_branched = false;
for (i = apr_hash_first(pool, changes); i; i = apr_hash_next(i)) {
svn_pool_clear(revpool);
apr_hash_this(i, &key, NULL, &val);
path = (char *)key;
change = (svn_fs_path_change_t *)val;
if ( debug_once )
{
fprintf( stderr, "path: %s... ", path );
debug_once = false;
}
// don't care about anything in the toplevel
if ( path[0] != '/' || strchr( path + 1, '/' ) == NULL )
continue;
string this_branch, fname;
// skip if we cannot find the branch
if ( !split_into_branch_filename( path, this_branch, fname ) )
continue;
// ignore the tags we do not want
if ( is_tag( path ) && Repositories::ignoreTag( this_branch ) )
continue;
SVN_ERR(svn_fs_is_dir(&is_dir, fs_root, path, revpool));
// detect creation of branch/tag
if ( is_dir && change->change_kind == svn_fs_path_change_add )
{
// create a new branch/tag?
if ( is_branch( path ) || is_tag( path ) )
{
bool branching = is_branch( path );
if ( fname.empty() )
{
// is it a new branch/tag
svn_revnum_t rev_from;
const char* path_from;
SVN_ERR( svn_fs_copied_from( &rev_from, &path_from, fs_root, path, revpool ) );
string from_branch, from_fname;
if ( path_from != NULL &&
split_into_branch_filename( path_from, from_branch, from_fname ) &&
from_fname.empty() )
{
Repositories::createBranchOrTag( branching,
rev_from, from_branch,
Committers::getAuthor( author->data ),
this_branch, rev,
epoch,
string( svnlog->data, svnlog->len ) );
tagged_or_branched = true;
}
continue;
}
}
}
// sanity check
if ( branch.empty() )
branch = this_branch;
else if ( branch != this_branch )
{
// we found a commit that belongs to more branches at once!
// let's commit what we have so far so that we can commit the
// rest to the other branch later
Repositories::commit( Committers::getAuthor( author->data ),
branch, rev,
epoch,
string( svnlog->data, svnlog->len ) );
branch = this_branch;
}
// add/remove/move the files
if ( change->change_kind == svn_fs_path_change_delete )
delete_hierarchy_rev( fs, rev, (char *)path, revpool );
else if ( is_dir )
{
svn_revnum_t rev_from;
const char* path_from;
SVN_ERR( svn_fs_copied_from( &rev_from, &path_from, fs_root, path, revpool ) );
if ( path_from == NULL )
continue;
copy_hierarchy( fs, rev_from, (char *)path_from, fname, revpool );
}
else
dump_blob( fs_root, (char *)path, fname, revpool );
no_changes = false;
}
if ( no_changes || branch.empty() )
{
fprintf( stderr, "%s.\n", tagged_or_branched? "created": "skipping" );
svn_pool_destroy( revpool );
return 0;
}
// setup the 'from' information, so that we can start with an arbitrary
// commit (provided that ':commit map=' is setup correcty)
std::vector< int > parents;
if ( rev != 1 )
parents.push_back( rev - 1 );
Repositories::commit( Committers::getAuthor( author->data ),
branch, rev,
epoch,
string( svnlog->data, svnlog->len ),
parents );
svn_pool_destroy( revpool );
fprintf( stderr, "done!\n" );
return 0;
}
int crawl_revisions( char *repos_path, const char* repos_config )
{
apr_pool_t *pool, *subpool;
svn_fs_t *fs;
svn_repos_t *repos;
svn_revnum_t youngest_rev, min_rev, max_rev, rev;
pool = svn_pool_create(NULL);
SVN_ERR(svn_fs_initialize(pool));
SVN_ERR(svn_repos_open(&repos, repos_path, pool));
if ((fs = svn_repos_fs(repos)) == NULL)
return -1;
SVN_ERR(svn_fs_youngest_rev(&youngest_rev, fs, pool));
min_rev = 1;
max_rev = youngest_rev;
int dummy = -1;
if ( !Repositories::load( repos_config, max_rev, dummy, trunk_base, trunk, branches, tags ) )
{
Error::report( "Must have at least one valid repository definition." );
return 1;
}
if ( dummy != -1 )
min_rev = dummy;
subpool = svn_pool_create(pool);
for (rev = min_rev; rev <= max_rev; rev++) {
svn_pool_clear(subpool);
export_revision(rev, fs, subpool);
}
svn_pool_destroy(pool);
return 0;
}
int main(int argc, char *argv[])
{
if (argc != 4) {
Error::report( string( "usage: " ) + argv[0] + " REPOS_PATH committers.txt reposlayout.txt\n" );
return Error::returnValue();
}
if (apr_initialize() != APR_SUCCESS) {
Error::report( "You lose at apr_initialize()." );
return Error::returnValue();
}
Committers::load( argv[2] );
crawl_revisions( argv[1], argv[3] );
apr_terminate();
Repositories::close();
return Error::returnValue();
}
|