/
importer.cpp
531 lines (453 loc) · 18.1 KB
/
importer.cpp
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
// Copyright Dave Abrahams 2013. Distributed under the Boost
// Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include "importer.hpp"
#include "ruleset.hpp"
#include "svn.hpp"
#include "log.hpp"
#include "path.hpp"
#include "to_string.hpp"
#include <boost/range/adaptor/map.hpp>
#include <boost/function_output_iterator.hpp>
#include <boost/range/as_literal.hpp>
#include <svn_fs.h>
#include <svn_version.h>
#include <apr_hash.h>
using boost::adaptors::map_values;
using boost::as_literal;
importer::importer(svn const& svn_repo, Ruleset const& ruleset)
: svn_repository(svn_repo), ruleset(ruleset), revnum(0)
{
for(auto const& rule : ruleset.repositories())
{
git_repository* repo = demand_repo(rule.name);
repo->set_super_module(
demand_repo(rule.submodule_in_repo), rule.submodule_path);
}
}
// Return a pointer to a git_repository object having the given
// repository name. If name is empty, return null
inline git_repository*
importer::demand_repo(std::string const& name)
{
if (name.empty())
return nullptr;
auto p = repositories.find(name);
if (p == repositories.end())
{
p = repositories.emplace_hint(
p, std::piecewise_construct,
std::make_tuple(name), std::make_tuple(name));
}
return &p->second;
};
// Return the number of the last SVN revision that was successfully
// convertd to Git
int importer::last_valid_svn_revision()
{
return revnum;
}
// Unless the Git ref specified by match has already been completely
// processed in this revision, find it, mark it for modification, and
// return it. Otherwise, discover_changes will be false.
git_repository::ref* importer::prepare_to_modify(Rule const* match, bool discover_changes)
{
auto& repo = repositories.find(match->git_repo_name())->second;
if (!discover_changes && changed_repositories.count(&repo) == 0)
return nullptr;
changed_repositories.insert(&repo);
if (auto s = repo.in_super_module())
changed_repositories.insert(s);
return repo.modify_ref(match->git_ref_name(), discover_changes);
}
path importer::add_svn_tree_to_delete(path const& svn_path, Rule const* match)
{
assert(match);
assert(svn_path.starts_with(match->svn_path()));
// Find the unmatched suffix of the path
path path_suffix = svn_path.sans_prefix(match->svn_path());
// Access the ref for modification
auto* ref = prepare_to_modify(match, true);
// Mark the git path to be deleted at the start of the commit
ref->pending_deletions.insert( match->git_path()/path_suffix );
return path_suffix;
}
void importer::invalidate_svn_tree(
svn::revision const& rev, path const& svn_path, Rule const* match)
{
path path_suffix = add_svn_tree_to_delete(svn_path, match);
add_svn_tree_to_convert(rev, svn_path);
// Mark every svn tree that's mapped into the rule's git subtree for
// (re-)conversion.
ruleset.matcher().git_subtree_rules(
// FIXME: concatenating a subpath to a git address is pretty ugly!
match->git_address()
+ (match->git_path().str().empty() ? "" : "/")
+ path_suffix.str(),
revnum,
boost::make_function_output_iterator(
[&](Rule const* r){ add_svn_tree_to_convert(rev, r->svn_path()); })
);
}
void importer::add_svn_tree_to_convert(
svn::revision const& rev, path const& svn_path)
{
// Mark this svn_path for conversion.
auto kind = svn::call(
svn_fs_check_path, rev.fs_root, svn_path.c_str(), rev.pool);
if (kind != svn_node_none) {
Log::trace() << "adding " << svn_path << " for conversion" << std::endl;
svn_paths_to_convert.insert(svn_path);
}
}
// Deal with all the SVN changes in this revision. We're not actually
// writing any file contents (blobs or trees) to Git in this step.
// Our job is merely to make a record of paths to be deleted in Git at
// the beginning of the commit and SVN files/directories to
// subsequently be traversed and converted to Git blobs and trees.
void importer::process_svn_changes(svn::revision const& rev)
{
apr_hash_t *changes = svn::call(svn_fs_paths_changed2, rev.fs_root, rev.pool);
for (apr_hash_index_t *i = apr_hash_first(rev.pool, changes); i; i = apr_hash_next(i))
{
const char *svn_path_ = 0;
svn_fs_path_change2_t *change = 0;
apr_hash_this(i, (const void**) &svn_path_, nullptr, (void**) &change);
// According to the APR docs, this means the hash entry was
// deleted, so it should never happen
assert(change != nullptr);
// Ignore changes that only edit properties
if (change->change_kind == svn_fs_path_change_modify && !change->text_mod)
continue;
path const svn_path(svn_path_);
// We have found a path being modified in SVN. Note: it's
// too early to error-out on unmapped SVN paths here: any that
// are problematic will be picked up later.
Rule const* const match = match_svn_path(svn_path, revnum, false);
// Start by marking its Git target for deletion.
if (match)
add_svn_tree_to_delete(svn_path, match);
// If it wasn't being deleted in SVN, also convert all of its
// files to Git.
if (change->change_kind != svn_fs_path_change_delete)
add_svn_tree_to_convert(rev, svn_path);
// Assume it's a directory if it's not known to be a file.
// This is conservative, in case node_kind == svn_node_unknown.
if (change->node_kind != svn_node_file)
process_svn_directory_change(rev, change, svn_path);
}
}
void importer::process_svn_directory_change(
svn::revision const& rev, svn_fs_path_change2_t *change, path const& svn_path)
{
// Remember directory copy sources
if (change->copyfrom_known && change->copyfrom_path != nullptr)
{
// It's OK to retain only the last source directory if
// this target was copied-to more than once
auto& copy = svn_directory_copies[svn_path];
copy.src_revision = change->copyfrom_rev;
copy.src_directory = change->copyfrom_path;
}
// Handle rules that map SVN subtrees of the deleted path
ruleset.matcher().svn_subtree_rules(
svn_path.str(), revnum,
// Mark the target Git tree for deletion, but
// also convert all SVN trees being mapped into a
// subtree of the Git tree.
boost::make_function_output_iterator(
[&](Rule const* r){
invalidate_svn_tree(rev, r->svn_path(), r); }));
}
void importer::import_revision(int revnum)
{
if (Log::get_level() >= Log::Trace)
{
Log::trace()
<< "################## importing revision "
<< revnum << " ##################" << std::endl;
}
else if (revnum % 1000 == 0)
{
Log::info() << "importing revision " << revnum << std::endl;
}
this->revnum = revnum;
svn::revision rev = svn_repository[revnum];
// Importing an SVN revision happens in two phases. In the first
// phase we discover actions to be performed: Git subtrees that
// must be deleted and SVN subtrees whose files must be
// (re-)convertd to Git. In the second phase, we actually do
// those deletions and translations.
//
// Phase I: Action Discovery.
//
svn_paths_to_convert.clear();
changed_repositories.clear();
svn_directory_copies.clear();
// Deal with rules becoming active/inactive in this revision
for (Rule const* r: ruleset.matcher().rules_in_transition(revnum))
invalidate_svn_tree(rev, r->svn_path(), r);
// Discover SVN paths that are being deleted/modified
process_svn_changes(rev);
Log::trace()
<< svn_paths_to_convert.size()
<< " SVN "
<< (svn_paths_to_convert.size() == 1 ? "path" : "paths")
<< " to convert" << std::endl;
discover_merges(rev);
//
// Phase II: Writing to Git
//
// Though it is expected to be rare, a single SVN commit can
// generate commits in multiple refs of the same Git repo.
// However, the changes in a single Git ref's commit must all be
// sent contiguously to the fast-import process. Therefore, for
// each Git ref that must be committed during this SVN revision, a
// separate pass over the SVN trees to convert is required.
//
// During each pass, we map SVN paths to Git, and may discover Git
// repositories and refs to that need to be committed in this SVN
// revision. As we handle refs and repositories, we remove them
// from the set of changed things. To avoid processing them
// again, this discovery is only enabled in the first pass. This
// explains the "discover_changes" parameters you see in many of
// this class' member functions.
int pass = 0;
do
{
Log::trace() << "pass " << pass << std::endl;
if (pass > 5000)
{
throw std::runtime_error(
"On pass " + to_string(pass) + " in r" + to_string(revnum) +
"; terminating to avoid infinite loop. Check program logic!");
}
// Make a copy so it can be modified as we work this pass
auto changed_repos = changed_repositories;
for (auto r : changed_repos)
r->open_commit(rev);
auto paths_to_convert = svn_paths_to_convert;
for (auto& svn_path : paths_to_convert)
convert_svn_tree(rev, svn_path.c_str(), pass == 0);
for (auto r : changed_repos)
r->prepare_to_close_commit();
std::vector<git_repository*> closed_repositories;
for (auto r : changed_repos)
{
if (r->close_commit())
closed_repositories.push_back(r);
}
for (auto r : closed_repositories)
changed_repositories.erase(r);
++pass;
}
while(!changed_repositories.empty());
if (pass > 100)
Log::warn() << "Processing r" << revnum << " took " << pass << "passes" << std::endl;
warn_about_cross_repository_copies();
}
void importer::warn_about_cross_repository_copies()
{
for (auto& kv: svn_directory_copies)
{
if (kv.second.crossed_repositories.empty())
continue;
auto& warn = Log::warn()
<< "In r" << revnum << ", SVN directory copy "
<< kv.second.src_directory << " => " << kv.first
<< " crossed Git repositories:";
for (auto& src_dst : kv.second.crossed_repositories)
{
warn << " (" << src_dst.first << " -> " << src_dst.second << ")";
}
warn << std::endl;
}
}
importer::~importer()
{
// Apparently there's at least some ordering constraint that is
// violated by simply closing and waiting for the death of each
// process, in sequence. If we don't close all the input streams
// here, we hang waiting for the first process to exit after
// closing its stream.
for (auto& repo : repositories | map_values)
repo.fast_import().close();
}
template <class F>
void for_each_svn_file(
svn::revision const& rev, path const& svn_path, F const& f, AprPool* pool_ = 0)
{
if (boost::contains(svn_path.str(), "/CVSROOT/"))
return;
auto& pool = pool_ ? *pool_ : rev.pool;
switch( svn::call(svn_fs_check_path, rev.fs_root, svn_path.c_str(), pool) )
{
case svn_node_none: // If it turns out there's nothing here, there's nothing to do.
Log::error() << svn_path << " doesn't exist!" << std::endl;
assert(!"We added a non-existent path to convert somehow?!");
return;
case svn_node_unknown:
Log::error() << svn_path << " has unknown type!" << std::endl;
assert(!"SVN should know the type of every node in its filesystem?!");
return;
# if SVN_VER_MAJOR > 1 || SVN_VER_MAJOR == 1 && SVN_VER_MINOR >= 8
case svn_node_symlink:
// FIXME: handle symlinks properly. In earlier SVN versions
// they are represented differently.
Log::error() << svn_path << " is a symlink; this case is unhandled" << std::endl;
break;
# endif
case svn_node_file:
f(svn_path);
break;
case svn_node_dir:
AprPool dir_pool = pool.make_subpool();
apr_hash_t *entries = svn::call(svn_fs_dir_entries, rev.fs_root, svn_path.c_str(), dir_pool);
for (apr_hash_index_t *i = apr_hash_first(dir_pool, entries); i; i = apr_hash_next(i))
{
char const* subpath;
void* value;
apr_hash_this(i, (void const **)&subpath, nullptr, nullptr);
for_each_svn_file(rev, svn_path/subpath, f, &dir_pool);
}
break;
};
}
void importer::discover_merges(svn::revision const& rev)
{
for (auto& kv : svn_directory_copies)
{
for_each_svn_file(
rev, kv.first,
[=](path const& file_path)
{
if (Rule const* const match = match_svn_path(file_path, revnum))
{
auto* dst_ref = prepare_to_modify(match, true);
record_merges(dst_ref, file_path, match);
}
});
}
}
void importer::convert_svn_tree(
svn::revision const& rev, path const& svn_path, bool discover_changes)
{
for_each_svn_file(
rev, svn_path,
[=,&rev](path const& file_path) {
convert_svn_file(rev, file_path, discover_changes);
});
}
extern "C"
{
svn_error_t *fast_import_raw_bytes(void *baton, const char *data, apr_size_t *len)
{
auto& fast_import = *static_cast<git_fast_import*>(baton);
try
{
fast_import.write_raw(data, *len);
return SVN_NO_ERROR;
}
catch(std::exception const& e)
{
return svn_error_createf(APR_EOF, SVN_NO_ERROR, "%s", e.what());
}
catch(...)
{
return svn_error_createf(APR_EOF, SVN_NO_ERROR, "unknown error");
}
}
}
void importer::convert_svn_file(
svn::revision const& rev, path const& svn_path, bool discover_changes)
{
Rule const* const match = match_svn_path(svn_path, revnum);
if (!match) return;
// There are two reasons we might skip processing this file in
// this pass and come back for it in a later one:
//
// 1. the target repository and/or ref has already been fully
// processed for this revision in an earlier pass over the
// invalidated SVN trees
auto* dst_ref = prepare_to_modify(match, discover_changes);
if (dst_ref == nullptr)
return;
// Mark the repository as having changes that will need to be written
changed_repositories.insert(dst_ref->repo);
// 2. A different target ref is currently being written in this
// repository.
if (dst_ref->repo->open_commit(rev) != dst_ref)
return;
auto& fast_import = dst_ref->repo->fast_import();
auto propvalue = svn::call(
svn_fs_node_prop, rev.fs_root, svn_path.c_str(), "svn:executable", rev.pool);
fast_import.filemodify_hdr(
match->git_path()/svn_path.sans_prefix(match->svn_path()),
propvalue ? 0100755 : 0100644 );
auto file_length = svn::call(
svn_fs_file_length, rev.fs_root, svn_path.c_str(), rev.pool);
AprPool scope = rev.pool.make_subpool();
svn_stream_t* in_stream = svn::call(
svn_fs_file_contents, rev.fs_root, svn_path.c_str(), scope);
// If it's a symlink, we may need to lop 5 bytes off the front of the stream.
/*
svn_string_t *special = svn::call(
svn_fs_node_prop, rev.fs_root, svn_path.c_str(), "svn:special", scope);
*/
fast_import.data_hdr(file_length);
svn_stream_t* out_stream = svn_stream_create(&fast_import, scope);
svn_stream_set_write(out_stream, fast_import_raw_bytes);
check_svn(svn_stream_copy3(in_stream, out_stream, nullptr, nullptr, scope));
fast_import << LF;
}
// Given the SVN path of a file being converted to Git, try to find an
// SVN directory copy that caused this file to be converted, and from
// that, extract Git merge information
void importer::record_merges(git_repository::ref* target, path const& dst_svn_path, Rule const* match)
{
// Look for an svn directory copy whose target contains svn_path
auto p = svn_directory_copies.lower_bound(dst_svn_path);
if (p == svn_directory_copies.begin())
return;
if (!dst_svn_path.starts_with((--p)->first))
return;
// compute the path and revision in SVN corresponding to the
// source of this file in that directory copy
auto src_revnum = p->second.src_revision;
auto src_svn_path = p->second.src_directory / dst_svn_path.sans_prefix(p->first);
// Find out where that path landed in Git
Rule const* const src_match = match_svn_path(src_svn_path, src_revnum);
if (!src_match) return;
// If in a different repository, there's nothing to be done but warn
auto const& src_repo_name = src_match->repo_rule->git_repo_name;
if (src_repo_name == target->repo->name())
{
// Update the latest source revision merged
target->repo->record_ancestor(
target, src_match->git_ref_name(), src_revnum);
}
else // Prepare to warn about cross-repository copies
{
// ignore all cross-repository copies into the sandbox. These
// may represent experimentation that was never merged back
// into the main work area. HACK/FIXME: this should not be
// hardcoded. A --sandbox= command-line option or an
// annotation in the repository grammar would work better.
if (target->repo->name() != "sandbox")
{
p->second.crossed_repositories.insert(
std::make_pair(src_repo_name, target->repo->name()));
}
}
}
Rule const* importer::match_svn_path(path const& svn_path, std::size_t revnum, bool require_match)
{
Rule const* match = ruleset.matcher().longest_match(svn_path.str(), revnum);
if (require_match && match == nullptr)
{
Log::error() << "Unmatched svn path " << svn_path
<< " in r" << revnum << std::endl;
assert(!"unmatched SVN path");
}
return match;
}