PageRenderTime 64ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/GitUI/CommitInfo.cs

https://github.com/eyalxjet/gitextensions
C# | 230 lines | 190 code | 34 blank | 6 comment | 22 complexity | 557140c170b2a726378625385c63b896 MD5 | raw file
  1. using System;
  2. using System.Diagnostics;
  3. using System.Text.RegularExpressions;
  4. using System.Windows.Forms;
  5. using GitCommands;
  6. using System.Threading;
  7. using ResourceManager.Translation;
  8. namespace GitUI
  9. {
  10. public partial class CommitInfo : GitExtensionsControl
  11. {
  12. private readonly TranslationString containedInBranches = new TranslationString("Contained in branches:");
  13. private readonly TranslationString containedInNoBranch = new TranslationString("Contained in no branch");
  14. private readonly TranslationString containedInTags = new TranslationString("Contained in tags:");
  15. private readonly TranslationString containedInNoTag = new TranslationString("Contained in no tag");
  16. private readonly SynchronizationContext _syncContext;
  17. public CommitInfo()
  18. {
  19. _syncContext = SynchronizationContext.Current;
  20. InitializeComponent();
  21. Translate();
  22. tableLayout.GrowStyle = TableLayoutPanelGrowStyle.FixedSize;
  23. tableLayout.AutoSizeMode = AutoSizeMode.GrowAndShrink;
  24. tableLayout.AutoSize = true;
  25. RevisionInfo.LinkClicked += RevisionInfoLinkClicked;
  26. }
  27. private static void RevisionInfoLinkClicked(object sender, LinkClickedEventArgs e)
  28. {
  29. try
  30. {
  31. new Process
  32. {
  33. EnableRaisingEvents = false,
  34. StartInfo = { FileName = e.LinkText }
  35. }.Start();
  36. }
  37. catch (Exception ex)
  38. {
  39. MessageBox.Show(ex.Message);
  40. }
  41. }
  42. private string _revision;
  43. public void SetRevision(string revision)
  44. {
  45. _revision = revision;
  46. ReloadCommitInfo();
  47. }
  48. private string _revisionInfo;
  49. private string _tagInfo;
  50. private string _branchInfo;
  51. private void ReloadCommitInfo()
  52. {
  53. showContainedInBranchesToolStripMenuItem.Checked = Settings.CommitInfoShowContainedInBranchesLocal;
  54. showContainedInBranchesRemoteToolStripMenuItem.Checked = Settings.CommitInfoShowContainedInBranchesRemote;
  55. showContainedInBranchesRemoteIfNoLocalToolStripMenuItem.Checked = Settings.CommitInfoShowContainedInBranchesRemoteIfNoLocal;
  56. showContainedInTagsToolStripMenuItem.Checked = Settings.CommitInfoShowContainedInTags;
  57. ResetTextAndImage();
  58. if (string.IsNullOrEmpty(_revision))
  59. return;
  60. _RevisionHeader.Text = string.Empty;
  61. _RevisionHeader.Refresh();
  62. CommitInformation commitInformation = CommitInformation.GetCommitInfo(_revision);
  63. _RevisionHeader.Text = commitInformation.Header;
  64. splitContainer1.SplitterDistance = _RevisionHeader.GetPreferredSize(new System.Drawing.Size(0, 0)).Height;
  65. _revisionInfo = commitInformation.Body;
  66. updateText();
  67. LoadAuthorImage();
  68. if (Settings.CommitInfoShowContainedInBranches)
  69. ThreadPool.QueueUserWorkItem(_ => loadBranchInfo(_revision));
  70. if (Settings.CommitInfoShowContainedInTags)
  71. ThreadPool.QueueUserWorkItem(_ => loadTagInfo(_revision));
  72. }
  73. private void loadTagInfo(string revision)
  74. {
  75. _tagInfo = GetTagsWhichContainsThisCommit(revision);
  76. _syncContext.Post( s => updateText(), null);
  77. }
  78. private void loadBranchInfo(string revision)
  79. {
  80. _branchInfo = GetBranchesWhichContainsThisCommit(revision);
  81. _syncContext.Post(s => updateText(), null);
  82. }
  83. private void updateText()
  84. {
  85. RevisionInfo.Text = _revisionInfo + _branchInfo + _tagInfo;
  86. RevisionInfo.Refresh();
  87. }
  88. private void ResetTextAndImage()
  89. {
  90. _revisionInfo = string.Empty;
  91. _branchInfo = string.Empty;
  92. _tagInfo = string.Empty;
  93. updateText();
  94. gravatar1.LoadImageForEmail("");
  95. }
  96. private void LoadAuthorImage()
  97. {
  98. var matches =
  99. Regex.Matches(
  100. _RevisionHeader.Text,
  101. @"([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})");
  102. if (matches.Count == 0)
  103. return;
  104. gravatar1.LoadImageForEmail(matches[0].Value);
  105. }
  106. private string GetBranchesWhichContainsThisCommit(string revision)
  107. {
  108. const string remotesPrefix= "remotes/";
  109. // Include local branches if explicitly requested or when needed to decide whether to show remotes
  110. bool getLocal = Settings.CommitInfoShowContainedInBranchesLocal ||
  111. Settings.CommitInfoShowContainedInBranchesRemoteIfNoLocal;
  112. // Include remote branches if requested
  113. bool getRemote = Settings.CommitInfoShowContainedInBranchesRemote ||
  114. Settings.CommitInfoShowContainedInBranchesRemoteIfNoLocal;
  115. var branches = CommitInformation.GetAllBranchesWhichContainGivenCommit(revision, getLocal, getRemote);
  116. var branchString = "";
  117. bool allowLocal = Settings.CommitInfoShowContainedInBranchesLocal;
  118. bool allowRemote = getRemote;
  119. foreach (var branch in branches)
  120. {
  121. string noPrefixBranch = branch;
  122. bool branchIsLocal;
  123. if (getLocal && getRemote)
  124. {
  125. // "git branch -a" prefixes remote branches with "remotes/"
  126. // It is possible to create a local branch named "remotes/origin/something"
  127. // so this check is not 100% reliable.
  128. // This shouldn't be a big problem if we're only displaying information.
  129. branchIsLocal = !branch.StartsWith(remotesPrefix);
  130. if (!branchIsLocal)
  131. noPrefixBranch = branch.Substring(remotesPrefix.Length);
  132. }
  133. else
  134. {
  135. branchIsLocal = !getRemote;
  136. }
  137. if ((branchIsLocal && allowLocal) || (!branchIsLocal && allowRemote))
  138. {
  139. if (branchString != string.Empty)
  140. branchString += ", ";
  141. branchString += noPrefixBranch;
  142. }
  143. if (branchIsLocal && Settings.CommitInfoShowContainedInBranchesRemoteIfNoLocal)
  144. allowRemote = false;
  145. }
  146. if (branchString != string.Empty)
  147. return Environment.NewLine + containedInBranches.Text + " " + branchString;
  148. return Environment.NewLine + containedInNoBranch.Text;
  149. }
  150. private string GetTagsWhichContainsThisCommit(string revision)
  151. {
  152. var tagString = "";
  153. foreach (var tag in CommitInformation.GetAllTagsWhichContainGivenCommit(revision))
  154. {
  155. if (tagString != string.Empty)
  156. tagString += ", ";
  157. tagString += tag;
  158. }
  159. if (tagString != string.Empty)
  160. return Environment.NewLine + containedInTags.Text + " " + tagString;
  161. return Environment.NewLine + containedInNoTag.Text;
  162. }
  163. private void tableLayout_Paint(object sender, PaintEventArgs e)
  164. {
  165. }
  166. private void showContainedInBranchesToolStripMenuItem_Click(object sender, EventArgs e)
  167. {
  168. Settings.CommitInfoShowContainedInBranchesLocal = !Settings.CommitInfoShowContainedInBranchesLocal;
  169. ReloadCommitInfo();
  170. }
  171. private void showContainedInTagsToolStripMenuItem_Click(object sender, EventArgs e)
  172. {
  173. Settings.CommitInfoShowContainedInTags = !Settings.CommitInfoShowContainedInTags;
  174. ReloadCommitInfo();
  175. }
  176. private void copyCommitInfoToolStripMenuItem_Click(object sender, EventArgs e)
  177. {
  178. Clipboard.SetText(string.Concat(_RevisionHeader.Text, Environment.NewLine, RevisionInfo.Text));
  179. }
  180. private void showContainedInBranchesRemoteToolStripMenuItem_Click(object sender, EventArgs e)
  181. {
  182. Settings.CommitInfoShowContainedInBranchesRemote = !Settings.CommitInfoShowContainedInBranchesRemote;
  183. ReloadCommitInfo();
  184. }
  185. private void showContainedInBranchesRemoteIfNoLocalToolStripMenuItem_Click(object sender, EventArgs e)
  186. {
  187. Settings.CommitInfoShowContainedInBranchesRemoteIfNoLocal = !Settings.CommitInfoShowContainedInBranchesRemoteIfNoLocal;
  188. ReloadCommitInfo();
  189. }
  190. private void addNoteToolStripMenuItem_Click(object sender, EventArgs e)
  191. {
  192. GitCommandHelpers.EditNotes(_revision);
  193. ReloadCommitInfo();
  194. }
  195. }
  196. }