public IEnumerable<HunkRangeInfo> GetGitDiffFor(ITextDocument textDocument, ITextSnapshot snapshot)
        {
            string fileName = textDocument.FilePath;
            GitFileStatusTracker tracker = new GitFileStatusTracker(Path.GetDirectoryName(fileName));
            if (!tracker.IsGit)
                yield break;

            GitFileStatus status = tracker.GetFileStatus(fileName);
            if (status == GitFileStatus.New || status == GitFileStatus.Added)
                yield break;

            HistogramDiff diff = new HistogramDiff();
            diff.SetFallbackAlgorithm(null);
            string currentText = snapshot.GetText();

            byte[] preamble = textDocument.Encoding.GetPreamble();
            byte[] content = textDocument.Encoding.GetBytes(currentText);
            if (preamble.Length > 0)
            {
                byte[] completeContent = new byte[preamble.Length + content.Length];
                Buffer.BlockCopy(preamble, 0, completeContent, 0, preamble.Length);
                Buffer.BlockCopy(content, 0, completeContent, preamble.Length, content.Length);
                content = completeContent;
            }

            byte[] previousContent = null; //GetPreviousRevision(tracker, fileName);
            RawText b = new RawText(content);
            RawText a = new RawText(previousContent ?? new byte[0]);
            EditList edits = diff.Diff(RawTextComparator.DEFAULT, a, b);
            foreach (Edit edit in edits)
                yield return new HunkRangeInfo(snapshot, edit, a, b);
        }
        public string DiffFile(string fileName, string commitId1, string commitId2)
        {
            try
            {
                if (!this.HasGitRepository)
                {
                    return("");
                }

                var tmpFileName = Path.ChangeExtension(Path.GetTempFileName(), ".diff");
                var fileNameRel = GetRelativeFileName(fileName);

                if (GitBash.Exists)
                {
                    GitBash.RunCmd(string.Format("diff {2} {3} -- \"{0}\" > \"{1}\"", fileNameRel, tmpFileName, commitId1, commitId2), this.GitWorkingDirectory);
                }
                else
                {
                    HistogramDiff hd = new HistogramDiff();
                    hd.SetFallbackAlgorithm(null);

                    RawText a = string.IsNullOrWhiteSpace(commitId1) ? new RawText(new byte[0]) :
                                new RawText(this.RepositoryGraph.GetFileContent(commitId1, fileNameRel) ?? new byte[0]);
                    RawText b = string.IsNullOrWhiteSpace(commitId2) ? new RawText(new byte[0]) :
                                new RawText(this.RepositoryGraph.GetFileContent(commitId2, fileNameRel) ?? new byte[0]);

                    var list = hd.Diff(RawTextComparator.DEFAULT, a, b);

                    using (Stream stream = new FileStream(tmpFileName, System.IO.FileMode.CreateNew))
                    {
                        DiffFormatter df = new DiffFormatter(stream);
                        df.Format(list, a, b);
                        df.Flush();
                    }

                    //using (Stream mstream = new MemoryStream(),
                    //      stream = new BufferedStream(mstream))
                    //{
                    //    DiffFormatter df = new DiffFormatter(stream);
                    //    df.Format(list, a, b);
                    //    df.Flush();
                    //    stream.Seek(0, SeekOrigin.Begin);
                    //    var ret = new StreamReader(stream).ReadToEnd();
                    //    ret = ret.Replace("\r", "").Replace("\n", "\r\n");
                    //    File.WriteAllText(tmpFileName, ret);
                    //}
                }

                return(tmpFileName);
            }
            catch (Exception ex)
            {
                Log.WriteLine("Refresh: {0}\r\n{1}", this.initFolder, ex.ToString());

                return("");
            }
        }
示例#3
0
        public IEnumerable <HunkRangeInfo> GetGitDiffFor(ITextDocument textDocument, ITextSnapshot snapshot)
        {
            string fileName = textDocument.FilePath;
            GitFileStatusTracker tracker = new GitFileStatusTracker(Path.GetDirectoryName(fileName));

            if (!tracker.IsGit)
            {
                yield break;
            }

            GitFileStatus status = tracker.GetFileStatus(fileName);

            if (status == GitFileStatus.New || status == GitFileStatus.Added)
            {
                yield break;
            }

            HistogramDiff diff = new HistogramDiff();

            diff.SetFallbackAlgorithm(null);
            string currentText = snapshot.GetText();

            byte[] preamble = textDocument.Encoding.GetPreamble();
            byte[] content  = textDocument.Encoding.GetBytes(currentText);
            if (preamble.Length > 0)
            {
                byte[] completeContent = new byte[preamble.Length + content.Length];
                Buffer.BlockCopy(preamble, 0, completeContent, 0, preamble.Length);
                Buffer.BlockCopy(content, 0, completeContent, preamble.Length, content.Length);
                content = completeContent;
            }

            byte[]   previousContent = null; //GetPreviousRevision(tracker, fileName);
            RawText  b     = new RawText(content);
            RawText  a     = new RawText(previousContent ?? new byte[0]);
            EditList edits = diff.Diff(RawTextComparator.DEFAULT, a, b);

            foreach (Edit edit in edits)
            {
                yield return(new HunkRangeInfo(snapshot, edit, a, b));
            }
        }
        /// <summary>
        /// Diff working file with last commit
        /// </summary>
        /// <param name="fileName">Expect relative path</param>
        /// <returns>diff file in temp folder</returns>
        public string DiffFile(string fileName)
        {
            try
            {
                if (!this.HasGitRepository)
                {
                    return("");
                }

                var tmpFileName = Path.ChangeExtension(Path.GetTempFileName(), ".diff");
                var status      = GetFileStatus(fileName);
                if (head == null || status == GitFileStatus.New || status == GitFileStatus.Added)
                {
                    tmpFileName = Path.ChangeExtension(tmpFileName, Path.GetExtension(fileName));
                    File.Copy(GetFullPath(fileName), tmpFileName);
                    return(tmpFileName);
                }

                if (GitBash.Exists)
                {
                    var fileNameRel = GetRelativeFileName(fileName);

                    GitBash.RunCmd(string.Format("diff HEAD -- \"{0}\" > \"{1}\"", fileNameRel, tmpFileName), this.GitWorkingDirectory);
                }
                else
                {
                    HistogramDiff hd = new HistogramDiff();
                    hd.SetFallbackAlgorithm(null);

                    var fullName = GetFullPath(fileName);

                    RawText b = new RawText(File.Exists(GetFullPath(fileName)) ?
                                            File.ReadAllBytes(fullName) : new byte[0]);
                    RawText a = new RawText(GetFileContent(fileName) ?? new byte[0]);

                    var list = hd.Diff(RawTextComparator.DEFAULT, a, b);

                    using (Stream stream = File.Create(tmpFileName))
                    {
                        DiffFormatter df = new DiffFormatter(stream);
                        df.Format(list, a, b);
                        df.Flush();
                    }

                    //using (Stream mstream = new MemoryStream(),
                    //              stream = new BufferedStream(mstream))
                    //{
                    //    DiffFormatter df = new DiffFormatter(stream);
                    //    df.Format(list, a, b);
                    //    df.Flush();
                    //    stream.Seek(0, SeekOrigin.Begin);
                    //    var ret = new StreamReader(stream).ReadToEnd();
                    //    File.WriteAllText(tmpFileName, ret);
                    //}
                }

                return(tmpFileName);
            }
            catch (Exception ex)
            {
                Log.WriteLine("Refresh: {0}\r\n{1}", this.initFolder, ex.ToString());

                return("");
            }
        }
        /// <summary>
        /// Diff working file with last commit
        /// </summary>
        /// <param name="fileName">Expect relative path</param>
        /// <returns>diff file in temp folder</returns>
        public string DiffFile(string fileName)
        {
            var tmpFileName = Path.ChangeExtension(Path.GetTempFileName(), ".diff");

            try
            {
                if (!this.HasGitRepository)
                {
                    return("");
                }

                var status = GetFileStatus(fileName);
                if (head == null || status == GitFileStatus.New || status == GitFileStatus.Added)
                {
                    tmpFileName = Path.ChangeExtension(tmpFileName, Path.GetExtension(fileName));
                    File.Copy(GetFullPath(fileName), tmpFileName);

                    if (IsBinaryFile(tmpFileName))
                    {
                        File.Delete(tmpFileName);
                        File.WriteAllText(tmpFileName, "Binary file: " + fileName);
                    }
                    return(tmpFileName);
                }

                if (GitBash.Exists)
                {
                    var fileNameRel = GetRelativeFileName(fileName);

                    GitBash.RunCmd(string.Format("diff HEAD -- \"{0}\" > \"{1}\"", fileNameRel, tmpFileName), this.GitWorkingDirectory);
                }
                else
                {
                    HistogramDiff hd = new HistogramDiff();
                    hd.SetFallbackAlgorithm(null);

                    var fullName = GetFullPath(fileName);

                    RawText b = new RawText(File.Exists(GetFullPath(fileName)) ?
                                            File.ReadAllBytes(fullName) : new byte[0]);
                    RawText a = new RawText(GetFileContent(fileName) ?? new byte[0]);

                    var list = hd.Diff(RawTextComparator.DEFAULT, a, b);

                    using (Stream stream = File.Create(tmpFileName))
                    {
                        DiffFormatter df = new DiffFormatter(stream);
                        df.Format(list, a, b);
                        df.Flush();
                    }
                }

                // normalize the line endings of the diff so VS doesn't ask to do it for us
                string text = File.ReadAllText(tmpFileName).Replace("\r\n", "\n").Replace('\r', '\n');
                // normalize to Environment.NewLine since this is only used for display in the IDE
                // and we want users to be able to copy from the diff and paste in a document without
                // changing line endings
                File.WriteAllText(tmpFileName, text.Replace("\n", Environment.NewLine));
            }
            catch (Exception ex)
            {
                Log.WriteLine("DiffFile: {0}\r\n{1}", this.initFolder, ex.ToString());
                //File.WriteAllText(tmpFileName, ex.ToString());
            }
            return(tmpFileName);
        }
        public string DiffFile(string fileName, string commitId1, string commitId2)
        {
            try
            {
                if (!this.HasGitRepository) return "";

                var tmpFileName = Path.ChangeExtension(Path.GetTempFileName(), ".diff");
                var fileNameRel = GetRelativeFileName(fileName);

                if (GitBash.Exists)
                {
                    GitBash.RunCmd(string.Format("diff {2} {3} -- \"{0}\" > \"{1}\"", fileNameRel, tmpFileName, commitId1, commitId2), this.GitWorkingDirectory);
                }
                else
                {
                    HistogramDiff hd = new HistogramDiff();
                    hd.SetFallbackAlgorithm(null);

                    RawText a = string.IsNullOrWhiteSpace(commitId1) ? new RawText(new byte[0]) :
                        new RawText(this.RepositoryGraph.GetFileContent(commitId1, fileNameRel) ?? new byte[0]);
                    RawText b = string.IsNullOrWhiteSpace(commitId2) ? new RawText(new byte[0]) :
                        new RawText(this.RepositoryGraph.GetFileContent(commitId2, fileNameRel) ?? new byte[0]);

                    var list = hd.Diff(RawTextComparator.DEFAULT, a, b);

                    using (Stream stream = new FileStream(tmpFileName, System.IO.FileMode.CreateNew))
                    {
                        DiffFormatter df = new DiffFormatter(stream);
                        df.Format(list, a, b);
                        df.Flush();
                    }

                    //using (Stream mstream = new MemoryStream(),
                    //      stream = new BufferedStream(mstream))
                    //{
                    //    DiffFormatter df = new DiffFormatter(stream);
                    //    df.Format(list, a, b);
                    //    df.Flush();
                    //    stream.Seek(0, SeekOrigin.Begin);
                    //    var ret = new StreamReader(stream).ReadToEnd();
                    //    ret = ret.Replace("\r", "").Replace("\n", "\r\n");
                    //    File.WriteAllText(tmpFileName, ret);
                    //}

                }

                return tmpFileName;
            }
            catch (Exception ex)
            {
                Log.WriteLine("Refresh: {0}\r\n{1}", this.initFolder, ex.ToString());

                return "";
            }
        }
        /// <summary>
        /// Diff working file with last commit
        /// </summary>
        /// <param name="fileName">Expect relative path</param>
        /// <returns>diff file in temp folder</returns>
        public string DiffFile(string fileName)
        {
            var tmpFileName = Path.ChangeExtension(Path.GetTempFileName(), ".diff");

            try
            {
                if (!this.HasGitRepository) return "";

                var status = GetFileStatus(fileName);
                if (head == null || status == GitFileStatus.New || status == GitFileStatus.Added)
                {
                    tmpFileName = Path.ChangeExtension(tmpFileName, Path.GetExtension(fileName));
                    File.Copy(GetFullPath(fileName), tmpFileName);

                    if (IsBinaryFile(tmpFileName))
                    {
                        File.Delete(tmpFileName);
                        File.WriteAllText(tmpFileName, "Binary file: " + fileName);
                    }
                    return tmpFileName;
                }

                if (GitBash.Exists)
                {
                    var fileNameRel = GetRelativeFileName(fileName);

                    GitBash.RunCmd(string.Format("diff HEAD -- \"{0}\" > \"{1}\"", fileNameRel, tmpFileName), this.GitWorkingDirectory);
                }
                else
                {
                    HistogramDiff hd = new HistogramDiff();
                    hd.SetFallbackAlgorithm(null);

                    var fullName = GetFullPath(fileName);

                    RawText b = new RawText(File.Exists(GetFullPath(fileName)) ?
                                            File.ReadAllBytes(fullName) : new byte[0]);
                    RawText a = new RawText(GetFileContent(fileName) ?? new byte[0]);

                    var list = hd.Diff(RawTextComparator.DEFAULT, a, b);

                    using (Stream stream = File.Create(tmpFileName))
                    {
                        DiffFormatter df = new DiffFormatter(stream);
                        df.Format(list, a, b);
                        df.Flush();
                    }
                }

                // normalize the line endings of the diff so VS doesn't ask to do it for us
                string text = File.ReadAllText(tmpFileName).Replace("\r\n", "\n").Replace('\r', '\n');
                // normalize to Environment.NewLine since this is only used for display in the IDE
                // and we want users to be able to copy from the diff and paste in a document without
                // changing line endings
                File.WriteAllText(tmpFileName, text.Replace("\n", Environment.NewLine));
            }
            catch (Exception ex)
            {
                Log.WriteLine("DiffFile: {0}\r\n{1}", this.initFolder, ex.ToString());
                //File.WriteAllText(tmpFileName, ex.ToString());
            }
            return tmpFileName;
        }
        /// <summary>
        /// Diff working file with last commit
        /// </summary>
        /// <param name="fileName">Expect relative path</param>
        /// <returns>diff file in temp folder</returns>
        public string DiffFile(string fileName)
        {
            try
            {
                if (!this.HasGitRepository) return "";

                var tmpFileName = Path.ChangeExtension(Path.GetTempFileName(), ".diff");
                var status = GetFileStatus(fileName);
                if (head == null || status == GitFileStatus.New || status == GitFileStatus.Added)
                {
                    tmpFileName = Path.ChangeExtension(tmpFileName, Path.GetExtension(fileName));
                    File.Copy(GetFullPath(fileName), tmpFileName);
                    return tmpFileName;
                }

                if (GitBash.Exists)
                {
                    var fileNameRel = GetRelativeFileName(fileName);

                    GitBash.RunCmd(string.Format("diff HEAD -- \"{0}\" > \"{1}\"", fileNameRel, tmpFileName), this.GitWorkingDirectory);
                }
                else
                {
                    HistogramDiff hd = new HistogramDiff();
                    hd.SetFallbackAlgorithm(null);

                    var fullName = GetFullPath(fileName);

                    RawText b = new RawText(File.Exists(GetFullPath(fileName)) ?
                                            File.ReadAllBytes(fullName) : new byte[0]);
                    RawText a = new RawText(GetFileContent(fileName) ?? new byte[0]);

                    var list = hd.Diff(RawTextComparator.DEFAULT, a, b);

                    using (Stream stream = File.Create(tmpFileName))
                    {
                        DiffFormatter df = new DiffFormatter(stream);
                        df.Format(list, a, b);
                        df.Flush();
                    }

                    //using (Stream mstream = new MemoryStream(),
                    //              stream = new BufferedStream(mstream))
                    //{
                    //    DiffFormatter df = new DiffFormatter(stream);
                    //    df.Format(list, a, b);
                    //    df.Flush();
                    //    stream.Seek(0, SeekOrigin.Begin);
                    //    var ret = new StreamReader(stream).ReadToEnd();
                    //    File.WriteAllText(tmpFileName, ret);
                    //}
                }

                return tmpFileName;
            }
            catch (Exception ex)
            {
                Log.WriteLine("Refresh: {0}\r\n{1}", this.initFolder, ex.ToString());

                return "";
            }
        }
        private void patchList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var selection = (this.patchList.SelectedItem) as Change;
            if (selection != null)
            {
                txtFileName.Text = "Diff: " + selection.Name;
                var dispatcher = Dispatcher.CurrentDispatcher;
                Action act = () =>
                {
                    HistogramDiff hd = new HistogramDiff();
                    hd.SetFallbackAlgorithm(null);

                    RawText a = string.IsNullOrWhiteSpace(commitId1) ? new RawText(new byte[0]) :
                        new RawText(tracker.RepositoryGraph.GetFileContent(commitId1, selection.Name) ?? new byte[0]);
                    RawText b = string.IsNullOrWhiteSpace(commitId2) ? new RawText(new byte[0]) :
                        new RawText(tracker.RepositoryGraph.GetFileContent(commitId2, selection.Name) ?? new byte[0]);

                    var list = hd.Diff(RawTextComparator.DEFAULT, a, b);

                    var tmpFileName = Path.ChangeExtension(Path.GetTempFileName(), ".diff");

                    //using (Stream stream = new FileStream(tmpFileName, FileMode.CreateNew))
                    //{
                    //    DiffFormatter df = new DiffFormatter(stream);
                    //    df.Format(list, a, b);
                    //    df.Flush();
                    //}

                    using (Stream mstream = new MemoryStream(),
                          stream = new BufferedStream(mstream))
                    {
                        DiffFormatter df = new DiffFormatter(stream);
                        df.Format(list, a, b);
                        df.Flush();
                        stream.Seek(0, SeekOrigin.Begin);
                        var ret = new StreamReader(stream).ReadToEnd();
                        ret = ret.Replace("\r", "").Replace("\n", "\r\n");
                        File.WriteAllText(tmpFileName, ret);
                    }

                    ShowFile(tmpFileName);
                };

                dispatcher.BeginInvoke(act, DispatcherPriority.ApplicationIdle);
            }
        }
        /// <summary>
        /// Diff working file with last commit
        /// </summary>
        /// <param name="fileName">Expect relative path</param>
        /// <returns></returns>
        public string DiffFile(string fileName)
        {
            try
            {
                if (!this.HasGitRepository) return "";

                HistogramDiff hd = new HistogramDiff();
                hd.SetFallbackAlgorithm(null);

                var fullName = GetFullPath(fileName);

                RawText b = new RawText(File.Exists(GetFullPath(fileName)) ?
                                        File.ReadAllBytes(fullName) : new byte[0]);
                RawText a = new RawText(GetFileContent(fileName) ?? new byte[0]);

                var list = hd.Diff(RawTextComparator.DEFAULT, a, b);

                using (Stream mstream = new MemoryStream(),
                              stream = new BufferedStream(mstream))
                {
                    DiffFormatter df = new DiffFormatter(stream);
                    df.Format(list, a, b);
                    df.Flush();
                    stream.Seek(0, SeekOrigin.Begin);
                    var ret = new StreamReader(stream).ReadToEnd();

                    return ret;
                }
            }
            catch (Exception ex)
            {
                Log.WriteLine("Refresh: {0}\r\n{1}", this.initFolder, ex.ToString());

                return "";
            }
        }