Example #1
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);
        }
Example #2
0
        public Change[] GetChanges(string commitId)
        {
            if (repository == null)
            {
                return(null);
            }
            RevWalk walk = null;

            try
            {
                var id = repository.Resolve(commitId);
                walk = new RevWalk(repository);
                RevCommit commit = walk.ParseCommit(id);
                if (commit == null || commit.ParentCount == 0)
                {
                    return(null);
                }

                var pid     = commit.Parents[0].Id;
                var pcommit = walk.ParseCommit(pid);
                return(GetChanges(repository, commit.Tree.Id, pcommit.Tree.Id));
            }
            finally
            {
                if (walk != null)
                {
                    walk.Dispose();
                }
            }
        }
Example #3
0
        public Commit GetCommit(string commitId)
        {
            //commitId = repository.Resolve(commitId).Name;
            //return Commits.Where(c => c.Id.StartsWith(commitId)).FirstOrDefault();
            var id = repository.Resolve(commitId);

            if (id == null)
            {
                return(null);
            }

            RevWalk   walk   = new RevWalk(repository);
            RevCommit commit = walk.ParseCommit(id);

            walk.Dispose();
            return(commit == null || commit.Tree == null ? null : new Commit
            {
                Id = commit.Id.Name,
                ParentIds = commit.Parents.Select(p => p.Id.Name).ToList(),
                CommitDateRelative = RelativeDateFormatter.Format(commit.GetAuthorIdent().GetWhen()),
                CommitterName = commit.GetCommitterIdent().GetName(),
                CommitterEmail = commit.GetCommitterIdent().GetEmailAddress(),
                CommitDate = commit.GetCommitterIdent().GetWhen(),
                Message = commit.GetShortMessage(),
            });
        }
 public void Dispose()
 {
     if (_walker != null)
     {
         _walker.Dispose();
     }
 }
Example #5
0
        public string GetFullMessage(string hash)
        {
            var walk = new RevWalk(this.git.GetRepository());

            RevCommit commit = walk.ParseCommit(ObjectId.FromString(hash));

            walk.Dispose();

            return commit.GetFullMessage();
        }
Example #6
0
        public string GetFullMessage(string hash)
        {
            var walk = new RevWalk(this.git.GetRepository());

            RevCommit commit = walk.ParseCommit(ObjectId.FromString(hash));

            walk.Dispose();

            return(commit.GetFullMessage());
        }
Example #7
0
        /// <exception cref="NGit.Api.Errors.GitAPIException"></exception>
        /// <exception cref="NGit.Api.Errors.InvalidRefNameException"></exception>
        public override ICollection <RevCommit> Call()
        {
            CheckCallable();
            try
            {
                if (repo.GetRef(Constants.R_STASH) == null)
                {
                    return(Sharpen.Collections.EmptyList <RevCommit>());
                }
            }
            catch (IOException e)
            {
                throw new InvalidRefNameException(MessageFormat.Format(JGitText.Get().cannotRead,
                                                                       Constants.R_STASH), e);
            }
            ReflogCommand refLog = new ReflogCommand(repo);

            refLog.SetRef(Constants.R_STASH);
            ICollection <ReflogEntry> stashEntries = refLog.Call();

            if (stashEntries.IsEmpty())
            {
                return(Sharpen.Collections.EmptyList <RevCommit>());
            }
            IList <RevCommit> stashCommits = new AList <RevCommit>(stashEntries.Count);
            RevWalk           walk         = new RevWalk(repo);

            walk.SetRetainBody(true);
            try
            {
                foreach (ReflogEntry entry in stashEntries)
                {
                    try
                    {
                        stashCommits.AddItem(walk.ParseCommit(entry.GetNewId()));
                    }
                    catch (IOException e)
                    {
                        throw new JGitInternalException(MessageFormat.Format(JGitText.Get().cannotReadCommit
                                                                             , entry.GetNewId()), e);
                    }
                }
            }
            finally
            {
                walk.Dispose();
            }
            return(stashCommits);
        }
Example #8
0
        private ObjectId GetTreeIdFromCommitId(Repository repository, string commitId)
        {
            var id = repository.Resolve(commitId);

            if (id == null)
            {
                return(null);
            }

            RevWalk   walk   = new RevWalk(repository);
            RevCommit commit = walk.ParseCommit(id);

            walk.Dispose();
            return(commit == null || commit.Tree == null ? null :
                   commit.Tree.Id);
        }
Example #9
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();
                }
            }
        }
Example #10
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();
         }
     }
 }
Example #11
0
 public void Dispose()
 {
     _walker.Dispose();
 }
Example #12
0
        /// <summary>
        /// Return a list of those objects in the index which differ from whats in
        /// HEAD
        /// </summary>
        /// <returns>a set of ObjectIds of changed objects in the index</returns>
        /// <exception cref="System.IO.IOException">System.IO.IOException</exception>
        /// <exception cref="NGit.Errors.CorruptObjectException">NGit.Errors.CorruptObjectException
        ///     </exception>
        /// <exception cref="NGit.Errors.NoWorkTreeException">NGit.Errors.NoWorkTreeException
        ///     </exception>
        private ICollection <ObjectId> ListNonHEADIndexObjects()
        {
            RevWalk revWalk = null;

            try
            {
                if (repo.GetIndexFile() == null)
                {
                    return(Sharpen.Collections.EmptySet <ObjectId>());
                }
            }
            catch (NoWorkTreeException)
            {
                return(Sharpen.Collections.EmptySet <ObjectId>());
            }
            TreeWalk treeWalk = new TreeWalk(repo);

            try
            {
                treeWalk.AddTree(new DirCacheIterator(repo.ReadDirCache()));
                ObjectId headID = repo.Resolve(Constants.HEAD);
                if (headID != null)
                {
                    revWalk = new RevWalk(repo);
                    treeWalk.AddTree(revWalk.ParseTree(headID));
                    revWalk.Dispose();
                    revWalk = null;
                }
                treeWalk.Filter    = TreeFilter.ANY_DIFF;
                treeWalk.Recursive = true;
                ICollection <ObjectId> ret = new HashSet <ObjectId>();
                while (treeWalk.Next())
                {
                    ObjectId objectId = treeWalk.GetObjectId(0);
                    switch (treeWalk.GetRawMode(0) & FileMode.TYPE_MASK)
                    {
                    case FileMode.TYPE_MISSING:
                    case FileMode.TYPE_GITLINK:
                    {
                        continue;
                        goto case FileMode.TYPE_TREE;
                    }

                    case FileMode.TYPE_TREE:
                    case FileMode.TYPE_FILE:
                    case FileMode.TYPE_SYMLINK:
                    {
                        ret.AddItem(objectId);
                        continue;
                        goto default;
                    }

                    default:
                    {
                        throw new IOException(MessageFormat.Format(JGitText.Get().corruptObjectInvalidMode3
                                                                   , string.Format("%o", Sharpen.Extensions.ValueOf(treeWalk.GetRawMode(0)), (objectId
                                                                                                                                              == null) ? "null" : objectId.Name, treeWalk.PathString, repo.GetIndexFile())));
                    }
                    }
                }
                return(ret);
            }
            finally
            {
                if (revWalk != null)
                {
                    revWalk.Dispose();
                }
                treeWalk.Release();
            }
        }
        private ObjectId GetTreeIdFromCommitId(Repository repository, string commitId)
        {
            var id = repository.Resolve(commitId);
            if (id == null) return null;

            RevWalk walk = new RevWalk(repository);
            RevCommit commit = walk.ParseCommit(id);
            walk.Dispose();
            return commit == null || commit.Tree == null ? null :
                commit.Tree.Id;
        }
        public Commit GetCommit(string commitId)
        {
            //commitId = repository.Resolve(commitId).Name;
            //return Commits.Where(c => c.Id.StartsWith(commitId)).FirstOrDefault();
            var id = repository.Resolve(commitId);
            if (id == null) return null;

            RevWalk walk = new RevWalk(repository);
            RevCommit commit = walk.ParseCommit(id);
            walk.Dispose();
            return commit == null || commit.Tree == null ? null : new Commit
                {
                    Id = commit.Id.Name,
                    ParentIds = commit.Parents.Select(p => p.Id.Name).ToList(),
                    CommitDateRelative = RelativeDateFormatter.Format(commit.GetAuthorIdent().GetWhen()),
                    CommitterName = commit.GetCommitterIdent().GetName(),
                    CommitterEmail = commit.GetCommitterIdent().GetEmailAddress(),
                    CommitDate = commit.GetCommitterIdent().GetWhen(),
                    Message = commit.GetShortMessage(),
                };
        }
        public Change[] GetChanges(string commitId)
        {
            if (repository == null) return null;
            RevWalk walk = null;
            try
            {
                var id = repository.Resolve(commitId);
                walk = new RevWalk(repository);
                RevCommit commit = walk.ParseCommit(id);
                if (commit == null || commit.ParentCount == 0) return null;

                var pid = commit.Parents[0].Id;
                var pcommit = walk.ParseCommit(pid);
                return GetChanges(repository, commit.Tree.Id, pcommit.Tree.Id);
            }
            finally
            {
                if (walk != null) walk.Dispose();
            }
        }