Skip to content

Commit 542d1ce

Browse files
committed
moved RepositoryExtensions to GitExtensions cleanup
1 parent 1f963ea commit 542d1ce

File tree

13 files changed

+316
-331
lines changed

13 files changed

+316
-331
lines changed

src/GitVersionCore.Tests/Extensions/GitToolsTestingExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static Branch FindBranch(this IGitRepository repository, string branchNam
2727

2828
public static void DumpGraph(this IGitRepository repository, Action<string> writer = null, int? maxCommits = null)
2929
{
30-
LibGitExtensions.DumpGraph(repository.Info.Path, writer, maxCommits);
30+
GitExtensions.DumpGraph(repository.Info.Path, writer, maxCommits);
3131
}
3232

3333
public static VersionVariables GetVersion(this RepositoryFixtureBase fixture, Config configuration = null, IRepository repository = null, string commitId = null, bool onlyTrackedBranches = true, string branch = null)

src/GitVersionCore.Tests/Mocks/MockRepository.cs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using LibGit2Sharp;
44
using Branch = GitVersion.Branch;
55
using BranchCollection = GitVersion.BranchCollection;
6+
using Commit = GitVersion.Commit;
67
using ReferenceCollection = GitVersion.ReferenceCollection;
78
using TagCollection = GitVersion.TagCollection;
89

@@ -11,18 +12,13 @@ namespace GitVersionCore.Tests.Mocks
1112
public class MockRepository : IGitRepository
1213
{
1314
private CommitCollection commits;
15+
public IGitRepositoryCommands Commands { get; }
1416

1517
public MockRepository()
1618
{
1719
Tags = new MockTagCollection();
1820
Refs = new MockReferenceCollection();
1921
}
20-
21-
public void Dispose()
22-
{
23-
throw new NotImplementedException();
24-
}
25-
2622
public Branch Head { get; set; }
2723
public ReferenceCollection Refs { get; set; }
2824

@@ -35,14 +31,11 @@ public CommitCollection Commits
3531
public BranchCollection Branches { get; set; }
3632
public TagCollection Tags { get; set; }
3733
public RepositoryInformation Info { get; set; }
38-
public Diff Diff { get; set; }
39-
public ObjectDatabase ObjectDatabase { get; set; }
4034

4135
public Network Network { get; set; }
42-
public RepositoryStatus RetrieveStatus()
43-
{
44-
throw new NotImplementedException();
45-
}
46-
public IGitRepositoryCommands Commands { get; }
36+
public int GetNumberOfUncommittedChanges() => 0;
37+
public Commit FindMergeBase(Commit commit, Commit otherCommit) => throw new NotImplementedException();
38+
public string ShortenObjectId(Commit commit) => throw new NotImplementedException();
39+
public void Dispose() => throw new NotImplementedException();
4740
}
4841
}

src/GitVersionCore/Core/Abstractions/IGitRepository.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ namespace GitVersion
66
public interface IGitRepository : IDisposable
77
{
88
IGitRepositoryCommands Commands { get; }
9-
ObjectDatabase ObjectDatabase { get; }
109
Branch Head { get; }
1110
CommitCollection Commits { get; }
1211
BranchCollection Branches { get; }
1312
TagCollection Tags { get; }
1413
ReferenceCollection Refs { get; }
15-
Diff Diff { get; }
1614
RepositoryInformation Info { get; }
1715
Network Network { get; }
18-
RepositoryStatus RetrieveStatus();
16+
int GetNumberOfUncommittedChanges();
17+
Commit FindMergeBase(Commit commit, Commit otherCommit);
18+
string ShortenObjectId(Commit commit);
1919
}
2020
}

src/GitVersionCore/Core/GitPreparer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ private void NormalizeGitDirectory(string gitDirectory, bool noFetch, string cur
314314
315315
To disable this error set an environmental variable called IGNORE_NORMALISATION_GIT_HEAD_MOVE to 1
316316
317-
Please run `git {LibGitExtensions.CreateGitLogArgs(100)}` and submit it along with your build log (with personal info removed) in a new issue at https://github.com/GitTools/GitVersion");
317+
Please run `git {GitExtensions.CreateGitLogArgs(100)}` and submit it along with your build log (with personal info removed) in a new issue at https://github.com/GitTools/GitVersion");
318318
}
319319
}
320320
}

src/GitVersionCore/Core/GitRepository.cs

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using LibGit2Sharp;
34
using Microsoft.Extensions.Options;
45

@@ -32,7 +33,44 @@ public void Dispose()
3233
if (repositoryLazy.IsValueCreated) repositoryInstance.Dispose();
3334
}
3435

35-
public RepositoryStatus RetrieveStatus() => repositoryInstance.RetrieveStatus();
36+
public int GetNumberOfUncommittedChanges()
37+
{
38+
// check if we have a branch tip at all to behave properly with empty repos
39+
// => return that we have actually uncomitted changes because we are apparently
40+
// running GitVersion on something which lives inside this brand new repo _/\Ö/\_
41+
if (repositoryInstance.Head?.Tip == null || repositoryInstance.Diff == null)
42+
{
43+
// this is a somewhat cumbersome way of figuring out the number of changes in the repo
44+
// which is more expensive than to use the Diff as it gathers more info, but
45+
// we can't use the other method when we are dealing with a new/empty repo
46+
try
47+
{
48+
var status = repositoryInstance.RetrieveStatus();
49+
return status.Untracked.Count() + status.Staged.Count();
50+
51+
}
52+
catch (Exception)
53+
{
54+
return Int32.MaxValue; // this should be somewhat puzzling to see,
55+
// so we may have reached our goal to show that
56+
// that repo is really "Dirty"...
57+
}
58+
}
59+
60+
// gets all changes of the last commit vs Staging area and WT
61+
var changes = repositoryInstance.Diff.Compare<TreeChanges>(repositoryInstance.Head.Tip.Tree,
62+
DiffTargets.Index | DiffTargets.WorkingDirectory);
63+
64+
return changes.Count;
65+
}
66+
public Commit FindMergeBase(Commit commit, Commit otherCommit)
67+
{
68+
return (Commit)repositoryInstance.ObjectDatabase.FindMergeBase(commit, otherCommit);
69+
}
70+
public string ShortenObjectId(Commit commit)
71+
{
72+
return repositoryInstance.ObjectDatabase.ShortenObjectId(commit);
73+
}
3674

3775
public Branch Head => (Branch)repositoryInstance.Head;
3876

@@ -46,10 +84,6 @@ public void Dispose()
4684

4785
public RepositoryInformation Info => repositoryInstance.Info;
4886

49-
public Diff Diff => repositoryInstance.Diff;
50-
51-
public ObjectDatabase ObjectDatabase => repositoryInstance.ObjectDatabase;
52-
5387
public Network Network => repositoryInstance.Network;
5488
}
5589
}

src/GitVersionCore/Core/RepositoryExtensions.cs

Lines changed: 0 additions & 116 deletions
This file was deleted.

src/GitVersionCore/Core/RepositoryMetadataProvider.cs

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using GitVersion.Logging;
99
using GitVersion.Model.Configuration;
1010
using GitVersion.VersionCalculation;
11-
using LibGit2Sharp;
1211

1312
namespace GitVersion
1413
{
@@ -51,7 +50,7 @@ public Commit FindMergeBase(Branch branch, Branch otherBranch)
5150
commitToFindCommonBase = otherBranch.Tip.Parents.First();
5251
}
5352

54-
var findMergeBase = (Commit)repository.ObjectDatabase.FindMergeBase(commit, commitToFindCommonBase);
53+
var findMergeBase = repository.FindMergeBase(commit, commitToFindCommonBase);
5554
if (findMergeBase != null)
5655
{
5756
log.Info($"Found merge base of {findMergeBase.Sha}");
@@ -67,7 +66,7 @@ public Commit FindMergeBase(Branch branch, Branch otherBranch)
6766
// TODO Fix the logging up in this section
6867
var second = forwardMerge.Parents.First();
6968
log.Debug("Second " + second.Sha);
70-
var mergeBase = (Commit)repository.ObjectDatabase.FindMergeBase(commit, second);
69+
var mergeBase = repository.FindMergeBase(commit, second);
7170
if (mergeBase == null)
7271
{
7372
log.Warning("Could not find mergbase for " + commit);
@@ -98,7 +97,7 @@ public Commit FindMergeBase(Branch branch, Branch otherBranch)
9897

9998
public Commit FindMergeBase(Commit commit, Commit mainlineTip)
10099
{
101-
return (Commit)repository.ObjectDatabase.FindMergeBase(commit, mainlineTip);
100+
return repository.FindMergeBase(commit, mainlineTip);
102101
}
103102

104103
public Commit GetCurrentCommit(Branch currentBranch, string commitId)
@@ -325,7 +324,8 @@ public SemanticVersion GetCurrentCommitTaggedVersion(Commit commit, EffectiveCon
325324
return repository.Tags
326325
.SelectMany(t =>
327326
{
328-
if (t.PeeledTarget() is LibGit2Sharp.Commit targetCommit && targetCommit == commit && SemanticVersion.TryParse(t.FriendlyName, config.GitTagPrefix, out var version))
327+
var targetCommit = t.PeeledTargetCommit();
328+
if (targetCommit != null && targetCommit == commit && SemanticVersion.TryParse(t.FriendlyName, config.GitTagPrefix, out var version))
329329
return new[]
330330
{
331331
version
@@ -366,10 +366,12 @@ public IEnumerable<Tuple<Tag, SemanticVersion>> GetValidVersionTags(string tagPr
366366

367367
foreach (var tag in repository.Tags)
368368
{
369-
if (!(tag.PeeledTarget() is LibGit2Sharp.Commit commit))
369+
var commit = tag.PeeledTargetCommit();
370+
371+
if (commit == null)
370372
continue;
371373

372-
if (olderThan.HasValue && ((Commit)commit).When() > olderThan.Value)
374+
if (olderThan.HasValue && commit.When() > olderThan.Value)
373375
continue;
374376

375377
if (SemanticVersion.TryParse(tag.FriendlyName, tagPrefixRegex, out var semver))
@@ -388,7 +390,7 @@ public CommitCollection GetCommitLog(Commit baseVersionSource, Commit currentCom
388390

389391
public string ShortenObjectId(Commit commit)
390392
{
391-
return repository.ObjectDatabase.ShortenObjectId(commit);
393+
return repository.ShortenObjectId(commit);
392394
}
393395

394396
public VersionField? DetermineIncrementedField(BaseVersion baseVersion, GitVersionContext context)
@@ -440,36 +442,7 @@ private IEnumerable<BranchCommit> GetMergeCommitsForBranch(Branch branch, Config
440442
return branchMergeBases;
441443
}
442444

443-
public int GetNumberOfUncommittedChanges()
444-
{
445-
// check if we have a branch tip at all to behave properly with empty repos
446-
// => return that we have actually uncomitted changes because we are apparently
447-
// running GitVersion on something which lives inside this brand new repo _/\Ö/\_
448-
if (repository.Head?.Tip == null || repository.Diff == null)
449-
{
450-
// this is a somewhat cumbersome way of figuring out the number of changes in the repo
451-
// which is more expensive than to use the Diff as it gathers more info, but
452-
// we can't use the other method when we are dealing with a new/empty repo
453-
try
454-
{
455-
var status = repository.RetrieveStatus();
456-
return status.Untracked.Count() + status.Staged.Count();
457-
458-
}
459-
catch (Exception)
460-
{
461-
return Int32.MaxValue; // this should be somewhat puzzling to see,
462-
// so we may have reached our goal to show that
463-
// that repo is really "Dirty"...
464-
}
465-
}
466-
467-
// gets all changes of the last commit vs Staging area and WT
468-
var changes = repository.Diff.Compare<TreeChanges>(repository.Head.Tip.Tree,
469-
DiffTargets.Index | DiffTargets.WorkingDirectory);
470-
471-
return changes.Count;
472-
}
445+
public int GetNumberOfUncommittedChanges() => repository.GetNumberOfUncommittedChanges();
473446

474447
private class MergeBaseData
475448
{

0 commit comments

Comments
 (0)