Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Hönel committed Nov 21, 2024
2 parents c749b9f + 6dcd69c commit 228b35c
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 14 deletions.
2 changes: 1 addition & 1 deletion GitDensity/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@

[assembly: InternalsVisibleTo("GitDensityTests")]

[assembly: AssemblyInformationalVersion("v2024.11+u2")]
[assembly: AssemblyInformationalVersion("v2024.11+u3")]
2 changes: 1 addition & 1 deletion GitDensityTests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

[assembly: AssemblyInformationalVersion("v2024.11+u2")]
[assembly: AssemblyInformationalVersion("v2024.11+u3")]
2 changes: 1 addition & 1 deletion GitHours/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@
[assembly: InternalsVisibleTo("GitDensity")]
[assembly: InternalsVisibleTo("GitHoursTests")]

[assembly: AssemblyInformationalVersion("v2024.11+u2")]
[assembly: AssemblyInformationalVersion("v2024.11+u3")]
2 changes: 1 addition & 1 deletion GitHoursTests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

[assembly: AssemblyInformationalVersion("v2024.11+u2")]
[assembly: AssemblyInformationalVersion("v2024.11+u3")]
2 changes: 1 addition & 1 deletion GitMetrics/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

[assembly: AssemblyInformationalVersion("v2024.11+u2")]
[assembly: AssemblyInformationalVersion("v2024.11+u3")]
20 changes: 18 additions & 2 deletions GitTools/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using GitTools.Analysis.SimpleAnalyzer;
using GitTools.Prompting;
using GitTools.SourceExport;
using LibGit2Sharp;
using LINQtoCSV;
using Microsoft.Extensions.Logging;
using MySql.Data.MySqlClient;
Expand All @@ -41,6 +42,8 @@
using Util.Extensions;
using Util.Logging;
using CompareOptions = LibGit2Sharp.CompareOptions;
using Configuration = Util.Configuration;
using LogLevel = Microsoft.Extensions.Logging.LogLevel;


namespace GitTools
Expand Down Expand Up @@ -276,7 +279,7 @@ static void Main(string[] args)
logger.LogWarning($"Context-Lines has been set to the maximum value of ({Int32.MaxValue}).");
}

var commits = span.FilteredCommits.ToList();
var commits = span.FilteredCommits.ToHashSet();
// For each of the span's commits, we will make pairs of commit and parent.
// This means, we will create one pair for each parent. Then, these pairs
// are processed according to the policy and returned.
Expand All @@ -285,6 +288,16 @@ static void Main(string[] args)
ContextLines = options.CmdExport_ContextLines.Value
};

// Let's check if parent generations should be exported.
if (options.CmdExport_ParentGens > 0u)
{
// Make a new set containing the original commits and their parents
logger.LogWarning($"Including {options.CmdExport_ParentGens} parent generations. Original number of commits is {commits.Count}.");
commits = new HashSet<Commit>(
commits.Concat(commits.SelectMany(commit => commit.ParentGenerations(numGenerations: options.CmdExport_ParentGens))));
logger.LogWarning($"Number of commits increased to {commits.Count} by including parent generations.");
}

var pairs = commits.SelectMany(commit =>
{
var parents = commit.Parents.ToList();
Expand All @@ -295,7 +308,7 @@ static void Main(string[] args)
return parents.Select(parent => new ExportCommitPair(repo: repo, child: commit, parent: parent, compareOptions: compOptions));
}).ToList();

logger.LogInformation($"Found {commits.Count} Commits and {pairs.Count} pairs.");
logger.LogInformation($"Found {commits.Count()} Commits and {pairs.Count} pairs.");
logger.LogInformation($"Processing all pairs {(options.ExecutionPolicy == ExecutionPolicy.Linear ? "sequentially" : "in parallel")}.");


Expand Down Expand Up @@ -604,6 +617,9 @@ internal class CommandLineOptions

[Option("full-code", Required = false, HelpText = "Optional Boolean. Option for the command --cmd-export-source. If present, overrides the command --context-lines by setting it to " + nameof(Int32) + "." + nameof(Int32.MaxValue) + " if true. If --context-lines was not specified, this option *defaults to true* if --cmd-export-source is either Files or Commits. Exporting full code allows to fully reconstruct the original source code.")]
public Boolean? CmdExport_FullCode { get; set; }

[Option("parent-gens", Required = false, DefaultValue = 0u, HelpText = "Optional. Option for the command --cmd-export-source. If greater than zero, will export data from up to n parent generations, for each commit.")]
public UInt32 CmdExport_ParentGens { get; set; }
#endregion

[Option('h', "help", Required = false, DefaultValue = false, HelpText = "Print this help-text and exit.")]
Expand Down
2 changes: 1 addition & 1 deletion GitTools/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@

[assembly: InternalsVisibleTo("GitToolsTests")]

[assembly: AssemblyInformationalVersion("v2024.11+u2")]
[assembly: AssemblyInformationalVersion("v2024.11+u3")]
2 changes: 1 addition & 1 deletion GitToolsTests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

[assembly: AssemblyInformationalVersion("v2024.11+u2")]
[assembly: AssemblyInformationalVersion("v2024.11+u3")]
39 changes: 37 additions & 2 deletions GitToolsTests/SourceExport/Export.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ public class Export
public static DirectoryInfo SolutionDirectory
=> new DirectoryInfo(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Locati‌​on)).Parent.Parent;

public static Tuple<ExportCommitPair, Repository, GitCommitSpan> GetPair(string sha1, int ctxLines = 3) {
public static Tuple<ExportCommitPair, Repository, GitCommitSpan> GetPair(string sha1, int ctxLines = 3)
{
var repo = SolutionDirectory.FullName.OpenRepository();
var span = new GitCommitSpan(repo, sinceDateTimeOrCommitSha: sha1, untilDatetimeOrCommitSha: sha1);

var commit = span.FilteredCommits.Single();
return Tuple.Create(new ExportCommitPair(repo, commit, commit.Parents.Single(), new LibGit2Sharp.CompareOptions() { ContextLines = ctxLines }), repo, span);
return Tuple.Create(new ExportCommitPair(repo, commit, commit.Parents.First(), new LibGit2Sharp.CompareOptions() { ContextLines = ctxLines }), repo, span);
}

/// <summary>
Expand Down Expand Up @@ -189,5 +190,39 @@ public void TestExportCommits_8f05()
tp.Item2.Dispose();
tp.Item3.Dispose();
}

[TestMethod]
public void TestGenerations_9b70()
{
var tp = GetPair("9b70");
var commit = tp.Item1.Child;

Assert.IsTrue(commit.ParentGenerations(numGenerations: 0).Count() == 0);

// In its first generation, it has two parents: ef1db10 and 8f05cad
var s = commit.ParentGenerations(numGenerations: 1);
Assert.AreEqual(2, s.Count);
Assert.AreEqual(1, s.Where(c => c.ShaShort() == "ef1db10").Count());
Assert.AreEqual(1, s.Where(c => c.ShaShort() == "8f05cad").Count());

// In its second generation, ef1db has one parent and 8f05 has one parent
s = commit.ParentGenerations(numGenerations: 2);
Assert.AreEqual(4, s.Count);
Assert.AreEqual(1, s.Where(c => c.ShaShort() == "ef1db10").Count());
Assert.AreEqual(1, s.Where(c => c.ShaShort() == "8f05cad").Count());
Assert.AreEqual(1, s.Where(c => c.ShaShort() == "7710093").Count());
Assert.AreEqual(1, s.Where(c => c.ShaShort() == "90ae132").Count());

// In its third generation, 7710093 has one parent and 90ae has two parents!
// However, one of 90ae's parents is 8f05cad again, so there should be 6 commits in the set!
s = commit.ParentGenerations(numGenerations: 3);
Assert.AreEqual(6, s.Count);
Assert.AreEqual(1, s.Where(c => c.ShaShort() == "ef1db10").Count());
Assert.AreEqual(1, s.Where(c => c.ShaShort() == "8f05cad").Count());
Assert.AreEqual(1, s.Where(c => c.ShaShort() == "7710093").Count());
Assert.AreEqual(1, s.Where(c => c.ShaShort() == "90ae132").Count());
Assert.AreEqual(1, s.Where(c => c.ShaShort() == "9992e0a").Count());
Assert.AreEqual(1, s.Where(c => c.ShaShort() == "4b938a6").Count());
}
}
}
82 changes: 82 additions & 0 deletions Util/Extensions/CommitExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using LibGit2Sharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Util.Extensions
{
/// <summary>
/// A simple equality comparer for commit objects based in their ID
/// and commit message.
/// </summary>
public class CommitEqualityComparer : IEqualityComparer<Commit>
{
/// <summary>
/// True if the reference is the same or both Id and Message are equal.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public bool Equals(Commit x, Commit y)
{
if (ReferenceEquals(x, y)) return true;

return x is Commit && y is Commit && x.Id == y.Id && x.Message == y.Message;
}

/// <summary>
/// Hashcode based on the commit itself, its Id and Message.
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public int GetHashCode(Commit obj)
{
return obj.GetHashCode() ^ obj.Id.GetHashCode() ^ obj.Message.GetHashCode();
}
}


/// <summary>
/// Static class with extensions for commits.
/// </summary>
public static class CommitExtensions
{
/// <summary>
/// Traverses the tree of parent commits and returns up to N generations
/// in a <see cref="HashSet{T}"/>. Note that within this tree, some commits
/// can point to the same parent. Therefore, a set is returned.
/// </summary>
/// <param name="commit"></param>
/// <param name="numGenerations"></param>
/// <returns></returns>
public static ISet<Commit> ParentGenerations(this Commit commit, UInt32 numGenerations)
{
var results = new HashSet<Commit>(comparer: new CommitEqualityComparer());
if (numGenerations == 0)
{
return results;
}

// Let's decrease it. Since it's an UInt32, it was > 0 before (no overflow here).
numGenerations--;


foreach (var parent in commit.Parents)
{
results.Add(parent);

if (numGenerations > 0)
{
foreach (var parent_parent in parent.ParentGenerations(numGenerations: numGenerations))
{
results.AddAll(parent.ParentGenerations(numGenerations: numGenerations));
}
}
}

return results;
}
}
}
3 changes: 2 additions & 1 deletion Util/Extensions/DirectoryInfoExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public static Boolean TryDelete(this DirectoryInfo directoryInfo, Boolean recurs
{
directoryInfo.Delete(recursive);
return true;
} catch
}
catch
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion Util/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@
[assembly: AssemblyFileVersion("1.0.0.0")]

[assembly: InternalsVisibleTo("UtilTests")]
[assembly: AssemblyInformationalVersion("v2024.11+u2")]
[assembly: AssemblyInformationalVersion("v2024.11+u3")]
1 change: 1 addition & 0 deletions Util/Util.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@
<Compile Include="Data\StringEnumMapper.cs" />
<Compile Include="Density\CommitPair.cs" />
<Compile Include="ExitCodes.cs" />
<Compile Include="Extensions\CommitExtensions.cs" />
<Compile Include="Extensions\DirectoryInfoExtensions.cs" />
<Compile Include="Extensions\EntityExtensions.cs" />
<Compile Include="Extensions\IEnumerableExtensions.cs" />
Expand Down
2 changes: 1 addition & 1 deletion UtilTests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

[assembly: AssemblyInformationalVersion("v2024.11+u2")]
[assembly: AssemblyInformationalVersion("v2024.11+u3")]

0 comments on commit 228b35c

Please sign in to comment.