Search code examples
c#gitlibgit2libgit2sharp

Libgit2Sharp: get files in all commits between two tags


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


Solution

  • 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 ();
        }