public byte[] GetFileContent(string fileName)
        {
            if (!HasGitRepository || string.IsNullOrEmpty(fileName))
            {
                return(null);
            }

            fileName = GetRelativeFileNameForGit(fileName);

            try
            {
                var     head    = repository.Resolve(Constants.HEAD);
                RevTree revTree = head == null ? null : new RevWalk(repository).ParseTree(head);
                if (revTree != null)
                {
                    var entry = TreeWalk.ForPath(repository, fileName, revTree);
                    if (entry != null && !entry.IsSubtree)
                    {
                        var blob = repository.Open(entry.GetObjectId(0));
                        if (blob != null)
                        {
                            return(blob.GetCachedBytes());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.WriteLine("Get File Content: {0}\r\n{1}", fileName, ex.ToString());
            }

            return(null);
        }
Example #2
0
        public virtual void TestAddUnstagedChanges()
        {
            FilePath file = new FilePath(db.WorkTree, "a.txt");

            FileUtils.CreateNewFile(file);
            PrintWriter writer = new PrintWriter(file);

            writer.Write("content");
            writer.Close();
            Git git = new Git(db);

            git.Add().AddFilepattern("a.txt").Call();
            RevCommit commit = git.Commit().SetMessage("initial commit").Call();
            TreeWalk  tw     = TreeWalk.ForPath(db, "a.txt", commit.Tree);

            NUnit.Framework.Assert.AreEqual("6b584e8ece562ebffc15d38808cd6b98fc3d97ea", tw.GetObjectId
                                                (0).GetName());
            writer = new PrintWriter(file);
            writer.Write("content2");
            writer.Close();
            commit = git.Commit().SetMessage("second commit").Call();
            tw     = TreeWalk.ForPath(db, "a.txt", commit.Tree);
            NUnit.Framework.Assert.AreEqual("6b584e8ece562ebffc15d38808cd6b98fc3d97ea", tw.GetObjectId
                                                (0).GetName());
            commit = git.Commit().SetAll(true).SetMessage("third commit").SetAll(true).Call();
            tw     = TreeWalk.ForPath(db, "a.txt", commit.Tree);
            NUnit.Framework.Assert.AreEqual("db00fd65b218578127ea51f3dffac701f12f486a", tw.GetObjectId
                                                (0).GetName());
        }
Example #3
0
        public async Task <ModpackInfo> GetLatestModpackInfo()
        {
            if (Repository == null)
            {
                return(await DownloadLatestModpackInfo());
            }

            return(await Task.Run(() => {
                var id = Repository.Resolve("origin/" + Settings.Branch);
                ObjectReader reader = null;
                try {
                    reader = Repository.NewObjectReader();
                    var walk = new RevWalk(reader);
                    var commit = walk.ParseCommit(id);
                    var treeWalk = TreeWalk.ForPath(reader, ModpackInfo.FileName, commit.Tree);
                    if (treeWalk == null)
                    {
                        return null;
                    }

                    byte[] data = reader.Open(treeWalk.GetObjectId(0)).GetBytes();
                    var modpackJson = Encoding.UTF8.GetString(data);
                    return ModpackInfo.Parse(modpackJson);
                } finally {
                    if (reader != null)
                    {
                        reader.Release();
                    }
                }
            }));
        }
Example #4
0
        public virtual void TestExecutableRetention()
        {
            StoredConfig config = ((FileBasedConfig)db.GetConfig());

            config.SetBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_FILEMODE
                              , true);
            config.Save();
            FS     executableFs = new _FS_87();
            Git    git          = Git.Open(db.Directory, executableFs);
            string path         = "a.txt";

            WriteTrashFile(path, "content");
            git.Add().AddFilepattern(path).Call();
            RevCommit commit1 = git.Commit().SetMessage("commit").Call();
            TreeWalk  walk    = TreeWalk.ForPath(db, path, commit1.Tree);

            NUnit.Framework.Assert.IsNotNull(walk);
            NUnit.Framework.Assert.AreEqual(FileMode.EXECUTABLE_FILE, walk.GetFileMode(0));
            FS nonExecutableFs = new _FS_132();

            config = ((FileBasedConfig)db.GetConfig());
            config.SetBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_FILEMODE
                              , false);
            config.Save();
            Git git2 = Git.Open(db.Directory, nonExecutableFs);

            WriteTrashFile(path, "content2");
            RevCommit commit2 = git2.Commit().SetOnly(path).SetMessage("commit2").Call();

            walk = TreeWalk.ForPath(db, path, commit2.Tree);
            NUnit.Framework.Assert.IsNotNull(walk);
            NUnit.Framework.Assert.AreEqual(FileMode.EXECUTABLE_FILE, walk.GetFileMode(0));
        }
Example #5
0
        public byte[] GetFileContent(string commitId, string fileName)
        {
            if (repository == null)
            {
                return(null);
            }
            RevWalk walk = null;

            try
            {
                var     head    = repository.Resolve(commitId);
                RevTree revTree = head == null ? null : new RevWalk(repository).ParseTree(head);

                var entry = TreeWalk.ForPath(repository, fileName, revTree);
                if (entry != null && !entry.IsSubtree)
                {
                    var blob = repository.Open(entry.GetObjectId(0));
                    if (blob != null)
                    {
                        return(blob.GetCachedBytes());
                    }
                }
            }
            catch { }
            finally
            {
                if (walk != null)
                {
                    walk.Dispose();
                }
            }
            return(null);
        }
        public GitFileStatus GetFileStatusNoCache(string fileName)
        {
            if (Directory.Exists(fileName))
            {
                return(GitFileStatus.Ignored);
            }

            var      fileNameRel = GetRelativeFileNameForGit(fileName);
            TreeWalk treeWalk    = new TreeWalk(this.repository)
            {
                Recursive = true
            };
            RevTree revTree = head == null ? null : new RevWalk(repository).ParseTree(head);

            if (revTree != null)
            {
                treeWalk.AddTree(revTree);
            }
            else
            {
                treeWalk.AddTree(new EmptyTreeIterator());
            }
            treeWalk.AddTree(new DirCacheIterator(dirCache));
            treeWalk.AddTree(new FileTreeIterator(this.repository));

            var filters = new TreeFilter[] {
                PathFilter.Create(fileNameRel),
                new SkipWorkTreeFilter(INDEX),
                new IndexDiffFilter(INDEX, WORKDIR)
            };

            treeWalk.Filter = AndTreeFilter.Create(filters);

            var status = GitFileStatus.NotControlled;

            if (treeWalk.Next())
            {
                status = GetFileStatus(treeWalk);
            }

            if (status == GitFileStatus.NotControlled)
            {
                var dirCacheEntry2 = dirCache.GetEntry(fileNameRel);
                if (dirCacheEntry2 != null)
                {
                    var treeEntry2 = TreeWalk.ForPath(repository, fileNameRel, revTree);
                    if (treeEntry2 != null && treeEntry2.GetObjectId(0).Equals(dirCacheEntry2.GetObjectId()))
                    {
                        return(GitFileStatus.Tracked);
                    }
                }
            }
            return(GitFileStatus.NotControlled);
        }
Example #7
0
        static RawText GetRawText(NGit.Repository repo, string file, RevCommit commit)
        {
            TreeWalk tw = TreeWalk.ForPath(repo, file, commit.Tree);

            if (tw == null)
            {
                return(new RawText(new byte[0]));
            }
            ObjectId objectID = tw.GetObjectId(0);

            byte[] data = repo.ObjectDatabase.Open(objectID).GetBytes();
            return(new RawText(data));
        }
Example #8
0
        public virtual void TestRules1thru3_NoIndexEntry()
        {
            ObjectId head     = BuildTree(Mk("foo"));
            TreeWalk tw       = TreeWalk.ForPath(db, "foo", head);
            ObjectId objectId = tw.GetObjectId(0);
            ObjectId merge    = db.NewObjectInserter().Insert(Constants.OBJ_TREE, new byte[0]);

            PrescanTwoTrees(head, merge);
            NUnit.Framework.Assert.IsTrue(GetRemoved().Contains("foo"));
            PrescanTwoTrees(merge, head);
            NUnit.Framework.Assert.AreEqual(objectId, GetUpdated().Get("foo"));
            merge = BuildTree(Mkmap("foo", "a"));
            tw    = TreeWalk.ForPath(db, "foo", merge);
            PrescanTwoTrees(head, merge);
            AssertConflict("foo");
        }
        public virtual void TestEditFlat()
        {
            RevBlob   a     = tr.Blob("a");
            RevBlob   b     = tr.Blob("b");
            RevBlob   data1 = tr.Blob("data1");
            RevBlob   data2 = tr.Blob("data2");
            RevCommit r     = tr.Commit().Add(a.Name, data1).Add(b.Name, data2).Add(".gitignore",
                                                                                    string.Empty).Add("zoo-animals.txt", b).Create();

            //
            //
            //
            //
            //
            tr.ParseBody(r);
            NoteMap map = NoteMap.Read(reader, r);

            map.Set(a, data2);
            map.Set(b, null);
            map.Set(data1, b);
            map.Set(data2, null);
            NUnit.Framework.Assert.AreEqual(data2, map.Get(a));
            NUnit.Framework.Assert.AreEqual(b, map.Get(data1));
            NUnit.Framework.Assert.IsFalse(map.Contains(b), "no b");
            NUnit.Framework.Assert.IsFalse(map.Contains(data2), "no data2");
            MutableObjectId id = new MutableObjectId();

            for (int p = 42; p > 0; p--)
            {
                id.SetByte(1, p);
                map.Set(id, data1);
            }
            for (int p_1 = 42; p_1 > 0; p_1--)
            {
                id.SetByte(1, p_1);
                NUnit.Framework.Assert.IsTrue(map.Contains(id), "contains " + id);
            }
            RevCommit n = CommitNoteMap(map);

            map = NoteMap.Read(reader, n);
            NUnit.Framework.Assert.AreEqual(data2, map.Get(a));
            NUnit.Framework.Assert.AreEqual(b, map.Get(data1));
            NUnit.Framework.Assert.IsFalse(map.Contains(b), "no b");
            NUnit.Framework.Assert.IsFalse(map.Contains(data2), "no data2");
            NUnit.Framework.Assert.AreEqual(b, TreeWalk.ForPath(reader, "zoo-animals.txt", n.
                                                                Tree).GetObjectId(0));
        }
Example #10
0
        /// <exception cref="NGit.Errors.MissingObjectException"></exception>
        /// <exception cref="NGit.Errors.IncorrectObjectTypeException"></exception>
        /// <exception cref="System.IO.IOException"></exception>
        private static byte[] Read(Repository db, AnyObjectId treeish, string path)
        {
            ObjectReader or = db.NewObjectReader();

            try
            {
                TreeWalk tree = TreeWalk.ForPath(or, path, AsTree(or, treeish));
                if (tree == null)
                {
                    throw new FileNotFoundException(MessageFormat.Format(JGitText.Get().entryNotFoundByPath
                                                                         , path));
                }
                return(Read(or, tree.GetObjectId(0)));
            }
            finally
            {
                or.Release();
            }
        }
        public virtual void TestLeafSplitsWhenFull()
        {
            RevBlob         data1 = tr.Blob("data1");
            MutableObjectId idBuf = new MutableObjectId();
            RevCommit       r     = tr.Commit().Add(data1.Name, data1).Create();

            //
            //
            tr.ParseBody(r);
            NoteMap map = NoteMap.Read(reader, r);

            for (int i = 0; i < 254; i++)
            {
                idBuf.SetByte(Constants.OBJECT_ID_LENGTH - 1, i);
                map.Set(idBuf, data1);
            }
            RevCommit n  = CommitNoteMap(map);
            TreeWalk  tw = new TreeWalk(reader);

            tw.Reset(n.Tree);
            while (tw.Next())
            {
                NUnit.Framework.Assert.IsFalse(tw.IsSubtree, "no fan-out subtree");
            }
            for (int i_1 = 254; i_1 < 256; i_1++)
            {
                idBuf.SetByte(Constants.OBJECT_ID_LENGTH - 1, i_1);
                map.Set(idBuf, data1);
            }
            idBuf.SetByte(Constants.OBJECT_ID_LENGTH - 2, 1);
            map.Set(idBuf, data1);
            n = CommitNoteMap(map);
            // The 00 bucket is fully split.
            string path = Fanout(38, idBuf.Name);

            tw = TreeWalk.ForPath(reader, path, n.Tree);
            NUnit.Framework.Assert.IsNotNull(tw, "has " + path);
            // The other bucket is not.
            path = Fanout(2, data1.Name);
            tw   = TreeWalk.ForPath(reader, path, n.Tree);
            NUnit.Framework.Assert.IsNotNull(tw, "has " + path);
        }
Example #12
0
        /// <summary>Checks if a file with the given path exists in the HEAD tree</summary>
        /// <param name="path"></param>
        /// <returns>true if the file exists</returns>
        /// <exception cref="System.IO.IOException">System.IO.IOException</exception>
        private bool InHead(string path)
        {
            ObjectId headId = db.Resolve(Constants.HEAD);
            RevWalk  rw     = new RevWalk(db);
            TreeWalk tw     = null;

            try
            {
                tw = TreeWalk.ForPath(db, path, rw.ParseTree(headId));
                return(tw != null);
            }
            finally
            {
                rw.Release();
                rw.Dispose();
                if (tw != null)
                {
                    tw.Release();
                }
            }
        }
        public virtual void TestEditFanout2_38()
        {
            RevBlob   a     = tr.Blob("a");
            RevBlob   b     = tr.Blob("b");
            RevBlob   data1 = tr.Blob("data1");
            RevBlob   data2 = tr.Blob("data2");
            RevCommit r     = tr.Commit().Add(Fanout(2, a.Name), data1).Add(Fanout(2, b.Name), data2
                                                                            ).Add(".gitignore", string.Empty).Add("zoo-animals.txt", b).Create();

            //
            //
            //
            //
            //
            tr.ParseBody(r);
            NoteMap map = NoteMap.Read(reader, r);

            map.Set(a, data2);
            map.Set(b, null);
            map.Set(data1, b);
            map.Set(data2, null);
            NUnit.Framework.Assert.AreEqual(data2, map.Get(a));
            NUnit.Framework.Assert.AreEqual(b, map.Get(data1));
            NUnit.Framework.Assert.IsFalse(map.Contains(b), "no b");
            NUnit.Framework.Assert.IsFalse(map.Contains(data2), "no data2");
            RevCommit n = CommitNoteMap(map);

            map.Set(a, null);
            map.Set(data1, null);
            NUnit.Framework.Assert.IsFalse(map.Contains(a), "no a");
            NUnit.Framework.Assert.IsFalse(map.Contains(data1), "no data1");
            map = NoteMap.Read(reader, n);
            NUnit.Framework.Assert.AreEqual(data2, map.Get(a));
            NUnit.Framework.Assert.AreEqual(b, map.Get(data1));
            NUnit.Framework.Assert.IsFalse(map.Contains(b), "no b");
            NUnit.Framework.Assert.IsFalse(map.Contains(data2), "no data2");
            NUnit.Framework.Assert.AreEqual(b, TreeWalk.ForPath(reader, "zoo-animals.txt", n.
                                                                Tree).GetObjectId(0));
        }
Example #14
0
        private static string GetSnapshot(Repository repository, Git git, RevTree revTree, String file)
        {
            //Get snapshot , Retrive older version of an object
            TreeWalk treeWalk = TreeWalk.ForPath(git.GetRepository(), file, revTree);

            byte[] data = repository.Open(treeWalk.GetObjectId(0)).GetBytes();
            return(System.Text.Encoding.UTF8.GetString(data));


            //ObjectLoader loader = repository.Open(rev.ToObjectId());
            //InputStream ttt = loader.OpenStream();
            //string result = ttt.ToString();
            //Retrive older version of an object
            //RevTree revTree = rev.Tree;
            //TreeWalk treeWalk = new TreeWalk(git.GetRepository());
            //treeWalk.AddTree(revTree);
            //while (treeWalk.Next())
            //{
            //    //compare treeWalk.NameString yourself
            //    byte[] bytes = treeWalk.ObjectReader.Open(rev.ToObjectId()).GetCachedBytes();
            //    string result = System.Text.Encoding.UTF8.GetString(rev.RawBuffer);
            //}
        }
Example #15
0
        public static RevCommit[] Blame(NGit.Repository repo, RevCommit commit, string file)
        {
            string   localFile = ToGitPath(repo, file);
            TreeWalk tw        = TreeWalk.ForPath(repo, localFile, commit.Tree);

            if (tw == null)
            {
                return(new RevCommit [0]);
            }
            int totalLines = GetFileLineCount(repo, tw);
            int lineCount  = totalLines;

            RevCommit[] lines     = new RevCommit [lineCount];
            RevWalk     revWalker = new RevWalk(repo);

            revWalker.MarkStart(commit);
            List <RevCommit> commitHistory = new List <RevCommit>();
            FilePath         localCpath    = FromGitPath(repo, localFile);

            foreach (RevCommit ancestorCommit in revWalker)
            {
                foreach (Change change in GetCommitChanges(repo, ancestorCommit))
                {
                    FilePath cpath = FromGitPath(repo, change.Path);
                    if (change.ChangeType != ChangeType.Deleted && (localCpath == cpath || cpath.IsChildPathOf(localCpath)))
                    {
                        commitHistory.Add(ancestorCommit);
                        break;
                    }
                }
            }

            int historySize = commitHistory.Count;

            if (historySize > 1)
            {
                RevCommit recentCommit  = commitHistory[0];
                RawText   latestRawText = GetRawText(repo, localFile, recentCommit);

                for (int i = 1; i < historySize; i++)
                {
                    RevCommit ancestorCommit  = commitHistory[i];
                    RawText   ancestorRawText = GetRawText(repo, localFile, ancestorCommit);
                    lineCount   -= SetBlameLines(repo, lines, recentCommit, latestRawText, ancestorRawText);
                    recentCommit = ancestorCommit;

                    if (lineCount <= 0)
                    {
                        break;
                    }
                }

                if (lineCount > 0)
                {
                    RevCommit firstCommit = commitHistory[historySize - 1];

                    for (int i = 0; i < totalLines; i++)
                    {
                        if (lines[i] == null)
                        {
                            lines[i] = firstCommit;
                        }
                    }
                }
            }
            else if (historySize == 1)
            {
                RevCommit firstCommit = commitHistory[0];

                for (int i = 0; i < totalLines; i++)
                {
                    lines[i] = firstCommit;
                }
            }

            return(lines);
        }