示例#1
0
        public string Merge(string origin, string left, string right, ConflictResolutionOptions options = ConflictResolutionOptions.RaiseException)
        {
            var diffOps = new DiffOperations();

            var mergedScript = diffOps.GetDifference(origin, left)
                               .MergeWith(diffOps.GetDifference(origin, right));

            return(mergedScript.ToText());
        }
示例#2
0
 public static TValue Merge <TValue>(TValue left, TValue right,
                                     ConflictResolutionOptions options = ConflictResolutionOptions.RaiseException)
 {
     if (CompareEqual(left, right))
     {
         return(right);
     }
     else
     {
         return(ResolveConflict(left, right, options));
     }
 }
示例#3
0
        public static TValue ResolveConflict <TValue>(TValue left, TValue right, ConflictResolutionOptions options)
        {
            switch (options)
            {
            case ConflictResolutionOptions.TakeLeft:
                return(left);

            case ConflictResolutionOptions.TakeRight:
                return(right);

            default:
                throw new MergeOperationException("There is conflicting change and no other resolution option was provided.");
            }
        }
        public void Test_Merge(IDictionary <TKey, TValue> origin, IDictionary <TKey, TValue> left, IDictionary <TKey, TValue> right,
                               IDictionary <TKey, TValue> expected, ConflictResolutionOptions options, Type expectedException = null)
        {
            var mergeHandler = new DictionaryMergeHandler <TKey, TValue>();

            if (expectedException == null)
            {
                var result = mergeHandler.Merge(origin, left, right, options);

                Assert.That(result.Keys, Is.EquivalentTo(expected.Keys), "Resulting Keys should match expectation");
                Assert.That(result.Keys.All(key => result[key].Equals(expected[key])), "Resulting Values at the given Keys should be Equal");
            }
            else
            {
                Assert.Throws(expectedException, () => mergeHandler.Merge(origin, left, right, options));
            }
        }
示例#5
0
        public T Merge(T origin, T left, T right, ConflictResolutionOptions options)
        {
            if (Comparer.Equals(left, right))
            {
                return(right);
            }
            if (Comparer.Equals(origin, left))
            {
                return(right);
            }
            if (Comparer.Equals(origin, right))
            {
                return(left);
            }

            return(DoMerge(origin, left, right, options));
        }
示例#6
0
        public IDictionary <TKey, TValue> Merge(IDictionary <TKey, TValue> origin, IDictionary <TKey, TValue> left,
                                                IDictionary <TKey, TValue> right, ConflictResolutionOptions options)
        {
            if (origin == null)
            {
                throw new ArgumentNullException(nameof(origin));
            }
            if (left == null)
            {
                throw new ArgumentNullException(nameof(left));
            }
            if (right == null)
            {
                throw new ArgumentException(nameof(right));
            }

            // Empty dictionaries left and right
            if (left.Count == 0 && right.Count == 0)
            {
                return(left);
            }

            var mergedDictionary = new Dictionary <TKey, TValue>();

            var lEditScript = KeysEditScript <TKey> .From(origin.Keys, left.Keys);

            var rEditScript = KeysEditScript <TKey> .From(origin.Keys, right.Keys);

            var resultScript = lEditScript.Merge(rEditScript);

            // Chack that the key was changed either left or right, otherwise apply ConflictResolutinOptions
            foreach (TKey key in resultScript.CommonKeys)
            {
                var originValue = origin[key];
                var leftValue   = left[key];
                var rightValue  = right[key];

                var mergedValue = MergeUtils.Merge <TValue>(originValue, leftValue, rightValue, options);
                mergedDictionary.Add(key, mergedValue);
            }

            // Inserted only left
            var insLeft = lEditScript.InsertedKeys.Except(rEditScript.InsertedKeys);

            foreach (var key in insLeft)
            {
                mergedDictionary.Add(key, left[key]);
            }

            //Inserted only right
            var insRight = rEditScript.InsertedKeys.Except(lEditScript.InsertedKeys);

            foreach (var key in insRight)
            {
                mergedDictionary.Add(key, right[key]);
            }

            var commonInsertedKeys = lEditScript.InsertedKeys.Intersect(rEditScript.InsertedKeys);

            // Check that the same values was inserted, otherwise apply ConflictResolutionOptions
            foreach (TKey key in commonInsertedKeys)
            {
                var leftValue  = left[key];
                var rightValue = right[key];

                var mergedValue = MergeUtils.Merge <TValue>(leftValue, rightValue, options);
                mergedDictionary.Add(key, mergedValue);
            }

            var delConflictLeft = rEditScript.DeletedKeys.Intersect(lEditScript.CommonKeys)
                                  .Where(key => !MergeUtils.CompareEqual(origin[key], left[key]));
            var delConflictRight = lEditScript.DeletedKeys.Intersect(rEditScript.CommonKeys)
                                   .Where(key => !MergeUtils.CompareEqual(origin[key], right[key]));

            switch (options)
            {
            case ConflictResolutionOptions.TakeRight:
                foreach (var key in delConflictRight)
                {
                    mergedDictionary.Add(key, right[key]);
                }
                break;

            case ConflictResolutionOptions.TakeLeft:
                foreach (var key in delConflictLeft)
                {
                    mergedDictionary.Add(key, left[key]);
                }
                break;

            default:
                if (delConflictLeft.Count() > 0 || delConflictRight.Count() > 0)
                {
                    throw new MergeOperationException("There are some keys deleted  at one side and modified at other.");
                }
                break;
            }


            return(mergedDictionary);
        }
 protected override string DoMerge(string origin, string left, string right, ConflictResolutionOptions options)
 {
     return(_strategy.Merge(origin, left, right, options));
 }
示例#8
0
 public TValue Merge(TValue origin, TValue left, TValue right,
                     ConflictResolutionOptions options = ConflictResolutionOptions.RaiseException)
 {
     return(MergeUtils.Merge(origin, left, right, options));
 }
        protected override TextDocument DoMerge(TextDocument origin, TextDocument left, TextDocument right, ConflictResolutionOptions options)
        {
            string text = textMergeHandler.Merge(origin.Text, left.Text, right.Text, options);
            var    meta = metadataHandler.Merge(origin.Metadata, left.Metadata, right.Metadata, options);

            return(new TextDocument(text, meta));
        }
示例#10
0
 protected abstract T DoMerge(T origin, T left, T right, ConflictResolutionOptions options);
示例#11
0
 public DocumentMetadata Merge(DocumentMetadata origin, DocumentMetadata left, DocumentMetadata right, ConflictResolutionOptions options)
 {
     return(new DocumentMetadata(mergeHandler.Merge(origin, left, right, options)));
 }
示例#12
0
 public int Test_ResolveConflicts(int left, int right, ConflictResolutionOptions options)
 {
     return(MergeUtils.ResolveConflict(left, right, options));
 }
示例#13
0
 public string Test_TwoWayMerge_WithOptions(string origin, string left, string right, ConflictResolutionOptions options)
 {
     return(MergeUtils.Merge(origin, left, right, options));
 }
示例#14
0
 public string Test_MergeTwoRefValues_WithOptions(string left, string right, ConflictResolutionOptions options)
 {
     return(MergeUtils.Merge(left, right, options));
 }
示例#15
0
 public int Test_MergeTwoValues_WithOptions(int left, int right, ConflictResolutionOptions options)
 {
     return(MergeUtils.Merge(left, right, options));
 }