Libgit2Sharp: get files in all commits between two tags

被刻印的时光 ゝ 提交于 2020-01-06 02:55:06

问题


I can do this in GitBash: $ git diff --name-only v01...HEAD -- *.sql

which gives:

Components/1/Database/Stored Procedures/spDC1.sql Components/1/Database/Stored Procedures/spDC2.sql

I can't see how I would do this in LibGit2Sharp. Any ideas? Thanks


回答1:


Here is an example from one of my projects that get a ICommitLog collection between two commits (current HEAD vs. the master branch):

    // git log HEAD..master --reverse
    public ICommitLog StalkerList {
        get {
            var filter = new CommitFilter { 
                SortBy = CommitSortStrategies.Reverse | CommitSortStrategies.Time,
                Since = master,
                Until = head.Tip,             
            };
            return repo.Commits.QueryBy (filter);
        }
    }

Once you have your ICommitLog collection of all the commits within the range that you need, you can cycle through each commit to get a list of the files that were effected within that commit (of course you would need to add filtering of the filename via your "*.sql" requirements):

    public String[] FilesToMerge (Commit commit)
    {
        var fileList = new List<String> ();
        foreach (var parent in commit.Parents) {
            foreach (TreeEntryChanges change in repo.Diff.Compare<TreeChanges>(parent.Tree, commit.Tree)) {
                fileList.Add (change.Path);
            }
        }
        return fileList.ToArray ();
    }



回答2:


I think SushiHangover's answer to this is pretty correct. Just a couple of amendments / updates. (P.S. Yeah I know this question's relatively old, but a complete answer would have helped me if I'd found it today, so putting one here for future peeps).

This bit should be an amendment comment, but I can't comment yet (low rep):

First, I think that in Sushi's example, master and head.Tip are the wrong way around. Until excludes a commit from the results and excludes its ancestors. So if you put head.Tip in there it'll exclude basically the entire history tree.

So AFAIK it should read more like this:

 // git log HEAD..master --reverse
public ICommitLog StalkerList {
    get {
        var filter = new CommitFilter { 
            SortBy = CommitSortStrategies.Reverse | CommitSortStrategies.Time,
            Since = head.Tip,
            Until = master       
        };
        return repo.Commits.QueryBy (filter);
    }
}

It's also SUPER important to realise that the order you give them in matters. If you just switch them around you'll get nothing back.

(Also note that Sushi's original example had a bad ',' after "head.Tip".

This bit's the update:

Also worth noting that the libgit2sharp library has been updated recently. Replacing "Since" and "Until" with "IncludeReachableFrom" and "ExcludeReachableFrom" respectively.

The names aren't particularly helpful until you realise that they're just way more verbose about what they're doing.

The comment for Exclude, for example, reads:

    /// A pointer to a commit object or a list of pointers which will be excluded (along with ancestors) from the enumeration.

So the latest implementation would look more like:

using (Repository r = new Repository(@"[repo_location]"))
{
    CommitFilter cf = new CommitFilter
    {
        SortBy = CommitSortStrategies.Reverse | CommitSortStrategies.Time,
        ExcludeReachableFrom = r.Branches["master"].Tip,
        IncludeReachableFrom = r.Head.Tip
    };

    var results = r.Commits.QueryBy(cf);

    foreach (var result in results)
    {
        //Process commits here.
    }
}

Tested this out in LINQPad and it seems to work. Might have missed something though as it was a quick draft. If so let me know.

Things to note: master and Head are actually properties of a Repository, I couldn't see that coming from anywhere in the old example but it may just be a version difference with the old version.



来源:https://stackoverflow.com/questions/36456718/libgit2sharp-get-files-in-all-commits-between-two-tags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!