Skip to content

Commit

Permalink
git plugin updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Puchaczov committed Nov 24, 2024
1 parent 09373fa commit c37d87b
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 76 deletions.
16 changes: 12 additions & 4 deletions Musoq.DataSources.Git.Tests/GitToSqlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ cross apply b.GetBranchSpecificCommits(r.Self, b.Self, false) c
var vm = CreateAndRunVirtualMachine(query);
var result = vm.Run();

Assert.IsTrue(result.Count == 2);
Assert.AreEqual(2, result.Count);

var row = result[0];

Expand Down Expand Up @@ -472,14 +472,14 @@ with BranchInfo as (
c.CommittedWhen
from #git.repository('{RepositoryPath}') r
cross apply r.SearchForBranches('feature/branch_2') b
cross apply b.GetBranchSpecificCommits(r.Self, b.Self) c
cross apply b.GetBranchSpecificCommits(r.Self, b.Self, false) c
)
select * from BranchInfo;".Replace("{RepositoryPath}", unpackedRepositoryPath);

var vm = CreateAndRunVirtualMachine(query);
var result = vm.Run();

Assert.IsTrue(result.Count == 2);
Assert.AreEqual(3, result.Count);

var row = result[0];

Expand All @@ -496,6 +496,14 @@ cross apply b.GetBranchSpecificCommits(r.Self, b.Self) c
Assert.IsTrue((string) row[2] == "anonymous");
Assert.IsTrue((string) row[3] == "[email protected]");
Assert.IsTrue((DateTimeOffset) row[4] == new DateTimeOffset(2024, 11, 08, 19, 56, 17, TimeSpan.FromHours(1)));

row = result[2];

Assert.IsTrue((string) row[0] == "655595cfb4bdfc4e42b9bb80d48212c2dca95086");
Assert.IsTrue((string) row[1] == "finished implementation for branch_1\n");
Assert.IsTrue((string) row[2] == "anonymous");
Assert.IsTrue((string) row[3] == "[email protected]");
Assert.IsTrue((DateTimeOffset) row[4] == new DateTimeOffset(2024, 11, 08, 19, 54, 08, TimeSpan.FromHours(1)));
}

[TestMethod]
Expand All @@ -520,7 +528,7 @@ cross apply b.GetBranchSpecificCommits(r.Self, b.Self, false) c
var vm = CreateAndRunVirtualMachine(query);
var result = vm.Run();

Assert.IsTrue(result.Count == 3);
Assert.AreEqual(3, result.Count);

var row = result[0];

Expand Down
84 changes: 45 additions & 39 deletions Musoq.DataSources.Git/Entities/BranchEntity.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using LibGit2Sharp;
Expand All @@ -11,7 +12,7 @@ namespace Musoq.DataSources.Git.Entities;
public class BranchEntity
{
private readonly Repository _libGitRepository;

internal readonly Branch LibGitBranch;

/// <summary>
Expand Down Expand Up @@ -69,7 +70,8 @@ public BranchEntity(Branch branch, Repository repository)
/// Gets the collection of commit entities in the branch.
/// </summary>
[BindablePropertyAsTable]
public IEnumerable<CommitEntity> Commits => LibGitBranch.Commits.Select(f => new CommitEntity(f, _libGitRepository));
public IEnumerable<CommitEntity> Commits =>
LibGitBranch.Commits.Select(f => new CommitEntity(f, _libGitRepository));

/// <summary>
/// Gets the canonical name of the upstream branch.
Expand All @@ -90,53 +92,57 @@ public BranchEntity? ParentBranch
{
var branch = LibGitBranch;
var libGitBranch = _libGitRepository.Branches[branch.FriendlyName];

if (libGitBranch == null)
return null;

if (libGitBranch.TrackedBranch != null)
return new BranchEntity(libGitBranch.TrackedBranch, _libGitRepository);

var branchTip = libGitBranch.Tip;
var candidates = new Dictionary<Branch, int>();

foreach (var possibleParent in _libGitRepository.Branches.Where(b => b.FriendlyName != branch.FriendlyName && !b.IsRemote))
try
{
try
{
var mergeBase = _libGitRepository.ObjectDatabase.FindMergeBase(branchTip, possibleParent.Tip);
if (mergeBase == null) continue;

var filter = new CommitFilter
var possibleParents = _libGitRepository.Branches
.Where(b => !b.IsRemote &&
b.FriendlyName != branch.FriendlyName &&
!b.FriendlyName.StartsWith("origin/"))
.ToList();

var branchesWithMergeBases = possibleParents
.Select(parentBranch => new
{
Branch = parentBranch,
MergeBase = _libGitRepository.ObjectDatabase.FindMergeBase(
libGitBranch.Tip,
parentBranch.Tip)
})
.Where(x => x.MergeBase != null)
.ToList();

var orderedBranches = branchesWithMergeBases
.Where(x => x.MergeBase.Sha != branch.Tip.Sha)
.Select(x => new
{
IncludeReachableFrom = branchTip,
ExcludeReachableFrom = possibleParent.Tip
};

var distance = _libGitRepository.Commits.QueryBy(filter).Count();

if (distance == 0)
continue;

candidates[possibleParent] = distance;
}
catch
{
// Skip if we can't find merge base
}
x.Branch,
x.MergeBase,
CommitCount = _libGitRepository.Commits.QueryBy(new CommitFilter
{
IncludeReachableFrom = branch.Tip,
ExcludeReachableFrom = x.MergeBase
}).Count()
})
.OrderBy(x => x.CommitCount);

var parentBranch = orderedBranches.FirstOrDefault()?.Branch;

return parentBranch != null
? new BranchEntity(parentBranch, _libGitRepository)
: null;
}

if (candidates.Count != 0)
catch
{
var closestCandidate = candidates.OrderBy(c => c.Value).First();
return new BranchEntity(closestCandidate.Key, _libGitRepository);
var defaultBranch = _libGitRepository.Branches["main"] ?? _libGitRepository.Branches["master"];
return defaultBranch != null ? new BranchEntity(defaultBranch, _libGitRepository) : null;
}

var defaultBranch = _libGitRepository.Branches["main"] ?? _libGitRepository.Branches["master"];
return defaultBranch != null ? new BranchEntity(defaultBranch, _libGitRepository) : null;
}
}

/// <summary>
/// Gets the branch entity itself.
/// </summary>
Expand Down
4 changes: 4 additions & 0 deletions Musoq.DataSources.Git/Entities/DifferenceEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public string? OldContent
return null;

var blob = repository.Lookup<Blob>(changes.OldOid);

if (blob == null)
return null;

return blob.GetContentText();
}
}
Expand Down
4 changes: 3 additions & 1 deletion Musoq.DataSources.Git/GitLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,9 @@ public IEnumerable<CommitEntity> GetBranchSpecificCommits(RepositoryEntity repos
var filter = new CommitFilter
{
IncludeReachableFrom = branch.LibGitBranch.Tip,
ExcludeReachableFrom = excludeMergeBase ? mergeBase.MergeBaseCommit.LibGitCommit : $"{mergeBase.MergeBaseCommit.LibGitCommit?.Sha}^",
ExcludeReachableFrom = excludeMergeBase
? mergeBase.MergeBaseCommit.LibGitCommit
: mergeBase.MergeBaseCommit.LibGitCommit?.Parents.FirstOrDefault(),
SortBy = CommitSortStrategies.Topological | CommitSortStrategies.Time
};

Expand Down
1 change: 1 addition & 0 deletions Musoq.DataSources.Git/GitSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ public class GitSchema : SchemaBase
/// <column name="LinesAdded" type="int">Lines added</column>
/// <column name="LinesDeleted" type="int">Lines deleted</column>
/// <column name="Content" type="string">Gets the full patch file of this diff</column>
/// <column name="Changes" type="PatchEntryChangesEntity[]">Gets the changes in this patch</column>
/// </columns>
/// </additional-table>
/// <additional-table>
Expand Down
2 changes: 1 addition & 1 deletion Musoq.DataSources.Git/Musoq.DataSources.Git.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.9.12</Version>
<Version>0.9.13</Version>
<Authors>Jakub Puchała</Authors>
<Product>Musoq</Product>
<PackageProjectUrl>https://github.com/Puchaczov/Musoq</PackageProjectUrl>
Expand Down
66 changes: 35 additions & 31 deletions Musoq.DataSources.OsAndGitTests/OsAndGitToSqlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,39 +36,41 @@ dir.Parent.Name as Name
""";

query = query.Replace("{RepositoriesDirectory}", new FileInfo(firstRepository).Directory!.Parent!.FullName);

{
var vm = CreateAndRunVirtualMachine(query);

var vm = CreateAndRunVirtualMachine(query);

var table = vm.Run();
var table = vm.Run();

Assert.AreEqual(9, table.Count);
Assert.AreEqual(9, table.Count);

Assert.AreEqual("Repository1", table[0][0]);
Assert.AreEqual("789f584ce162424f61b33e020e2138aad47e60ba", table[0][1]);
Assert.AreEqual("Repository1", table[0][0]);
Assert.AreEqual("789f584ce162424f61b33e020e2138aad47e60ba", table[0][1]);

Assert.AreEqual("Repository5", table[1][0]);
Assert.AreEqual("789f584ce162424f61b33e020e2138aad47e60ba", table[1][1]);
Assert.AreEqual("Repository5", table[1][0]);
Assert.AreEqual("789f584ce162424f61b33e020e2138aad47e60ba", table[1][1]);

Assert.AreEqual("Repository5", table[2][0]);
Assert.AreEqual("595b3f0f51071f84909861e5abc15225a4ef4555", table[2][1]);
Assert.AreEqual("Repository5", table[2][0]);
Assert.AreEqual("595b3f0f51071f84909861e5abc15225a4ef4555", table[2][1]);

Assert.AreEqual("Repository5", table[3][0]);
Assert.AreEqual("02c2e53d8712210a4254fc9bd6ee5548a0f1d211", table[3][1]);
Assert.AreEqual("Repository5", table[3][0]);
Assert.AreEqual("02c2e53d8712210a4254fc9bd6ee5548a0f1d211", table[3][1]);

Assert.AreEqual("Repository5", table[4][0]);
Assert.AreEqual("3250d89501e0569115a5cda34807e15ba7de0aa6", table[4][1]);
Assert.AreEqual("Repository5", table[4][0]);
Assert.AreEqual("3250d89501e0569115a5cda34807e15ba7de0aa6", table[4][1]);

Assert.AreEqual("Repository5", table[5][0]);
Assert.AreEqual("bf8542548c686f98d3c562d2fc78259640d07cbb", table[5][1]);
Assert.AreEqual("Repository5", table[5][0]);
Assert.AreEqual("bf8542548c686f98d3c562d2fc78259640d07cbb", table[5][1]);

Assert.AreEqual("Repository5", table[6][0]);
Assert.AreEqual("655595cfb4bdfc4e42b9bb80d48212c2dca95086", table[6][1]);
Assert.AreEqual("Repository5", table[6][0]);
Assert.AreEqual("655595cfb4bdfc4e42b9bb80d48212c2dca95086", table[6][1]);

Assert.AreEqual("Repository5", table[7][0]);
Assert.AreEqual("fb24727b684a511e7f93df2910e4b280f6b9072f", table[7][1]);
Assert.AreEqual("Repository5", table[7][0]);
Assert.AreEqual("fb24727b684a511e7f93df2910e4b280f6b9072f", table[7][1]);

Assert.AreEqual("Repository5", table[8][0]);
Assert.AreEqual("389642ba15392c4540e82628bdff9c99dc6f7923", table[8][1]);
Assert.AreEqual("Repository5", table[8][0]);
Assert.AreEqual("389642ba15392c4540e82628bdff9c99dc6f7923", table[8][1]);
}
}

[TestMethod]
Expand All @@ -95,18 +97,20 @@ order by p.Name
""";

query = query.Replace("{RepositoriesDirectory}", new FileInfo(firstRepository).Directory!.Parent!.FullName);

{
var vm = CreateAndRunVirtualMachine(query);

var vm = CreateAndRunVirtualMachine(query);

var table = vm.Run();
var table = vm.Run();

Assert.AreEqual(2, table.Count);
Assert.AreEqual(2, table.Count);

Assert.AreEqual("Repository1", table[0][0]);
Assert.AreEqual(1, table[0][1]);
Assert.AreEqual("Repository1", table[0][0]);
Assert.AreEqual(1, table[0][1]);

Assert.AreEqual("Repository5", table[1][0]);
Assert.AreEqual(8, table[1][1]);
Assert.AreEqual("Repository5", table[1][0]);
Assert.AreEqual(8, table[1][1]);
}
}

static OsAndGitToSqlTests()
Expand Down Expand Up @@ -180,7 +184,7 @@ public UnpackedRepository(string path)
IsCounter.AddOrUpdate(path, 1, (_, value) => value + 1);
}

public string Path { get; }
private string Path { get; }

public void Dispose()
{
Expand Down

0 comments on commit c37d87b

Please sign in to comment.