Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add file link to cli table output #2

Merged
merged 1 commit into from
Aug 22, 2021
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
28 changes: 18 additions & 10 deletions Walrus.CLI/Commands/QueryCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,18 @@ private void HandleQuery(WalrusQuery query, bool printTable)
.OrderBy(c => c.Timestamp)
.AsEnumerable();

// Avoid calling Count() on the commits enumerable because it is slow
int? commitCount = null;
if (printTable)
{
PrintTable(commits);
commitCount = PrintTable(commits);
}

commitCount ??= commits.Count();

Console.WriteLine(new string('=', Console.WindowWidth / 2));
Console.WriteLine("Total Commits: {0}", commitCount);
Console.WriteLine(new string('=', Console.WindowWidth / 2));
}

/// <summary>
Expand All @@ -89,31 +97,31 @@ private void HandleQuery(WalrusQuery query, bool printTable)
/// SHA and commit title are shown for each commit.
/// </summary>
/// <param name="commits">Commits to print</param>
private void PrintTable(IEnumerable<WalrusCommit> commits)
/// <returns>Count of commits in commit</returns>
private int PrintTable(IEnumerable<WalrusCommit> commits)
{
var count = 0;
var header = new string('-', Console.WindowWidth / 2);

foreach (var groupRepo in commits.GroupBy(c => c.RepoName))
{
Console.WriteLine($"Repository: {groupRepo.Key}");
Console.WriteLine($"Repository: {groupRepo.Key} [file://{groupRepo.First().RepoPath}]");
Console.WriteLine(header);

foreach (var groupDay in groupRepo.GroupBy(g => g.Timestamp.Date))
{
Console.WriteLine($"{groupDay.Key:d}: {groupDay.Count()} Commits");

foreach(var commit in groupDay)
foreach (var commit in groupDay)
{
Console.WriteLine($"\t{commit.Sha} {commit.Message}");
Console.WriteLine($"\t{commit.Timestamp:HH:mm} {commit.Sha} {commit.Message}");
++count;
}
}
Console.WriteLine();
Console.WriteLine(Environment.NewLine);
}

var count = commits.Count();

Console.WriteLine(new string('=', Console.WindowWidth / 2));
Console.WriteLine("Total Commits: {0}", count);
return count;
}
}
}
16 changes: 13 additions & 3 deletions Walrus.Core/WalrusCommit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,34 @@
{
using LibGit2Sharp;
using System;
using System.IO;
using Walrus.Core.Internal;

/// <summary>
/// TODO: do we want to wrap this type or use the raw LibGit2 type?
/// </summary>
public class WalrusCommit
{
private readonly WalrusRepository _repository;
private readonly Commit _commit;

public WalrusCommit(string repoName, Commit commit)
public WalrusCommit(WalrusRepository repository, Commit commit)
{
RepoName = repoName;
Ensure.IsNotNull(nameof(repository), repository);
Ensure.IsNotNull(nameof(commit), commit);

_repository = repository;
_commit = commit;
}
/// <summary>
/// Path to repository containing this commit5
/// </summary>
public string RepoPath => _repository.RepositoryPath!;

/// <summary>
/// Name of Git repo this commit belongs to
/// </summary>
public string RepoName { get; }
public string RepoName => _repository.RepositoryName!;

/// <summary>
/// Commit message text
Expand Down
6 changes: 3 additions & 3 deletions Walrus.Core/WalrusRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ internal WalrusRepository(Repository repository)
/// <summary>
/// Name of folder containing Git repo
/// </summary>
public string? RepositoryName => Path.GetFileName(RepositoryPath);
public string RepositoryName => Path.GetFileName(RepositoryPath);

/// <summary>
/// Absolute path to Git repo
/// </summary>
public string? RepositoryPath => Path.GetDirectoryName(_repository?.Info?.WorkingDirectory);
public string RepositoryPath => Path.GetDirectoryName(_repository?.Info?.WorkingDirectory)!;

/// <summary>
/// Most recent commit message
Expand Down Expand Up @@ -103,7 +103,7 @@ private IEnumerable<WalrusCommit> SafeGitCommitEnumeration(IEnumerator<Commit> c
var commit = commitIter.Current;
if (IsMatch(commit, query))
{
yield return new WalrusCommit(RepositoryName!, commit);
yield return new WalrusCommit(this, commit);
}
} while (true);

Expand Down