PageRenderTime 27ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/GitCommands/Git/GitVersion.cs

http://github.com/spdr870/gitextensions
C# | 208 lines | 153 code | 42 blank | 13 comment | 31 complexity | 820cbb8b7d4699022531d2b4588be1f9 MD5 | raw file
Possible License(s): GPL-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace GitCommands
  5. {
  6. public class GitVersion : IComparable<GitVersion>
  7. {
  8. private static readonly GitVersion v1_7_1 = new GitVersion("1.7.1");
  9. private static readonly GitVersion v1_7_7 = new GitVersion("1.7.7");
  10. private static readonly GitVersion v1_7_11 = new GitVersion("1.7.11");
  11. private static readonly GitVersion v1_8_4 = new GitVersion("1.8.4");
  12. private static readonly GitVersion v1_8_5 = new GitVersion("1.8.5");
  13. private static readonly GitVersion v2_0_1 = new GitVersion("2.0.1");
  14. private static readonly GitVersion v2_5_1 = new GitVersion("2.5.1");
  15. private static readonly GitVersion v2_7_0 = new GitVersion("2.7.0");
  16. private static readonly GitVersion v2_9_0 = new GitVersion("2.9.0");
  17. private static readonly GitVersion v2_11_0 = new GitVersion("2.11.0");
  18. private static readonly GitVersion v2_15_2 = new GitVersion("2.15.2");
  19. public static readonly GitVersion LastSupportedVersion = v2_11_0;
  20. public static readonly GitVersion LastRecommendedVersion = new GitVersion("2.19.1");
  21. private static GitVersion _current;
  22. public static GitVersion Current
  23. {
  24. get
  25. {
  26. if (_current == null || _current.IsUnknown)
  27. {
  28. var output = new Executable(AppSettings.GitCommand).GetOutput("--version");
  29. _current = new GitVersion(output);
  30. }
  31. return _current;
  32. }
  33. }
  34. public readonly string Full;
  35. private readonly int _a;
  36. private readonly int _b;
  37. private readonly int _c;
  38. private readonly int _d;
  39. public GitVersion(string version)
  40. {
  41. Full = Fix();
  42. var numbers = GetNumbers();
  43. _a = Get(numbers, 0);
  44. _b = Get(numbers, 1);
  45. _c = Get(numbers, 2);
  46. _d = Get(numbers, 3);
  47. string Fix()
  48. {
  49. if (version == null)
  50. {
  51. return "";
  52. }
  53. const string Prefix = "git version";
  54. if (version.StartsWith(Prefix))
  55. {
  56. return version.Substring(Prefix.Length).Trim();
  57. }
  58. return version.Trim();
  59. }
  60. IReadOnlyList<int> GetNumbers()
  61. {
  62. return ParseNumbers().ToList();
  63. IEnumerable<int> ParseNumbers()
  64. {
  65. foreach (var number in Full.Split('.'))
  66. {
  67. if (int.TryParse(number, out var value))
  68. {
  69. yield return value;
  70. }
  71. }
  72. }
  73. }
  74. int Get(IReadOnlyList<int> values, int index)
  75. {
  76. return index < values.Count ? values[index] : 0;
  77. }
  78. }
  79. public bool FetchCanAskForProgress => this >= v1_7_1;
  80. public bool LogFormatRecodesCommitMessage => this >= v1_8_4;
  81. public bool PushCanAskForProgress => this >= v1_7_1;
  82. public bool StashUntrackedFilesSupported => this >= v1_7_7;
  83. public bool SupportPushWithRecursiveSubmodulesCheck => this >= v1_7_7;
  84. public bool SupportPushWithRecursiveSubmodulesOnDemand => this >= v1_7_11;
  85. public bool SupportPushForceWithLease => this >= v1_8_5;
  86. public bool RaceConditionWhenGitStatusIsUpdatingIndex => this < v2_0_1;
  87. public bool SupportWorktree => this >= v2_5_1;
  88. public bool SupportWorktreeList => this >= v2_7_0;
  89. public bool SupportMergeUnrelatedHistory => this >= v2_9_0;
  90. public bool SupportStatusPorcelainV2 => this >= v2_11_0;
  91. public bool SupportNoOptionalLocks => this >= v2_15_2;
  92. public bool IsUnknown => _a == 0 && _b == 0 && _c == 0 && _d == 0;
  93. // Returns true if it's possible to pass given string as command line
  94. // argument to git for searching.
  95. // As of msysgit 1.7.3.1 git-rev-list requires its search arguments
  96. // (--author, --committer, --regex) to be encoded with the exact encoding
  97. // used at commit time.
  98. // This causes problems under Windows, where command line arguments are
  99. // passed as WideChars. Git uses argv, which contains strings
  100. // recoded into 8-bit system codepage, and that means searching for strings
  101. // outside ASCII range gets crippled, unless commit messages in git
  102. // are encoded according to system codepage.
  103. // For versions of git displaying such behaviour, this function should return
  104. // false if its argument isn't command-line safe, i.e. it contains chars
  105. // outside ASCII (7bit) range.
  106. public bool IsRegExStringCmdPassable(string s)
  107. {
  108. if (s == null)
  109. {
  110. return true;
  111. }
  112. foreach (char ch in s)
  113. {
  114. if ((uint)ch >= 0x80)
  115. {
  116. return false;
  117. }
  118. }
  119. return true;
  120. }
  121. private static int Compare(GitVersion left, GitVersion right)
  122. {
  123. if (left == null && right == null)
  124. {
  125. return 0;
  126. }
  127. if (right == null)
  128. {
  129. return 1;
  130. }
  131. if (left == null)
  132. {
  133. return -1;
  134. }
  135. int compareA = left._a.CompareTo(right._a);
  136. if (compareA != 0)
  137. {
  138. return compareA;
  139. }
  140. int compareB = left._b.CompareTo(right._b);
  141. if (compareB != 0)
  142. {
  143. return compareB;
  144. }
  145. int compareC = left._c.CompareTo(right._c);
  146. if (compareC != 0)
  147. {
  148. return compareC;
  149. }
  150. return left._d.CompareTo(right._d);
  151. }
  152. public int CompareTo(GitVersion other) => Compare(this, other);
  153. public static bool operator >(GitVersion left, GitVersion right) => Compare(left, right) > 0;
  154. public static bool operator <(GitVersion left, GitVersion right) => Compare(left, right) < 0;
  155. public static bool operator >=(GitVersion left, GitVersion right) => Compare(left, right) >= 0;
  156. public static bool operator <=(GitVersion left, GitVersion right) => Compare(left, right) <= 0;
  157. public override string ToString()
  158. {
  159. return Full
  160. .Replace(".msysgit.0", "")
  161. .Replace(".msysgit.1", "")
  162. .Replace(".windows.0", "")
  163. .Replace(".windows.1", "");
  164. }
  165. }
  166. }