Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/GitVersion.LibGit2Sharp/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("GitVersionCore.Tests")]
[assembly: InternalsVisibleTo("GitVersionExe.Tests")]
[assembly: InternalsVisibleTo("GitVersion.MsBuild.Tests")]
41 changes: 41 additions & 0 deletions src/GitVersion.LibGit2Sharp/Git/Branch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace GitVersion
{
internal class Branch : IBranch
{
private readonly LibGit2Sharp.Branch innerBranch;

internal Branch(LibGit2Sharp.Branch branch)
{
innerBranch = branch;
}

protected Branch()
{
}
public static implicit operator LibGit2Sharp.Branch(Branch d) => d?.innerBranch;

public virtual string CanonicalName => innerBranch?.CanonicalName;
public virtual string FriendlyName => innerBranch?.FriendlyName;
public virtual ICommit Tip
{
get
{
var commit = innerBranch?.Tip;
return commit is null ? null : new Commit(commit);
}
}

public virtual ICommitCollection Commits
{
get
{

var commits = innerBranch?.Commits;
return commits is null ? null : new CommitCollection(commits);
}
}

public virtual bool IsRemote => innerBranch != null && innerBranch.IsRemote;
public virtual bool IsTracking => innerBranch != null && innerBranch.IsTracking;
}
}
35 changes: 35 additions & 0 deletions src/GitVersion.LibGit2Sharp/Git/BranchCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace GitVersion
{
internal class BranchCollection : IBranchCollection
{
private readonly LibGit2Sharp.BranchCollection innerCollection;
internal BranchCollection(LibGit2Sharp.BranchCollection collection) => innerCollection = collection;

protected BranchCollection()
{
}

public virtual IEnumerator<IBranch> GetEnumerator()
{
return innerCollection.Select(branch => new Branch(branch)).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public virtual IBranch this[string friendlyName]
{
get
{
var branch = innerCollection?[friendlyName];
return branch is null ? null : new Branch(branch);
}
}

public void UpdateTrackedBranch(IBranch branch, string remoteTrackingReferenceName)
{
innerCollection.Update((Branch)branch, b => b.TrackedBranch = remoteTrackingReferenceName);
}
}
}
55 changes: 55 additions & 0 deletions src/GitVersion.LibGit2Sharp/Git/Commit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using GitVersion.Helpers;

namespace GitVersion
{
internal class Commit : ICommit
{
private static readonly LambdaEqualityHelper<ICommit> equalityHelper =
new LambdaEqualityHelper<ICommit>(x => x.Id);

private readonly LibGit2Sharp.Commit innerObjectId;

internal Commit(LibGit2Sharp.Commit objectId)
{
innerObjectId = objectId;
}

protected Commit()
{
}

public override bool Equals(object obj) => Equals(obj as ICommit);
public bool Equals(ICommit other) => equalityHelper.Equals(this, other);
public override int GetHashCode() => equalityHelper.GetHashCode(this);

public static implicit operator LibGit2Sharp.Commit(Commit d) => d?.innerObjectId;

public virtual IEnumerable<ICommit> Parents
{
get
{
if (innerObjectId == null) yield return null;
else
foreach (var parent in innerObjectId.Parents)
yield return new Commit(parent);
}
}

public virtual string Sha => innerObjectId?.Sha;

public virtual IObjectId Id
{
get
{
var objectId = innerObjectId?.Id;
return objectId is null ? null : new ObjectId(objectId);
}
}

public virtual DateTimeOffset? CommitterWhen => innerObjectId?.Committer.When;
public virtual string Message => innerObjectId?.Message;
}

}
49 changes: 49 additions & 0 deletions src/GitVersion.LibGit2Sharp/Git/CommitCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using LibGit2Sharp;

namespace GitVersion
{
internal class CommitCollection : ICommitCollection
{
private readonly ICommitLog innerCollection;
internal CommitCollection(ICommitLog collection) => innerCollection = collection;

protected CommitCollection()
{
}

public virtual IEnumerator<ICommit> GetEnumerator()
{
return innerCollection.Select(commit => new Commit(commit)).GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

public virtual ICommitCollection QueryBy(CommitFilter commitFilter)
{
static object GetReacheableFrom(object item)
{
return item switch
{
Commit c => (LibGit2Sharp.Commit)c,
Branch b => (LibGit2Sharp.Branch)b,
_ => null
};
}

var includeReachableFrom = GetReacheableFrom(commitFilter.IncludeReachableFrom);
var excludeReachableFrom = GetReacheableFrom(commitFilter.ExcludeReachableFrom);
var filter = new LibGit2Sharp.CommitFilter
{
IncludeReachableFrom = includeReachableFrom,
ExcludeReachableFrom = excludeReachableFrom,
FirstParentOnly = commitFilter.FirstParentOnly,
SortBy = (LibGit2Sharp.CommitSortStrategies)commitFilter.SortBy,
};
var commitLog = ((IQueryableCommitLog)innerCollection).QueryBy(filter);
return new CommitCollection(commitLog);
}
}
}
Loading