internal static NGit.Diff.DiffEntry Modify(string path)
 {
     NGit.Diff.DiffEntry e = new NGit.Diff.DiffEntry();
     e.oldMode    = FileMode.REGULAR_FILE;
     e.oldPath    = path;
     e.newMode    = FileMode.REGULAR_FILE;
     e.newPath    = path;
     e.changeType = DiffEntry.ChangeType.MODIFY;
     return(e);
 }
 internal static NGit.Diff.DiffEntry Delete(string path, AnyObjectId id)
 {
     NGit.Diff.DiffEntry e = new NGit.Diff.DiffEntry();
     e.oldId      = AbbreviatedObjectId.FromObjectId(id);
     e.oldMode    = FileMode.REGULAR_FILE;
     e.oldPath    = path;
     e.newId      = A_ZERO;
     e.newMode    = FileMode.MISSING;
     e.newPath    = DEV_NULL;
     e.changeType = DiffEntry.ChangeType.DELETE;
     return(e);
 }
 internal static NGit.Diff.DiffEntry Pair(DiffEntry.ChangeType changeType, NGit.Diff.DiffEntry
                                          src, NGit.Diff.DiffEntry dst, int score)
 {
     NGit.Diff.DiffEntry r = new NGit.Diff.DiffEntry();
     r.oldId      = src.oldId;
     r.oldMode    = src.oldMode;
     r.oldPath    = src.oldPath;
     r.newId      = dst.newId;
     r.newMode    = dst.newMode;
     r.newPath    = dst.newPath;
     r.changeType = changeType;
     r.score      = score;
     return(r);
 }
Example #4
0
        // reduce the visibility of the default constructor
        /// <summary>Convert the TreeWalk into DiffEntry headers.</summary>
        /// <remarks>Convert the TreeWalk into DiffEntry headers.</remarks>
        /// <param name="walk">the TreeWalk to walk through. Must have exactly two trees.</param>
        /// <returns>headers describing the changed files.</returns>
        /// <exception cref="System.IO.IOException">the repository cannot be accessed.</exception>
        public static IList <NGit.Diff.DiffEntry> Scan(TreeWalk walk)
        {
            IList <NGit.Diff.DiffEntry> r     = new AList <NGit.Diff.DiffEntry>();
            MutableObjectId             idBuf = new MutableObjectId();

            while (walk.Next())
            {
                NGit.Diff.DiffEntry entry = new NGit.Diff.DiffEntry();
                walk.GetObjectId(idBuf, 0);
                entry.oldId = AbbreviatedObjectId.FromObjectId(idBuf);
                walk.GetObjectId(idBuf, 1);
                entry.newId   = AbbreviatedObjectId.FromObjectId(idBuf);
                entry.oldMode = walk.GetFileMode(0);
                entry.newMode = walk.GetFileMode(1);
                entry.newPath = entry.oldPath = walk.PathString;
                if (entry.oldMode == FileMode.MISSING)
                {
                    entry.oldPath    = NGit.Diff.DiffEntry.DEV_NULL;
                    entry.changeType = DiffEntry.ChangeType.ADD;
                    r.AddItem(entry);
                }
                else
                {
                    if (entry.newMode == FileMode.MISSING)
                    {
                        entry.newPath    = NGit.Diff.DiffEntry.DEV_NULL;
                        entry.changeType = DiffEntry.ChangeType.DELETE;
                        r.AddItem(entry);
                    }
                    else
                    {
                        entry.changeType = DiffEntry.ChangeType.MODIFY;
                        if (RenameDetector.SameType(entry.oldMode, entry.newMode))
                        {
                            r.AddItem(entry);
                        }
                        else
                        {
                            Sharpen.Collections.AddAll(r, BreakModify(entry));
                        }
                    }
                }
            }
            return(r);
        }
        public virtual void TestExactRename_OneDeleteManyAdds()
        {
            ObjectId  foo = Blob("foo");
            DiffEntry a   = DiffEntry.Add("src/com/foo/a.java", foo);
            DiffEntry b   = DiffEntry.Add("src/com/foo/b.java", foo);
            DiffEntry c   = DiffEntry.Add("c.txt", foo);
            DiffEntry d   = DiffEntry.Delete("d.txt", foo);

            rd.Add(a);
            rd.Add(b);
            rd.Add(c);
            rd.Add(d);
            IList <DiffEntry> entries = rd.Compute();

            NUnit.Framework.Assert.AreEqual(3, entries.Count);
            AssertRename(d, c, 100, entries[0]);
            AssertCopy(d, a, 100, entries[1]);
            AssertCopy(d, b, 100, entries[2]);
        }
        public virtual void TestBreakModify_BreakNone()
        {
            ObjectId  aId = Blob("foo");
            ObjectId  bId = Blob("bar");
            DiffEntry m   = DiffEntry.Modify(PATH_A);

            m.oldId = AbbreviatedObjectId.FromObjectId(aId);
            m.newId = AbbreviatedObjectId.FromObjectId(bId);
            DiffEntry a = DiffEntry.Add(PATH_B, aId);

            rd.Add(a);
            rd.Add(m);
            rd.SetBreakScore(-1);
            IList <DiffEntry> entries = rd.Compute();

            NUnit.Framework.Assert.AreEqual(2, entries.Count);
            NUnit.Framework.Assert.AreSame(m, entries[0]);
            NUnit.Framework.Assert.AreSame(a, entries[1]);
        }
        public virtual void TestExactRename_ManyRenames()
        {
            ObjectId  foo = Blob("foo");
            ObjectId  bar = Blob("bar");
            DiffEntry a   = DiffEntry.Add(PATH_A, foo);
            DiffEntry b   = DiffEntry.Delete(PATH_Q, foo);
            DiffEntry c   = DiffEntry.Add(PATH_H, bar);
            DiffEntry d   = DiffEntry.Delete(PATH_B, bar);

            rd.Add(a);
            rd.Add(b);
            rd.Add(c);
            rd.Add(d);
            IList <DiffEntry> entries = rd.Compute();

            NUnit.Framework.Assert.AreEqual(2, entries.Count);
            AssertRename(b, a, 100, entries[0]);
            AssertRename(d, c, 100, entries[1]);
        }
Example #8
0
        public virtual void TestCreateFileHeader_GitLink()
        {
            ObjectId aId        = Blob("a\n");
            ObjectId bId        = Blob("b\n");
            string   diffHeader = MakeDiffHeaderModeChange(PATH_A, PATH_A, aId, bId, GITLINK, REGULAR_FILE
                                                           ) + "-Subproject commit " + aId.Name + "\n";
            DiffEntry ad = DiffEntry.Delete(PATH_A, aId);

            ad.oldMode = FileMode.GITLINK;
            DiffEntry  abcd = DiffEntry.Add(PATH_A, bId);
            DiffEntry  mod  = DiffEntry.Pair(DiffEntry.ChangeType.MODIFY, ad, abcd, 0);
            FileHeader fh   = df.ToFileHeader(mod);

            NUnit.Framework.Assert.AreEqual(diffHeader, RawParseUtils.Decode(fh.GetBuffer()));
            NUnit.Framework.Assert.AreEqual(1, fh.GetHunks().Count);
            HunkHeader hh = fh.GetHunks()[0];

            NUnit.Framework.Assert.AreEqual(0, hh.ToEditList().Count);
        }
        public virtual void TestBreakModify_BreakAll()
        {
            ObjectId  aId = Blob("foo");
            ObjectId  bId = Blob("bar");
            DiffEntry m   = DiffEntry.Modify(PATH_A);

            m.oldId = AbbreviatedObjectId.FromObjectId(aId);
            m.newId = AbbreviatedObjectId.FromObjectId(bId);
            DiffEntry a = DiffEntry.Add(PATH_B, aId);

            rd.Add(a);
            rd.Add(m);
            rd.SetBreakScore(101);
            IList <DiffEntry> entries = rd.Compute();

            NUnit.Framework.Assert.AreEqual(2, entries.Count);
            AssertAdd(PATH_A, bId, FileMode.REGULAR_FILE, entries[0]);
            AssertRename(DiffEntry.BreakModify(m)[0], a, 100, entries[1]);
        }
Example #10
0
        /// <summary>
        /// Find the best match by file path for a given DiffEntry from a list of
        /// DiffEntrys.
        /// </summary>
        /// <remarks>
        /// Find the best match by file path for a given DiffEntry from a list of
        /// DiffEntrys. The returned DiffEntry will be of the same type as <src>. If
        /// no DiffEntry can be found that has the same type, this method will return
        /// null.
        /// </remarks>
        /// <param name="src">the DiffEntry to try to find a match for</param>
        /// <param name="list">a list of DiffEntrys to search through</param>
        /// <returns>the DiffEntry from <list> who's file path best matches <src></returns>
        private static DiffEntry BestPathMatch(DiffEntry src, IList <DiffEntry> list)
        {
            DiffEntry best  = null;
            int       score = -1;

            foreach (DiffEntry d in list)
            {
                if (SameType(Mode(d), Mode(src)))
                {
                    int tmp = SimilarityRenameDetector.NameScore(Path(d), Path(src));
                    if (tmp > score)
                    {
                        best  = d;
                        score = tmp;
                    }
                }
            }
            return(best);
        }
        public virtual void TestExactRename_MultipleIdenticalDeletes()
        {
            ObjectId  foo = Blob("foo");
            DiffEntry a   = DiffEntry.Delete(PATH_A, foo);
            DiffEntry b   = DiffEntry.Delete(PATH_B, foo);
            DiffEntry c   = DiffEntry.Delete(PATH_H, foo);
            DiffEntry d   = DiffEntry.Add(PATH_Q, foo);

            rd.Add(a);
            rd.Add(b);
            rd.Add(c);
            rd.Add(d);
            // Pairs the add with the first delete added
            IList <DiffEntry> entries = rd.Compute();

            NUnit.Framework.Assert.AreEqual(3, entries.Count);
            NUnit.Framework.Assert.AreEqual(b, entries[0]);
            NUnit.Framework.Assert.AreEqual(c, entries[1]);
            AssertRename(a, d, 100, entries[2]);
        }
 /// <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));
 }
Example #13
0
            /// <summary>Open the object.</summary>
            /// <remarks>Open the object.</remarks>
            /// <param name="side">which side of the entry to read (OLD or NEW).</param>
            /// <param name="ent">the entry to examine.</param>
            /// <returns>
            /// a loader that can supply the content of the file. The loader
            /// must be used before another loader can be obtained from this
            /// same source.
            /// </returns>
            /// <exception cref="System.IO.IOException">the file cannot be accessed.</exception>
            public ObjectLoader Open(DiffEntry.Side side, DiffEntry ent)
            {
                switch (side)
                {
                case DiffEntry.Side.OLD:
                {
                    return(oldSource.Open(ent.oldPath, ent.oldId.ToObjectId()));
                }

                case DiffEntry.Side.NEW:
                {
                    return(newSource.Open(ent.newPath, ent.newId.ToObjectId()));
                }

                default:
                {
                    throw new ArgumentException();
                }
                }
            }
        public virtual void TestBreakModify_DontBreakAboveScore()
        {
            ObjectId  aId = Blob("blah\nblah\nfoo");
            ObjectId  bId = Blob("blah\nblah\nbar");
            DiffEntry m   = DiffEntry.Modify(PATH_A);

            m.oldId = AbbreviatedObjectId.FromObjectId(aId);
            m.newId = AbbreviatedObjectId.FromObjectId(bId);
            DiffEntry a = DiffEntry.Add(PATH_B, aId);

            rd.Add(a);
            rd.Add(m);
            rd.SetBreakScore(20);
            // Should not break the modify
            IList <DiffEntry> entries = rd.Compute();

            NUnit.Framework.Assert.AreEqual(2, entries.Count);
            NUnit.Framework.Assert.AreSame(m, entries[0]);
            NUnit.Framework.Assert.AreSame(a, entries[1]);
        }
        public virtual void TestExactRename_PathBreaksTie()
        {
            ObjectId  foo = Blob("foo");
            DiffEntry a   = DiffEntry.Add("src/com/foo/a.java", foo);
            DiffEntry b   = DiffEntry.Delete("src/com/foo/b.java", foo);
            DiffEntry c   = DiffEntry.Add("c.txt", foo);
            DiffEntry d   = DiffEntry.Delete("d.txt", foo);
            DiffEntry e   = DiffEntry.Add("the_e_file.txt", foo);

            // Add out of order to avoid first-match succeeding
            rd.Add(a);
            rd.Add(d);
            rd.Add(e);
            rd.Add(b);
            rd.Add(c);
            IList <DiffEntry> entries = rd.Compute();

            NUnit.Framework.Assert.AreEqual(3, entries.Count);
            AssertRename(d, c, 100, entries[0]);
            AssertRename(b, a, 100, entries[1]);
            AssertCopy(d, e, 100, entries[2]);
        }
 /// <exception cref="System.IO.IOException"></exception>
 private int CalculateModifyScore(ContentSource.Pair reader, DiffEntry d)
 {
     try
     {
         SimilarityIndex src = new SimilarityIndex();
         src.Hash(reader.Open(DiffEntry.Side.OLD, d));
         src.Sort();
         SimilarityIndex dst = new SimilarityIndex();
         dst.Hash(reader.Open(DiffEntry.Side.NEW, d));
         dst.Sort();
         return(src.Score(dst, 100));
     }
     catch (SimilarityIndex.TableFullException)
     {
         // If either table overflowed while being constructed, don't allow
         // the pair to be broken. Returning 1 higher than breakScore will
         // ensure its not similar, but not quite dissimilar enough to break.
         //
         overRenameLimit = true;
         return(breakScore + 1);
     }
 }
        public virtual void TestInexactRename_OneRenameTwoUnrelatedFiles()
        {
            ObjectId  aId = Blob("foo\nbar\nbaz\nblarg\n");
            ObjectId  bId = Blob("foo\nbar\nbaz\nblah\n");
            DiffEntry a   = DiffEntry.Add(PATH_A, aId);
            DiffEntry b   = DiffEntry.Delete(PATH_Q, bId);
            ObjectId  cId = Blob("some\nsort\nof\ntext\n");
            ObjectId  dId = Blob("completely\nunrelated\ntext\n");
            DiffEntry c   = DiffEntry.Add(PATH_B, cId);
            DiffEntry d   = DiffEntry.Delete(PATH_H, dId);

            rd.Add(a);
            rd.Add(b);
            rd.Add(c);
            rd.Add(d);
            IList <DiffEntry> entries = rd.Compute();

            NUnit.Framework.Assert.AreEqual(3, entries.Count);
            AssertRename(b, a, 66, entries[0]);
            NUnit.Framework.Assert.AreSame(c, entries[1]);
            NUnit.Framework.Assert.AreSame(d, entries[2]);
        }
        private void RejoinModifies(ProgressMonitor pm)
        {
            Dictionary <string, DiffEntry> nameMap = new Dictionary <string, DiffEntry>();
            AList <DiffEntry> newAdded             = new AList <DiffEntry>(added.Count);

            pm.BeginTask(JGitText.Get().renamesRejoiningModifies, added.Count + deleted.Count
                         );
            foreach (DiffEntry src in deleted)
            {
                nameMap.Put(src.oldPath, src);
                pm.Update(1);
            }
            foreach (DiffEntry dst in added)
            {
                DiffEntry src_1 = Sharpen.Collections.Remove(nameMap, dst.newPath);
                if (src_1 != null)
                {
                    if (SameType(src_1.oldMode, dst.newMode))
                    {
                        entries.AddItem(DiffEntry.Pair(DiffEntry.ChangeType.MODIFY, src_1, dst, src_1.score
                                                       ));
                    }
                    else
                    {
                        nameMap.Put(src_1.oldPath, src_1);
                        newAdded.AddItem(dst);
                    }
                }
                else
                {
                    newAdded.AddItem(dst);
                }
                pm.Update(1);
            }
            added   = newAdded;
            deleted = new AList <DiffEntry>(nameMap.Values);
        }
Example #19
0
        public virtual void ShouldListModificationInDirWithModifiedTrees()
        {
            // 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);
            IList <DiffEntry> result = DiffEntry.Scan(walk, true);

            // then
            Assert.IsNotNull(result);
            Assert.AreEqual(3, result.Count);;
            DiffEntry entry = result[0];

            Assert.AreEqual(entry.GetChangeType(), DiffEntry.ChangeType.MODIFY);
            Assert.AreEqual(entry.GetNewPath(), "a");
            entry = result[1];
            Assert.AreEqual(entry.GetChangeType(), DiffEntry.ChangeType.MODIFY);
            Assert.AreEqual(entry.GetNewPath(), "a/b");
            entry = result[2];
            Assert.AreEqual(entry.GetChangeType(), DiffEntry.ChangeType.MODIFY);
            Assert.AreEqual(entry.GetNewPath(), "a/b/c.txt");
        }
Example #20
0
        /// <exception cref="System.IO.IOException"></exception>
        private void FormatOldNewPaths(ByteArrayOutputStream o, DiffEntry ent)
        {
            if (ent.oldId.Equals(ent.newId))
            {
                return;
            }
            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"));
        }
Example #21
0
        public virtual void ShouldListAddedFileInInitialCommit()
        {
            // given
            WriteTrashFile("a.txt", "content");
            Git git = new Git(db);

            git.Add().AddFilepattern("a.txt").Call();
            RevCommit c = git.Commit().SetMessage("initial commit").Call();
            // when
            TreeWalk walk = new TreeWalk(db);

            walk.AddTree(new EmptyTreeIterator());
            walk.AddTree(c.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);
        }
Example #22
0
		private static void AssertAdd(string newName, ObjectId newId, FileMode newMode, DiffEntry
			 add)
		{
			NUnit.Framework.Assert.AreEqual(DiffEntry.DEV_NULL, add.oldPath);
			NUnit.Framework.Assert.AreEqual(DiffEntry.A_ZERO, add.oldId);
			NUnit.Framework.Assert.AreEqual(FileMode.MISSING, add.oldMode);
			NUnit.Framework.Assert.AreEqual(DiffEntry.ChangeType.ADD, add.changeType);
			NUnit.Framework.Assert.AreEqual(newName, add.newPath);
			NUnit.Framework.Assert.AreEqual(AbbreviatedObjectId.FromObjectId(newId), add.newId
				);
			NUnit.Framework.Assert.AreEqual(newMode, add.newMode);
		}
        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 DiffEntry ExactCopy(DiffEntry src, DiffEntry dst)
 {
     return(DiffEntry.Pair(DiffEntry.ChangeType.COPY, src, dst, EXACT_RENAME_SCORE));
 }
Example #25
0
		/// <summary>Get the path associated with this file.</summary>
		/// <remarks>Get the path associated with this file.</remarks>
		/// <param name="side">which path to obtain.</param>
		/// <returns>name for this file.</returns>
		public virtual string GetPath(DiffEntry.Side side)
		{
			return side == DiffEntry.Side.OLD ? GetOldPath() : GetNewPath();
		}
Example #26
0
		/// <summary>Get the object id.</summary>
		/// <remarks>Get the object id.</remarks>
		/// <param name="side">the side of the id to get.</param>
		/// <returns>the object id; null if there is no index line</returns>
		public virtual AbbreviatedObjectId GetId(DiffEntry.Side side)
		{
			return side == DiffEntry.Side.OLD ? GetOldId() : GetNewId();
		}
Example #27
0
		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());
		}
Example #28
0
		/// <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);
		}
Example #29
0
		// The hunk header is not taken into account for patch id calculation
		/// <exception cref="System.IO.IOException"></exception>
		protected internal override void FormatIndexLine(OutputStream o, DiffEntry ent)
		{
		}
Example #30
0
		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());
		}
Example #31
0
 /// <summary>
 /// Convert the TreeWalk into DiffEntry headers, depending on
 /// <code>includeTrees</code>
 /// it will add tree objects into result or not.
 /// </summary>
 /// <param name="walk">
 /// the TreeWalk to walk through. Must have exactly two trees and
 /// when
 /// <code>includeTrees</code>
 /// parameter is
 /// <code>true</code>
 /// it can't
 /// be recursive.
 /// </param>
 /// <param name="includeTrees">include tree object's.</param>
 /// <returns>headers describing the changed files.</returns>
 /// <exception cref="System.IO.IOException">the repository cannot be accessed.</exception>
 /// <exception cref="System.ArgumentException">
 /// when
 /// <code>includeTrees</code>
 /// is true and given TreeWalk is
 /// recursive. Or when given TreeWalk doesn't have exactly two
 /// trees
 /// </exception>
 public static IList<NGit.Diff.DiffEntry> Scan(TreeWalk walk, bool includeTrees)
 {
     if (walk.TreeCount != 2)
     {
         throw new ArgumentException(JGitText.Get().treeWalkMustHaveExactlyTwoTrees);
     }
     if (includeTrees && walk.Recursive)
     {
         throw new ArgumentException(JGitText.Get().cannotBeRecursiveWhenTreesAreIncluded);
     }
     IList<NGit.Diff.DiffEntry> r = new AList<NGit.Diff.DiffEntry>();
     MutableObjectId idBuf = new MutableObjectId();
     while (walk.Next())
     {
         NGit.Diff.DiffEntry entry = new NGit.Diff.DiffEntry();
         walk.GetObjectId(idBuf, 0);
         entry.oldId = AbbreviatedObjectId.FromObjectId(idBuf);
         walk.GetObjectId(idBuf, 1);
         entry.newId = AbbreviatedObjectId.FromObjectId(idBuf);
         entry.oldMode = walk.GetFileMode(0);
         entry.newMode = walk.GetFileMode(1);
         entry.newPath = entry.oldPath = walk.PathString;
         if (entry.oldMode == FileMode.MISSING)
         {
             entry.oldPath = NGit.Diff.DiffEntry.DEV_NULL;
             entry.changeType = DiffEntry.ChangeType.ADD;
             r.AddItem(entry);
         }
         else
         {
             if (entry.newMode == FileMode.MISSING)
             {
                 entry.newPath = NGit.Diff.DiffEntry.DEV_NULL;
                 entry.changeType = DiffEntry.ChangeType.DELETE;
                 r.AddItem(entry);
             }
             else
             {
                 if (!entry.oldId.Equals(entry.newId))
                 {
                     entry.changeType = DiffEntry.ChangeType.MODIFY;
                     if (RenameDetector.SameType(entry.oldMode, entry.newMode))
                     {
                         r.AddItem(entry);
                     }
                     else
                     {
                         Sharpen.Collections.AddAll(r, BreakModify(entry));
                     }
                 }
                 else
                 {
                     if (entry.oldMode != entry.newMode)
                     {
                         entry.changeType = DiffEntry.ChangeType.MODIFY;
                         r.AddItem(entry);
                     }
                 }
             }
         }
         if (includeTrees && walk.IsSubtree)
         {
             walk.EnterSubtree();
         }
     }
     return r;
 }
		/// <exception cref="System.IO.IOException"></exception>
		private long Size(DiffEntry.Side side, DiffEntry ent)
		{
			return reader.Size(side, ent);
		}
		/// <exception cref="System.IO.IOException"></exception>
		/// <exception cref="NGit.Diff.SimilarityIndex.TableFullException"></exception>
		private SimilarityIndex Hash(DiffEntry.Side side, DiffEntry ent)
		{
			SimilarityIndex r = new SimilarityIndex();
			r.Hash(reader.Open(side, ent));
			r.Sort();
			return r;
		}
Example #34
0
		internal static NGit.Diff.DiffEntry Delete(string path, AnyObjectId id)
		{
			NGit.Diff.DiffEntry e = new NGit.Diff.DiffEntry();
			e.oldId = AbbreviatedObjectId.FromObjectId(id);
			e.oldMode = FileMode.REGULAR_FILE;
			e.oldPath = path;
			e.newId = A_ZERO;
			e.newMode = FileMode.MISSING;
			e.newPath = DEV_NULL;
			e.changeType = DiffEntry.ChangeType.DELETE;
			return e;
		}
        private static FileActionEnum GetAction(DiffEntry.ChangeType changeType)
        {
            switch (changeType)
            {
                case DiffEntry.ChangeType.MODIFY:
                case DiffEntry.ChangeType.COPY:
                    return FileActionEnum.Modify;

                case DiffEntry.ChangeType.ADD:
                    return FileActionEnum.Add;

                case DiffEntry.ChangeType.DELETE:
                    return FileActionEnum.Delete;

                case DiffEntry.ChangeType.RENAME:
                    return FileActionEnum.Rename;
                default:
                    return FileActionEnum.None;
            }
        }
Example #36
0
		internal static NGit.Diff.DiffEntry Modify(string path)
		{
			NGit.Diff.DiffEntry e = new NGit.Diff.DiffEntry();
			e.oldMode = FileMode.REGULAR_FILE;
			e.oldPath = path;
			e.newMode = FileMode.REGULAR_FILE;
			e.newPath = path;
			e.changeType = DiffEntry.ChangeType.MODIFY;
			return e;
		}
Example #37
0
			/// <summary>Open the object.</summary>
			/// <remarks>Open the object.</remarks>
			/// <param name="side">which side of the entry to read (OLD or NEW).</param>
			/// <param name="ent">the entry to examine.</param>
			/// <returns>
			/// a loader that can supply the content of the file. The loader
			/// must be used before another loader can be obtained from this
			/// same source.
			/// </returns>
			/// <exception cref="System.IO.IOException">the file cannot be accessed.</exception>
			public ObjectLoader Open(DiffEntry.Side side, DiffEntry ent)
			{
				switch (side)
				{
					case DiffEntry.Side.OLD:
					{
						return oldSource.Open(ent.oldPath, ent.oldId.ToObjectId());
					}

					case DiffEntry.Side.NEW:
					{
						return newSource.Open(ent.newPath, ent.newId.ToObjectId());
					}

					default:
					{
						throw new ArgumentException();
					}
				}
			}
Example #38
0
		internal static NGit.Diff.DiffEntry Pair(DiffEntry.ChangeType changeType, NGit.Diff.DiffEntry
			 src, NGit.Diff.DiffEntry dst, int score)
		{
			NGit.Diff.DiffEntry r = new NGit.Diff.DiffEntry();
			r.oldId = src.oldId;
			r.oldMode = src.oldMode;
			r.oldPath = src.oldPath;
			r.newId = dst.newId;
			r.newMode = dst.newMode;
			r.newPath = dst.newPath;
			r.changeType = changeType;
			r.score = score;
			return r;
		}
Example #39
0
 /// <summary>Format a patch script for one file entry.</summary>
 /// <remarks>Format a patch script for one file entry.</remarks>
 /// <param name="ent">the entry to be formatted.</param>
 /// <exception cref="System.IO.IOException">
 /// a file's content cannot be read, or the output stream cannot
 /// be written to.
 /// </exception>
 public virtual void Format(DiffEntry ent)
 {
     DiffFormatter.FormatResult res = CreateFormatResult(ent);
     Format(res.header, res.a, res.b);
 }
Example #40
0
		/// <summary>Get the mode associated with this file.</summary>
		/// <remarks>Get the mode associated with this file.</remarks>
		/// <param name="side">which mode to obtain.</param>
		/// <returns>the mode.</returns>
		public virtual FileMode GetMode(DiffEntry.Side side)
		{
			return side == DiffEntry.Side.OLD ? GetOldMode() : GetNewMode();
		}
Example #41
0
 /// <summary>
 /// Creates a
 /// <see cref="NGit.Patch.FileHeader">NGit.Patch.FileHeader</see>
 /// representing the given
 /// <see cref="DiffEntry">DiffEntry</see>
 /// <p>
 /// This method does not use the OutputStream associated with this
 /// DiffFormatter instance. It is therefore safe to instantiate this
 /// DiffFormatter instance with a
 /// <see cref="NGit.Util.IO.DisabledOutputStream">NGit.Util.IO.DisabledOutputStream</see>
 /// if this method
 /// is the only one that will be used.
 /// </summary>
 /// <param name="ent">the DiffEntry to create the FileHeader for</param>
 /// <returns>
 /// a FileHeader representing the DiffEntry. The FileHeader's buffer
 /// will contain only the header of the diff output. It will also
 /// contain one
 /// <see cref="NGit.Patch.HunkHeader">NGit.Patch.HunkHeader</see>
 /// .
 /// </returns>
 /// <exception cref="System.IO.IOException">
 /// the stream threw an exception while writing to it, or one of
 /// the blobs referenced by the DiffEntry could not be read.
 /// </exception>
 /// <exception cref="NGit.Errors.CorruptObjectException">one of the blobs referenced by the DiffEntry is corrupt.
 ///     </exception>
 /// <exception cref="NGit.Errors.MissingObjectException">one of the blobs referenced by the DiffEntry is missing.
 ///     </exception>
 public virtual FileHeader ToFileHeader(DiffEntry ent)
 {
     return(CreateFormatResult(ent).header);
 }
Example #42
0
		// reduce the visibility of the default constructor
		/// <summary>Convert the TreeWalk into DiffEntry headers.</summary>
		/// <remarks>Convert the TreeWalk into DiffEntry headers.</remarks>
		/// <param name="walk">the TreeWalk to walk through. Must have exactly two trees.</param>
		/// <returns>headers describing the changed files.</returns>
		/// <exception cref="System.IO.IOException">the repository cannot be accessed.</exception>
		public static IList<NGit.Diff.DiffEntry> Scan(TreeWalk walk)
		{
			IList<NGit.Diff.DiffEntry> r = new AList<NGit.Diff.DiffEntry>();
			MutableObjectId idBuf = new MutableObjectId();
			while (walk.Next())
			{
				NGit.Diff.DiffEntry entry = new NGit.Diff.DiffEntry();
				walk.GetObjectId(idBuf, 0);
				entry.oldId = AbbreviatedObjectId.FromObjectId(idBuf);
				walk.GetObjectId(idBuf, 1);
				entry.newId = AbbreviatedObjectId.FromObjectId(idBuf);
				entry.oldMode = walk.GetFileMode(0);
				entry.newMode = walk.GetFileMode(1);
				entry.newPath = entry.oldPath = walk.PathString;
				if (entry.oldMode == FileMode.MISSING)
				{
					entry.oldPath = NGit.Diff.DiffEntry.DEV_NULL;
					entry.changeType = DiffEntry.ChangeType.ADD;
					r.AddItem(entry);
				}
				else
				{
					if (entry.newMode == FileMode.MISSING)
					{
						entry.newPath = NGit.Diff.DiffEntry.DEV_NULL;
						entry.changeType = DiffEntry.ChangeType.DELETE;
						r.AddItem(entry);
					}
					else
					{
						entry.changeType = DiffEntry.ChangeType.MODIFY;
						if (RenameDetector.SameType(entry.oldMode, entry.newMode))
						{
							r.AddItem(entry);
						}
						else
						{
							Sharpen.Collections.AddAll(r, BreakModify(entry));
						}
					}
				}
			}
			return r;
		}
Example #43
0
 // The hunk header is not taken into account for patch id calculation
 /// <exception cref="System.IO.IOException"></exception>
 protected internal override void FormatIndexLine(OutputStream o, DiffEntry ent)
 {
 }
        private static RevisionEntryInfo ConvertToRevisionEntryInfo(DiffEntry difference)
        {
            var path = Path(difference);
            var revisionEntryInfo = new RevisionEntryInfo {Path = path, Action = GetAction(difference.GetChangeType())};

            return revisionEntryInfo;
        }
 private static DiffEntry ExactRename(DiffEntry src, DiffEntry dst)
 {
     return(DiffEntry.Pair(DiffEntry.ChangeType.RENAME, src, dst, EXACT_RENAME_SCORE));
 }
Example #46
0
 private static bool IsRename(DiffEntry ent)
 {
     return ent.GetChangeType() == DiffEntry.ChangeType.RENAME || ent.GetChangeType()
         == DiffEntry.ChangeType.COPY;
 }
Example #47
0
 private static bool IsRename(DiffEntry ent)
 {
     return(ent.GetChangeType() == DiffEntry.ChangeType.RENAME || ent.GetChangeType()
            == DiffEntry.ChangeType.COPY);
 }
        /// <summary>
        /// Convert the TreeWalk into DiffEntry headers, depending on
        /// <code>includeTrees</code>
        /// it will add tree objects into result or not.
        /// </summary>
        /// <param name="walk">
        /// the TreeWalk to walk through. Must have exactly two trees and
        /// when
        /// <code>includeTrees</code>
        /// parameter is
        /// <code>true</code>
        /// it can't
        /// be recursive.
        /// </param>
        /// <param name="includeTrees">include tree object's.</param>
        /// <returns>headers describing the changed files.</returns>
        /// <exception cref="System.IO.IOException">the repository cannot be accessed.</exception>
        /// <exception cref="System.ArgumentException">
        /// when
        /// <code>includeTrees</code>
        /// is true and given TreeWalk is
        /// recursive. Or when given TreeWalk doesn't have exactly two
        /// trees
        /// </exception>
        public static IList <NGit.Diff.DiffEntry> Scan(TreeWalk walk, bool includeTrees)
        {
            if (walk.TreeCount != 2)
            {
                throw new ArgumentException(JGitText.Get().treeWalkMustHaveExactlyTwoTrees);
            }
            if (includeTrees && walk.Recursive)
            {
                throw new ArgumentException(JGitText.Get().cannotBeRecursiveWhenTreesAreIncluded);
            }
            IList <NGit.Diff.DiffEntry> r     = new AList <NGit.Diff.DiffEntry>();
            MutableObjectId             idBuf = new MutableObjectId();

            while (walk.Next())
            {
                NGit.Diff.DiffEntry entry = new NGit.Diff.DiffEntry();
                walk.GetObjectId(idBuf, 0);
                entry.oldId = AbbreviatedObjectId.FromObjectId(idBuf);
                walk.GetObjectId(idBuf, 1);
                entry.newId   = AbbreviatedObjectId.FromObjectId(idBuf);
                entry.oldMode = walk.GetFileMode(0);
                entry.newMode = walk.GetFileMode(1);
                entry.newPath = entry.oldPath = walk.PathString;
                if (entry.oldMode == FileMode.MISSING)
                {
                    entry.oldPath    = NGit.Diff.DiffEntry.DEV_NULL;
                    entry.changeType = DiffEntry.ChangeType.ADD;
                    r.AddItem(entry);
                }
                else
                {
                    if (entry.newMode == FileMode.MISSING)
                    {
                        entry.newPath    = NGit.Diff.DiffEntry.DEV_NULL;
                        entry.changeType = DiffEntry.ChangeType.DELETE;
                        r.AddItem(entry);
                    }
                    else
                    {
                        if (!entry.oldId.Equals(entry.newId))
                        {
                            entry.changeType = DiffEntry.ChangeType.MODIFY;
                            if (RenameDetector.SameType(entry.oldMode, entry.newMode))
                            {
                                r.AddItem(entry);
                            }
                            else
                            {
                                Sharpen.Collections.AddAll(r, BreakModify(entry));
                            }
                        }
                        else
                        {
                            if (entry.oldMode != entry.newMode)
                            {
                                entry.changeType = DiffEntry.ChangeType.MODIFY;
                                r.AddItem(entry);
                            }
                        }
                    }
                }
                if (includeTrees && walk.IsSubtree)
                {
                    walk.EnterSubtree();
                }
            }
            return(r);
        }
Example #49
0
        /// <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);
            }
        }
Example #50
0
        private static DiffType ToDiffType(DiffEntry.ChangeType changeType)
        {
            switch (changeType)
            {
                case DiffEntry.ChangeType.MODIFY:
                    return DiffType.Modify;

                case DiffEntry.ChangeType.ADD:
                    return DiffType.Add;

                case DiffEntry.ChangeType.DELETE:
                    return DiffType.Delete;

                case DiffEntry.ChangeType.COPY:
                    return DiffType.Copy;

                case DiffEntry.ChangeType.RENAME:
                    return DiffType.Rename;
            }

            throw new InvalidOperationException();
        }
Example #51
0
		/// <summary>
		/// Called whenever a diff was found that is actually a rename or copy of a
		/// file.
		/// </summary>
		/// <remarks>
		/// Called whenever a diff was found that is actually a rename or copy of a
		/// file.
		/// </remarks>
		/// <param name="entry">the entry representing the rename/copy</param>
		public abstract void Renamed(DiffEntry entry);
Example #52
0
        /// <exception cref="System.IO.IOException"></exception>
        private byte[] Open(DiffEntry.Side side, DiffEntry entry)
        {
            if (entry.GetMode(side) == FileMode.MISSING)
            {
                return(EMPTY);
            }
            if (entry.GetMode(side).GetObjectType() != Constants.OBJ_BLOB)
            {
                return(EMPTY);
            }
            if (IsBinary())
            {
                return(BINARY);
            }
            AbbreviatedObjectId id = entry.GetId(side);

            if (!id.IsComplete)
            {
                ICollection <ObjectId> ids = reader.Resolve(id);
                if (ids.Count == 1)
                {
                    id = AbbreviatedObjectId.FromObjectId(ids.Iterator().Next());
                    switch (side)
                    {
                    case DiffEntry.Side.OLD:
                    {
                        entry.oldId = id;
                        break;
                    }

                    case DiffEntry.Side.NEW:
                    {
                        entry.newId = id;
                        break;
                    }
                    }
                }
                else
                {
                    if (ids.Count == 0)
                    {
                        throw new MissingObjectException(id, Constants.OBJ_BLOB);
                    }
                    else
                    {
                        throw new AmbiguousObjectException(id, ids);
                    }
                }
            }
            try
            {
                ObjectLoader ldr = source.Open(side, entry);
                return(ldr.GetBytes(binaryFileThreshold));
            }
            catch (LargeObjectException.ExceedsLimit)
            {
                return(BINARY);
            }
            catch (LargeObjectException.ExceedsByteArrayLimit)
            {
                return(BINARY);
            }
            catch (LargeObjectException.OutOfMemory)
            {
                return(BINARY);
            }
            catch (LargeObjectException tooBig)
            {
                tooBig.SetObjectId(id.ToObjectId());
                throw;
            }
        }
			public override void Renamed(DiffEntry diff)
			{
				diffs.AddItem(diff);
			}