0

I'm using Lib2GitSharp to join the Git logs with information from some other systems and I would like to be able to retrieve all git logs since a certain date (or between dates).

I found documentation on querying the log here but it does not appear that there is a way to query the log by date. What would be the equivalent of

git log --since="2013-08-20"

in LibGit2Sharp?

Edit This seems to work but perhaps there a better and/or more elegant way?

using (var repo = new Repository(options.Repo))
{
    var since = new DateTime(2013, 8, 20);
    var commits = repo.Commits.Where(c => c.Committer.When.CompareTo(since) > 0);
    foreach (Commit commit in commits)
    {
        Console.WriteLine("A commit happened at " + commit.Committer.When.ToLocalTime());
    }
}
Ryan Taylor
  • 8,740
  • 15
  • 65
  • 98

1 Answers1

3

Using Linq I can query each commit object in the repository for its date and return only those whose data is greater than the specified one. I am not sure if there is cleaner, faster, more elegant way but this does work.

using (var repo = new Repository(options.Repo))
{
    var since = new DateTimeOffset(new DateTime(2013, 8, 20));
    var filter = new CommitFilter { Since = repo.Branches };
    var commitLog = repo.Commits.QueryBy(filter);
    var commits = commitLog.Where(c => c.Committer.When > since);
    foreach (Commit commit in commits)
    {
        Console.WriteLine("A commit happened at " + commit.Committer.When.ToLocalTime());
    }
}

EDIT: Incorporated suggestions from nulltoken

Ryan Taylor
  • 8,740
  • 15
  • 65
  • 98
  • 1
    Nice answer. Upvoted! Few comments, though: - `repo.Commits` will enumerate commits that are reachable from the HEAD. If you want to log the commits from a different branch, use `repo.Commit.QueryBy()` - Prefer `DateTimeOffset` over `DateTime`. Far less headeaches when dealing with committers from different timezones ;-) – nulltoken Sep 01 '13 at 18:07
  • Should I use `var filter = new CommitFilter { Since = repo.Branches };` or `var filter = new CommitFilter { Since = repo.Branches };`? Both seem to work with my sample data set but perhaps there is a difference that I do not see at the moment. – Ryan Taylor Sep 01 '13 at 20:51
  • `var filter = new CommitFilter { Since = repo.Branches }` will target all the commits from ALL the branches. `var filter = new CommitFilter { Since = new [] { repo.Branch["master"], repo.Branch["other"] } };` will target branches `master` and `other`. – nulltoken Sep 02 '13 at 06:20