static void TestValidDiff(Hashtable a, Hashtable b, HashDiff diff)
        {
            diff.Apply(a);
            HashDiff newdiff = JsonDiff.Diff(a, b);

            System.Diagnostics.Debug.Assert(newdiff.Empty());
        }
        static void TestMerge(string s_parent, string s_left, string s_right)
        {
            Hashtable parent = Sjson(s_parent);
            Hashtable left   = Sjson(s_left);
            Hashtable right  = Sjson(s_right);

            Hashtable res = JsonDiff.Merge(parent, left, right);

            System.Diagnostics.Debug.WriteLine(SJSON.Encode(res));
        }
        static void TestDiff(string s_a, string s_b)
        {
            Hashtable a = Sjson(s_a);
            Hashtable b = Sjson(s_b);

            HashDiff diff = JsonDiff.Diff(a, b);

            PrintDiff(diff);
            TestValidDiff(a, b, diff);
        }
示例#4
0
        /// <summary>
        /// Computes the three-way-merge of Json hashtables.
        /// </summary>
        public static Hashtable Merge(Hashtable parent, Hashtable left, Hashtable right)
        {
            HashDiff left_diff  = JsonDiff.Diff(parent, left);
            HashDiff right_diff = JsonDiff.Diff(parent, right);
            HashDiff diff       = HashDiff.Merge(left_diff, right_diff);

            Hashtable res = parent.DeepClone();

            diff.Apply(res);
            return(res);
        }
示例#5
0
        /// <summary>
        /// Computes the three-way-merge of Json hashtables and returns the intermediate diff results.
        /// </summary>
        public static Hashtable MergeDetailed(Hashtable parent, Hashtable left, Hashtable right,
                                              out HashDiff left_diff, out HashDiff right_diff, out HashDiff merged_diff)
        {
            left_diff   = JsonDiff.Diff(parent, left);
            right_diff  = JsonDiff.Diff(parent, right);
            merged_diff = HashDiff.Merge(left_diff, right_diff);

            Hashtable res = parent.DeepClone();

            merged_diff.Apply(res);
            return(res);
        }
示例#6
0
        static void Main(string[] args)
        {
            // We need to set this to prevent parse errors in JSON/SJSON parsing.
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

            // Print usage information if there where no arguments.
            if (args.Length == 0)
            {
                PrintUsage();
            }

            // -hold can be used as a debugging tool, to give a debugger the chance
            // to attach to the process when it is just starting up.
            bool escape_hold = false;

            int i = 0;

            while (i < args.Length)
            {
                if (args[i] == "-json")
                {
                    _use_json = true;
                    ++i;
                }
                else if (args[i] == "-window")
                {
                    _window = true;
                    ++i;
                }
                else if (args[i] == "-diff")
                {
                    if (i + 2 >= args.Length)
                    {
                        Error("Not enough arguments for -diff");
                    }
                    Hashtable a    = Load(args[i + 1]);
                    Hashtable b    = Load(args[i + 2]);
                    HashDiff  diff = JsonDiff.Diff(a, b);
                    if (_window)
                    {
                        DiffVisualForm form = new DiffVisualForm(a, b, diff, _use_json);
                        form.ShowDialog();
                    }
                    else
                    {
                        StringBuilder sb = new StringBuilder();
                        diff.Write(sb);
                        Show(sb.ToString(), "Diff");
                    }
                    i += 3;
                }
                else if (args[i] == "-merge")
                {
                    if (i + 4 >= args.Length)
                    {
                        Error("Not enough arguments for -merge");
                    }
                    Hashtable parent = Load(args[i + 1]);
                    Hashtable theirs = Load(args[i + 2]);
                    Hashtable mine   = Load(args[i + 3]);
                    if (_window)
                    {
                        HashDiff  theirs_diff, mine_diff, merged_diff;
                        Hashtable result = JsonDiff.MergeDetailed(parent, theirs, mine,
                                                                  out theirs_diff, out mine_diff, out merged_diff);
                        MergeVisualForm form = new MergeVisualForm(parent, theirs, theirs_diff,
                                                                   mine, mine_diff, result, merged_diff, _use_json);
                        form.ShowDialog();
                        Save(result, args[i + 4]);
                    }
                    else
                    {
                        Hashtable result = JsonDiff.Merge(parent, theirs, mine);
                        Save(result, args[i + 4]);
                    }

                    // Remove source files (should their be an option for this?)
                    File.Delete(args[i + 1]);
                    File.Delete(args[i + 2]);
                    File.Delete(args[i + 3]);
                    i += 5;
                }
                else if (args[i] == "-help")
                {
                    PrintUsage();
                    ++i;
                }
                else if (args[i] == "-hold")
                {
                    while (!escape_hold)
                    {
                        ;
                    }
                    ++i;
                }
                else if (args[i] == "-test")
                {
                    Test.TestAll();
                    ++i;
                }
            }
        }