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
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace vv.tests;

public class SetupTest
public class RepositoriesHandleTest
{
[Fact]
public void TestGettingRepos()
Expand All @@ -17,7 +17,7 @@ public void TestGettingRepos()
var reposFolderPath = Directory.GetParent(root).FullName;
SetupHandle.WriteSetupToJson(new(reposFolderPath));

var reposNames = SetupHandle.GetRepositoriesNamesFromSetup();
var reposNames = RepositoriesHandle.GetRepositoriesNamesFromSetup();

Assert.Contains(Path.GetFileName(root), reposNames);
}
Expand Down
83 changes: 22 additions & 61 deletions src/vv/CLI/Commands/Git/GitCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using LibGit2Sharp;
using vv.CLI.Rendering;
using vv.CLI.Settings;
using vv.Core;

namespace vv.CLI.Commands;

Expand All @@ -13,40 +14,8 @@ protected override int ExecuteImpl(GitSettings settings)
var repoPath = ResolvePath.ResolveRepoPath(settings);
using var repo = new Repository(repoPath);

var currentBranchLastCommit = repo.Head.Commits.First();
var currentBranchFirstCommit = repo.Head.Commits.Last();

var lastCommit = repo.Commits.First();
var firstCommit = repo.Commits.Last();

var contributors = repo.Commits
.GroupBy(c => c.Author.Email)
.Select(g => new
{
Name = g.First().Author.Name,
Email = g.Key,
CommitCount = g.Count()
})
.OrderByDescending(c => c.CommitCount)
.ToList();

string currentBranchName = repo.Head.FriendlyName;
int currentBranchTotalCommits = repo.Head.Commits.Count();
string currentBranchLastCommitDate = currentBranchLastCommit.Committer.When.ToString("dd.MM.yyyy HH:mm:ss");
string currentBranchLastCommitMessage = currentBranchLastCommit.MessageShort;
string currentBranchFirstCommitDate = currentBranchFirstCommit.Committer.When.ToString("dd.MM.yyyy HH:mm:ss");
string currentBranchFirstCommitMessage = currentBranchFirstCommit.MessageShort;

int totalCommits = repo.Commits.Count();
string lastCommitMessage = lastCommit.MessageShort;
string lastCommitDate = lastCommit.Committer.When.ToString("dd.MM.yyyy HH:mm:ss");
string firstCommitMessage = firstCommit.MessageShort;
string firstCommitDate = firstCommit.Committer.When.ToString("dd.MM.yyyy HH:mm:ss");

int totalBranches = repo.Branches.Count(b => !b.IsRemote);

int totalContributors = contributors.Count;
var topContributors = contributors.Take(MAX_TOP_COUNT);
var repoData = RepositoriesHandle.GetRepoData(repo, MAX_TOP_COUNT);
var currentBranchData = RepositoriesHandle.GetBranchData(repo.Head);

DefaultRendering.Rule("[blue]Repo git stats[/]");

Expand All @@ -57,53 +26,45 @@ protected override int ExecuteImpl(GitSettings settings)

var specifiedBranchTable = DefaultRendering.Table("Content", "Value");

var specifiedBranch = repo.Branches[specifiedBranchGivenName];
var specifiedBranchFirstCommit = specifiedBranch.Commits.Last();
var specifiedBranchLastCommit = specifiedBranch.Commits.First();
var specifiedBranchData = RepositoriesHandle.GetBranchData(repo.Branches[specifiedBranchGivenName]);

string specifiedBranchName = specifiedBranch.FriendlyName;
int specifiedBranchTotalCommits = specifiedBranch.Commits.Count();

string specifiedBranchLastCommitDate = specifiedBranchLastCommit.Committer.When.ToString("dd.MM.yyyy HH:mm:ss"); ;
string specifiedBranchLastCommitMessage = specifiedBranchLastCommit.MessageShort;

string specifiedBranchFirstCommitDate = specifiedBranchFirstCommit.Committer.When.ToString("dd.MM.yyyy HH:mm:ss"); ;
string specifiedBranchFirstCommitMessage = specifiedBranchFirstCommit.MessageShort;

DisplayBranch(specifiedBranchName, specifiedBranchTotalCommits, specifiedBranchFirstCommitMessage,
specifiedBranchFirstCommitDate, specifiedBranchLastCommitMessage, specifiedBranchLastCommitDate, specifiedBranchTable);
DisplayBranch(specifiedBranchData.Name, specifiedBranchData.TotalCommits,
specifiedBranchData.FirstCommitMessage, specifiedBranchData.FirstCommitDate,
specifiedBranchData.LastCommitMessage, specifiedBranchData.LastCommitDate,
specifiedBranchTable);

AnsiConsole.Write(specifiedBranchTable);
}

var statsTable = DefaultRendering.Table("Content", "Value");

DisplayBranch(currentBranchName, currentBranchTotalCommits, currentBranchFirstCommitMessage,
currentBranchFirstCommitDate, currentBranchLastCommitMessage, currentBranchLastCommitDate, statsTable);
DisplayBranch(currentBranchData.Name, currentBranchData.TotalCommits,
currentBranchData.FirstCommitMessage, currentBranchData.FirstCommitDate,
currentBranchData.LastCommitMessage, currentBranchData.LastCommitDate,
statsTable);

// Don't use DisplayBranch since it's global repo stats
statsTable.AddEmptyRow();
statsTable.AddRow("Commits", $"{totalCommits}");
statsTable.AddRow("First commit message", firstCommitMessage);
statsTable.AddRow("First commit date", firstCommitDate);
statsTable.AddRow("Last commit message", lastCommitMessage);
statsTable.AddRow("Last commit date", lastCommitDate);
statsTable.AddRow("Commits", $"{repoData.TotalCommits}");
statsTable.AddRow("First commit date", repoData.FirstCommitDate);
statsTable.AddRow("Last commit date", repoData.LastCommitDate);
statsTable.AddEmptyRow();
statsTable.AddRow("Branches", $"{totalBranches}");
statsTable.AddRow("Branches", $"{repoData.TotalBranches}");
statsTable.AddEmptyRow();
statsTable.AddRow("Contributors", $"{totalContributors}");
statsTable.AddRow("Contributors", $"{repoData.TotalCommits}");

foreach(var contributor in topContributors)
statsTable.AddRow(contributor.Name, $"{contributor.CommitCount}");
foreach(var contributor in repoData.TopContributors)
statsTable.AddRow(contributor.Name, $"{contributor.CommitsCount}");

AnsiConsole.Write(statsTable);
return 0;
}

private void DisplayBranch(string branchName, int branchTotalCommits, string firstCommitMessage,
string firstCommitDate, string lastCommitMessage, string lastCommitDate, Table table,
string displayBranchName = "Current")
string firstCommitDate, string lastCommitMessage, string lastCommitDate, Table table)
{
var displayBranchName = "Current";

table.AddRow($"{displayBranchName} branch", branchName);
table.AddRow($"{displayBranchName} branch commits", $"{branchTotalCommits}");
table.AddRow($"{displayBranchName} branch first commit", firstCommitMessage);
Expand Down
2 changes: 1 addition & 1 deletion src/vv/CLI/Rendering/ResolvePath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static string ResolveRepoPath<T>(T settings) where T : BaseSettings
var repoPath = settings.RepoPath;
if (string.IsNullOrEmpty(repoPath))
{
var reposNames = SetupHandle.GetRepositoriesNamesFromSetup();
var reposNames = RepositoriesHandle.GetRepositoriesNamesFromSetup();

if (reposNames.Count == 0)
throw new UserException(
Expand Down
73 changes: 73 additions & 0 deletions src/vv/Core/RepositoriesHandle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using LibGit2Sharp;

namespace vv.Core;

internal readonly record struct BranchData(
string Name, int TotalCommits, string LastCommitDate, string LastCommitMessage,
string FirstCommitDate, string FirstCommitMessage);

internal readonly record struct RepoData(
IEnumerable<(string Name, string Email, int CommitsCount)> TopContributors, int TotalCommits,
int TotalBranches, int TotalContributors, string LastCommitDate, string FirstCommitDate);

internal static class RepositoriesHandle
{
public static RepoData GetRepoData(Repository repo, int topContributorsCount)
{
var lastCommit = repo.Commits.First();
var firstCommit = repo.Commits.Last();

var contributors = repo.Commits
.GroupBy(c => c.Author.Email)
.Select(g => (
g.First().Author.Name,
Email: g.Key,
CommitsCount: g.Count()
))
.OrderByDescending(c => c.CommitsCount)
.ToList();

var totalCommits = repo.Commits.Count();
string lastCommitDate = lastCommit.Committer.When.ToString("dd.MM.yyyy HH:mm:ss");
string firstCommitDate = firstCommit.Committer.When.ToString("dd.MM.yyyy HH:mm:ss");

var totalBranches = repo.Branches.Count(b => !b.IsRemote);
var totalContributors = contributors.Count;

var topContributors = contributors.Take(topContributorsCount);

return new(topContributors, totalCommits, totalBranches, totalContributors, lastCommitDate, firstCommitDate);
}


public static BranchData GetBranchData(Branch branch)
{
var lastCommit = branch.Commits.First();
var firstCommit = branch.Commits.Last();

var branchName = branch.FriendlyName;
var totalCommits = branch.Commits.Count();

string lastCommitDate = lastCommit.Committer.When.ToString("dd.MM.yyyy HH:mm:ss");
var lastCommitMessage = lastCommit.MessageShort;

string firstCommitDate = firstCommit.Committer.When.ToString("dd.MM.yyyy HH:mm:ss");
var firstCommitMessage = firstCommit.MessageShort;

return new(branchName, totalCommits, lastCommitDate, lastCommitMessage, firstCommitDate, firstCommitMessage);
}

public static List<string> GetRepositoriesNamesFromSetup()
{
var repositoriesNames = new List<string>();

var setup = SetupHandle.ReadSetupFromJson();

foreach (var folder in Directory.EnumerateDirectories(setup.RepositoriesFolder))
{
if (Repository.IsValid(folder)) repositoriesNames.Add(Path.GetFileName(folder));
}

return repositoriesNames;
}
}
14 changes: 0 additions & 14 deletions src/vv/Core/SetupHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,4 @@ public static SetupData ReadSetupFromJson()

return JsonUtils.ReadFromJson<SetupData>(SetupFilePath);
}

public static List<string> GetRepositoriesNamesFromSetup()
{
var repositories = new List<string>();

var setup = ReadSetupFromJson();

foreach(var folder in Directory.EnumerateDirectories(setup.RepositoriesFolder))
{
if (Repository.IsValid(folder)) repositories.Add(Path.GetFileName(folder));
}

return repositories;
}
}