clsGitHead.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.Text;
  3. using System.Linq;
  4. namespace TFABot.Git
  5. {
  6. public class clsGitHead: IDisposable
  7. {
  8. clsGit Git;
  9. bool DisposeofGit = false;
  10. public clsGitHead(clsGit git)
  11. {
  12. Git = git;
  13. }
  14. public String BranchName {get{return Git.Repo.Head.FriendlyName;}}
  15. public String LocalSha {get{return Git.Repo.Head?.Tip?.Sha ?? ""; }}
  16. public String LocalShaShort {get{return Git.Repo.Head?.Tip?.Sha.Substring(0,7) ?? "";}}
  17. public DateTime? LocalDate {get{return Git.Repo.Head?.Tip?.Committer.When.UtcDateTime;}}
  18. public String LocalTag {get{return Git.GetTag(Git.Repo.Head)?.FriendlyName ?? "";}}
  19. public String LocalCommiter {get{return Git.Repo.Head?.Tip?.Committer?.Name ?? "";}}
  20. public String RemoteSha {get{return Git.Repo.Head?.TrackedBranch.Tip?.Sha ?? ""; }}
  21. public String RemoteShaShort {get{return Git.Repo.Head?.TrackedBranch.Tip?.Sha.Substring(0,7) ?? "";}}
  22. public DateTime? RemoteDate {get{return Git.Repo.Head?.TrackedBranch.Tip?.Committer.When.UtcDateTime;}}
  23. public String RemoteTag {get{return Git.GetTag(Git.Repo.Head.TrackedBranch)?.FriendlyName ?? "";}}
  24. public String RemoteCommiter {get{return Git.Repo.Head?.TrackedBranch.Tip?.Committer?.Name ?? "";}}
  25. public int DiffCount
  26. {
  27. get
  28. {
  29. if (Git.Repo?.Head?.Tip?.Sha == null) return 0;
  30. if (Git.Repo?.Head?.TrackedBranch?.Tip?.Sha == null) return 0;
  31. int count=0;
  32. int? head = null;
  33. int? newversion = null;
  34. foreach(var commits in Git.Repo.Head.TrackedBranch.Commits)
  35. {
  36. if (commits.Sha == Git.Repo.Head.Tip.Sha) head=count;
  37. if (commits.Sha == Git.Repo.Head.TrackedBranch.Tip.Sha) newversion=count;
  38. if (head.HasValue && newversion.HasValue) break;
  39. count++;
  40. }
  41. return newversion.Value - head.Value;
  42. }
  43. }
  44. static public string GetHeadToString()
  45. {
  46. try
  47. {
  48. using (var git = new clsGit())
  49. {
  50. return git.Head.ToString();
  51. }
  52. }
  53. catch (Exception ex)
  54. {
  55. return $"Git unavailabe";
  56. }
  57. }
  58. public new string ToString()
  59. {
  60. var sb = new StringBuilder();
  61. if (!String.IsNullOrEmpty(LocalSha))
  62. {
  63. sb.AppendLine();
  64. sb.AppendLine($"HEAD @ {BranchName} {LocalShaShort} {LocalCommiter} {LocalDate:yyyy-MM-dd HH:mm} {LocalTag}");
  65. if (Git.Repo.Head.Tip.Sha != Git.Repo.Head.TrackedBranch.Tip.Sha)
  66. {
  67. int commitcount = DiffCount;
  68. sb.AppendLine(commitcount < 0 ? $"{-commitcount} commit(s) behind": $"{commitcount} commit(s) ahead of");
  69. sb.Append ($" {BranchName} {RemoteShaShort} {RemoteCommiter} {RemoteDate:yyyy-MM-dd HH:mm} {RemoteTag}");
  70. }
  71. if (Git.Repo.Head.Tip.Sha!= clsVersion.GitCommitHash)
  72. {
  73. sb.AppendLine($"\nNOTE: Compiled version does not match HEAD {clsVersion.GitCommitHash.Substring(0,7)}");
  74. }
  75. }
  76. else
  77. {
  78. sb.AppendLine("HEAD: No information");
  79. }
  80. return sb.ToString();
  81. }
  82. public void Dispose()
  83. {
  84. if (DisposeofGit) Git.Dispose();
  85. }
  86. }
  87. }