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

/GitCommands/Git/GitVersion.cs

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