Skip to content

Add RevWalker - #2038

Open
jairbubbles wants to merge 33 commits into
libgit2:masterfrom
jairbubbles:add-revwalker
Open

Add RevWalker#2038
jairbubbles wants to merge 33 commits into
libgit2:masterfrom
jairbubbles:add-revwalker

Conversation

@jairbubbles

Copy link
Copy Markdown
Contributor

This allows more flexibility than CommitLog and allows to traverse the repository multiple times more efficiently.

using var repository = new 仓库(@"<repo_path>");
using var revWalker = new RevWalker(repository);

// Store commits in a cache to speed up multiple walks on same repository
var commitsCache = new Dictionary<ObjectId, Commit>();
 
revWalker.Reset();
revWalker.Sorting(CommitSortStrategies.Topological);

revWalker.PushGlob("refs/tags/*");

var commits = Get提交().ToArray();

// Subsequent walk will reuse commits cache and internal RevWalker cache
revWalker.Reset();
revWalker.PushRef("HEAD");

commits = Get提交().ToArray();
 
IEnumerable<Commit> Get提交()
{
    while (true)
    {
        var objectId = revWalker.Next();
        if (objectId == null)
            break;
    
        if (!commitsCache.TryGetValue(objectId, out var commit))
        {
            commit = repository.Lookup<Commit>(objectId);
            commitsCache[objectId] = commit;
        }

        yield return commit;
    }
}
 

注册 for free to join this conversation on GitHub. Already have an account? 登录 to comment

标签

None yet

项目

None yet

Development

Successfully merging this pull request may close these issues.

3 participants