File: git_export.cc

package info (click to toggle)
monotone 0.48-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 20,096 kB
  • ctags: 8,077
  • sloc: cpp: 81,000; sh: 6,402; perl: 1,241; lisp: 1,045; makefile: 655; python: 566; sql: 112; ansic: 52
file content (561 lines) | stat: -rw-r--r-- 17,809 bytes parent folder | download | duplicates (4)
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
// Copyright (C) 2009 Derek Scherger <derek@echologic.com>
//
// This program is made available under the GNU GPL version 2.0 or
// greater. See the accompanying file COPYING for details.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE.

#include "base.hh"
#include "cert.hh"
#include "database.hh"
#include "dates.hh"
#include "file_io.hh"
#include "git_change.hh"
#include "git_export.hh"
#include "lua_hooks.hh"
#include "outdated_indicator.hh"
#include "project.hh"
#include "revision.hh"
#include "roster.hh"
#include "simplestring_xform.hh"
#include "transforms.hh"
#include "ui.hh"

#include <iostream>
#include <sstream>

using std::cout;
using std::istringstream;
using std::map;
using std::ostringstream;
using std::set;
using std::string;
using std::vector;

namespace
{
  string quote_path(file_path const & path)
  {
    string raw = path.as_internal();
    string quoted;
    quoted.reserve(raw.size() + 8);

    quoted += "\"";

    for (string::const_iterator i = raw.begin(); i != raw.end(); ++i)
      {
        if (*i == '"')
          quoted += "\\";
        quoted += *i;
      }

    quoted += "\"";

    return quoted;
  }
};

void
read_mappings(system_path const & path, map<string, string> & mappings)
{
  data names;
  vector<string> lines;

  read_data(path, names);
  split_into_lines(names(), lines);

  for (vector<string>::const_iterator i = lines.begin(); i != lines.end(); ++i)
    {
      string line = trim(*i);
      size_t index = line.find('=');
      if (index != string::npos || index < line.length()-1)
        {
          string key = trim(line.substr(0, index));
          string value = trim(line.substr(index+1));
          mappings[key] = value;
        }
      else if (!line.empty())
        W(F("ignored invalid mapping '%s'") % line);
    }
}

void
validate_author_mappings(lua_hooks & lua,
                         map<string, string> const & authors)
{
  for (map<string, string>::const_iterator i = authors.begin(); 
       i != authors.end(); ++i)
    {
      E(lua.hook_validate_git_author(i->second), origin::user,
        F("invalid git author '%s' mapped from monotone author '%s'")
        % i->second % i->first);
    }
}

void
import_marks(system_path const & marks_file,
             map<revision_id, size_t> & marked_revs)
{
  size_t mark_id = 1;

  data mark_data;
  read_data(marks_file, mark_data);
  istringstream marks(mark_data());
  marks.peek();
  while (!marks.eof())
    {
      char c;
      size_t mark;
      string tmp;

      marks.get(c);
      E(c == ':', origin::user, F("missing leading ':' in marks file"));
      marks >> mark;

      marks.get(c);
      E(c == ' ', origin::user, F("missing space after mark"));
      marks >> tmp;
      E(tmp.size() == 40, origin::user, F("bad revision id in marks file"));
      revision_id revid(decode_hexenc(tmp, origin::user), origin::user);

      marks.get(c);
      E(c == '\n', origin::user, F("incomplete line in marks file"));

      marked_revs[revid] = mark;
      if (mark > mark_id) mark_id = mark+1;
      marks.peek();
    }
}


void
export_marks(system_path const & marks_file,
             map<revision_id, size_t> const & marked_revs)
{
  ostringstream marks;
  for (map<revision_id, size_t>::const_iterator
         i = marked_revs.begin(); i != marked_revs.end(); ++i)
    marks << ":" << i->second << " " << i->first << "\n";

  data mark_data(marks.str(), origin::internal);
  system_path tmp("."); // use the current directory for tmp
  write_data(marks_file, mark_data, tmp);
}

void
load_changes(database & db,
             vector<revision_id> const & revisions,
             map<revision_id, git_change> & change_map)
{
  // process revisions in reverse order and calculate the git changes for
  // each revision. these are cached in a map for use in the export phase
  // where revisions are processed in forward order. this trades off memory
  // for speed, loading rosters in reverse order is ~5x faster than loading
  // them in forward order and the memory required for git changes is
  // generally quite small. the memory required here should be comparable to
  // that for all of the revision texts in the database being exported.
  //
  // testing exports of a current monotone database with ~18MB of revision
  // text in ~15K revisions and a current pidgin database with ~20MB of
  // revision text in ~27K revisions indicate that this is a reasonable
  // approach. the export process reaches around 203MB VSS and 126MB RSS
  // for the monotone database and around 206MB VSS and 129MB RSS for the
  // pidgin database.

  ticker loaded(_("loading"), "r", 1);
  loaded.set_total(revisions.size());

  for (vector<revision_id>::const_reverse_iterator
         r = revisions.rbegin(); r != revisions.rend(); ++r)
    {
      revision_t revision;
      db.get_revision(*r, revision);

      // we apparently only need/want the changes from the first parent.
      // including the changes from the second parent seems to cause
      // failures due to repeated renames. verification of git merge nodes
      // against the monotone source seems to show that they are correct.
      // presumably this is somehow because of the 'from' and 'merge'
      // lines in exported commits below.

      revision_id parent1;
      edge_map::const_iterator edge = revision.edges.begin();
      parent1 = edge_old_revision(edge);

      roster_t old_roster, new_roster;
      db.get_roster(parent1, old_roster);
      db.get_roster(*r, new_roster);

      git_change changes;
      get_change(old_roster, new_roster, changes);
      change_map[*r] = changes;

      ++loaded;
    }
}

void
export_changes(database & db, lua_hooks & lua,
               vector<revision_id> const & revisions,
               map<revision_id, size_t> & marked_revs,
               map<string, string> const & author_map,
               map<string, string> const & branch_map,
               map<revision_id, git_change> const & change_map,
               bool log_revids, bool log_certs,
               bool use_one_changelog)
{
  size_t revnum = 0;
  size_t revmax = revisions.size();

  size_t mark_id = 0;
  for (map<revision_id, size_t>::const_iterator i = marked_revs.begin();
       i != marked_revs.end(); ++i)
    if (i->second > mark_id) mark_id = i->second;
  mark_id++;

  map<file_id, size_t> marked_files;

  // process the revisions in forward order and write out the fast-export
  // data stream.

  ticker exported(_("exporting"), "r", 1);
  exported.set_total(revisions.size());

  // keep a map of valid authors to avoid redundant lua validation calls
  map<string, string> valid_authors(author_map);

  for (vector<revision_id>::const_iterator
         r = revisions.begin(); r != revisions.end(); ++r)
    {
      revnum++;

      typedef vector<cert> cert_vector;
      typedef cert_vector::const_iterator cert_iterator;
      typedef map<string, string>::const_iterator lookup_iterator;

      cert_vector certs;
      cert_vector authors;
      cert_vector branches;
      cert_vector changelogs;
      cert_vector comments;
      cert_vector dates;
      cert_vector tags;

      db.get_revision_certs(*r, certs);

      for (cert_iterator i = certs.begin(); i != certs.end(); ++i)
        {
          if (i->name == author_cert_name)
            authors.push_back(*i);
          else if (i->name == branch_cert_name)
            branches.push_back(*i);
          else if (i->name == changelog_cert_name)
            changelogs.push_back(*i);
          else if (i->name == date_cert_name)
            dates.push_back(*i);
          else if (i->name == tag_cert_name)
            tags.push_back(*i);
          else if (i->name == comment_cert_name)
            comments.push_back(*i);
        }

      // default to <unknown> committer and author if no author certs exist
      // this may be mapped to a different value with the authors-file option
      string author_name = "Unknown <unknown>"; // used as the git author
      string author_key  = "Unknown <unknown>"; // used as the git committer
      date_t author_date = date_t::now();

      cert_iterator author = authors.begin();

      if (author != authors.end())
        {
          author_name = trim(author->value());
          if (db.public_key_exists(author->key))
            {
              rsa_pub_key pub;
              key_name name;
              db.get_pubkey(author->key, name, pub);
              author_key = trim(name());
            }
        }

      // all monotone keys and authors that don't follow the "Name <email>"
      // convention used by git must be mapped or they may cause the import
      // to fail. the full list of these values is available from monotone
      // using the 'db execute' command. the following queries will list all
      // author keys and author cert values.
      //
      // all values from author certs:
      //
      // 'select distinct value from revision_certs where name = "author"'
      //
      // all keys that have signed author certs:
      //
      // 'select distinct public_keys.name 
      //  from public_keys
      //  left join revision_certs on revision_certs.keypair_id = public_keys.id
      //  where revision_certs.name = "author"'

      lookup_iterator key_lookup = valid_authors.find(author_key);

      if (key_lookup != valid_authors.end())
        {
          author_key = key_lookup->second;
        }
      else
        {
          string unmapped_key;
          lua.hook_unmapped_git_author(author_key, unmapped_key);
          E(lua.hook_validate_git_author(unmapped_key), origin::user,
            F("invalid git author '%s' from monotone author key '%s'")
            % unmapped_key % author_key);
          valid_authors.insert(make_pair(author_key, unmapped_key));
          author_key = unmapped_key;
        }

      lookup_iterator name_lookup = valid_authors.find(author_name);

      if (name_lookup != valid_authors.end())
        {
          author_name = name_lookup->second;
        }
      else
        {
          string unmapped_name;
          lua.hook_unmapped_git_author(author_name, unmapped_name);
          E(lua.hook_validate_git_author(unmapped_name), origin::user,
            F("invalid git author '%s' from monotone author value '%s'")
            % unmapped_name % author_name);
          valid_authors.insert(make_pair(author_name, unmapped_name));
          author_name = unmapped_name;
        }

      cert_iterator date = dates.begin();

      if (date != dates.end())
        author_date = date_t(date->value());

      // default to unknown branch if no branch certs exist
      // this may be mapped to a different value with the branches-file option
      string branch_name = "unknown";

      if (!branches.empty())
        branch_name = branches.begin()->value();

      branch_name = trim(branch_name);

      lookup_iterator branch_lookup = branch_map.find(branch_name);

      if (branch_lookup != branch_map.end())
        branch_name = branch_lookup->second;

      ostringstream message;
      set<string> messages;

      // process comment certs with changelog certs

      if (!use_one_changelog)
        changelogs.insert(changelogs.end(),
                          comments.begin(), comments.end());

      for (cert_iterator changelog = changelogs.begin();
           changelog != changelogs.end(); ++changelog)
        {
          string value = changelog->value();
          if (messages.find(value) == messages.end())
            {
              messages.insert(value);
              message << value;
              if (value[value.size()-1] != '\n')
                message << "\n";
              if (use_one_changelog)
                break;
            }
        }

      revision_t revision;
      db.get_revision(*r, revision);

      edge_map::const_iterator edge = revision.edges.begin();

      revision_id parent1, parent2;

      if (revision.edges.size() == 1)
        {
          parent1 = edge_old_revision(edge);
        }
      else if (revision.edges.size() == 2)
        {
          parent1 = edge_old_revision(edge);
          ++edge;
          parent2 = edge_old_revision(edge);
        }
      else
        I(false);

      map<revision_id, git_change>::const_iterator f = change_map.find(*r);
      I(f != change_map.end());
      git_change const & change = f->second;

      vector<git_rename> reordered_renames;
      reorder_renames(change.renames, reordered_renames);

      // emit file data blobs for modified and added files

      for (add_iterator
             i = change.additions.begin(); i != change.additions.end(); ++i)
        {
          if (marked_files.find(i->content) == marked_files.end())
            {
              // only mark and emit a blob the first time it is encountered
              file_data data;
              db.get_file_version(i->content, data);
              marked_files[i->content] = mark_id++;
              cout << "blob\n"
                   << "mark :" << marked_files[i->content] << "\n"
                   << "data " << data.inner()().size() << "\n"
                   << data.inner()() << "\n";
            }
        }

      if (log_revids)
        {
          message << "\n";

          if (!null_id(parent1))
            message << "Monotone-Parent: " << parent1 << "\n";

          if (!null_id(parent2))
            message << "Monotone-Parent: " << parent2 << "\n";

          message << "Monotone-Revision: " << *r << "\n";
        }

      if (log_certs)
        {
          message << "\n";
          for ( ; author != authors.end(); ++author)
            message << "Monotone-Author: " << author->value() << "\n";

          for ( ; date != dates.end(); ++date)
            message << "Monotone-Date: " << date->value() << "\n";

          for (cert_iterator
                 branch = branches.begin() ; branch != branches.end(); ++branch)
            message << "Monotone-Branch: " << branch->value() << "\n";

          for (cert_iterator tag = tags.begin(); tag != tags.end(); ++tag)
            message << "Monotone-Tag: " << tag->value() << "\n";
        }

      string data = message.str();

      marked_revs[*r] = mark_id++;

      cout << "commit refs/heads/" << branch_name << "\n"
           << "mark :" << marked_revs[*r] << "\n"
           << "author " << author_name << " "
           << (author_date.as_millisecs_since_unix_epoch() / 1000) << " +0000\n"
           << "committer " << author_key << " "
           << (author_date.as_millisecs_since_unix_epoch() / 1000) << " +0000\n"
           << "data " << data.size() << "\n" << data << "\n";

      if (!null_id(parent1))
        cout << "from :" << marked_revs[parent1] << "\n";

      if (!null_id(parent2))
        cout << "merge :" << marked_revs[parent2] << "\n";

      for (delete_iterator
             i = change.deletions.begin(); i != change.deletions.end(); ++i)
        cout << "D " << quote_path(*i) << "\n";

      for (rename_iterator
             i = reordered_renames.begin(); i != reordered_renames.end(); ++i)
        cout << "R "
             << quote_path(i->first) << " "
             << quote_path(i->second) << "\n";

      for (add_iterator
             i = change.additions.begin(); i != change.additions.end(); ++i)
        cout << "M " << i->mode << " :"
             << marked_files[i->content] << " "
             << quote_path(i->path) << "\n";

      // create additional branch refs
      if (!branches.empty())
        {
          cert_iterator branch = branches.begin();
          branch++;
          for ( ; branch != branches.end(); ++branch)
            {
              branch_name = trim(branch->value());

              lookup_iterator branch_lookup = branch_map.find(branch_name);

              if (branch_lookup != branch_map.end())
                branch_name = branch_lookup->second;

              cout << "reset refs/heads/" << branch_name << "\n"
                   << "from :" << marked_revs[*r] << "\n";
            }
        }

      // create tag refs
      for (cert_iterator tag = tags.begin(); tag != tags.end(); ++tag)
        cout << "reset refs/tags/" << tag->value() << "\n"
             << "from :" << marked_revs[*r] << "\n";

      // report progress to the export file which will be reported during import
      cout << "progress revision " << *r
           << " (" << revnum << "/" << revmax << ")\n"
           << "#############################################################\n";

      ++exported;
    }
}

void
export_rev_refs(vector<revision_id> const & revisions,
                map<revision_id, size_t> & marked_revs)
{
  for (vector<revision_id>::const_iterator
         i = revisions.begin(); i != revisions.end(); ++i)
    cout << "reset refs/mtn/revs/" << *i << "\n"
         << "from :" << marked_revs[*i] << "\n";
}

void
export_root_refs(database & db,
                map<revision_id, size_t> & marked_revs)
{
  set<revision_id> roots;
  revision_id nullid;
  db.get_revision_children(nullid, roots);
  for (set<revision_id>::const_iterator
         i = roots.begin(); i != roots.end(); ++i)
    cout << "reset refs/mtn/roots/" << *i << "\n"
         << "from :" << marked_revs[*i] << "\n";
}

void
export_leaf_refs(database & db,
                 map<revision_id, size_t> & marked_revs)
{
  set<revision_id> leaves;
  db.get_leaves(leaves);
  for (set<revision_id>::const_iterator
         i = leaves.begin(); i != leaves.end(); ++i)
    cout << "reset refs/mtn/leaves/" << *i << "\n"
         << "from :" << marked_revs[*i] << "\n";
}

// Local Variables:
// mode: C++
// fill-column: 76
// c-file-style: "gnu"
// indent-tabs-mode: nil
// End:
// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s: