/GitUI/CommandsDialogs/FormCommit.cs
C# | 2542 lines | 2079 code | 406 blank | 57 comment | 361 complexity | e1288041fa82567c97d9465bfe8cf52a MD5 | raw file
Possible License(s): GPL-3.0
Large files files are truncated, but you can click here to view the full file
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Diagnostics;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using GitCommands;
- using GitCommands.Config;
- using GitCommands.Utils;
- using GitUI.AutoCompletion;
- using GitUI.CommandsDialogs.CommitDialog;
- using GitUI.HelperDialogs;
- using GitUI.Hotkey;
- using GitUI.Script;
- using PatchApply;
- using ResourceManager;
- using Timer = System.Windows.Forms.Timer;
- namespace GitUI.CommandsDialogs
- {
- public sealed partial class FormCommit : GitModuleForm //, IHotkeyable
- {
- #region Translation
- private readonly TranslationString _amendCommit =
- new TranslationString("You are about to rewrite history." + Environment.NewLine +
- "Only use amend if the commit is not published yet!" + Environment.NewLine +
- Environment.NewLine + "Do you want to continue?");
- private readonly TranslationString _amendCommitCaption = new TranslationString("Amend commit");
- private readonly TranslationString _deleteFailed = new TranslationString("Delete file failed");
- private readonly TranslationString _deleteSelectedFiles =
- new TranslationString("Are you sure you want delete the selected file(s)?");
- private readonly TranslationString _deleteSelectedFilesCaption = new TranslationString("Delete");
- private readonly TranslationString _deleteUntrackedFiles =
- new TranslationString("Are you sure you want to delete all untracked files?");
- private readonly TranslationString _deleteUntrackedFilesCaption =
- new TranslationString("Delete untracked files.");
- private readonly TranslationString _enterCommitMessage = new TranslationString("Please enter commit message");
- private readonly TranslationString _enterCommitMessageCaption = new TranslationString("Commit message");
- private readonly TranslationString _commitMessageDisabled = new TranslationString("Commit Message is requested during commit");
- private readonly TranslationString _enterCommitMessageHint = new TranslationString("Enter commit message");
- private readonly TranslationString _mergeConflicts =
- new TranslationString("There are unresolved mergeconflicts, solve mergeconflicts before committing.");
- private readonly TranslationString _mergeConflictsCaption = new TranslationString("Merge conflicts");
- private readonly TranslationString _noFilesStagedAndNothingToCommit =
- new TranslationString("There are no files staged for this commit.");
- private readonly TranslationString _noFilesStagedButSuggestToCommitAllUnstaged =
- new TranslationString("There are no files staged for this commit. Stage and commit all unstaged files?");
- private readonly TranslationString _noFilesStagedAndConfirmAnEmptyMergeCommit =
- new TranslationString("There are no files staged for this commit.\nAre you sure you want to commit?");
- private readonly TranslationString _noStagedChanges = new TranslationString("There are no staged changes");
- private readonly TranslationString _noUnstagedChanges = new TranslationString("There are no unstaged changes");
- private readonly TranslationString _notOnBranchMainInstruction = new TranslationString("You are not working on a branch");
- private readonly TranslationString _notOnBranch =
- new TranslationString("This commit will be unreferenced when switching to another branch and can be lost." +
- Environment.NewLine + "" + Environment.NewLine + "Do you want to continue?");
- private readonly TranslationString _notOnBranchButtons = new TranslationString("Checkout branch|Create branch|Continue");
- private readonly TranslationString _notOnBranchCaption = new TranslationString("Not on a branch");
- private readonly TranslationString _onlyStageChunkOfSingleFileError =
- new TranslationString("You can only use this option when selecting a single file");
- private readonly TranslationString _resetChangesCaption = new TranslationString("Reset changes");
- private readonly TranslationString _resetSelectedChangesText =
- new TranslationString("Are you sure you want to reset all selected files?");
- private readonly TranslationString _resetStageChunkOfFileCaption = new TranslationString("Unstage chunk of file");
- private readonly TranslationString _stageDetails = new TranslationString("Stage Details");
- private readonly TranslationString _stageFiles = new TranslationString("Stage {0} files");
- private readonly TranslationString _selectOnlyOneFile = new TranslationString("You must have only one file selected.");
- private readonly TranslationString _selectOnlyOneFileCaption = new TranslationString("Error");
- private readonly TranslationString _stageSelectedLines = new TranslationString("Stage selected line(s)");
- private readonly TranslationString _unstageSelectedLines = new TranslationString("Unstage selected line(s)");
- private readonly TranslationString _resetSelectedLines = new TranslationString("Reset selected line(s)");
- private readonly TranslationString _resetSelectedLinesConfirmation = new TranslationString("Are you sure you want to reset the changes to the selected lines?");
- private readonly TranslationString _formTitle = new TranslationString("Commit to {0} ({1})");
- private readonly TranslationString _selectionFilterToolTip = new TranslationString("Enter a regular expression to select unstaged files.");
- private readonly TranslationString _selectionFilterErrorToolTip = new TranslationString("Error {0}");
- private readonly TranslationString _commitMsgFirstLineInvalid = new TranslationString("First line of commit message contains too many characters."
- + Environment.NewLine + "Do you want to continue?");
- private readonly TranslationString _commitMsgLineInvalid = new TranslationString("The following line of commit message contains too many characters:"
- + Environment.NewLine + Environment.NewLine + "{0}" + Environment.NewLine + Environment.NewLine + "Do you want to continue?");
- private readonly TranslationString _commitMsgSecondLineNotEmpty = new TranslationString("Second line of commit message is not empty." + Environment.NewLine + "Do you want to continue?");
- private readonly TranslationString _commitMsgRegExNotMatched = new TranslationString("Commit message does not match RegEx." + Environment.NewLine + "Do you want to continue?");
- private readonly TranslationString _commitValidationCaption = new TranslationString("Commit validation");
- private readonly TranslationString _commitTemplateSettings = new TranslationString("Settings");
- private readonly TranslationString _commitAuthorInfo = new TranslationString("Author");
- private readonly TranslationString _commitCommitterInfo = new TranslationString("Committer");
- private readonly TranslationString _commitCommitterToolTip = new TranslationString("Click to change committer information.");
- #endregion
- private FileStatusList _currentFilesList;
- private bool _skipUpdate;
- private readonly TaskScheduler _taskScheduler;
- private GitItemStatus _currentItem;
- private bool _currentItemStaged;
- private readonly CommitKind _commitKind;
- private readonly GitRevision _editedCommit;
- private readonly ToolStripMenuItem _stageSelectedLinesToolStripMenuItem;
- private readonly ToolStripMenuItem _resetSelectedLinesToolStripMenuItem;
- private string _commitTemplate;
- private bool IsMergeCommit { get; set; }
- private bool _shouldRescanChanges = true;
- private bool _shouldReloadCommitTemplates = true;
- private readonly AsyncLoader _unstagedLoader;
- private readonly bool _useFormCommitMessage;
- private CancellationTokenSource _interactiveAddBashCloseWaitCts = new CancellationTokenSource();
- private string _userName = "";
- private string _userEmail = "";
- /// <summary>
- /// For VS designer
- /// </summary>
- private FormCommit()
- : this(null)
- {
- }
- public FormCommit(GitUICommands aCommands)
- : this(aCommands, CommitKind.Normal, null)
- { }
- public FormCommit(GitUICommands aCommands, CommitKind commitKind, GitRevision editedCommit)
- : base(true, aCommands)
- {
- _taskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
- _unstagedLoader = new AsyncLoader(_taskScheduler);
- _useFormCommitMessage = AppSettings.UseFormCommitMessage;
- InitializeComponent();
- Message.TextChanged += Message_TextChanged;
- Message.TextAssigned += Message_TextAssigned;
- if (Module != null)
- Message.AddAutoCompleteProvider(new CommitAutoCompleteProvider(Module));
- Loading.Image = Properties.Resources.loadingpanel;
- Translate();
- SolveMergeconflicts.Font = new Font(SystemFonts.MessageBoxFont, FontStyle.Bold);
- SelectedDiff.ExtraDiffArgumentsChanged += SelectedDiffExtraDiffArgumentsChanged;
- if (IsUICommandsInitialized)
- StageInSuperproject.Visible = Module.SuperprojectModule != null;
- StageInSuperproject.Checked = AppSettings.StageInSuperprojectAfterCommit;
- closeDialogAfterEachCommitToolStripMenuItem.Checked = AppSettings.CloseCommitDialogAfterCommit;
- closeDialogAfterAllFilesCommittedToolStripMenuItem.Checked = AppSettings.CloseCommitDialogAfterLastCommit;
- refreshDialogOnFormFocusToolStripMenuItem.Checked = AppSettings.RefreshCommitDialogOnFormFocus;
- Unstaged.SetNoFilesText(_noUnstagedChanges.Text);
- Unstaged.FilterVisible = true;
- Staged.SetNoFilesText(_noStagedChanges.Text);
- Message.Enabled = _useFormCommitMessage;
- commitMessageToolStripMenuItem.Enabled = _useFormCommitMessage;
- commitTemplatesToolStripMenuItem.Enabled = _useFormCommitMessage;
- Message.WatermarkText = _useFormCommitMessage
- ? _enterCommitMessageHint.Text
- : _commitMessageDisabled.Text;
- _commitKind = commitKind;
- _editedCommit = editedCommit;
- HotkeysEnabled = true;
- Hotkeys = HotkeySettingsManager.LoadHotkeys(HotkeySettingsName);
- SelectedDiff.AddContextMenuSeparator();
- _stageSelectedLinesToolStripMenuItem = SelectedDiff.AddContextMenuEntry(_stageSelectedLines.Text, StageSelectedLinesToolStripMenuItemClick);
- _stageSelectedLinesToolStripMenuItem.ShortcutKeyDisplayString = GetShortcutKeys((int)Commands.StageSelectedFile).ToShortcutKeyDisplayString();
- _resetSelectedLinesToolStripMenuItem = SelectedDiff.AddContextMenuEntry(_resetSelectedLines.Text, ResetSelectedLinesToolStripMenuItemClick);
- _resetSelectedLinesToolStripMenuItem.ShortcutKeyDisplayString = GetShortcutKeys((int)Commands.ResetSelectedFiles).ToShortcutKeyDisplayString();
- _resetSelectedLinesToolStripMenuItem.Image = Reset.Image;
- resetChanges.ShortcutKeyDisplayString = _resetSelectedLinesToolStripMenuItem.ShortcutKeyDisplayString;
- stagedResetChanges.ShortcutKeyDisplayString = _resetSelectedLinesToolStripMenuItem.ShortcutKeyDisplayString;
- deleteFileToolStripMenuItem.ShortcutKeyDisplayString = GetShortcutKeys((int)Commands.DeleteSelectedFiles).ToShortcutKeyDisplayString();
- viewFileHistoryToolStripItem.ShortcutKeyDisplayString = GetShortcutKeys((int)Commands.ShowHistory).ToShortcutKeyDisplayString();
- toolStripMenuItem6.ShortcutKeyDisplayString = GetShortcutKeys((int)Commands.ShowHistory).ToShortcutKeyDisplayString();
- commitAuthorStatus.ToolTipText = _commitCommitterToolTip.Text;
- toolAuthor.Control.PreviewKeyDown += ToolAuthor_PreviewKeyDown;
- }
- void ToolAuthor_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
- {
- if (e.Alt)
- e.IsInputKey = true;
- }
- private void FormCommit_Load(object sender, EventArgs e)
- {
- if (AppSettings.CommitDialogSplitter != -1)
- splitMain.SplitterDistance = AppSettings.CommitDialogSplitter;
- if (AppSettings.CommitDialogRightSplitter != -1)
- splitRight.SplitterDistance = AppSettings.CommitDialogRightSplitter;
- Reset.Visible = AppSettings.ShowResetAllChanges;
- ResetUnStaged.Visible = AppSettings.ShowResetUnstagedChanges;
- CommitAndPush.Visible = AppSettings.ShowCommitAndPush;
- AdjustCommitButtonPanelHeight();
- }
- private void AdjustCommitButtonPanelHeight()
- {
- splitRight.Panel2MinSize = Math.Max(splitRight.Panel2MinSize, flowCommitButtons.PreferredSize.Height);
- splitRight.SplitterDistance = Math.Min(splitRight.SplitterDistance, splitRight.Height - splitRight.Panel2MinSize);
- }
- private void FormCommitFormClosing(object sender, FormClosingEventArgs e)
- {
- Message.CancelAutoComplete();
- // Do not remember commit message of fixup or squash commits, since they have
- // a special meaning, and can be dangerous if used inappropriately.
- if (CommitKind.Normal == _commitKind)
- GitCommands.CommitHelper.SetCommitMessage(Module, Message.Text);
- AppSettings.CommitDialogSplitter = splitMain.SplitterDistance;
- AppSettings.CommitDialogRightSplitter = splitRight.SplitterDistance;
- }
- void SelectedDiff_ContextMenuOpening(object sender, System.ComponentModel.CancelEventArgs e)
- {
- _stageSelectedLinesToolStripMenuItem.Enabled = SelectedDiff.HasAnyPatches() || _currentItem != null && _currentItem.IsNew;
- _resetSelectedLinesToolStripMenuItem.Enabled = _stageSelectedLinesToolStripMenuItem.Enabled;
- }
- #region Hotkey commands
- public const string HotkeySettingsName = "Commit";
- internal enum Commands
- {
- AddToGitIgnore,
- DeleteSelectedFiles,
- FocusUnstagedFiles,
- FocusSelectedDiff,
- FocusStagedFiles,
- FocusCommitMessage,
- ResetSelectedFiles,
- StageSelectedFile,
- UnStageSelectedFile,
- ShowHistory,
- ToggleSelectionFilter
- }
- private bool AddToGitIgnore()
- {
- if (Unstaged.Focused)
- {
- AddFileTogitignoreToolStripMenuItemClick(this, null);
- return true;
- }
- return false;
- }
- private bool DeleteSelectedFiles()
- {
- if (Unstaged.Focused)
- {
- DeleteFileToolStripMenuItemClick(this, null);
- return true;
- }
- return false;
- }
- private bool FocusStagedFiles()
- {
- Staged.Focus();
- return true;
- }
- private bool FocusUnstagedFiles()
- {
- Unstaged.Focus();
- return true;
- }
- private bool FocusSelectedDiff()
- {
- SelectedDiff.Focus();
- return true;
- }
- private bool FocusCommitMessage()
- {
- Message.Focus();
- return true;
- }
- private bool ResetSelectedFiles()
- {
- if (Unstaged.Focused || Staged.Focused)
- {
- ResetSoftClick(this, null);
- return true;
- }
- else if (SelectedDiff.ContainsFocus && _resetSelectedLinesToolStripMenuItem.Enabled)
- {
- ResetSelectedLinesToolStripMenuItemClick(this, null);
- return true;
- }
- return false;
- }
- private bool StageSelectedFile()
- {
- if (Unstaged.Focused)
- {
- StageClick(this, null);
- return true;
- }
- else if (SelectedDiff.ContainsFocus && !_currentItemStaged && _stageSelectedLinesToolStripMenuItem.Enabled)
- {
- StageSelectedLinesToolStripMenuItemClick(this, null);
- return true;
- }
- return false;
- }
- private bool UnStageSelectedFile()
- {
- if (Staged.Focused)
- {
- UnstageFilesClick(this, null);
- return true;
- }
- else if (SelectedDiff.ContainsFocus && _currentItemStaged && _stageSelectedLinesToolStripMenuItem.Enabled)
- {
- StageSelectedLinesToolStripMenuItemClick(this, null);
- return true;
- }
- return false;
- }
- private bool StartFileHistoryDialog()
- {
- if (Staged.Focused || Unstaged.Focused)
- {
- if (_currentFilesList.SelectedItem != null)
- {
- if ((!_currentFilesList.SelectedItem.IsNew) && (!_currentFilesList.SelectedItem.IsRenamed))
- {
- UICommands.StartFileHistoryDialog(this, _currentFilesList.SelectedItem.Name, null);
- }
- }
- return true;
- }
- return false;
- }
- private bool ToggleSelectionFilter()
- {
- selectionFilterToolStripMenuItem.Checked = !selectionFilterToolStripMenuItem.Checked;
- toolbarSelectionFilter.Visible = selectionFilterToolStripMenuItem.Checked;
- return true;
- }
- protected override bool ExecuteCommand(int cmd)
- {
- switch ((Commands)cmd)
- {
- case Commands.AddToGitIgnore: return AddToGitIgnore();
- case Commands.DeleteSelectedFiles: return DeleteSelectedFiles();
- case Commands.FocusStagedFiles: return FocusStagedFiles();
- case Commands.FocusUnstagedFiles: return FocusUnstagedFiles();
- case Commands.FocusSelectedDiff: return FocusSelectedDiff();
- case Commands.FocusCommitMessage: return FocusCommitMessage();
- case Commands.ResetSelectedFiles: return ResetSelectedFiles();
- case Commands.StageSelectedFile: return StageSelectedFile();
- case Commands.UnStageSelectedFile: return UnStageSelectedFile();
- case Commands.ShowHistory: return StartFileHistoryDialog();
- case Commands.ToggleSelectionFilter: return ToggleSelectionFilter();
- default: return base.ExecuteCommand(cmd);
- }
- }
- #endregion
- public void ShowDialogWhenChanges()
- {
- ShowDialogWhenChanges(null);
- }
- private void ComputeUnstagedFiles(Action<IList<GitItemStatus>> onComputed, bool DoAsync)
- {
- Func<IList<GitItemStatus>> getAllChangedFilesWithSubmodulesStatus = () => Module.GetAllChangedFilesWithSubmodulesStatus(
- !showIgnoredFilesToolStripMenuItem.Checked,
- !showAssumeUnchangedFilesToolStripMenuItem.Checked,
- showUntrackedFilesToolStripMenuItem.Checked ? UntrackedFilesMode.Default : UntrackedFilesMode.No);
- if (DoAsync)
- _unstagedLoader.Load(getAllChangedFilesWithSubmodulesStatus, onComputed);
- else
- {
- _unstagedLoader.Cancel();
- onComputed(getAllChangedFilesWithSubmodulesStatus());
- }
- }
- public void ShowDialogWhenChanges(IWin32Window owner)
- {
- ComputeUnstagedFiles((allChangedFiles) =>
- {
- if (allChangedFiles.Count > 0)
- {
- LoadUnstagedOutput(allChangedFiles);
- Initialize(false);
- ShowDialog(owner);
- }
- else
- Close();
- #if !__MonoCS__ // animated GIFs are not supported in Mono/Linux
- //trying to properly dispose loading image issue #1037
- Loading.Image.Dispose();
- #endif
- }, false
- );
- }
- private bool _selectedDiffReloaded = true;
- private void StageSelectedLinesToolStripMenuItemClick(object sender, EventArgs e)
- {
- //to prevent multiple clicks
- if (!_selectedDiffReloaded)
- return;
- Debug.Assert(_currentItem != null);
- // Prepare git command
- string args = "apply --cached --whitespace=nowarn";
- if (_currentItemStaged) //staged
- args += " --reverse";
- byte[] patch;
- if (!_currentItemStaged && _currentItem.IsNew)
- patch = PatchManager.GetSelectedLinesAsNewPatch(Module, _currentItem.Name,
- SelectedDiff.GetText(), SelectedDiff.GetSelectionPosition(),
- SelectedDiff.GetSelectionLength(), SelectedDiff.Encoding, false, SelectedDiff.FilePreabmle);
- else
- patch = PatchManager.GetSelectedLinesAsPatch(Module, SelectedDiff.GetText(),
- SelectedDiff.GetSelectionPosition(), SelectedDiff.GetSelectionLength(),
- _currentItemStaged, SelectedDiff.Encoding, _currentItem.IsNew);
- if (patch != null && patch.Length > 0)
- {
- string output = Module.RunGitCmd(args, null, patch);
- ProcessApplyOutput(output, patch);
- }
- }
- private void ProcessApplyOutput(string output, byte[] patch)
- {
- if (!string.IsNullOrEmpty(output))
- {
- MessageBox.Show(this, output + "\n\n" + SelectedDiff.Encoding.GetString(patch));
- }
- if (_currentItemStaged)
- Staged.StoreNextIndexToSelect();
- else
- Unstaged.StoreNextIndexToSelect();
- ScheduleGoToLine();
- _selectedDiffReloaded = false;
- RescanChanges();
- }
- private void ScheduleGoToLine()
- {
- int selectedDifflineToSelect = SelectedDiff.GetText().Substring(0, SelectedDiff.GetSelectionPosition()).Count(c => c == '\n');
- int scrollPosition = SelectedDiff.ScrollPos;
- string selectedFileName = _currentItem.Name;
- Action stageAreaLoaded = null;
- stageAreaLoaded = () =>
- {
- EventHandler textLoaded = null;
- textLoaded = (a, b) =>
- {
- if (_currentItem != null && _currentItem.Name.Equals(selectedFileName))
- {
- SelectedDiff.GoToLine(selectedDifflineToSelect);
- SelectedDiff.ScrollPos = scrollPosition;
- }
- SelectedDiff.TextLoaded -= textLoaded;
- _selectedDiffReloaded = true;
- };
- SelectedDiff.TextLoaded += textLoaded;
- OnStageAreaLoaded -= stageAreaLoaded;
- };
- OnStageAreaLoaded += stageAreaLoaded;
- }
- private void ResetSelectedLinesToolStripMenuItemClick(object sender, EventArgs e)
- {
- //to prevent multiple clicks
- if (!_selectedDiffReloaded)
- return;
- if (MessageBox.Show(this, _resetSelectedLinesConfirmation.Text, _resetChangesCaption.Text,
- MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
- return;
- Debug.Assert(_currentItem != null);
- // Prepare git command
- string args = "apply --whitespace=nowarn";
- if (_currentItemStaged) //staged
- args += " --reverse --index";
- byte[] patch;
- if (_currentItemStaged)
- patch = PatchManager.GetSelectedLinesAsPatch(Module, SelectedDiff.GetText(),
- SelectedDiff.GetSelectionPosition(), SelectedDiff.GetSelectionLength(),
- _currentItemStaged, SelectedDiff.Encoding, _currentItem.IsNew);
- else if (_currentItem.IsNew)
- patch = PatchManager.GetSelectedLinesAsNewPatch(Module, _currentItem.Name,
- SelectedDiff.GetText(), SelectedDiff.GetSelectionPosition(), SelectedDiff.GetSelectionLength(),
- SelectedDiff.Encoding, true, SelectedDiff.FilePreabmle);
- else
- patch = PatchManager.GetResetUnstagedLinesAsPatch(Module, SelectedDiff.GetText(),
- SelectedDiff.GetSelectionPosition(), SelectedDiff.GetSelectionLength(),
- _currentItemStaged, SelectedDiff.Encoding);
- if (patch != null && patch.Length > 0)
- {
- string output = Module.RunGitCmd(args, null, patch);
- if (EnvUtils.RunningOnWindows())
- {
- //remove file mode warnings on windows
- Regex regEx = new Regex("warning: .*has type .* expected .*", RegexOptions.Compiled);
- output = output.RemoveLines(regEx.IsMatch);
- }
- ProcessApplyOutput(output, patch);
- }
- }
- private void EnableStageButtons(bool enable)
- {
- toolUnstageItem.Enabled = enable;
- toolUnstageAllItem.Enabled = enable;
- toolStageItem.Enabled = enable;
- toolStageAllItem.Enabled = enable;
- workingToolStripMenuItem.Enabled = enable;
- ResetUnStaged.Enabled = Unstaged.AllItems.Any();
- }
- private bool _initialized;
- private void Initialize(bool loadUnstaged)
- {
- _initialized = true;
- Task.Factory.StartNew(() => string.Format(_formTitle.Text, Module.GetSelectedBranch(),
- Module.WorkingDir))
- .ContinueWith(task => Text = task.Result, _taskScheduler);
- Cursor.Current = Cursors.WaitCursor;
- if (loadUnstaged)
- {
- Loading.Visible = true;
- LoadingStaged.Visible = true;
- Commit.Enabled = false;
- CommitAndPush.Enabled = false;
- Amend.Enabled = false;
- Reset.Enabled = false;
- ResetUnStaged.Enabled = false;
- EnableStageButtons(false);
- ComputeUnstagedFiles(LoadUnstagedOutput, true);
- }
- UpdateMergeHead();
- Message.TextBoxFont = AppSettings.CommitFont;
- // Check if commit.template is used
- string fileName = Module.GetEffectivePathSetting("commit.template");
- if (!string.IsNullOrEmpty(fileName))
- {
- using (var commitReader = new StreamReader(fileName))
- {
- _commitTemplate = commitReader.ReadToEnd().Replace("\r", "");
- }
- Message.Text = _commitTemplate;
- }
- Cursor.Current = Cursors.Default;
- }
- private void Initialize()
- {
- Initialize(true);
- }
- private void UpdateMergeHead()
- {
- var mergeHead = Module.RevParse("MERGE_HEAD");
- IsMergeCommit = Regex.IsMatch(mergeHead, GitRevision.Sha1HashPattern);
- }
- private void InitializedStaged()
- {
- Cursor.Current = Cursors.WaitCursor;
- SolveMergeconflicts.Visible = Module.InTheMiddleOfConflictedMerge();
- Staged.GitItemStatuses = Module.GetStagedFilesWithSubmodulesStatus();
- Cursor.Current = Cursors.Default;
- }
- private event Action OnStageAreaLoaded;
- private bool _loadUnstagedOutputFirstTime = true;
- /// <summary>
- /// Loads the unstaged output.
- /// This method is passed in to the SetTextCallBack delegate
- /// to set the Text property of textBox1.
- /// </summary>
- private void LoadUnstagedOutput(IList<GitItemStatus> allChangedFiles)
- {
- var lastSelection = new List<GitItemStatus>();
- if (_currentFilesList != null)
- lastSelection = _currentSelection;
- var unStagedFiles = new List<GitItemStatus>();
- var stagedFiles = new List<GitItemStatus>();
- foreach (var fileStatus in allChangedFiles)
- {
- if (fileStatus.IsStaged)
- stagedFiles.Add(fileStatus);
- else
- unStagedFiles.Add(fileStatus);
- }
- Unstaged.GitItemStatuses = unStagedFiles;
- Staged.GitItemStatuses = stagedFiles;
- Loading.Visible = false;
- LoadingStaged.Visible = false;
- Commit.Enabled = true;
- CommitAndPush.Enabled = true;
- Amend.Enabled = true;
- Reset.Enabled = DoChangesExist();
- EnableStageButtons(true);
- workingToolStripMenuItem.Enabled = true;
- var inTheMiddleOfConflictedMerge = Module.InTheMiddleOfConflictedMerge();
- SolveMergeconflicts.Visible = inTheMiddleOfConflictedMerge;
- if (Staged.IsEmpty)
- {
- _currentFilesList = Unstaged;
- if (Staged.ContainsFocus)
- Unstaged.Focus();
- }
- else if (Unstaged.IsEmpty)
- {
- _currentFilesList = Staged;
- if (Unstaged.ContainsFocus)
- Staged.Focus();
- }
- RestoreSelectedFiles(unStagedFiles, stagedFiles, lastSelection);
- if (OnStageAreaLoaded != null)
- OnStageAreaLoaded();
- if (_loadUnstagedOutputFirstTime)
- {
- var fc = this.FindFocusedControl();
- if (fc == this.Ok)
- {
- if (Unstaged.GitItemStatuses.Any())
- Unstaged.Focus();
- else if (Staged.GitItemStatuses.Any())
- Message.Focus();
- else
- Amend.Focus();
- }
- _loadUnstagedOutputFirstTime = false;
- }
- }
- private void SelectStoredNextIndex()
- {
- Unstaged.SelectStoredNextIndex(0);
- if (Unstaged.GitItemStatuses.Any())
- {
- Staged.SelectStoredNextIndex();
- }
- else
- {
- Staged.SelectStoredNextIndex(0);
- }
- }
- private void RestoreSelectedFiles(IList<GitItemStatus> unStagedFiles, IList<GitItemStatus> stagedFiles, IList<GitItemStatus> lastSelection)
- {
- if (_currentFilesList == null || _currentFilesList.IsEmpty)
- {
- SelectStoredNextIndex();
- return;
- }
- var newItems = _currentFilesList == Staged ? stagedFiles : unStagedFiles;
- var names = lastSelection.ToHashSet(x => x.Name);
- var newSelection = newItems.Where(x => names.Contains(x.Name)).ToList();
- if (newSelection.Any())
- _currentFilesList.SelectedItems = newSelection;
- else
- SelectStoredNextIndex();
- }
- /// <summary>Returns if there are any changes at all, staged or unstaged.</summary>
- private bool DoChangesExist()
- {
- return Unstaged.AllItems.Any() || Staged.AllItems.Any();
- }
- private void ShowChanges(GitItemStatus item, bool staged)
- {
- _currentItem = item;
- _currentItemStaged = staged;
- if (item == null)
- return;
- long length = GetItemLength(item.Name);
- if (length < 5 * 1024 * 1024) // 5Mb
- SetSelectedDiff(item, staged);
- else
- {
- SelectedDiff.Clear();
- SelectedDiff.Refresh();
- llShowPreview.Show();
- }
- _stageSelectedLinesToolStripMenuItem.Text = staged ? _unstageSelectedLines.Text : _stageSelectedLines.Text;
- _stageSelectedLinesToolStripMenuItem.Image = staged ? toolUnstageItem.Image : toolStageItem.Image;
- _stageSelectedLinesToolStripMenuItem.ShortcutKeyDisplayString =
- GetShortcutKeys((int)(staged ? Commands.UnStageSelectedFile : Commands.StageSelectedFile)).ToShortcutKeyDisplayString();
- }
- private long GetItemLength(string fileName)
- {
- long length = -1;
- string path = fileName;
- if (!File.Exists(fileName))
- path = Path.Combine(Module.WorkingDir, fileName);
- if (File.Exists(path))
- {
- FileInfo fi = new FileInfo(path);
- length = fi.Length;
- }
- return length;
- }
- private void llShowPreview_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
- {
- llShowPreview.Hide();
- SetSelectedDiff(_currentItem, _currentItemStaged);
- }
- private void SetSelectedDiff(GitItemStatus item, bool staged)
- {
- if (item.Name.EndsWith(".png"))
- {
- SelectedDiff.ViewFile(item.Name);
- }
- else if (item.IsTracked)
- {
- SelectedDiff.ViewCurrentChanges(item, staged);
- }
- else
- {
- SelectedDiff.ViewFile(item.Name);
- }
- }
- private List<GitItemStatus> _currentSelection;
- private void ClearDiffViewIfNoFilesLeft()
- {
- llShowPreview.Hide();
- if (Staged.IsEmpty && Unstaged.IsEmpty)
- SelectedDiff.Clear();
- }
- private void CommitClick(object sender, EventArgs e)
- {
- ExecuteCommitCommand();
- }
- private void CheckForStagedAndCommit(bool amend, bool push)
- {
- if (amend)
- {
- // This is an amend commit. Confirm the user understands the implications. We don't want to prompt for an empty
- // commit, because amend may be used just to change the commit message or timestamp.
- if (!AppSettings.DontConfirmAmend)
- if (MessageBox.Show(this, _amendCommit.Text, _amendCommitCaption.Text, MessageBoxButtons.YesNo) != DialogResult.Yes)
- return;
- }
- else if (Staged.IsEmpty)
- {
- if (IsMergeCommit)
- {
- // it is a merge commit, so user can commit just for merging two branches even the changeset is empty,
- // but also user may forget to add files, so only ask for confirmation that user really wants to commit an empty changeset
- if (MessageBox.Show(this, _noFilesStagedAndConfirmAnEmptyMergeCommit.Text, _noStagedChanges.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
- return;
- }
- else
- {
- if (Unstaged.IsEmpty)
- {
- MessageBox.Show(this, _noFilesStagedAndNothingToCommit.Text, _noStagedChanges.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
- return;
- }
- // there are no staged files, but there are unstaged files. Most probably user forgot to stage them.
- if (MessageBox.Show(this, _noFilesStagedButSuggestToCommitAllUnstaged.Text, _noStagedChanges.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
- return;
- StageAllAccordingToFilter();
- // if staging failed (i.e. line endings conflict), user already got error message, don't try to commit empty changeset.
- if (Staged.IsEmpty)
- return;
- }
- }
- DoCommit(amend, push);
- }
- private void DoCommit(bool amend, bool push)
- {
- if (Module.InTheMiddleOfConflictedMerge())
- {
- MessageBox.Show(this, _mergeConflicts.Text, _mergeConflictsCaption.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- if (_useFormCommitMessage && (string.IsNullOrEmpty(Message.Text) || Message.Text == _commitTemplate))
- {
- MessageBox.Show(this, _enterCommitMessage.Text, _enterCommitMessageCaption.Text, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
- return;
- }
- if (_useFormCommitMessage && !ValidCommitMessage())
- return;
- if (Module.IsDetachedHead() && !Module.InTheMiddleOfRebase())
- {
- int idx = PSTaskDialog.cTaskDialog.ShowCommandBox(this,
- _notOnBranchCaption.Text,
- _notOnBranchMainInstruction.Text,
- _notOnBranch.Text,
- _notOnBranchButtons.Text,
- true);
- switch (idx)
- {
- case 0:
- string revision = _editedCommit != null ? _editedCommit.Guid : "";
- if (!UICommands.StartCheckoutBranch(this, new[] {revision}))
- return;
- break;
- case 1:
- if (!UICommands.StartCreateBranchDialog(this, _editedCommit))
- return;
- break;
- case -1:
- return;
- }
- }
- try
- {
- if (_useFormCommitMessage)
- {
- SetCommitMessageFromTextBox(Message.Text);
- }
- ScriptManager.RunEventScripts(this, ScriptEvent.BeforeCommit);
- var errorOccurred = !FormProcess.ShowDialog(this, Module.CommitCmd(amend, signOffToolStripMenuItem.Checked, toolAuthor.Text, _useFormCommitMessage, noVerifyToolStripMenuItem.Checked));
- UICommands.RepoChangedNotifier.Notify();
- if (errorOccurred)
- return;
- Amend.Checked = false;
- noVerifyToolStripMenuItem.Checked = false;
- ScriptManager.RunEventScripts(this, ScriptEvent.AfterCommit);
- Message.Text = string.Empty;
- CommitHelper.SetCommitMessage(Module, string.Empty);
- bool pushCompleted = true;
- if (push)
- {
- UICommands.StartPushDialog(this, true, out pushCompleted);
- }
- if (pushCompleted && Module.SuperprojectModule != null && AppSettings.StageInSuperprojectAfterCommit)
- Module.SuperprojectModule.StageFile(Module.SubmodulePath);
- if (AppSettings.CloseCommitDialogAfterCommit)
- {
- Close();
- return;
- }
- if (Unstaged.GitItemStatuses.Any())
- {
- InitializedStaged();
- return;
- }
- if (AppSettings.CloseCommitDialogAfterLastCommit)
- Close();
- else
- InitializedStaged();
- }
- catch (Exception e)
- {
- MessageBox.Show(this, string.Format("Exception: {0}", e.Message));
- }
- }
- private bool ValidCommitMessage()
- {
- if (AppSettings.CommitValidationMaxCntCharsFirstLine > 0)
- {
- var firstLine = Message.Text.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries)[0];
- if (firstLine.Length > AppSettings.CommitValidationMaxCntCharsFirstLine)
- {
- if (DialogResult.No == MessageBox.Show(this, _commitMsgFirstLineInvalid.Text, _commitValidationCaption.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk))
- return false;
- }
- }
- if (AppSettings.CommitValidationMaxCntCharsPerLine > 0)
- {
- var lines = Message.Text.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
- foreach (var line in lines)
- {
- if (line.Length > AppSettings.CommitValidationMaxCntCharsPerLine)
- {
- if (DialogResult.No == MessageBox.Show(this, String.Format(_commitMsgLineInvalid.Text, line), _commitValidationCaption.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk))
- return false;
- }
- }
- }
- if (AppSettings.CommitValidationSecondLineMustBeEmpty)
- {
- var lines = Message.Text.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
- if (lines.Length > 2)
- {
- if (lines[1].Length != 0)
- {
- if (DialogResult.No == MessageBox.Show(this, _commitMsgSecondLineNotEmpty.Text, _commitValidationCaption.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk))
- return false;
- }
- }
- }
- if (!AppSettings.CommitValidationRegEx.IsNullOrEmpty())
- {
- try
- {
- if (!Regex.IsMatch(Message.Text, AppSettings.CommitValidationRegEx))
- {
- if (DialogResult.No == MessageBox.Show(this, _commitMsgRegExNotMatched.Text, _commitValidationCaption.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk))
- return false;
- }
- }
- catch
- {
- }
- }
- return true;
- }
- private void RescanChanges()
- {
- if (_shouldRescanChanges)
- {
- toolRefreshItem.Enabled = false;
- Initialize();
- toolRefreshItem.Enabled = true;
- }
- }
- private void UnstageFilesClick(object sender, EventArgs e)
- {
- if (_currentFilesList != Staged)
- return;
- Unstage();
- }
- void Staged_DoubleClick(object sender, EventArgs e)
- {
- _currentFilesList = Staged;
- Unstage();
- }
- private void UnstageAllToolStripMenuItemClick(object sender, EventArgs e)
- {
- var lastSelection = new List<GitItemStatus>();
- if (_currentFilesList != null)
- lastSelection = _currentSelection;
- Action stageAreaLoaded = null;
- stageAreaLoaded = () =>
- {
- _currentFilesList = Unstaged;
- RestoreSelectedFiles(Unstaged.GitItemStatuses, Staged.GitItemStatuses, lastSelection);
- Unstaged.Focus();
- OnStageAreaLoaded -= stageAreaLoaded;
- };
- OnStageAreaLoaded += stageAreaLoaded;
- Module.ResetMixed("HEAD");
- Initialize();
- }
- private void UnstagedSelectionChanged(object sender, EventArgs e)
- {
- if (_currentFilesList != Unstaged || _skipUpdate)
- return;
- ClearDiffViewIfNoFilesLeft();
- Unstaged.ContextMenuStrip = null;
- if (!Unstaged.SelectedItems.Any())
- return;
- Staged.ClearSelected();
- _currentSelection = Unstaged.SelectedItems.ToList();
- GitItemStatus item = _currentSelection.FirstOrDefault();
- ShowChanges(item, false);
- if (!item.IsSubmodule)
- Unstaged.ContextMenuStrip = UnstagedFileContext;
- else
- Unstaged.ContextMenuStrip = UnstagedSubmoduleContext;
- }
- private void Unstaged_Enter(object sender, EventArgs e)
- {
- if (_currentFilesList != Unstaged)
- {
- _currentFilesList = Unstaged;
- _skipUpdate = false;
- UnstagedSelectionChanged(Unstaged, null);
- }
- }
- private void Unstage()
- {
- if (Staged.GitItemStatuses.Count() > 10 && Staged.SelectedItems.Count() == Staged.GitItemStatuses.Count())
- {
- UnstageAllToolStripMenuItemClick(null, null);
- return;
- }
- Cursor.Current = Cursors.WaitCursor;
- EnableStageButtons(false);
- try
- {
- var lastSelection = new List<GitItemStatus>();
- if (_currentFilesList != null)
- lastSelection = _currentSelection;
- toolStripProgressBar1.Visible = true;
- toolStripProgressBar1.Maximum = Staged.SelectedItems.Count() * 2;
- toolStripProgressBar1.Value = 0;
- Staged.StoreNextIndexToSelect();
- var files = new List<GitItemStatus>();
- var allFiles = new List<GitItemStatus>();
- foreach (var item in Staged.SelectedItems)
- {
- toolStripProgressBar1.Value = Math.Min(toolStripProgressBar1.Maximum - 1, toolStripProgressBar1.Value + 1);
- if (!item.IsNew)
- {
- toolStripProgressBar1.Value = Math.Min(toolStripProgressBar1.Maximum - 1, toolStripProgressBar1.Value + 1);
- Module.UnstageFileToRemove(item.Name);
- if (item.IsRenamed)
- Module.UnstageFileToRemove(item.OldName);
- }
- else
- {
- files.Add(item);
- }
- allFiles.Add(item);
- }
- Module.UnstageFiles(files);
- _skipUpdate = true;
- InitializedStaged();
- var stagedFiles = Staged.GitItemStatuses.ToList();
- var unStagedFiles = Unstaged.GitItemStatuses.ToList();
- foreach (var item in allFiles)
- {
- var item1 = item;
- if (stagedFiles.Exists(i => i.Name == item1.Name))
- continue;
- var item2 = item;
- if (unStagedFiles.Exists(i => i.Name == item2.Name))
- continue;
- if (item.IsNew && !item.IsChanged && !item.IsDeleted)
- item.IsTracked = false;
- else
- item.IsTracked = true;
- if (item.IsRenamed)
- {
- var clone = new GitItemStatus
- {
- Name = item.OldName,
- IsDeleted = true,
- IsTracked = true,
- IsStaged = false
- };
- unStagedFiles.Add(clone);
- item.IsRenamed = false;
- item.IsNew = true;
- item.IsTracked = false;
- item.OldName = string.Empty;
- }
- item.IsStaged = false;
- unStagedFiles.Add(item);
- }
- Staged.GitItemStatuses = stagedFiles;
- Unstaged.GitItemStatuses = unStagedFiles;
- _skipUpdate = false;
- Staged.SelectStoredNextIndex();
- toolStripProgressBar1.Value = toolStripProgressBar1.Maximum;
- toolStripProgressBar1.Visible = false;
- if (Staged.IsEmpty)
- {
- _currentFilesList = Unstaged;
- RestoreSelectedFiles(Unstaged.GitItemStatuses, Staged.GitItemStatuses, lastSelection);
- Unstaged.Focus();
- }
- }
- catch (Exception ex)
- {
- Trace.WriteLine(ex.Message);
- }
- EnableStageButtons(true);
- Cursor.Current = Cursors.Default;
- if (AppSettings.RevisionGraphShowWorkingDirChanges)
- UICommands.RepoChangedNotifier.Notify();
- }
- private void StageClick(object sender, EventArgs e)
- {
- if (_currentFilesList != Unstaged)
- return;
- Stage(Unstaged.SelectedItems.ToList());
- if (U…
Large files files are truncated, but you can click here to view the full file