Exemplo n.º 1
0
 public NodeEqualityComparer(AssetDiff diffManager)
 {
     if (diffManager == null)
     {
         throw new ArgumentNullException("diffManager");
     }
     this.diffManager = diffManager;
 }
Exemplo n.º 2
0
        /// <summary>
        /// Merges the specified assets from <c>base</c> and <c>from2</c> into <c>from1</c>.
        /// </summary>
        /// <param name="assetDiff">A precomputed asset difference.</param>
        /// <param name="mergePolicy">The merge policy.</param>
        /// <param name="previewOnly">if set to <c>true</c> then the merge will not change the object.</param>
        /// <returns>MergePreviewResult.</returns>
        /// <exception cref="System.ArgumentNullException">assetDiff
        /// or
        /// mergePolicy</exception>
        public static MergeResult Merge(AssetDiff assetDiff, MergePolicyDelegate mergePolicy, bool previewOnly = false)
        {
            if (assetDiff == null)
            {
                throw new ArgumentNullException("assetDiff");
            }
            if (mergePolicy == null)
            {
                throw new ArgumentNullException("mergePolicy");
            }

            var allDiffs = assetDiff.Compute();
            var diff3    = allDiffs.FindDifferences().ToList();

            var result = new MergeResult(assetDiff.Asset1);

            // Try to merge
            foreach (var diff3Node in diff3)
            {
                Diff3ChangeType changeType;
                try
                {
                    changeType = mergePolicy(diff3Node);

                    if (changeType >= Diff3ChangeType.Conflict)
                    {
                        result.Error("Unresolved conflict [{0}] on node [{1}/{2}/{3}]", diff3Node.ChangeType, diff3Node.BaseNode, diff3Node.Asset1Node, diff3Node.Asset2Node);
                        continue;
                    }

                    // If we are in preview only mode, just skip the update
                    if (previewOnly)
                    {
                        continue;
                    }

                    // As we are merging into asset1, the only relevant changes can only come from asset2
                    if (changeType != Diff3ChangeType.MergeFromAsset2)
                    {
                        continue;
                    }

                    var dataInstance = diff3Node.Asset2Node != null ? diff3Node.Asset2Node.Instance : null;

                    // Sets the value on the node
                    diff3Node.ReplaceValue(dataInstance, node => node.Asset1Node, diff3Node.Asset2Node == null);
                }
                catch (Exception ex)
                {
                    result.Error("Unexpected error while merging [{0}] on node [{1}]", ex, diff3Node.ChangeType, diff3Node.InstanceType);
                    break;
                }
            }

            return(result);
        }
Exemplo n.º 3
0
        public static Diff3Node Compute(Asset baseAsset, Asset asset1, Asset asset2)
        {
            var diff3 = new AssetDiff(baseAsset, asset1, asset2);

            return(diff3.Compute());
        }
Exemplo n.º 4
0
        /// <summary>
        /// Merges the specified assets from <c>base</c> and <c>from2</c> into <c>from1</c>.
        /// </summary>
        /// <param name="assetDiff">A precomputed asset difference.</param>
        /// <param name="mergePolicy">The merge policy.</param>
        /// <param name="previewOnly">if set to <c>true</c> then the merge will not change the object.</param>
        /// <returns>MergePreviewResult.</returns>
        /// <exception cref="System.ArgumentNullException">assetDiff
        /// or
        /// mergePolicy</exception>
        public static MergeResult Merge(AssetDiff assetDiff, MergePolicyDelegate mergePolicy, bool previewOnly = false)
        {
            if (assetDiff == null)
            {
                throw new ArgumentNullException("assetDiff");
            }
            if (mergePolicy == null)
            {
                throw new ArgumentNullException("mergePolicy");
            }

            var allDiffs = assetDiff.Compute();
            var diff3    = allDiffs.FindDifferences().ToList();

            var result = new MergeResult(assetDiff.Asset1);

            // Try to merge
            foreach (var diff3Node in diff3)
            {
                Diff3ChangeType changeType;
                try
                {
                    changeType = mergePolicy(diff3Node);

                    if (changeType >= Diff3ChangeType.Conflict)
                    {
                        result.Error("Unresolved conflict [{0}] on node [{1}/{2}/{3}]", diff3Node.ChangeType, diff3Node.BaseNode, diff3Node.Asset1Node, diff3Node.Asset2Node);
                        continue;
                    }

                    // If we are in preview only mode, just skip the update
                    if (previewOnly)
                    {
                        continue;
                    }

                    object dataInstance = null;
                    bool   replaceValue = false;

                    switch (changeType)
                    {
                    case Diff3ChangeType.MergeFromAsset2:

                        // Because for collection, the merge is performed by the MergeContainer
                        // Skip any merge for individual items, as they should have been merged by MergeContainer
                        // TODO: This is a workaround as FindDifferences().ToList() is giving changes inside collection while we rebuild collection with MergeContainer
                        if (diff3Node.Parent == null || diff3Node.Parent.Type != Diff3NodeType.Collection)
                        {
                            // As we are merging into asset1, the only relevant changes can only come from asset2
                            dataInstance = diff3Node.Asset2Node?.Instance;
                            replaceValue = true;
                        }
                        break;

                    case Diff3ChangeType.Children:
                        MergeContainer(diff3Node, out dataInstance);
                        replaceValue = dataInstance != null;
                        break;

                    default:
                        continue;
                    }

                    // Sets the value on the node
                    if (replaceValue)
                    {
                        diff3Node.ReplaceValue(dataInstance, node => node.Asset1Node);
                    }

                    // Applies the override for this node
                    diff3Node.ApplyOverride();
                }
                catch (Exception ex)
                {
                    result.Error("Unexpected error while merging [{0}] on node [{1}]", ex, diff3Node.ChangeType, diff3Node.InstanceType);
                    break;
                }
            }

            if (!previewOnly)
            {
                foreach (var node in allDiffs.Asset1Node.Children(node => true))
                {
                    if (node.Instance is IDiffProxy)
                    {
                        ((IDiffProxy)node.Instance).ApplyChanges();
                    }
                }
            }

            return(result);
        }