public virtual void ShouldReportFileModeChange() { WriteTrashFile("a.txt", "content"); Git git = new Git(db); git.Add().AddFilepattern("a.txt").Call(); RevCommit c1 = git.Commit().SetMessage("initial commit").Call(); DirCache cache = db.LockDirCache(); DirCacheEditor editor = cache.Editor(); TreeWalk walk = new TreeWalk(db); walk.AddTree(c1.Tree); walk.Recursive = true; NUnit.Framework.Assert.IsTrue(walk.Next()); editor.Add(new _PathEdit_318(walk, "a.txt")); NUnit.Framework.Assert.IsTrue(editor.Commit()); RevCommit c2 = git.Commit().SetMessage("second commit").Call(); walk.Reset(); walk.AddTree(c1.Tree); walk.AddTree(c2.Tree); IList <DiffEntry> diffs = DiffEntry.Scan(walk, false); NUnit.Framework.Assert.AreEqual(1, diffs.Count); DiffEntry diff = diffs[0]; NUnit.Framework.Assert.AreEqual(DiffEntry.ChangeType.MODIFY, diff.GetChangeType() ); NUnit.Framework.Assert.AreEqual(diff.GetOldId(), diff.GetNewId()); NUnit.Framework.Assert.AreEqual("a.txt", diff.GetOldPath()); NUnit.Framework.Assert.AreEqual(diff.GetOldPath(), diff.GetNewPath()); NUnit.Framework.Assert.AreEqual(FileMode.EXECUTABLE_FILE, diff.GetNewMode()); NUnit.Framework.Assert.AreEqual(FileMode.REGULAR_FILE, diff.GetOldMode()); }
public virtual void ShouldListAddedFileBetweenTwoCommits() { // given Git git = new Git(db); RevCommit c1 = git.Commit().SetMessage("initial commit").Call(); WriteTrashFile("a.txt", "content"); git.Add().AddFilepattern("a.txt").Call(); RevCommit c2 = git.Commit().SetMessage("second commit").Call(); // when TreeWalk walk = new TreeWalk(db); walk.AddTree(c1.Tree); walk.AddTree(c2.Tree); IList <DiffEntry> result = DiffEntry.Scan(walk); // then Assert.IsNotNull(result); Assert.AreEqual(1, result.Count); DiffEntry entry = result[0]; Assert.AreEqual(entry.GetChangeType(), DiffEntry.ChangeType.ADD); Assert.AreEqual(entry.GetNewPath(), "a.txt"); Assert.AreEqual(entry.GetOldPath(), DiffEntry.DEV_NULL); }
public virtual void ShouldListModificationInDirWithoutModifiedTrees() { // given Git git = new Git(db); FilePath tree = new FilePath(new FilePath(db.WorkTree, "a"), "b"); FileUtils.Mkdirs(tree); FilePath file = new FilePath(tree, "c.txt"); FileUtils.CreateNewFile(file); Write(file, "content"); git.Add().AddFilepattern("a").Call(); RevCommit c1 = git.Commit().SetMessage("initial commit").Call(); Write(file, "new line"); RevCommit c2 = git.Commit().SetAll(true).SetMessage("second commit").Call(); // when TreeWalk walk = new TreeWalk(db); walk.AddTree(c1.Tree); walk.AddTree(c2.Tree); walk.Recursive = true; IList <DiffEntry> result = DiffEntry.Scan(walk); // then Assert.IsNotNull(result); Assert.AreEqual(1, result.Count); DiffEntry entry = result[0]; Assert.AreEqual(entry.GetChangeType(), DiffEntry.ChangeType.MODIFY); Assert.AreEqual(entry.GetNewPath(), "a/b/c.txt"); }
public virtual void ShouldListChangesInWorkingTree() { // given WriteTrashFile("a.txt", "content"); Git git = new Git(db); git.Add().AddFilepattern("a.txt").Call(); RevCommit c = git.Commit().SetMessage("initial commit").Call(); WriteTrashFile("b.txt", "new line"); // when TreeWalk walk = new TreeWalk(db); walk.AddTree(c.Tree); walk.AddTree(new FileTreeIterator(db)); IList <DiffEntry> result = DiffEntry.Scan(walk, true); // then Assert.AreEqual(1, result.Count); DiffEntry entry = result[0]; Assert.AreEqual(entry.GetChangeType(), DiffEntry.ChangeType.ADD); Assert.AreEqual(entry.GetNewPath(), "b.txt"); }
/// <exception cref="System.IO.IOException"></exception> private void FormatOldNewPaths(ByteArrayOutputStream o, DiffEntry ent) { string oldp; string newp; switch (ent.GetChangeType()) { case DiffEntry.ChangeType.ADD: { oldp = DiffEntry.DEV_NULL; newp = QuotePath(newPrefix + ent.GetNewPath()); break; } case DiffEntry.ChangeType.DELETE: { oldp = QuotePath(oldPrefix + ent.GetOldPath()); newp = DiffEntry.DEV_NULL; break; } default: { oldp = QuotePath(oldPrefix + ent.GetOldPath()); newp = QuotePath(newPrefix + ent.GetNewPath()); break; break; } } o.Write(Constants.Encode("--- " + oldp + "\n")); o.Write(Constants.Encode("+++ " + newp + "\n")); }
/// <exception cref="System.IO.IOException"></exception> private void BreakModifies(ContentSource.Pair reader, ProgressMonitor pm) { AList <DiffEntry> newEntries = new AList <DiffEntry>(entries.Count); pm.BeginTask(JGitText.Get().renamesBreakingModifies, entries.Count); for (int i = 0; i < entries.Count; i++) { DiffEntry e = entries[i]; if (e.GetChangeType() == DiffEntry.ChangeType.MODIFY) { int score = CalculateModifyScore(reader, e); if (score < breakScore) { IList <DiffEntry> tmp = DiffEntry.BreakModify(e); DiffEntry del = tmp[0]; del.score = score; deleted.AddItem(del); added.AddItem(tmp[1]); } else { newEntries.AddItem(e); } } else { newEntries.AddItem(e); } pm.Update(1); } entries = newEntries; }
/// <exception cref="System.IO.IOException"></exception> /// <exception cref="NGit.Errors.CorruptObjectException"></exception> /// <exception cref="NGit.Errors.MissingObjectException"></exception> private DiffFormatter.FormatResult CreateFormatResult(DiffEntry ent) { DiffFormatter.FormatResult res = new DiffFormatter.FormatResult(); ByteArrayOutputStream buf = new ByteArrayOutputStream(); EditList editList; FileHeader.PatchType type; FormatHeader(buf, ent); if (ent.GetOldMode() == FileMode.GITLINK || ent.GetNewMode() == FileMode.GITLINK) { FormatOldNewPaths(buf, ent); WriteGitLinkDiffText(buf, ent); editList = new EditList(); type = FileHeader.PatchType.UNIFIED; } else { AssertHaveRepository(); byte[] aRaw = Open(DiffEntry.Side.OLD, ent); byte[] bRaw = Open(DiffEntry.Side.NEW, ent); if (aRaw == BINARY || bRaw == BINARY || RawText.IsBinary(aRaw) || RawText.IsBinary (bRaw)) { // FormatOldNewPaths(buf, ent); buf.Write(Constants.EncodeASCII("Binary files differ\n")); editList = new EditList(); type = FileHeader.PatchType.BINARY; } else { res.a = new RawText(aRaw); res.b = new RawText(bRaw); editList = Diff(res.a, res.b); type = FileHeader.PatchType.UNIFIED; switch (ent.GetChangeType()) { case DiffEntry.ChangeType.RENAME: case DiffEntry.ChangeType.COPY: { if (!editList.IsEmpty()) { FormatOldNewPaths(buf, ent); } break; } default: { FormatOldNewPaths(buf, ent); break; break; } } } } res.header = new FileHeader(buf.ToByteArray(), editList, type); return(res); }
private static void AssertCopy(DiffEntry o, DiffEntry n, int score, DiffEntry copy ) { NUnit.Framework.Assert.AreEqual(DiffEntry.ChangeType.COPY, copy.GetChangeType()); NUnit.Framework.Assert.AreEqual(o.GetOldPath(), copy.GetOldPath()); NUnit.Framework.Assert.AreEqual(n.GetNewPath(), copy.GetNewPath()); NUnit.Framework.Assert.AreEqual(o.GetOldMode(), copy.GetOldMode()); NUnit.Framework.Assert.AreEqual(n.GetNewMode(), copy.GetNewMode()); NUnit.Framework.Assert.AreEqual(o.GetOldId(), copy.GetOldId()); NUnit.Framework.Assert.AreEqual(n.GetNewId(), copy.GetNewId()); NUnit.Framework.Assert.AreEqual(score, copy.GetScore()); }
private static void AssertRename(DiffEntry o, DiffEntry n, int score, DiffEntry rename ) { NUnit.Framework.Assert.AreEqual(DiffEntry.ChangeType.RENAME, rename.GetChangeType ()); NUnit.Framework.Assert.AreEqual(o.GetOldPath(), rename.GetOldPath()); NUnit.Framework.Assert.AreEqual(n.GetNewPath(), rename.GetNewPath()); NUnit.Framework.Assert.AreEqual(o.GetOldMode(), rename.GetOldMode()); NUnit.Framework.Assert.AreEqual(n.GetNewMode(), rename.GetNewMode()); NUnit.Framework.Assert.AreEqual(o.GetOldId(), rename.GetOldId()); NUnit.Framework.Assert.AreEqual(n.GetNewId(), rename.GetNewId()); NUnit.Framework.Assert.AreEqual(score, rename.GetScore()); }
private static bool IsRename(DiffEntry ent) { return ent.GetChangeType() == DiffEntry.ChangeType.RENAME || ent.GetChangeType() == DiffEntry.ChangeType.COPY; }
private static RevisionEntryInfo ConvertToRevisionEntryInfo(DiffEntry difference) { var path = Path(difference); var revisionEntryInfo = new RevisionEntryInfo {Path = path, Action = GetAction(difference.GetChangeType())}; return revisionEntryInfo; }
private static string Path(DiffEntry difference) { switch (difference.GetChangeType()) { case DiffEntry.ChangeType.ADD: return difference.GetNewPath(); case DiffEntry.ChangeType.COPY: return string.Format("{0} -> {1}", difference.GetOldPath(), difference.GetNewPath()); case DiffEntry.ChangeType.DELETE: return difference.GetOldPath(); case DiffEntry.ChangeType.MODIFY: return difference.GetOldPath(); case DiffEntry.ChangeType.RENAME: return string.Format("{0} -> {1}", difference.GetOldPath(), difference.GetNewPath()); default: return difference.ToString(); } }
private static bool IsRename(DiffEntry ent) { return(ent.GetChangeType() == DiffEntry.ChangeType.RENAME || ent.GetChangeType() == DiffEntry.ChangeType.COPY); }
/// <exception cref="System.IO.IOException"></exception> private void FormatHeader(ByteArrayOutputStream o, DiffEntry ent) { DiffEntry.ChangeType type = ent.GetChangeType(); string oldp = ent.GetOldPath(); string newp = ent.GetNewPath(); FileMode oldMode = ent.GetOldMode(); FileMode newMode = ent.GetNewMode(); o.Write(Constants.EncodeASCII("diff --git ")); o.Write(Constants.Encode(QuotePath(oldPrefix + (type == DiffEntry.ChangeType.ADD ? newp : oldp)))); o.Write(' '); o.Write(Constants.Encode(QuotePath(newPrefix + (type == DiffEntry.ChangeType.DELETE ? oldp : newp)))); o.Write('\n'); switch (type) { case DiffEntry.ChangeType.ADD: { o.Write(Constants.EncodeASCII("new file mode ")); newMode.CopyTo(o); o.Write('\n'); break; } case DiffEntry.ChangeType.DELETE: { o.Write(Constants.EncodeASCII("deleted file mode ")); oldMode.CopyTo(o); o.Write('\n'); break; } case DiffEntry.ChangeType.RENAME: { o.Write(Constants.EncodeASCII("similarity index " + ent.GetScore() + "%")); o.Write('\n'); o.Write(Constants.Encode("rename from " + QuotePath(oldp))); o.Write('\n'); o.Write(Constants.Encode("rename to " + QuotePath(newp))); o.Write('\n'); break; } case DiffEntry.ChangeType.COPY: { o.Write(Constants.EncodeASCII("similarity index " + ent.GetScore() + "%")); o.Write('\n'); o.Write(Constants.Encode("copy from " + QuotePath(oldp))); o.Write('\n'); o.Write(Constants.Encode("copy to " + QuotePath(newp))); o.Write('\n'); if (!oldMode.Equals(newMode)) { o.Write(Constants.EncodeASCII("new file mode ")); newMode.CopyTo(o); o.Write('\n'); } break; } case DiffEntry.ChangeType.MODIFY: { if (0 < ent.GetScore()) { o.Write(Constants.EncodeASCII("dissimilarity index " + (100 - ent.GetScore()) + "%" )); o.Write('\n'); } break; } } if ((type == DiffEntry.ChangeType.MODIFY || type == DiffEntry.ChangeType.RENAME) && !oldMode.Equals(newMode)) { o.Write(Constants.EncodeASCII("old mode ")); oldMode.CopyTo(o); o.Write('\n'); o.Write(Constants.EncodeASCII("new mode ")); newMode.CopyTo(o); o.Write('\n'); } if (!ent.GetOldId().Equals(ent.GetNewId())) { FormatIndexLine(o, ent); } }
private static void AssertCopy(DiffEntry o, DiffEntry n, int score, DiffEntry copy ) { NUnit.Framework.Assert.AreEqual(DiffEntry.ChangeType.COPY, copy.GetChangeType()); NUnit.Framework.Assert.AreEqual(o.GetOldPath(), copy.GetOldPath()); NUnit.Framework.Assert.AreEqual(n.GetNewPath(), copy.GetNewPath()); NUnit.Framework.Assert.AreEqual(o.GetOldMode(), copy.GetOldMode()); NUnit.Framework.Assert.AreEqual(n.GetNewMode(), copy.GetNewMode()); NUnit.Framework.Assert.AreEqual(o.GetOldId(), copy.GetOldId()); NUnit.Framework.Assert.AreEqual(n.GetNewId(), copy.GetNewId()); NUnit.Framework.Assert.AreEqual(score, copy.GetScore()); }
private static void AssertRename(DiffEntry o, DiffEntry n, int score, DiffEntry rename ) { NUnit.Framework.Assert.AreEqual(DiffEntry.ChangeType.RENAME, rename.GetChangeType ()); NUnit.Framework.Assert.AreEqual(o.GetOldPath(), rename.GetOldPath()); NUnit.Framework.Assert.AreEqual(n.GetNewPath(), rename.GetNewPath()); NUnit.Framework.Assert.AreEqual(o.GetOldMode(), rename.GetOldMode()); NUnit.Framework.Assert.AreEqual(n.GetNewMode(), rename.GetNewMode()); NUnit.Framework.Assert.AreEqual(o.GetOldId(), rename.GetOldId()); NUnit.Framework.Assert.AreEqual(n.GetNewId(), rename.GetNewId()); NUnit.Framework.Assert.AreEqual(score, rename.GetScore()); }