PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/GitCommands/Git/GitVersion.cs

https://github.com/jotux/gitextensions
C# | 163 lines | 119 code | 31 blank | 13 comment | 27 complexity | d611bc3d492c7cb36dd17a3f647063ab MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. namespace GitCommands
  4. {
  5. public class GitVersion : IComparable<GitVersion>
  6. {
  7. private static readonly GitVersion v1_7_0 = new GitVersion("1.7.0");
  8. private static readonly GitVersion v1_7_1 = new GitVersion("1.7.1");
  9. public static readonly GitVersion LastSupportedVersion = v1_7_0;
  10. private const string Prefix = "git version";
  11. private readonly string full;
  12. private readonly int a;
  13. private readonly int b;
  14. private readonly int c;
  15. private readonly int d;
  16. public GitVersion(string version)
  17. {
  18. full = Fix(version);
  19. IList<int> numbers = GetNumbers(full);
  20. a = Get(numbers, 0);
  21. b = Get(numbers, 1);
  22. c = Get(numbers, 2);
  23. d = Get(numbers, 3);
  24. }
  25. public bool SupportGitStatusPorcelain
  26. {
  27. get { return this >= v1_7_0; }
  28. }
  29. public bool CloneCanAskForProgress
  30. {
  31. get { return this >= v1_7_0; }
  32. }
  33. public bool FetchCanAskForProgress
  34. {
  35. get { return this >= v1_7_1; }
  36. }
  37. public bool GuiDiffToolExist
  38. {
  39. get { return this >= v1_7_0; }
  40. }
  41. public bool IsUnknown
  42. {
  43. get { return a == 0 && b == 0 && c == 0 && d == 0; }
  44. }
  45. // Returns true if it's possible to pass given string as command line
  46. // argument to git for searching.
  47. // As of msysgit 1.7.3.1 git-rev-list requires its search arguments
  48. // (--author, --committer, --regex) to be encoded with the exact encoding
  49. // used at commit time.
  50. // This causes problems under Windows, where command line arguments are
  51. // passed as WideChars. Git uses argv, which contains strings
  52. // recoded into 8-bit system codepage, and that means searching for strings
  53. // outside ASCII range gets crippled, unless commit messages in git
  54. // are encoded according to system codepage.
  55. // For versions of git displaying such behaviour, this function should return
  56. // false if its argument isn't command-line safe, i.e. it contains chars
  57. // outside ASCII (7bit) range.
  58. public bool IsRegExStringCmdPassable(string s)
  59. {
  60. if (s==null) return true;
  61. foreach (char ch in s)
  62. if ((uint)ch >= 0x80) return false;
  63. return true;
  64. }
  65. private static string Fix(string version)
  66. {
  67. if (version == null)
  68. return String.Empty;
  69. if (version.StartsWith(Prefix))
  70. return version.Substring(Prefix.Length).Trim();
  71. return version.Trim();
  72. }
  73. private static int Get(IList<int> values, int index)
  74. {
  75. return index < values.Count ? values[index] : 0;
  76. }
  77. private static IList<int> GetNumbers(string version)
  78. {
  79. IEnumerable<int> numbers = ParseNumbers(version);
  80. return new List<int>(numbers);
  81. }
  82. private static IEnumerable<int> ParseNumbers(string version)
  83. {
  84. string[] numbers = version.Split('.');
  85. foreach (var number in numbers)
  86. {
  87. int value;
  88. if (Int32.TryParse(number, out value))
  89. {
  90. yield return value;
  91. }
  92. }
  93. }
  94. public int CompareTo(GitVersion other)
  95. {
  96. return Compare(this, other);
  97. }
  98. private static int Compare(GitVersion left, GitVersion right)
  99. {
  100. if (left == null && right == null) return 0;
  101. if (right == null) return 1;
  102. if (left == null) return -1;
  103. int compareA = left.a.CompareTo(right.a);
  104. if (compareA != 0) return compareA;
  105. int compareB = left.b.CompareTo(right.b);
  106. if (compareB != 0) return compareB;
  107. int compareC = left.c.CompareTo(right.c);
  108. if (compareC != 0) return compareC;
  109. return left.d.CompareTo(right.d);
  110. }
  111. public static bool operator >(GitVersion left, GitVersion right)
  112. {
  113. return Compare(left, right) > 0;
  114. }
  115. public static bool operator <(GitVersion left, GitVersion right)
  116. {
  117. return Compare(left, right) < 0;
  118. }
  119. public static bool operator >=(GitVersion left, GitVersion right)
  120. {
  121. return Compare(left, right) >= 0;
  122. }
  123. public static bool operator <=(GitVersion left, GitVersion right)
  124. {
  125. return Compare(left, right) <= 0;
  126. }
  127. public override string ToString()
  128. {
  129. return full.Replace(".msysgit.0", "").Replace(".msysgit.1", "");
  130. }
  131. }
  132. }