File: merge_content.cc

package info (click to toggle)
monotone 1.1-4%2Bdeb8u2
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 20,664 kB
  • ctags: 8,113
  • sloc: cpp: 86,443; sh: 6,906; perl: 924; makefile: 838; python: 517; lisp: 379; sql: 118; exp: 91; ansic: 52
file content (901 lines) | stat: -rw-r--r-- 31,514 bytes parent folder | download | duplicates (3)
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
// Copyright (C) 2008 Nathaniel Smith <njs@pobox.com>
//               2008, 2010, 2012, 2013 Stephen Leake <stephen_leake@stephe-leake.org>
//
// 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 "merge_content.hh"

#include "constants.hh"
#include "database.hh"
#include "diff_output.hh"
#include "file_io.hh"
#include "lua_hooks.hh"
#include "revision.hh"
#include "merge_roster.hh"
#include "simplestring_xform.hh"
#include "transforms.hh"
#include "xdelta.hh"

#include "safe_map.hh"
#include "vector.hh"
#include <iostream>
#include <boost/shared_ptr.hpp>

using std::make_pair;
using std::map;
using std::set;
using std::string;
using std::vector;
using boost::shared_ptr;

///////////////////////////////////////////////////////////////////////////
// content_merge_database_adaptor
///////////////////////////////////////////////////////////////////////////

content_merge_database_adaptor::content_merge_database_adaptor(database & db,
                                                               revision_id const & left,
                                                               revision_id const & right,
                                                               marking_map const & left_mm,
                                                               marking_map const & right_mm,
                                                               set<revision_id> left_uncommon_ancestors,
                                                               set<revision_id> right_uncommon_ancestors)
  : db(db), left_rid (left), right_rid (right), left_mm(left_mm), right_mm(right_mm),
  left_uncommon_ancestors (left_uncommon_ancestors), right_uncommon_ancestors (right_uncommon_ancestors)
{
  // FIXME: possibly refactor to run this lazily, as we don't
  // need to find common ancestors if we're never actually
  // called on to do content merging.
  find_common_ancestor_for_merge(db, left, right, lca);
}

void
content_merge_database_adaptor::record_merge(file_id const & left_ident,
                                             file_id const & right_ident,
                                             file_id const & merged_ident,
                                             file_data const & left_data,
                                             file_data const & right_data,
                                             file_data const & merged_data)
{
  L(FL("recording successful merge of %s <-> %s into %s")
    % left_ident
    % right_ident
    % merged_ident);

  transaction_guard guard(db);

  if (!(left_ident == merged_ident))
    {
      delta left_delta;
      diff(left_data.inner(), merged_data.inner(), left_delta);
      db.put_file_version(left_ident, merged_ident, file_delta(left_delta));
    }
  if (!(right_ident == merged_ident))
    {
      delta right_delta;
      diff(right_data.inner(), merged_data.inner(), right_delta);
      db.put_file_version(right_ident, merged_ident, file_delta(right_delta));
    }
  guard.commit();
}

void
content_merge_database_adaptor::record_file(file_id const & ident,
                                            file_data const & data)
{
  L(FL("recording new file %s")
    % ident);

  transaction_guard guard(db);

  db.put_file(ident, data);

  guard.commit();
}

void
content_merge_database_adaptor::record_file(file_id const & parent_ident,
                                            file_id const & merged_ident,
                                            file_data const & parent_data,
                                            file_data const & merged_data)
{
  L(FL("recording file %s -> %s")
    % parent_ident
    % merged_ident);

  transaction_guard guard(db);

  if (!(parent_ident == merged_ident))
    {
      delta parent_delta;
      diff(parent_data.inner(), merged_data.inner(), parent_delta);
      db.put_file_version(parent_ident, merged_ident, file_delta(parent_delta));
    }
  guard.commit();
}

void
content_merge_database_adaptor::cache_roster(revision_id const & rid,
                                             boost::shared_ptr<roster_t const> roster)
{
  safe_insert(rosters, make_pair(rid, roster));
};

static void
load_and_cache_roster(database & db, revision_id const & rid,
                      map<revision_id, shared_ptr<roster_t const> > & rmap,
                      shared_ptr<roster_t const> & rout)
{
  map<revision_id, shared_ptr<roster_t const> >::const_iterator i = rmap.find(rid);
  if (i != rmap.end())
    rout = i->second;
  else
    {
      cached_roster cr;
      db.get_roster(rid, cr);
      safe_insert(rmap, make_pair(rid, cr.first));
      rout = cr.first;
    }
}

void
content_merge_database_adaptor::get_ancestral_roster(node_id nid,
                                                     revision_id & rid,
                                                     shared_ptr<roster_t const> & anc)
{
  // Given a file, if the lca is nonzero and its roster contains the file,
  // then we use its roster.  Otherwise we use the roster at the file's
  // birth revision, which is the "per-file worst case" lca.

  // Begin by loading any non-empty file lca roster
  rid = lca;
  if (!lca.inner()().empty())
    load_and_cache_roster(db, lca, rosters, anc);

  // If there is no LCA, or the LCA's roster doesn't contain the file,
  // then use the file's birth roster.
  if (!anc || !anc->has_node(nid))
    {
      if (!left_mm.contains(nid))
        {
          rid = right_mm.get_marking(nid)->birth_revision;
        }
      else if (!right_mm.contains(nid))
        {
          rid = left_mm.get_marking(nid)->birth_revision;
        }
      else
        {
          const_marking_t const & lm = left_mm.get_marking(nid);
          const_marking_t const & rm = right_mm.get_marking(nid);
          I(lm->birth_revision == rm->birth_revision);
          rid = lm->birth_revision;
        }

      load_and_cache_roster(db, rid, rosters, anc);
    }
  I(anc);
}

void
content_merge_database_adaptor::get_dropped_details(set<revision_id> const & uncommon_ancestors,
                                                    revision_id const &      least_common_ancestor,
                                                    node_id const            nid,
                                                    revision_id &            dropped_rev_id,
                                                    file_path   &            dropped_name,
                                                    file_id     &            dropped_file_id)
{
  roster_t roster;
  marking_map marking_map;

  // graph.cc ensures that uncommon_ancestors is in topological order, leaf-most first.
  for (set<revision_id>::const_iterator i = uncommon_ancestors.begin();
       i != uncommon_ancestors.end(); ++i)
    {
      db.get_roster(*i, roster, marking_map);
      if (roster.has_node(nid))
        {
          dropped_rev_id = *i;
          roster.get_file_details(nid, dropped_file_id, dropped_name);
          return;
        }
    }

  db.get_roster(least_common_ancestor, roster, marking_map);
  if (roster.has_node(nid))
    {
      dropped_rev_id = least_common_ancestor;
      roster.get_file_details(nid, dropped_file_id, dropped_name);
      return;
    }
  I(false);
}

void
content_merge_database_adaptor::get_version(file_id const & ident,
                                            file_data & dat) const
{
  db.get_file_version(ident, dat);
}


///////////////////////////////////////////////////////////////////////////
// content_merge_workspace_adaptor
///////////////////////////////////////////////////////////////////////////

void
content_merge_workspace_adaptor::cache_roster(revision_id const & rid,
                                              boost::shared_ptr<roster_t const> roster)
{
  rosters.insert(std::make_pair(rid, roster));
}

void
content_merge_workspace_adaptor::record_merge(file_id const & left_id,
                                              file_id const & right_id,
                                              file_id const & merged_id,
                                              file_data const & left_data,
                                              file_data const & right_data,
                                              file_data const & merged_data)
{
  L(FL("temporarily recording merge of %s <-> %s into %s")
    % left_id
    % right_id
    % merged_id);
  // this is an insert instead of a safe_insert because it is perfectly
  // legal (though rare) to have multiple merges resolve to the same file
  // contents.
  temporary_store.insert(make_pair(merged_id, merged_data));
}

void
content_merge_workspace_adaptor::record_file(file_id const & id,
                                             file_data const & data)
{
  L(FL("temporarily recording file %s")
    % id);
  // this is an insert instead of a safe_insert because it is perfectly
  // legal (though rare) to have multiple merges resolve to the same file
  // contents.
  temporary_store.insert(make_pair(id, data));
}

void
content_merge_workspace_adaptor::record_file(file_id const & parent_id,
                                             file_id const & merged_id,
                                             file_data const & parent_data,
                                             file_data const & merged_data)
{
  L(FL("temporarily recording file %s -> %s")
    % parent_id
    % merged_id);
  // this is an insert instead of a safe_insert because it is perfectly
  // legal (though rare) to have multiple merges resolve to the same file
  // contents.
  temporary_store.insert(make_pair(merged_id, merged_data));
}

void
content_merge_workspace_adaptor::get_ancestral_roster(node_id nid,
                                                      revision_id & rid,
                                                      shared_ptr<roster_t const> & anc)
{
  // Begin by loading any non-empty file lca roster
  if (base->has_node(nid))
    {
      rid = lca;
      anc = base;
    }
  else
    {
      if (!left_mm.contains(nid))
        {
          rid = right_mm.get_marking(nid)->birth_revision;
        }
      else if (!right_mm.contains(nid))
        {
          rid = left_mm.get_marking(nid)->birth_revision;
        }
      else
        {
          const_marking_t const & lm = left_mm.get_marking(nid);
          const_marking_t const & rm = right_mm.get_marking(nid);
          I(lm->birth_revision == rm->birth_revision);
          rid = lm->birth_revision;
        }

      load_and_cache_roster(db, rid, rosters, anc);
    }
  I(anc);
}

void
content_merge_workspace_adaptor::get_version(file_id const & ident,
                                             file_data & dat) const
{
  map<file_id,file_data>::const_iterator i = temporary_store.find(ident);
  if (i != temporary_store.end())
    dat = i->second;
  else if (db.file_version_exists(ident))
    db.get_file_version(ident, dat);
  else
    {
      data tmp;
      file_id fid;
      map<file_id, file_path>::const_iterator i = content_paths.find(ident);
      I(i != content_paths.end());

      require_path_is_file(i->second,
                           F("file '%s' does not exist in workspace") % i->second,
                           F("'%s' in workspace is a directory, not a file") % i->second);
      read_data(i->second, tmp);
      calculate_ident(file_data(tmp), fid);
      E(fid == ident, origin::system,
        F("file '%s' in workspace has id %s, wanted %s")
        % i->second
        % fid
        % ident);
      dat = file_data(tmp);
    }
}


///////////////////////////////////////////////////////////////////////////
// content_merge_checkout_adaptor
///////////////////////////////////////////////////////////////////////////

void
content_merge_checkout_adaptor::record_merge(file_id const & left_ident,
                                             file_id const & right_ident,
                                             file_id const & merged_ident,
                                             file_data const & left_data,
                                             file_data const & right_data,
                                             file_data const & merged_data)
{
  I(false);
}

void
content_merge_checkout_adaptor::record_file(file_id const & parent_ident,
                                            file_id const & merged_ident,
                                            file_data const & parent_data,
                                            file_data const & merged_data)
{
  I(false);
}

void
content_merge_checkout_adaptor::record_file(file_id const & ident,
                                            file_data const & data)
{
  I(false);
}

void
content_merge_checkout_adaptor::get_ancestral_roster(node_id nid,
                                                     revision_id & rid,
                                                     shared_ptr<roster_t const> & anc)
{
  I(false);
}

void
content_merge_checkout_adaptor::get_version(file_id const & ident,
                                            file_data & dat) const
{
  db.get_file_version(ident, dat);
}

///////////////////////////////////////////////////////////////////////////
// content_merge_empty_adaptor
///////////////////////////////////////////////////////////////////////////

void
content_merge_empty_adaptor::record_merge(file_id const & left_ident,
                                          file_id const & right_ident,
                                          file_id const & merged_ident,
                                          file_data const & left_data,
                                          file_data const & right_data,
                                          file_data const & merged_data)
{
  I(false);
}

void
content_merge_empty_adaptor::record_file(file_id const & ident,
                                         file_data const & data)
{
  I(false);
}

void
content_merge_empty_adaptor::record_file(file_id const & parent_ident,
                                         file_id const & merged_ident,
                                         file_data const & parent_data,
                                         file_data const & merged_data)
{
  I(false);
}

void
content_merge_empty_adaptor::get_ancestral_roster(node_id nid,
                                                  revision_id & rid,
                                                  shared_ptr<roster_t const> & anc)
{
  I(false);
}

void
content_merge_empty_adaptor::get_version(file_id const & ident,
                                         file_data & dat) const
{
  I(false);
}

///////////////////////////////////////////////////////////////////////////
// content_merger
///////////////////////////////////////////////////////////////////////////

string
content_merger::get_file_encoding(file_path const & path,
                                  roster_t const & ros)
{
  attr_value v;
  if (ros.get_attr(path, attr_key(constants::encoding_attribute), v))
    return v();
  return constants::default_encoding;
}

bool
content_merger::attribute_manual_merge(file_path const & path,
                                       roster_t const & ros)
{
  attr_value v;
  if (ros.get_attr(path, attr_key(constants::manual_merge_attribute), v)
      && v() == "true")
    return true;
  return false; // default: enable auto merge
}

bool
content_merger::attempt_auto_merge(file_path const & anc_path, // inputs
                                   file_path const & left_path,
                                   file_path const & right_path,
                                   file_id const & ancestor_id,
                                   file_id const & left_id,
                                   file_id const & right_id,
                                   file_data & left_data, // outputs
                                   file_data & right_data,
                                   file_data & merge_data)
{
  I(left_id != right_id);

  if (attribute_manual_merge(left_path, left_ros) ||
      attribute_manual_merge(right_path, right_ros))
    {
      return false;
    }

  // both files mergeable by monotone internal algorithm, try to merge
  // note: the ancestor is not considered for manual merging. Forcing the
  // user to merge manually just because of an ancestor mistakenly marked
  // manual seems too harsh

  file_data ancestor_data;

  adaptor.get_version(left_id, left_data);
  adaptor.get_version(ancestor_id, ancestor_data);
  adaptor.get_version(right_id, right_data);

  data const left_unpacked = left_data.inner();
  data const ancestor_unpacked = ancestor_data.inner();
  data const right_unpacked = right_data.inner();

  string const left_encoding(get_file_encoding(left_path, left_ros));
  string const anc_encoding(get_file_encoding(anc_path, anc_ros));
  string const right_encoding(get_file_encoding(right_path, right_ros));

  vector<string> left_lines, ancestor_lines, right_lines, merged_lines;
  split_into_lines(left_unpacked(), left_encoding, left_lines,
                   split_flags::keep_endings);
  split_into_lines(ancestor_unpacked(), anc_encoding, ancestor_lines,
                   split_flags::keep_endings);
  split_into_lines(right_unpacked(), right_encoding, right_lines,
                   split_flags::keep_endings);

  if (merge3(ancestor_lines, left_lines, right_lines, merged_lines))
    {
      string tmp;

      join_lines(merged_lines, tmp, "");
      merge_data = file_data(tmp, origin::internal);
      return true;
    }

  return false;
}

bool
content_merger::try_auto_merge(file_path const & anc_path,
                               file_path const & left_path,
                               file_path const & right_path,
                               file_path const & merged_path,
                               file_id const & ancestor_id,
                               file_id const & left_id,
                               file_id const & right_id,
                               file_id & merged_id)
{
  // This version of try_to_merge_files should only be called when there is a
  // real merge3 to perform.
  I(!null_id(ancestor_id));
  I(!null_id(left_id));
  I(!null_id(right_id));

  L(FL("trying auto merge '%s' %s <-> %s (ancestor: %s)")
    % merged_path
    % left_id
    % right_id
    % ancestor_id);

  if (left_id == right_id)
    {
      L(FL("files are identical"));
      merged_id = left_id;
      return true;
    }

  file_data left_data, right_data, merge_data;

  if (attempt_auto_merge(anc_path, left_path, right_path,
                         ancestor_id, left_id, right_id,
                         left_data, right_data, merge_data))
    {
      L(FL("internal 3-way merged ok"));
      calculate_ident(merge_data, merged_id);

      adaptor.record_merge(left_id, right_id, merged_id,
                           left_data, right_data, merge_data);

      return true;
    }

  return false;
}

bool
content_merger::try_user_merge(file_path const & anc_path,
                               file_path const & left_path,
                               file_path const & right_path,
                               file_path const & merged_path,
                               file_id const & ancestor_id,
                               file_id const & left_id,
                               file_id const & right_id,
                               file_id & merged_id)
{
  // This version of try_to_merge_files should only be called when there is a
  // real merge3 to perform.
  I(!null_id(ancestor_id));
  I(!null_id(left_id));
  I(!null_id(right_id));

  L(FL("trying user merge '%s' %s <-> %s (ancestor: %s)")
    % merged_path
    % left_id
    % right_id
    % ancestor_id);

  if (left_id == right_id)
    {
      L(FL("files are identical"));
      merged_id = left_id;
      return true;
    }

  file_data left_data, right_data, ancestor_data;
  data left_unpacked, ancestor_unpacked, right_unpacked, merged_unpacked;

  adaptor.get_version(left_id, left_data);
  adaptor.get_version(ancestor_id, ancestor_data);
  adaptor.get_version(right_id, right_data);

  left_unpacked = left_data.inner();
  ancestor_unpacked = ancestor_data.inner();
  right_unpacked = right_data.inner();

  P(F("help required for 3-way merge\n"
      "[ancestor] %s\n"
      "[    left] %s\n"
      "[   right] %s\n"
      "[  merged] %s")
    % anc_path
    % left_path
    % right_path
    % merged_path);

  if (lua.hook_merge3(anc_path, left_path, right_path, merged_path,
                      ancestor_unpacked, left_unpacked,
                      right_unpacked, merged_unpacked))
    {
      file_data merge_data(merged_unpacked);

      L(FL("lua merge3 hook merged ok"));
      calculate_ident(merge_data, merged_id);

      adaptor.record_merge(left_id, right_id, merged_id,
                           left_data, right_data, merge_data);
      return true;
    }

  return false;
}

enum merge_method { auto_merge, user_merge };

static void
try_to_merge_files(lua_hooks & lua,
                   roster_t const & left_roster, roster_t const & right_roster,
                   roster_merge_result & result, content_merge_adaptor & adaptor,
                   merge_method const method)
{
  size_t cnt;
  size_t total_conflicts = result.file_content_conflicts.size();
  std::vector<file_content_conflict>::iterator it;

  for (cnt = 1, it = result.file_content_conflicts.begin();
       it != result.file_content_conflicts.end(); ++cnt)
    {
      file_content_conflict const & conflict = *it;

      MM(conflict);

      revision_id rid;
      shared_ptr<roster_t const> roster_for_file_lca;
      adaptor.get_ancestral_roster(conflict.nid, rid, roster_for_file_lca);

      // Now we should certainly have a roster, which has the node.
      I(roster_for_file_lca);
      I(roster_for_file_lca->has_node(conflict.nid));

      file_id anc_id, left_id, right_id;
      file_path anc_path, left_path, right_path;
      roster_for_file_lca->get_file_details(conflict.nid, anc_id, anc_path);
      left_roster.get_file_details(conflict.nid, left_id, left_path);
      right_roster.get_file_details(conflict.nid, right_id, right_path);

      file_id merged_id;

      content_merger cm(lua, *roster_for_file_lca,
                        left_roster, right_roster,
                        adaptor);

      bool merged = false;

      switch (method)
        {
        case auto_merge:
          merged = cm.try_auto_merge(anc_path, left_path, right_path,
                                     right_path, anc_id, left_id, right_id,
                                     merged_id);
          break;

        case user_merge:
          merged = cm.try_user_merge(anc_path, left_path, right_path,
                                     right_path, anc_id, left_id, right_id,
                                     merged_id);

          // If the user merge has failed, there's no point
          // trying to continue -- we'll only frustrate users by
          // encouraging them to continue working with their merge
          // tool on a merge that is now destined to fail.
          if (!merged)
            return;

          break;
        }

      if (merged)
        {
          L(FL("resolved content conflict %d / %d on file '%s'")
            % cnt % total_conflicts % right_path);
          file_t f = downcast_to_file_t(result.roster.get_node_for_update(conflict.nid));
          f->content = merged_id;

          it = result.file_content_conflicts.erase(it);
        }
      else
        {
          ++it;
        }
    }
}

void
resolve_merge_conflicts(lua_hooks & lua,
                        options const & opts,
                        roster_t const & left_roster,
                        roster_t const & right_roster,
                        roster_merge_result & result,
                        content_merge_adaptor & adaptor,
                        temp_node_id_source & nis,
                        const bool resolutions_given)
{
  if (!result.is_clean())
    {
      result.log_conflicts();

      if (resolutions_given)
        {
          // We require --resolve-conflicts to enable processing attr
          // mtn:resolve_conflict.

          // If there are any conflicts for which we don't currently support
          // resolutions, give a nice error message.
          char const * const msg = "conflict resolution for %s not yet supported";

          E(!result.missing_root_conflict, origin::user,
            F(msg) % "missing_root_dir");
          E(result.invalid_name_conflicts.size() == 0, origin::user,
            F(msg) % "invalid_name_conflicts");
          E(result.directory_loop_conflicts.size() == 0, origin::user,
            F(msg) % "directory_loop_conflicts");
          E(result.multiple_name_conflicts.size() == 0, origin::user,
            F(msg) % "multiple_name_conflicts");
          E(result.attribute_conflicts.size() == 0, origin::user,
            F(msg) % "attribute_conflicts");

          // Resolve the ones we can, if they have resolutions specified. Each
          // conflict list is deleted once all are resolved.
          result.resolve_orphaned_node_conflicts(lua, left_roster, right_roster, adaptor);
          result.resolve_dropped_modified_conflicts(lua, left_roster, right_roster,
                                                    dynamic_cast <content_merge_database_adaptor&>(adaptor), nis);
          result.resolve_duplicate_name_conflicts(lua, left_roster, right_roster, adaptor);

          result.resolve_file_content_conflicts (lua, left_roster, right_roster, adaptor);
        }
    }

  if (result.has_non_content_conflicts())
    {
      result.report_missing_root_conflicts(left_roster, right_roster, adaptor, false, std::cout);
      result.report_invalid_name_conflicts(left_roster, right_roster, adaptor, false, std::cout);
      result.report_directory_loop_conflicts(left_roster, right_roster, adaptor, false, std::cout);

      result.report_orphaned_node_conflicts(left_roster, right_roster, adaptor, false, std::cout);
      result.report_multiple_name_conflicts(left_roster, right_roster, adaptor, false, std::cout);
      result.report_dropped_modified_conflicts(left_roster, right_roster, adaptor, false, std::cout);
      result.report_duplicate_name_conflicts(left_roster, right_roster, adaptor, false, std::cout);

      result.report_attribute_conflicts(left_roster, right_roster, adaptor, false, std::cout);
      result.report_file_content_conflicts(lua, left_roster, right_roster, adaptor, false, std::cout);
    }
  else if (result.has_content_conflicts())
    {
      // Attempt to auto-resolve any content conflicts using the line-merger.
      // To do this requires finding a merge ancestor.

      L(FL("examining content conflicts"));

      try_to_merge_files(lua, left_roster, right_roster,
                         result, adaptor, auto_merge);

      size_t remaining = result.file_content_conflicts.size();
      if (remaining > 0)
        {
          P(FP("%d content conflict requires user intervention",
               "%d content conflicts require user intervention",
               remaining) % remaining);

          // We don't spawn the merger here, because some mergers
          // (such as opendiff) require prompting the user via
          // stdin/stdout, and that's what 'non_interactive' prevents.
          // Note that 'automate stdio' sets non_interactive true,
          // because it doesn't support prompting.
          //
          // Another option would be to pass the option to the merger,
          // and let it decide. We are not doing that, because it is
          // felt this whole design is already too complicated; we are
          // working to find a better solution. See thread at
          // http://lists.gnu.org/archive/html/monotone-devel/2010-04/msg00000.html
          E(!opts.non_interactive, origin::user,
            F("can't spawn external merger when non-interactive"));

          result.report_file_content_conflicts(lua, left_roster, right_roster, adaptor, false, std::cout);

          try_to_merge_files(lua, left_roster, right_roster,
                             result, adaptor, user_merge);
        }
    }

  E(result.is_clean(), origin::user,
    F("merge failed due to unresolved conflicts"));
}

void
interactive_merge_and_store(lua_hooks & lua,
                            database & db,
                            options const & opts,
                            revision_id const & left_rid,
                            revision_id const & right_rid,
                            revision_id & merged_rid)
{
  roster_t left_roster, right_roster;
  marking_map left_marking_map, right_marking_map;
  set<revision_id> left_uncommon_ancestors, right_uncommon_ancestors;

  db.get_roster(left_rid, left_roster, left_marking_map);
  db.get_roster(right_rid, right_roster, right_marking_map);
  db.get_uncommon_ancestors(left_rid, right_rid,
                            left_uncommon_ancestors, right_uncommon_ancestors);

  roster_merge_result result;

  roster_merge(left_roster, left_marking_map, left_uncommon_ancestors,
               right_roster, right_marking_map, right_uncommon_ancestors,
               result);

  bool resolutions_given;
  temp_node_id_source nis;
  content_merge_database_adaptor dba(db, left_rid, right_rid,
                                     left_marking_map, right_marking_map,
                                     left_uncommon_ancestors, right_uncommon_ancestors);

  parse_resolve_conflicts_opts (opts, left_rid, left_roster, right_rid, right_roster, result, resolutions_given);

  resolve_merge_conflicts(lua, opts, left_roster, right_roster, result, dba, nis, resolutions_given);

  // write new files into the db
  store_roster_merge_result(db,
                            left_roster, right_roster, result,
                            left_rid, right_rid, merged_rid);
}

void
store_roster_merge_result(database & db,
                          roster_t const & left_roster,
                          roster_t const & right_roster,
                          roster_merge_result & result,
                          revision_id const & left_rid,
                          revision_id const & right_rid,
                          revision_id & merged_rid)
{
  I(result.is_clean());
  roster_t & merged_roster = result.roster;
  merged_roster.check_sane(true); // resolve conflicts can create new nodes

  revision_t merged_rev;
  merged_rev.made_for = made_for_database;

  calculate_ident(merged_roster, merged_rev.new_manifest);

  shared_ptr<cset> left_to_merged(new cset);
  make_cset(left_roster, merged_roster, *left_to_merged);
  safe_insert(merged_rev.edges, make_pair(left_rid, left_to_merged));

  shared_ptr<cset> right_to_merged(new cset);
  make_cset(right_roster, merged_roster, *right_to_merged);
  safe_insert(merged_rev.edges, make_pair(right_rid, right_to_merged));

  revision_data merged_data;
  write_revision(merged_rev, merged_data);
  calculate_ident(merged_data, merged_rid);
  {
    transaction_guard guard(db);

    db.put_revision(merged_rid, merged_rev);

    guard.commit();
  }
}

// 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: