Skip to content

Commit

Permalink
moved extensions into IBranchCollection & ICommitCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
arturcic committed Jan 19, 2021
1 parent e81093c commit c0c7383
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 35 deletions.
6 changes: 3 additions & 3 deletions src/GitVersion.LibGit2Sharp/Git/BranchCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public virtual IBranch this[string friendlyName]
}
}

public void UpdateTrackedBranch(IBranch branch, string remoteTrackingReferenceName)
{
public IEnumerable<IBranch> ExcludeBranches(IEnumerable<IBranch> branchesToExclude) =>
this.Where(b => branchesToExclude.All(bte => !b.IsSameBranch(bte)));
public void UpdateTrackedBranch(IBranch branch, string remoteTrackingReferenceName) =>
innerCollection.Update((Branch)branch, b => b.TrackedBranch = remoteTrackingReferenceName);
}
}
}
2 changes: 2 additions & 0 deletions src/GitVersion.LibGit2Sharp/Git/CommitCollection.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -21,6 +22,7 @@ public virtual IEnumerator<ICommit> GetEnumerator()

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

public IEnumerable<ICommit> GetCommitsPriorTo(DateTimeOffset olderThan) => this.SkipWhile(c => c.CommitterWhen > olderThan);
public virtual ICommitCollection QueryBy(CommitFilter commitFilter)
{
static object GetReacheableFrom(object item)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ private BranchConfig InheritBranchConfiguration(IBranch targetBranch, BranchConf
}

excludedInheritBranches ??= repositoryMetadataProvider.GetExcludedInheritBranches(configuration).ToList();

excludedBranches = excludedBranches.Where(b => excludedInheritBranches.All(bte => !b.IsSameBranch(bte))).ToArray();
// Add new excluded branches.
foreach (var excludedBranch in excludedBranches.ExcludingBranches(excludedInheritBranches))
foreach (var excludedBranch in excludedBranches)
{
excludedInheritBranches.Add(excludedBranch);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace GitVersion
public interface IBranchCollection : IEnumerable<IBranch>
{
IBranch this[string friendlyName] { get; }
IEnumerable<IBranch> ExcludeBranches(IEnumerable<IBranch> branchesToExclude);
void UpdateTrackedBranch(IBranch branch, string remoteTrackingReferenceName);
}
}
2 changes: 2 additions & 0 deletions src/GitVersionCore/Core/Abstractions/Git/ICommitCollection.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;

namespace GitVersion
{
public interface ICommitCollection : IEnumerable<ICommit>
{
IEnumerable<ICommit> GetCommitsPriorTo(DateTimeOffset olderThan);
ICommitCollection QueryBy(CommitFilter commitFilter);
}
}
2 changes: 1 addition & 1 deletion src/GitVersionCore/Core/RepositoryMetadataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public IEnumerable<IBranch> GetReleaseBranches(IEnumerable<KeyValuePair<string,

public IEnumerable<IBranch> ExcludingBranches(IEnumerable<IBranch> branchesToExclude)
{
return repository.Branches.ExcludingBranches(branchesToExclude);
return repository.Branches.ExcludeBranches(branchesToExclude);
}

// TODO Should we cache this?
Expand Down
33 changes: 5 additions & 28 deletions src/GitVersionCore/Extensions/GitExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using GitVersion.Helpers;

namespace GitVersion.Extensions
{
public static class GitExtensions
{
/// <summary>
/// Exclude the given branches (by value equality according to friendly name).
/// </summary>
public static IEnumerable<BranchCommit> ExcludingBranches(this IEnumerable<BranchCommit> branches, IEnumerable<IBranch> branchesToExclude)
{
return branches.Where(b => branchesToExclude.All(bte => !b.Branch.IsSameBranch(bte)));
}

/// <summary>
/// Exclude the given branches (by value equality according to friendly name).
/// </summary>
public static IEnumerable<IBranch> ExcludingBranches(this IEnumerable<IBranch> branches, IEnumerable<IBranch> branchesToExclude)
{
return branches.Where(b => branchesToExclude.All(bte => !b.IsSameBranch(bte)));
}
public static IEnumerable<ICommit> CommitsPriorToThan(this IBranch branch, DateTimeOffset olderThan)
{
return branch.Commits.SkipWhile(c => c.CommitterWhen > olderThan);
}

public static void DumpGraph(string workingDirectory, Action<string> writer = null, int? maxCommits = null)
{
var output = new StringBuilder();
Expand Down Expand Up @@ -63,20 +41,19 @@ public static void DumpGraph(string workingDirectory, Action<string> writer = nu
}
}

public static bool IsBranch(this string branchName, string branchNameToCompareAgainst)
public static bool IsBranch(this string branchName, string branchNameToCompare)
{
// "develop" == "develop"
if (string.Equals(branchName, branchNameToCompareAgainst, StringComparison.OrdinalIgnoreCase))
return true;

// "refs/head/develop" == "develop"
return branchName.EndsWith($"/{branchNameToCompareAgainst}", StringComparison.OrdinalIgnoreCase);
return string.Equals(branchName, branchNameToCompare, StringComparison.OrdinalIgnoreCase)
|| branchName.EndsWith($"/{branchNameToCompare}", StringComparison.OrdinalIgnoreCase);

}

public static string CreateGitLogArgs(int? maxCommits)
{
return @"log --graph --format=""%h %cr %d"" --decorate --date=relative --all --remotes=*" + (maxCommits != null ? $" -n {maxCommits}" : null);
var commits = maxCommits != null ? $" -n {maxCommits}" : null;
return $@"log --graph --format=""%h %cr %d"" --decorate --date=relative --all --remotes=*{commits}";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ public MergeMessageVersionStrategy(ILog log, Lazy<GitVersionContext> versionCont

public override IEnumerable<BaseVersion> GetVersions()
{
var commitsPriorToThan = Context.CurrentBranch
.CommitsPriorToThan(Context.CurrentCommit.CommitterWhen.Value);
var commitsPriorToThan = Context.CurrentBranch.Commits.GetCommitsPriorTo(Context.CurrentCommit.CommitterWhen.Value);
var baseVersions = commitsPriorToThan
.SelectMany(c =>
{
Expand Down

0 comments on commit c0c7383

Please sign in to comment.