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()); }
/// <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()); }
/// <exception cref="System.IO.IOException"></exception> private void WriteGitLinkDiffText(OutputStream o, DiffEntry ent) { if (ent.GetOldMode() == FileMode.GITLINK) { o.Write(Constants.EncodeASCII("-Subproject commit " + ent.GetOldId().Name + "\n") ); } if (ent.GetNewMode() == FileMode.GITLINK) { o.Write(Constants.EncodeASCII("+Subproject commit " + ent.GetNewId().Name + "\n") ); } }
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()); }
/// <param name="o">the stream the formatter will write line data to</param> /// <param name="ent">the DiffEntry to create the FileHeader for</param> /// <exception cref="System.IO.IOException">writing to the supplied stream failed.</exception> protected internal virtual void FormatIndexLine(OutputStream o, DiffEntry ent) { o.Write(Constants.EncodeASCII("index " + Format(ent.GetOldId()) + ".." + Format(ent .GetNewId()))); // // // if (ent.GetOldMode().Equals(ent.GetNewMode())) { o.Write(' '); ent.GetNewMode().CopyTo(o); } o.Write('\n'); }
/// <summary>Breaks apart a DiffEntry into two entries, one DELETE and one ADD.</summary> /// <remarks>Breaks apart a DiffEntry into two entries, one DELETE and one ADD.</remarks> /// <param name="entry">the DiffEntry to break apart.</param> /// <returns> /// a list containing two entries. Calling /// <see cref="GetChangeType()">GetChangeType()</see> /// on the first entry will return ChangeType.DELETE. Calling it on /// the second entry will return ChangeType.ADD. /// </returns> internal static IList <NGit.Diff.DiffEntry> BreakModify(NGit.Diff.DiffEntry entry) { NGit.Diff.DiffEntry del = new NGit.Diff.DiffEntry(); del.oldId = entry.GetOldId(); del.oldMode = entry.GetOldMode(); del.oldPath = entry.GetOldPath(); del.newId = A_ZERO; del.newMode = FileMode.MISSING; del.newPath = NGit.Diff.DiffEntry.DEV_NULL; del.changeType = DiffEntry.ChangeType.DELETE; NGit.Diff.DiffEntry add = new NGit.Diff.DiffEntry(); add.oldId = A_ZERO; add.oldMode = FileMode.MISSING; add.oldPath = NGit.Diff.DiffEntry.DEV_NULL; add.newId = entry.GetNewId(); add.newMode = entry.GetNewMode(); add.newPath = entry.GetNewPath(); add.changeType = DiffEntry.ChangeType.ADD; return(Arrays.AsList(del, add)); }
/// <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()); }