/// <summary>
 /// Initializes a new instance of <see cref="DiffCommandHandler"/>
 /// </summary>
 /// <param name="bus"></param>
 /// <param name="diffEngine"></param>
 /// <param name="payLoadRepository"></param>
 /// <param name="cache"></param>
 public DiffCommandHandler(IMediatorHandler bus, IDiffEngine diffEngine, IPayLoadRepository payLoadRepository,
                           ICache cache)
 {
     _bus               = bus ?? throw new ArgumentNullException(nameof(bus));
     _diffEngine        = diffEngine ?? throw new ArgumentNullException(nameof(diffEngine));
     _payLoadRepository = payLoadRepository ?? throw new ArgumentNullException(nameof(payLoadRepository));
     _cache             = cache ?? throw new ArgumentNullException(nameof(cache));
 }
Esempio n. 2
0
        public DiffCommandHandlerTests()
        {
            _bus = Substitute.For <IMediatorHandler>();
            _payLoadRepository = Substitute.For <IPayLoadRepository>();
            _diffEngine        = Substitute.For <IDiffEngine>();
            _cache             = Substitute.For <ICache>();

            _sut = new DiffCommandHandler(_bus, _diffEngine, _payLoadRepository, _cache);
        }
Esempio n. 3
0
        public static int Main(string[] args)
        {
            if (args.Length < 2)
            {
                Usage(Console.Out);
                return(-1);
            }

            string input1 = args[0];
            string input2 = args[1];

            string[] options = new string[args.Length - 2];
            for (int i = 0; i < options.Length; i++)
            {
                options[i] = args[i + 2];
            }

            TextWriter output = Console.Out;

            for (int i = 0; i < options.Length - 1; i++)
            {
                switch (options[i].ToLower())
                {
                case "/o": goto case "-output";

                case "-o": goto case "-output";

                case "/output": goto case "-output";

                case "-output": output = new StreamWriter(options[i + 1], false, Encoding.UTF8); break;
                }
            }

            IDiffEngineFactory diffFactory = new BestQualityDiffEngineFactory(new IDiffEngine[]
            {
                new DirectoryDiffEngine(),
                new FileDiffEngine(),
                new VersionedFileDiffEngine(),
                new TextFileDiffEngine(),
                new MsiDiffEngine(),
                new CabDiffEngine(),
                new MspDiffEngine(),
            });

            IDiffEngine diffEngine = diffFactory.GetDiffEngine(input1, input2, options);

            if (diffEngine != null)
            {
                bool different = diffEngine.GetDiff(input1, input2, options, output, "", diffFactory);
                return(different ? 1 : 0);
            }
            else
            {
                Console.Error.WriteLine("Dont know how to diff those inputs.");
                return(-1);
            }
        }
Esempio n. 4
0
        public virtual IDiffEngine GetDiffEngine(string diffInput1, string diffInput2, string[] options)
        {
            float       bestDiffQuality = 0;
            IDiffEngine bestDiffEngine  = null;

            foreach (IDiffEngine diffEngine in diffEngines)
            {
                float diffQuality = diffEngine.GetDiffQuality(diffInput1, diffInput2, options, this);
                if (diffQuality > bestDiffQuality)
                {
                    bestDiffQuality = diffQuality;
                    bestDiffEngine  = diffEngine;
                }
            }
            return(bestDiffEngine != null ? bestDiffEngine.Clone() : null);
        }
Esempio n. 5
0
        public bool GetDiff(string diffInput1, string diffInput2, string[] options, TextWriter diffOutput, string linePrefix, IDiffEngineFactory diffFactory)
        {
            bool      difference  = false;
            IComparer caseInsComp = CaseInsensitiveComparer.Default;

            string[] files1 = Directory.GetFiles(diffInput1);
            string[] files2 = Directory.GetFiles(diffInput2);
            for (int i1 = 0; i1 < files1.Length; i1++)
            {
                files1[i1] = Path.GetFileName(files1[i1]);
            }
            for (int i2 = 0; i2 < files2.Length; i2++)
            {
                files2[i2] = Path.GetFileName(files2[i2]);
            }
            Array.Sort(files1, caseInsComp);
            Array.Sort(files2, caseInsComp);

            for (int i1 = 0, i2 = 0; i1 < files1.Length || i2 < files2.Length;)
            {
                int comp;
                if (i1 == files1.Length)
                {
                    comp = 1;
                }
                else if (i2 == files2.Length)
                {
                    comp = -1;
                }
                else
                {
                    comp = caseInsComp.Compare(files1[i1], files2[i2]);
                }
                if (comp < 0)
                {
                    diffOutput.WriteLine("{0}< {1}", linePrefix, files1[i1]);
                    i1++;
                    difference = true;
                }
                else if (comp > 0)
                {
                    diffOutput.WriteLine("{0}> {1}", linePrefix, files2[i2]);
                    i2++;
                    difference = true;
                }
                else
                {
                    string       file1      = Path.Combine(diffInput1, files1[i1]);
                    string       file2      = Path.Combine(diffInput2, files2[i2]);
                    IDiffEngine  diffEngine = diffFactory.GetDiffEngine(file1, file2, options);
                    StringWriter sw         = new StringWriter();
                    if (diffEngine.GetDiff(file1, file2, options, sw, linePrefix + "    ", diffFactory))
                    {
                        diffOutput.WriteLine("{0}{1}", linePrefix, files1[i1]);
                        diffOutput.Write(sw.ToString());
                        difference = true;
                    }
                    i1++;
                    i2++;
                }
            }

            string[] dirs1 = Directory.GetDirectories(diffInput1);
            string[] dirs2 = Directory.GetDirectories(diffInput2);
            for (int i1 = 0; i1 < dirs1.Length; i1++)
            {
                dirs1[i1] = Path.GetFileName(dirs1[i1]);
            }
            for (int i2 = 0; i2 < dirs2.Length; i2++)
            {
                dirs2[i2] = Path.GetFileName(dirs2[i2]);
            }
            Array.Sort(dirs1, caseInsComp);
            Array.Sort(dirs2, caseInsComp);

            for (int i1 = 0, i2 = 0; i1 < dirs1.Length || i2 < dirs2.Length;)
            {
                int comp;
                if (i1 == dirs1.Length)
                {
                    comp = 1;
                }
                else if (i2 == dirs2.Length)
                {
                    comp = -1;
                }
                else
                {
                    comp = caseInsComp.Compare(dirs1[i1], dirs2[i2]);
                }
                if (comp < 0)
                {
                    diffOutput.WriteLine("{0}< {1}", linePrefix, dirs1[i1]);
                    i1++;
                    difference = true;
                }
                else if (comp > 0)
                {
                    diffOutput.WriteLine("{0}> {1}", linePrefix, dirs2[i2]);
                    i2++;
                    difference = true;
                }
                else
                {
                    string       dir1       = Path.Combine(diffInput1, dirs1[i1]);
                    string       dir2       = Path.Combine(diffInput2, dirs2[i2]);
                    IDiffEngine  diffEngine = diffFactory.GetDiffEngine(dir1, dir2, options);
                    StringWriter sw         = new StringWriter();
                    if (diffEngine.GetDiff(dir1, dir2, options, sw, linePrefix + "    ", diffFactory))
                    {
                        diffOutput.WriteLine("{0}{1}\\", linePrefix, dirs1[i1]);
                        diffOutput.Write(sw.ToString());
                        difference = true;
                    }
                    i1++;
                    i2++;
                }
            }
            return(difference);
        }
Esempio n. 6
0
 public virtual void Remove(IDiffEngine diffEngine)
 {
     diffEngines.Remove(diffEngine);
 }
Esempio n. 7
0
 public virtual void Add(IDiffEngine diffEngine)
 {
     diffEngines.Add(diffEngine);
 }
Esempio n. 8
0
 public BestQualityDiffEngineFactory(IDiffEngine[] diffEngines)
 {
     this.diffEngines = (diffEngines != null ? new ArrayList(diffEngines) : new ArrayList());
 }
Esempio n. 9
0
 public virtual void Remove(IDiffEngine diffEngine)
 {
     diffEngines.Remove(diffEngine);
 }
Esempio n. 10
0
 public virtual void Add(IDiffEngine diffEngine)
 {
     diffEngines.Add(diffEngine);
 }
        public bool GetDiff(string diffInput1, string diffInput2, string[] options, TextWriter diffOutput, string linePrefix, IDiffEngineFactory diffFactory)
        {
            bool      difference  = false;
            IComparer caseInsComp = CaseInsensitiveComparer.Default;

            // TODO: Make this faster by extracting the whole cab at once.
            // TODO: Optimize for the match case by first comparing the whole cab files.

            CabInfo             cab1          = new CabInfo(diffInput1);
            CabInfo             cab2          = new CabInfo(diffInput2);
            IList <CabFileInfo> cabFilesList1 = cab1.GetFiles();
            IList <CabFileInfo> cabFilesList2 = cab2.GetFiles();

            CabFileInfo[] cabFiles1 = new CabFileInfo[cabFilesList1.Count];
            CabFileInfo[] cabFiles2 = new CabFileInfo[cabFilesList2.Count];
            cabFilesList1.CopyTo(cabFiles1, 0);
            cabFilesList2.CopyTo(cabFiles2, 0);
            string[] files1 = new string[cabFiles1.Length];
            string[] files2 = new string[cabFiles2.Length];
            for (int i1 = 0; i1 < cabFiles1.Length; i1++)
            {
                files1[i1] = cabFiles1[i1].Name;
            }
            for (int i2 = 0; i2 < cabFiles2.Length; i2++)
            {
                files2[i2] = cabFiles2[i2].Name;
            }
            Array.Sort(files1, cabFiles1, caseInsComp);
            Array.Sort(files2, cabFiles2, caseInsComp);


            for (int i1 = 0, i2 = 0; i1 < files1.Length || i2 < files2.Length;)
            {
                int comp;
                if (i1 == files1.Length)
                {
                    comp = 1;
                }
                else if (i2 == files2.Length)
                {
                    comp = -1;
                }
                else
                {
                    comp = caseInsComp.Compare(files1[i1], files2[i2]);
                }
                if (comp < 0)
                {
                    diffOutput.WriteLine("{0}< {1}", linePrefix, files1[i1]);
                    i1++;
                    difference = true;
                }
                else if (comp > 0)
                {
                    diffOutput.WriteLine("{0}> {1}", linePrefix, files2[i2]);
                    i2++;
                    difference = true;
                }
                else
                {
                    string tempFile1 = Path.GetTempFileName();
                    string tempFile2 = Path.GetTempFileName();
                    cabFiles1[i1].CopyTo(tempFile1, true);
                    cabFiles2[i2].CopyTo(tempFile2, true);
                    IDiffEngine  diffEngine = diffFactory.GetDiffEngine(tempFile1, tempFile2, options);
                    StringWriter sw         = new StringWriter();
                    if (diffEngine.GetDiff(tempFile1, tempFile2, options, sw, linePrefix + "    ", diffFactory))
                    {
                        diffOutput.WriteLine("{0}{1}", linePrefix, files1[i1]);
                        diffOutput.Write(sw.ToString());
                        difference = true;
                    }

                    File.SetAttributes(tempFile1, File.GetAttributes(tempFile1) & ~FileAttributes.ReadOnly);
                    File.SetAttributes(tempFile2, File.GetAttributes(tempFile2) & ~FileAttributes.ReadOnly);
                    try
                    {
                        File.Delete(tempFile1);
                        File.Delete(tempFile2);
                    }
                    catch (IOException)
                    {
#if DEBUG
                        Console.WriteLine("Could not delete temporary files {0} and {1}", tempFile1, tempFile2);
#endif
                    }
                    i1++;
                    i2++;
                }
            }

            return(difference);
        }
        protected bool GetStreamsDiff(Database db1, Database db2, string[] options, TextWriter diffOutput, string linePrefix, IDiffEngineFactory diffFactory)
        {
            bool difference = false;

            IList <string> streams1List = db1.ExecuteStringQuery("SELECT `Name` FROM `_Streams`");
            IList <string> streams2List = db2.ExecuteStringQuery("SELECT `Name` FROM `_Streams`");

            string[] streams1 = new string[streams1List.Count];
            string[] streams2 = new string[streams2List.Count];
            streams1List.CopyTo(streams1, 0);
            streams2List.CopyTo(streams2, 0);

            IComparer caseInsComp = CaseInsensitiveComparer.Default;

            Array.Sort(streams1, caseInsComp);
            Array.Sort(streams2, caseInsComp);

            for (int i1 = 0, i2 = 0; i1 < streams1.Length || i2 < streams2.Length;)
            {
                int comp;
                if (i1 == streams1.Length)
                {
                    comp = 1;
                }
                else if (i2 == streams2.Length)
                {
                    comp = -1;
                }
                else
                {
                    comp = caseInsComp.Compare(streams1[i1], streams2[i2]);
                }
                if (comp < 0)
                {
                    diffOutput.WriteLine("{0}< {1}", linePrefix, streams1[i1]);
                    i1++;
                    difference = true;
                }
                else if (comp > 0)
                {
                    diffOutput.WriteLine("{0}> {1}", linePrefix, streams2[i2]);
                    i2++;
                    difference = true;
                }
                else
                {
                    if (streams1[i1] != ("" + ((char)5) + "SummaryInformation"))
                    {
                        string tempFile1 = Path.GetTempFileName();
                        string tempFile2 = Path.GetTempFileName();

                        using (View view = db1.OpenView(String.Format("SELECT `Data` FROM `_Streams` WHERE `Name` = '{0}'", streams1[i1])))
                        {
                            view.Execute();

                            using (Record rec = view.Fetch())
                            {
                                rec.GetStream(1, tempFile1);
                            }
                        }

                        using (View view = db2.OpenView(String.Format("SELECT `Data` FROM `_Streams` WHERE `Name` = '{0}'", streams2[i2])))
                        {
                            view.Execute();

                            using (Record rec = view.Fetch())
                            {
                                rec.GetStream(1, tempFile2);
                            }
                        }

                        IDiffEngine  diffEngine = diffFactory.GetDiffEngine(tempFile1, tempFile2, options);
                        StringWriter sw         = new StringWriter();
                        if (diffEngine.GetDiff(tempFile1, tempFile2, options, sw, linePrefix + "    ", diffFactory))
                        {
                            diffOutput.WriteLine("{0}{1}", linePrefix, streams1[i1]);
                            diffOutput.Write(sw.ToString());
                            difference = true;
                        }

                        File.Delete(tempFile1);
                        File.Delete(tempFile2);
                    }
                    i1++;
                    i2++;
                }
            }

            return(difference);
        }