예제 #1
0
        /// <summary>
        /// Get the difference between the two versions of text using the system defined algorithm.
        /// </summary>
        /// <param name="olderText">The older version of the text in the comparison.</param>
        /// <param name="newerText">The newer version of the text in the comparison.</param>
        /// <returns>A patch file that describes the differences.</returns>
        public static string Diff(string olderText, string newerText)
        {
            string algorithm = Properties.Settings.Default["DiffAlgorithm"] as string;

            switch (algorithm)
            {
            case "Myer":
                return(DiffUtility.Myer(olderText, newerText));

            default:
                return(DiffUtility.Patience(olderText, newerText));
            }
        }
예제 #2
0
        /// <summary>
        /// Compare a file in the given commit with the previous version of the file.
        /// </summary>
        /// <param name="fileName">The file name to get the diff for.</param>
        /// <param name="commit">The commit to compare with.</param>
        /// <returns>The diff for the file.</returns>
        public string GetChangedFileDiff(string fileName, SimpleRepositoryCommit commit)
        {
            if (commit == null)
            {
                throw new ArgumentNullException("commit");
            }
            if (String.IsNullOrWhiteSpace(fileName))
            {
                throw new ArgumentException("fileName is null or whitespace.", "fileName");
            }

            if (!String.IsNullOrWhiteSpace(SubFolder))
            {
                fileName = Path.Combine(SubFolder, fileName);
            }

            string newerText = String.Empty;
            string olderText = String.Empty;

            Validate();

            using (Repository repo = Init(false))
            {
                Commit c = repo.Lookup(commit.Sha) as Commit;
                if (c != null)
                {
                    TreeEntry newerEntry = c[fileName];
                    if (newerEntry != null && newerEntry.Target is Blob)
                    {
                        newerText = (newerEntry.Target as Blob).GetContentText();
                    }

                    if (c.Parents != null)
                    {
                        Commit p = c.Parents.FirstOrDefault();
                        if (p != null)
                        {
                            TreeEntry olderEntry = p[fileName];
                            if (olderEntry != null && olderEntry.Target is Blob)
                            {
                                olderText = (olderEntry.Target as Blob).GetContentText();
                            }
                        }
                    }
                }
            }

            return(DiffUtility.Diff(olderText, newerText));
        }
예제 #3
0
        /// <summary>
        /// Get the difference between the two versions of the same file.
        /// </summary>
        /// <param name="file">The file to get differences for.</param>
        /// <param name="older">The older version of the file.</param>
        /// <param name="newer">The newer version of the file.</param>
        /// <returns>A patch file that describes the differences.</returns>
        public string Diff(SourceFile file, SimpleRepositoryCommit older, SimpleRepositoryCommit newer)
        {
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }
            if (older == null)
            {
                throw new ArgumentNullException("older");
            }
            if (newer == null)
            {
                throw new ArgumentNullException("newer");
            }

            string fileName = (!String.IsNullOrWhiteSpace(SubFolder)) ?
                              Path.Combine(SubFolder, file.FileName) :
                              file.FileName;

            Validate();

            using (Repository repo = Init(false))
            {
                // get the different versions of the file
                Commit olderCommit = repo.Lookup(older.Sha) as Commit;
                Commit newerCommit = repo.Lookup(newer.Sha) as Commit;

                if (olderCommit != null && newerCommit != null)
                {
                    TreeEntry olderEntry = olderCommit[fileName];
                    TreeEntry newerEntry = newerCommit[fileName];

                    if (olderEntry != null && olderEntry.Target is Blob &&
                        newerEntry != null && newerEntry.Target is Blob)
                    {
                        string olderText = (olderEntry.Target as Blob).GetContentText();
                        string newerText = (newerEntry.Target as Blob).GetContentText();

                        // do diff
                        return(DiffUtility.Diff(olderText, newerText));
                    }
                }
            }

            return(String.Empty);
        }