internal static async Task <GitActionResult <string> > Commit(GitRepository repository, string message, bool signoff = false) { var result = new GitActionResult <string>(); SolutionExtensions.WriteMessageToOutputPane("Commiting"); if (String.IsNullOrWhiteSpace(message)) { result.Succeeded = false; result.ErrorMessage = ErrorMessages.CommitMissingComment; SolutionExtensions.WriteMessageToOutputPane(ErrorMessages.CommitMissingComment); return(result); } var stagedCount = repository?.ChangedFiles.Count(f => f.IsStaged) ?? 0; if (stagedCount <= 0) { result.Succeeded = false; result.ErrorMessage = ErrorMessages.CommitNoFilesStaged; SolutionExtensions.WriteMessageToOutputPane(ErrorMessages.CommitNoFilesStaged); return(result); } result = repository?.Commit(message, false, signoff); if (result.Succeeded) { SolutionExtensions.WriteMessageToOutputPane("Commit successfully. Commit Hash: " + result.Item); } return(result); }
private void RepoOnChangeEnd(RepositoryWatcher repositoryWatcher, GitRepository gitRepository) { Console.WriteLine("Changed End."); UpdateMenu(); gitRepository.AddAll(); gitRepository.Commit(); }
public static int ChangeModule(Package package, string moduleName, string pushUrl, string fetchUrl) { using (var tempDir = new TempDirectory()) { var repo = new GitRepository("modules_git", tempDir.Path, Log); repo.Clone(package.Url); var toChange = FindModule(repo, moduleName); if (toChange == null) { ConsoleWriter.WriteError("Unable to find module " + moduleName + " in package " + package.Name); return(-1); } if (toChange.Url == fetchUrl && toChange.Pushurl == pushUrl) { ConsoleWriter.WriteInfo("Your changes were already made"); return(0); } ChangeModuleDescription(repo, toChange, new Module(moduleName, fetchUrl, pushUrl)); var message = "(!)cement comment: changed module '" + moduleName + "'"; repo.Commit(new[] { "-am", message }); repo.Push("master"); } ConsoleWriter.WriteOk("Success changed " + moduleName + " in " + package.Name); return(0); }
public static int AddModule(Package package, string moduleName, string pushUrl, string fetchUrl) { if (fetchUrl.StartsWith("https://git.skbkontur.ru/")) { throw new CementException("HTTPS url not allowed for gitlab. You should use SSH url."); } using (var tempDir = new TempDirectory()) { var repo = new GitRepository("modules_git", tempDir.Path, Log); repo.Clone(package.Url); if (FindModule(repo, moduleName) != null) { ConsoleWriter.WriteError("Module " + moduleName + " already exists in " + package.Name); return(-1); } WriteModuleDescription(moduleName, pushUrl, fetchUrl, repo); var message = "(!)cement comment: added module '" + moduleName + "'"; repo.Commit(new[] { "-am", message }); repo.Push("master"); } ConsoleWriter.WriteOk("Success added " + moduleName + " to " + package.Name); return(0); }
public void AmendCommitTest() { GitRepository.Init(tempFolder); File.WriteAllLines(tempFilePath, lines); GitRepository tracker = new GitRepository(tempFolder); tracker.StageFile(tempFile); tracker.Commit("中文 1čtestč"); Assert.IsTrue(tracker.LastCommitMessage.Equals("中文 1čtestč")); File.WriteAllText(tempFile, "changed text"); tracker.StageFile(tempFile); tracker.Commit("new message", true); Assert.IsTrue(tracker.LastCommitMessage.Equals("new message")); }
public void LastCommitMessageTest() { GitRepository.Init(tempFolder); File.WriteAllLines(tempFilePath, lines); GitRepository tracker = new GitRepository(tempFolder); tracker.StageFile(tempFile); tracker.Commit("中文 1čtestč"); Assert.IsTrue(tracker.LastCommitMessage.Equals("中文 1čtestč")); }
public void DiffFileTest() { GitRepository.Init(tempFolder); File.WriteAllLines(tempFilePath, lines); GitRepository tracker = new GitRepository(tempFolder); tracker.StageFile(tempFile); tracker.Commit("test message"); File.WriteAllText(tempFilePath, "changed text"); var diffFile = tracker.DiffFile(tempFile); var diff = File.ReadAllText(diffFile); Assert.IsTrue(diff.Contains("@@ -1,3 +1 @@")); }
public void GetFileStatusTest() { GitRepository.Init(tempFolder); GitRepository tracker = new GitRepository(tempFolder); File.WriteAllLines(tempFilePath, lines); Assert.AreEqual(GitFileStatus.New, tracker.GetFileStatus(tempFile)); tracker.StageFile(tempFile); tracker.Refresh(); Assert.AreEqual(GitFileStatus.Added, tracker.GetFileStatus(tempFile)); tracker.UnStageFile(tempFile); tracker.Refresh(); Assert.AreEqual(GitFileStatus.New, tracker.GetFileStatus(tempFile)); tracker.StageFile(tempFile); tracker.Refresh(); Assert.AreEqual(GitFileStatus.Added, tracker.GetFileStatus(tempFile)); tracker.Commit("中文 1čtestč"); tracker.Refresh(); Assert.AreEqual(GitFileStatus.Tracked, tracker.GetFileStatus(tempFile)); File.WriteAllText(tempFilePath, "changed text"); tracker.Refresh(); Assert.AreEqual(GitFileStatus.Modified, tracker.GetFileStatus(tempFile)); tracker.StageFile(tempFile); tracker.Refresh(); Assert.AreEqual(GitFileStatus.Staged, tracker.GetFileStatus(tempFile)); tracker.UnStageFile(tempFile); tracker.Refresh(); Assert.AreEqual(GitFileStatus.Modified, tracker.GetFileStatus(tempFile)); File.Delete(tempFilePath); tracker.Refresh(); Assert.AreEqual(GitFileStatus.Deleted, tracker.GetFileStatus(tempFile)); tracker.StageFile(tempFile); tracker.Refresh(); Assert.AreEqual(GitFileStatus.Removed, tracker.GetFileStatus(tempFile)); tracker.UnStageFile(tempFile); tracker.Refresh(); Assert.AreEqual(GitFileStatus.Deleted, tracker.GetFileStatus(tempFile)); }
public void GetChangedFilesTest() { GitRepository.Init(tempFolder); File.WriteAllLines(tempFilePath, lines); GitRepository tracker = new GitRepository(tempFolder); tracker.StageFile(tempFile); tracker.Refresh(); Assert.AreEqual(GitFileStatus.Added, tracker.GetFileStatus(tempFile)); tracker.Commit("中文 1čtestč"); tracker.Refresh(); Assert.AreEqual(0, tracker.ChangedFiles.Count()); File.WriteAllText(tempFilePath, "a"); tracker.Refresh(); Assert.AreEqual(GitFileStatus.Modified, tracker.GetFileStatus(tempFile)); }
/// <summary> /// Commits the file. /// </summary> /// <param name="folderPath">Root folder path</param> /// <param name="branchId">The target branch identifier.</param> /// <param name="message">The commit message.</param> /// <param name="fileName">Local name of the file.</param> /// <param name="fileBody">The file body.</param> public void Commit(string folderPath, string branchId, string message, string fileName, byte[] fileBody) { var branchPath = Path.Combine(folderPath, branchId); var filePath = Path.Combine(branchPath, fileName); File.WriteAllBytes(filePath, fileBody); var gitRepo = new GitRepository(branchPath); gitRepo.Commit(new TakeCommit(branchId, fileName, GetSetting(GitSettingKeys.Email)) { Message = message, Username = GetSetting(GitSettingKeys.Username), Password = GetSetting(GitSettingKeys.Password) }); }
public void CheckOutFileTest() { GitRepository.Init(tempFolder); File.WriteAllLines(tempFilePath, lines); GitRepository tracker = new GitRepository(tempFolder); tracker.StageFile(tempFile); tracker.Commit("test"); File.WriteAllText(tempFilePath, "changed text"); tracker.CheckOutFile(tempFile); var newlines = File.ReadAllLines(tempFilePath); Assert.AreEqual(lines[0], newlines[0]); Assert.AreEqual(lines[1], newlines[1]); Assert.AreEqual(lines[2], newlines[2]); }
public void SaveFileFromRepositoryTest() { GitRepository.Init(tempFolder); File.WriteAllLines(tempFilePath, lines); GitRepository tracker = new GitRepository(tempFolder); tracker.StageFile(tempFile); tracker.Commit("test"); var tmp = Path.Combine(Path.GetTempPath(), tempFile) + ".bk"; tracker.SaveFileFromLastCommit(tempFile, tmp); var newlines = File.ReadAllLines(tmp); Assert.AreEqual(lines[0], newlines[0]); Assert.AreEqual(lines[1], newlines[1]); Assert.AreEqual(lines[2], newlines[2]); }
public void FileNameCaseTest() { GitRepository.Init(tempFolder); File.WriteAllLines(tempFilePath, lines); GitRepository tracker = new GitRepository(tempFolder); tracker.StageFile(tempFile); tracker.Commit("test message"); Assert.IsTrue(tracker.LastCommitMessage.StartsWith("test message")); tempFile = tempFile.Replace("test", "TEST"); File.WriteAllText(tempFilePath, "changed text"); tracker.Refresh(); //This test fails all cases because status check uses ngit, never git.exe //Assert.AreEqual(GitFileStatus.Modified, tracker.GetFileStatus(tempFile)); var file = tracker.ChangedFiles.First(); Assert.AreEqual(GitFileStatus.Modified, file.Status); }
internal static async Task <GitActionResult <string> > AmendCommit(GitRepository repository, string message, bool signoff = false) { var result = new GitActionResult <string>(); SolutionExtensions.WriteMessageToOutputPane("Amending Commiti"); if (String.IsNullOrWhiteSpace(message)) { result.Succeeded = false; result.ErrorMessage = ErrorMessages.CommitMissingComment; SolutionExtensions.WriteMessageToOutputPane(ErrorMessages.CommitMissingComment); return(result); } result = repository?.Commit(message, true, signoff); if (result.Succeeded) { SolutionExtensions.WriteMessageToOutputPane("Amend last commit successfully. Commit Hash: " + result.Item); } return(result); }
public void GetBranchTest() { GitRepository.Init(tempFolder); File.WriteAllLines(tempFilePath, lines); GitRepository tracker = new GitRepository(tempFolder); Assert.AreEqual("master", tracker.CurrentBranch); tracker.StageFile(tempFile); Assert.AreEqual("master", tracker.CurrentBranch); tracker.Commit("test message"); Assert.AreEqual("master", tracker.CurrentBranch); tempFile = tempFile.Replace("test", "TEST"); File.WriteAllText(tempFilePath, "changed text"); tracker.CheckOutBranch("dev", true); Assert.AreEqual("dev", tracker.CurrentBranch); }
internal void OnCommit() { if (tracker == null) { return; } try { //service.NoRefresh = true; //if (chkNewBranch.IsChecked == true) //{ // if (string.IsNullOrWhiteSpace(txtNewBranch.Text)) // { // MessageBox.Show("Please enter new branch name.", "Commit", // MessageBoxButton.OK, MessageBoxImage.Exclamation); // txtNewBranch.Focus(); // return; // } // tracker.CheckOutBranch(txtNewBranch.Text, true); //} var isAmend = chkAmend.IsChecked == true; if (string.IsNullOrWhiteSpace(Comments)) { MessageBox.Show("Please enter comments for the commit.", "Commit", MessageBoxButton.OK, MessageBoxImage.Exclamation); return; } ShowStatusMessage("Staging files ..."); StageSelectedFiles(); if (!isAmend) { tracker.Refresh(); bool hasStaged = tracker == null ? false : tracker.ChangedFiles.Any(f => f.IsStaged); if (!hasStaged) { MessageBox.Show("No file has been selected/staged for commit.", "Commit", MessageBoxButton.OK, MessageBoxImage.Exclamation); return; } } else { const string amendMsg = @"You are about to amend a commit that has tags or remotes, which could cause issues in local and remote repositories. Are you sure you want to continue?"; if (tracker.CurrentCommitHasRefs() && MessageBox.Show(amendMsg, "Amend Last Commit", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.No) { return; } } var id = tracker.Commit(Comments, isAmend, chkSignOff.IsChecked == true); ShowStatusMessage("Commit successfully. Commit Hash: " + id); ClearUI(); //service.NoRefresh = false; tracker.Refresh(); toolWindow.Refresh(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation); ShowStatusMessage(ex.Message); } }
internal async Task OnCommit() { if (tracker == null) { return; } try { var isAmend = chkAmend.IsChecked == true; if (string.IsNullOrWhiteSpace(Comments)) { Comments = tracker.GetCommitTemplate(); if (string.IsNullOrWhiteSpace(Comments)) { MessageBox.Show("Please enter comments for the commit.", "Commit", MessageBoxButton.OK, MessageBoxImage.Exclamation); } return; } var unstaged = this.listView1.Items.Cast <GitFile>() .Where(item => item.IsSelected && !item.IsStaged); var count = unstaged.Count(); var advancedMode = this.chkAdvMode.IsChecked == true; var changed = this.listUnstaged.ItemsSource.Cast <GitFile>(); ShowStatusMessage("Staging files ..."); if (!isAmend) { tracker.Refresh(); bool hasStaged = false; if (advancedMode) { // advanced mode hasStaged = tracker == null ? false : tracker.ChangedFiles.Any(f => f.X != ' ') || count > 0; // if nothing staged, staged to be all changes if (!hasStaged) { hasStaged = changed.Count() > 0; } } else { // simple mode hasStaged = tracker == null ? false : tracker.ChangedFiles.Any(f => f.IsStaged) || count > 0; } if (!hasStaged) { MessageBox.Show("No file has been selected/staged for commit.", "Commit", MessageBoxButton.OK, MessageBoxImage.Exclamation); return; } } else { const string amendMsg = @"You are about to amend a commit that has tags or remotes, which could cause issues in local and remote repositories. Are you sure you want to continue?"; if (tracker.CurrentCommitHasRefs() && MessageBox.Show(amendMsg, "Amend Last Commit", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.No) { return; } } this.toolWindow.Service.NoRefresh = true; int i = 1; bool signoff = chkSignOff.IsChecked == true; await Task.Run(() => { if (advancedMode && changed.Count() > 0 && listStaged.Items.Count == 0) { count = changed.Count(); // auto stage all changes if nothing is staged foreach (var item in changed) { tracker.StageFile(item.FileName); ShowStatusMessage(string.Format("Staged ({0}/{1}): {2}", i++, count, item.FileName)); } } else { foreach (var item in unstaged) { tracker.StageFile(item.FileName); ShowStatusMessage(string.Format("Staged ({0}/{1}): {2}", i++, count, item.FileName)); } } var id = tracker.Commit(Comments, isAmend, signoff); ShowStatusMessage("Commit successfully. Commit Hash: " + id); }); ClearUI(); Comments = tracker.GetCommitTemplate(); this.toolWindow.Service.NoRefresh = false; await this.toolWindow.Service.RefreshToolWindows(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation); ShowStatusMessage(ex.Message); } }