Skip to content

Commit 4bd6d54

Browse files
Change repositories logic (#8)
1 parent 49e2f0e commit 4bd6d54

5 files changed

Lines changed: 98 additions & 78 deletions

File tree

src/vv.tests/Tests/SetupTest.cs renamed to src/vv.tests/Tests/RepositoriesHandleTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace vv.tests;
55

6-
public class SetupTest
6+
public class RepositoriesHandleTest
77
{
88
[Fact]
99
public void TestGettingRepos()
@@ -17,7 +17,7 @@ public void TestGettingRepos()
1717
var reposFolderPath = Directory.GetParent(root).FullName;
1818
SetupHandle.WriteSetupToJson(new(reposFolderPath));
1919

20-
var reposNames = SetupHandle.GetRepositoriesNamesFromSetup();
20+
var reposNames = RepositoriesHandle.GetRepositoriesNamesFromSetup();
2121

2222
Assert.Contains(Path.GetFileName(root), reposNames);
2323
}

src/vv/CLI/Commands/Git/GitCommand.cs

Lines changed: 22 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using LibGit2Sharp;
22
using vv.CLI.Rendering;
33
using vv.CLI.Settings;
4+
using vv.Core;
45

56
namespace vv.CLI.Commands;
67

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

16-
var currentBranchLastCommit = repo.Head.Commits.First();
17-
var currentBranchFirstCommit = repo.Head.Commits.Last();
18-
19-
var lastCommit = repo.Commits.First();
20-
var firstCommit = repo.Commits.Last();
21-
22-
var contributors = repo.Commits
23-
.GroupBy(c => c.Author.Email)
24-
.Select(g => new
25-
{
26-
Name = g.First().Author.Name,
27-
Email = g.Key,
28-
CommitCount = g.Count()
29-
})
30-
.OrderByDescending(c => c.CommitCount)
31-
.ToList();
32-
33-
string currentBranchName = repo.Head.FriendlyName;
34-
int currentBranchTotalCommits = repo.Head.Commits.Count();
35-
string currentBranchLastCommitDate = currentBranchLastCommit.Committer.When.ToString("dd.MM.yyyy HH:mm:ss");
36-
string currentBranchLastCommitMessage = currentBranchLastCommit.MessageShort;
37-
string currentBranchFirstCommitDate = currentBranchFirstCommit.Committer.When.ToString("dd.MM.yyyy HH:mm:ss");
38-
string currentBranchFirstCommitMessage = currentBranchFirstCommit.MessageShort;
39-
40-
int totalCommits = repo.Commits.Count();
41-
string lastCommitMessage = lastCommit.MessageShort;
42-
string lastCommitDate = lastCommit.Committer.When.ToString("dd.MM.yyyy HH:mm:ss");
43-
string firstCommitMessage = firstCommit.MessageShort;
44-
string firstCommitDate = firstCommit.Committer.When.ToString("dd.MM.yyyy HH:mm:ss");
45-
46-
int totalBranches = repo.Branches.Count(b => !b.IsRemote);
47-
48-
int totalContributors = contributors.Count;
49-
var topContributors = contributors.Take(MAX_TOP_COUNT);
17+
var repoData = RepositoriesHandle.GetRepoData(repo, MAX_TOP_COUNT);
18+
var currentBranchData = RepositoriesHandle.GetBranchData(repo.Head);
5019

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

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

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

60-
var specifiedBranch = repo.Branches[specifiedBranchGivenName];
61-
var specifiedBranchFirstCommit = specifiedBranch.Commits.Last();
62-
var specifiedBranchLastCommit = specifiedBranch.Commits.First();
29+
var specifiedBranchData = RepositoriesHandle.GetBranchData(repo.Branches[specifiedBranchGivenName]);
6330

64-
string specifiedBranchName = specifiedBranch.FriendlyName;
65-
int specifiedBranchTotalCommits = specifiedBranch.Commits.Count();
66-
67-
string specifiedBranchLastCommitDate = specifiedBranchLastCommit.Committer.When.ToString("dd.MM.yyyy HH:mm:ss"); ;
68-
string specifiedBranchLastCommitMessage = specifiedBranchLastCommit.MessageShort;
69-
70-
string specifiedBranchFirstCommitDate = specifiedBranchFirstCommit.Committer.When.ToString("dd.MM.yyyy HH:mm:ss"); ;
71-
string specifiedBranchFirstCommitMessage = specifiedBranchFirstCommit.MessageShort;
72-
73-
DisplayBranch(specifiedBranchName, specifiedBranchTotalCommits, specifiedBranchFirstCommitMessage,
74-
specifiedBranchFirstCommitDate, specifiedBranchLastCommitMessage, specifiedBranchLastCommitDate, specifiedBranchTable);
31+
DisplayBranch(specifiedBranchData.Name, specifiedBranchData.TotalCommits,
32+
specifiedBranchData.FirstCommitMessage, specifiedBranchData.FirstCommitDate,
33+
specifiedBranchData.LastCommitMessage, specifiedBranchData.LastCommitDate,
34+
specifiedBranchTable);
7535

7636
AnsiConsole.Write(specifiedBranchTable);
7737
}
7838

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

81-
DisplayBranch(currentBranchName, currentBranchTotalCommits, currentBranchFirstCommitMessage,
82-
currentBranchFirstCommitDate, currentBranchLastCommitMessage, currentBranchLastCommitDate, statsTable);
41+
DisplayBranch(currentBranchData.Name, currentBranchData.TotalCommits,
42+
currentBranchData.FirstCommitMessage, currentBranchData.FirstCommitDate,
43+
currentBranchData.LastCommitMessage, currentBranchData.LastCommitDate,
44+
statsTable);
8345

8446
// Don't use DisplayBranch since it's global repo stats
8547
statsTable.AddEmptyRow();
86-
statsTable.AddRow("Commits", $"{totalCommits}");
87-
statsTable.AddRow("First commit message", firstCommitMessage);
88-
statsTable.AddRow("First commit date", firstCommitDate);
89-
statsTable.AddRow("Last commit message", lastCommitMessage);
90-
statsTable.AddRow("Last commit date", lastCommitDate);
48+
statsTable.AddRow("Commits", $"{repoData.TotalCommits}");
49+
statsTable.AddRow("First commit date", repoData.FirstCommitDate);
50+
statsTable.AddRow("Last commit date", repoData.LastCommitDate);
9151
statsTable.AddEmptyRow();
92-
statsTable.AddRow("Branches", $"{totalBranches}");
52+
statsTable.AddRow("Branches", $"{repoData.TotalBranches}");
9353
statsTable.AddEmptyRow();
94-
statsTable.AddRow("Contributors", $"{totalContributors}");
54+
statsTable.AddRow("Contributors", $"{repoData.TotalCommits}");
9555

96-
foreach(var contributor in topContributors)
97-
statsTable.AddRow(contributor.Name, $"{contributor.CommitCount}");
56+
foreach(var contributor in repoData.TopContributors)
57+
statsTable.AddRow(contributor.Name, $"{contributor.CommitsCount}");
9858

9959
AnsiConsole.Write(statsTable);
10060
return 0;
10161
}
10262

10363
private void DisplayBranch(string branchName, int branchTotalCommits, string firstCommitMessage,
104-
string firstCommitDate, string lastCommitMessage, string lastCommitDate, Table table,
105-
string displayBranchName = "Current")
64+
string firstCommitDate, string lastCommitMessage, string lastCommitDate, Table table)
10665
{
66+
var displayBranchName = "Current";
67+
10768
table.AddRow($"{displayBranchName} branch", branchName);
10869
table.AddRow($"{displayBranchName} branch commits", $"{branchTotalCommits}");
10970
table.AddRow($"{displayBranchName} branch first commit", firstCommitMessage);

src/vv/CLI/Rendering/ResolvePath.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static string ResolveRepoPath<T>(T settings) where T : BaseSettings
1111
var repoPath = settings.RepoPath;
1212
if (string.IsNullOrEmpty(repoPath))
1313
{
14-
var reposNames = SetupHandle.GetRepositoriesNamesFromSetup();
14+
var reposNames = RepositoriesHandle.GetRepositoriesNamesFromSetup();
1515

1616
if (reposNames.Count == 0)
1717
throw new UserException(

src/vv/Core/RepositoriesHandle.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using LibGit2Sharp;
2+
3+
namespace vv.Core;
4+
5+
internal readonly record struct BranchData(
6+
string Name, int TotalCommits, string LastCommitDate, string LastCommitMessage,
7+
string FirstCommitDate, string FirstCommitMessage);
8+
9+
internal readonly record struct RepoData(
10+
IEnumerable<(string Name, string Email, int CommitsCount)> TopContributors, int TotalCommits,
11+
int TotalBranches, int TotalContributors, string LastCommitDate, string FirstCommitDate);
12+
13+
internal static class RepositoriesHandle
14+
{
15+
public static RepoData GetRepoData(Repository repo, int topContributorsCount)
16+
{
17+
var lastCommit = repo.Commits.First();
18+
var firstCommit = repo.Commits.Last();
19+
20+
var contributors = repo.Commits
21+
.GroupBy(c => c.Author.Email)
22+
.Select(g => (
23+
g.First().Author.Name,
24+
Email: g.Key,
25+
CommitsCount: g.Count()
26+
))
27+
.OrderByDescending(c => c.CommitsCount)
28+
.ToList();
29+
30+
var totalCommits = repo.Commits.Count();
31+
string lastCommitDate = lastCommit.Committer.When.ToString("dd.MM.yyyy HH:mm:ss");
32+
string firstCommitDate = firstCommit.Committer.When.ToString("dd.MM.yyyy HH:mm:ss");
33+
34+
var totalBranches = repo.Branches.Count(b => !b.IsRemote);
35+
var totalContributors = contributors.Count;
36+
37+
var topContributors = contributors.Take(topContributorsCount);
38+
39+
return new(topContributors, totalCommits, totalBranches, totalContributors, lastCommitDate, firstCommitDate);
40+
}
41+
42+
43+
public static BranchData GetBranchData(Branch branch)
44+
{
45+
var lastCommit = branch.Commits.First();
46+
var firstCommit = branch.Commits.Last();
47+
48+
var branchName = branch.FriendlyName;
49+
var totalCommits = branch.Commits.Count();
50+
51+
string lastCommitDate = lastCommit.Committer.When.ToString("dd.MM.yyyy HH:mm:ss");
52+
var lastCommitMessage = lastCommit.MessageShort;
53+
54+
string firstCommitDate = firstCommit.Committer.When.ToString("dd.MM.yyyy HH:mm:ss");
55+
var firstCommitMessage = firstCommit.MessageShort;
56+
57+
return new(branchName, totalCommits, lastCommitDate, lastCommitMessage, firstCommitDate, firstCommitMessage);
58+
}
59+
60+
public static List<string> GetRepositoriesNamesFromSetup()
61+
{
62+
var repositoriesNames = new List<string>();
63+
64+
var setup = SetupHandle.ReadSetupFromJson();
65+
66+
foreach (var folder in Directory.EnumerateDirectories(setup.RepositoriesFolder))
67+
{
68+
if (Repository.IsValid(folder)) repositoriesNames.Add(Path.GetFileName(folder));
69+
}
70+
71+
return repositoriesNames;
72+
}
73+
}

src/vv/Core/SetupHandle.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,4 @@ public static SetupData ReadSetupFromJson()
2424

2525
return JsonUtils.ReadFromJson<SetupData>(SetupFilePath);
2626
}
27-
28-
public static List<string> GetRepositoriesNamesFromSetup()
29-
{
30-
var repositories = new List<string>();
31-
32-
var setup = ReadSetupFromJson();
33-
34-
foreach(var folder in Directory.EnumerateDirectories(setup.RepositoriesFolder))
35-
{
36-
if (Repository.IsValid(folder)) repositories.Add(Path.GetFileName(folder));
37-
}
38-
39-
return repositories;
40-
}
4127
}

0 commit comments

Comments
 (0)