/// <summary> /// The the content for the given file and the given version. /// </summary> /// <param name="file">The file to get the content for.</param> /// <param name="version">The version of the file content to get. If null the most recent commit is returned.</param> /// <returns>The requested content or null if not found.</returns> public string GetContent(SourceFile file, SimpleRepositoryCommit version) { if (file == null) { throw new ArgumentNullException("file"); } return(GetContent(file.FileName, version)); }
/// <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)); }
/// <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); }
/// <summary> /// The the content for the given file and the given version. /// </summary> /// <param name="fileName">The file name to get the content for.</param> /// <param name="version">The version of the file content to get. If null the most recent commit is returned.</param> /// <returns>The requested content or null if not found.</returns> public string GetContent(string fileName, SimpleRepositoryCommit version) { if (String.IsNullOrWhiteSpace(fileName)) { return(null); } // get the requested version of the file if (version == null) { SimpleRepositoryCommit[] commits = GetHistory(fileName); if (commits.Length > 0) { version = commits[0]; } } if (!String.IsNullOrWhiteSpace(SubFolder)) { fileName = Path.Combine(SubFolder, fileName); } if (version != null) { Validate(); using (Repository repo = Init(false)) { Commit commit = repo.Lookup(version.Sha) as Commit; if (commit != null) { TreeEntry entry = commit[fileName]; if (entry != null && entry.Target is Blob) { return((entry.Target as Blob).GetContentText()); } } } } // not found return(null); }
/// <summary> /// Get a list of the files that were changed in the commit. /// </summary> /// <param name="commit">The commit to get changed files for.</param> /// <returns>The changed files.</returns> public string[] GetChangedFiles(SimpleRepositoryCommit commit) { if (commit == null) { throw new ArgumentNullException("commit"); } List <string> result = new List <string>(); Validate(); using (Repository repo = Init(false)) { Commit c = repo.Lookup(commit.Sha) as Commit; if (c == null) { return(result.ToArray()); } Commit p = null; if (c.Parents != null && c.Parents.Count() == 1) { p = c.Parents.ElementAt(0); } // initial commit if (p == null) { Stack <TreeEntry> folderStack = new Stack <TreeEntry>(); foreach (TreeEntry te in c.Tree) { if (te.Mode == Mode.Directory) { folderStack.Push(te); } else { result.Add(String.Format("+ {0}", RemoveSubFolderFromPath(te.Path))); } } while (folderStack.Count > 0) { TreeEntry te = folderStack.Pop(); Tree tree = te.Target as Tree; if (tree != null) { foreach (TreeEntry child in tree) { if (child.Mode == Mode.Directory) { folderStack.Push(child); } else { result.Add(String.Format("+ {0}", RemoveSubFolderFromPath(child.Path))); } } } } } // normal compare else { TreeChanges changes = repo.Diff.Compare <TreeChanges>(p.Tree, c.Tree); if (changes == null) { return(result.ToArray()); } if (changes.Modified != null) { foreach (TreeEntryChanges tec in changes.Modified) { result.Add(String.Format(" {0}", RemoveSubFolderFromPath(tec.Path))); } } if (changes.Added != null) { foreach (TreeEntryChanges tec in changes.Added) { result.Add(String.Format("+ {0}", RemoveSubFolderFromPath(tec.Path))); } } if (changes.Deleted != null) { foreach (TreeEntryChanges tec in changes.Deleted) { result.Add(String.Format("- {0}", RemoveSubFolderFromPath(tec.Path))); } } } } return(result.OrderBy(f => f.Substring(1)).ToArray()); }