Advertisement
North_Point

08.Commits

Aug 7th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.01 KB | None | 0 0
  1. using System.Linq;
  2.  
  3. namespace _13.Regex_Exercises
  4. {
  5.     using System;
  6.     using System.Collections.Generic;
  7.     using System.Text.RegularExpressions;
  8.  
  9.     class Exercises
  10.     {
  11.         static void Main()
  12.         {
  13.             // Task 5
  14.             Commits();
  15.         }
  16.  
  17.         private static void Commits()
  18.         {
  19.             Dictionary<string, Dictionary<string, List<Commit>>> usersAndRepos = new Dictionary<string, Dictionary<string, List<Commit>>>();
  20.  
  21.             while (true)
  22.             {
  23.                 string input = Console.ReadLine();
  24.  
  25.                 if (input.ToLower() == "git push")
  26.                     break;
  27.  
  28.                 string pattern = "https:\\/\\/github\\.com\\/(?<user>[A-Za-z0-9-]+)\\/(?<repo>[A-Z-a-z-_]+)\\/commit\\/(?<hash>[0-9a-f]{40}),(?<message>[^\\r\\n|\\r|\\n]+),(?<additions>[0-9]+),(?<deletions>[0-9]+)";
  29.                 Regex regex = new Regex(pattern);
  30.  
  31.                 if (regex.IsMatch(input))
  32.                 {
  33.                     Match match = regex.Match(input);
  34.                     string user = match.Groups["user"].Value;
  35.                     string repo = match.Groups["repo"].Value;
  36.                     string hash = match.Groups["hash"].Value;
  37.                     string message = match.Groups["message"].Value;
  38.                     double additions = double.Parse(match.Groups["additions"].Value);
  39.                     double deletions = double.Parse(match.Groups["deletions"].Value);
  40.  
  41.                     if (!usersAndRepos.ContainsKey(user))
  42.                     {
  43.                         usersAndRepos[user] = new Dictionary<string, List<Commit>>();
  44.                     }
  45.  
  46.                     if (!usersAndRepos[user].ContainsKey(repo))
  47.                     {
  48.                         usersAndRepos[user][repo] = new List<Commit>();
  49.                     }
  50.  
  51.                     Commit commit = new Commit
  52.                     {
  53.                         Message = message,
  54.                         Hash = hash,
  55.                         Additions = additions,
  56.                         Deletions = deletions
  57.                     };
  58.  
  59.                     usersAndRepos[user][repo].Add(commit);
  60.                 }
  61.             }
  62.  
  63.              foreach (KeyValuePair<string, Dictionary<string, List<Commit>>> user in usersAndRepos.OrderBy(u => u.Key))
  64.              {
  65.                  Console.WriteLine($"{user.Key}:");
  66.              
  67.                  foreach (KeyValuePair<string, List<Commit>> repo in user.Value.OrderBy(r => r.Key))
  68.                  {
  69.                      Console.WriteLine($"  {repo.Key}:");
  70.              
  71.                      foreach (Commit commit in repo.Value)
  72.                      {
  73.                          Console.WriteLine($"    commit {commit.Hash}: {commit.Message} ({commit.Additions} additions, {commit.Deletions} deletions)");
  74.                      }
  75.              
  76.                      Console.WriteLine($"    Total: {repo.Value.Sum(commit => commit.Additions)} additions, {repo.Value.Sum   (commit => commit.Deletions)} deletions");
  77.                  }
  78.              }
  79.  
  80.        //   usersAndRepos
  81.        //       .OrderBy(u => u.Key)
  82.        //       .ToList()
  83.        //       .ForEach(user => user.Value.OrderBy(r => r.Key).ToList()
  84.        //       .ForEach(repo =>
  85.        //           Console.WriteLine($"{user.Key}:\n  " +
  86.        //                             $"{repo.Key}:\n    " +
  87.        //                             $"commit {string.Join("\n    commit ", repo.Value)}\n    " +
  88.        //                             $"Total: {repo.Value.Sum(c => c.Additions)} additions, {repo.Value.Sum(c => c.Deletions)} deletions")));
  89.         }
  90.     }
  91.  
  92.     class Commit
  93.     {
  94.         public string Hash { get; set; }
  95.         public string Message { get; set; }
  96.         public double Additions { get; set; }
  97.         public double Deletions { get; set; }
  98.  
  99.         public override string ToString()
  100.         {
  101.             return string.Format($"{this.Hash}: {this.Message} ({this.Additions} additions, {this.Deletions} deletions)");
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement