public static ExecResult Run(string gitcmd, bool async) { // Pick up git commands that take long time to execute and run them // using a threaded execution if (gitcmd.StartsWith("clone --progress") || gitcmd.StartsWith("fetch") || gitcmd.StartsWith("pull ") || gitcmd.StartsWith("push ")) { FormGitRun formGitRun = new FormGitRun(Properties.Settings.Default.GitPath, gitcmd); formGitRun.ShowDialog(); return(formGitRun.GetResult()); } if (!async) { return(Exec.Run(Properties.Settings.Default.GitPath, gitcmd)); } var job = new Exec(Properties.Settings.Default.GitPath, gitcmd); job.AsyncRun(s => App.PrintStatusMessage(s, MessageType.Output), s => App.PrintStatusMessage(s, MessageType.Error), null); return(new ExecResult()); }
/// <summary> /// Constructor creates a shell executable file that echoes the PASSWORD /// environment variable when called. When GIT_ASKPASS is present, Git /// obtains a password for its HTTPS operations by calling it. /// </summary> public ClassGitPasswd() { // WAR: Do a different kind of shell script dependent on the OS) if (ClassUtils.IsMono()) { // Mono: Use the Shell script pathPasswordBatchHelper = Path.Combine(App.AppHome, "passwd.sh"); File.WriteAllText(pathPasswordBatchHelper, "echo $PASSWORD" + Environment.NewLine); // Set the execute bit if (Exec.Run("chmod", "+x " + pathPasswordBatchHelper).Success() == false) { App.PrintLogMessage("ClassGitPasswd: Unable to chmod +x on " + pathPasswordBatchHelper, MessageType.Error); } } else { // Windows: Use the CMD BAT script // Note: Using "App.AppHome" directory to host the batch helper file // fails on XP where that directory has spaces in the name ("Documents and Settings") // which git cannot handle in this context. Similarly, git will fail with // any other path that contains a space. // This redirection is used to provide the password in an automated way. pathPasswordBatchHelper = Path.Combine(Path.GetTempPath(), "passwd.bat"); File.WriteAllText(pathPasswordBatchHelper, "@echo %PASSWORD%" + Environment.NewLine); pathPasswordBatchHelper = ClassUtils.GetShortPathName(pathPasswordBatchHelper); } ClassUtils.AddEnvar("GIT_ASKPASS", pathPasswordBatchHelper); App.PrintLogMessage("Created HTTP password helper file: " + pathPasswordBatchHelper, MessageType.General); }
/// <summary> /// Checks the path to git and checks that the git executable is functional. This call /// should be made only once upon the program start. /// It returns true if git executable can be run, false otherwise. /// </summary> public bool Initialize() { bool retValue = true; // Check that we have a functional version of git at an already set path gitPath = Properties.Settings.Default.GitPath; if (Exec.Run(gitPath, "--version").stdout.Contains("git version") == false) { // If we are running on Linux, get the git path by 'which' command if (ClassUtils.IsMono()) { gitPath = Exec.Run("which", "git").stdout.Trim(); if (Exec.Run(gitPath, "--version").stdout.Contains("git version") == false) { MessageBox.Show( "Could not locate 'git'!\n\nPlease install git by running 'sudo apt-get install git'\nMake sure it is on your path, then rerun this application.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); retValue = false; } } else { // Check if a version of git is installed at a known location (or guess a location) string programFilesPath = Environment.GetEnvironmentVariable("PROGRAMFILES(X86)") ?? Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles); // If a git executable does not exist at the default location, try the 64-bit program file folder instead if (!File.Exists(programFilesPath) && programFilesPath.Contains(" (x86)")) { programFilesPath = programFilesPath.Replace(" (x86)", ""); } gitPath = Path.Combine(programFilesPath, @"Git\bin\git.exe"); if (Exec.Run(gitPath, "--version").stdout.Contains("git version") == false) { // Ask user to show us where the git is installed FormPathToGit formPath = new FormPathToGit(programFilesPath, gitPath); while (retValue = (formPath.ShowDialog() == DialogResult.OK)) { gitPath = formPath.PathToGit; if (Exec.Run(gitPath, "--version").stdout.Contains("git version")) { break; } } } } } if (retValue) { // Run the version again to get the version code (for simplicity did not save it earlier) string version = string.Format("Using {0} at {1}", Exec.Run(gitPath, "--version"), gitPath); App.PrintLogMessage(version, MessageType.General); Properties.Settings.Default.GitPath = gitPath; } return(retValue); }
/// <summary> /// Initializes SSH connection by running the PLINK using the specified /// connection parameters. This function blocks until the PLINK returns. /// </summary> public void ImportRemoteSshKey(ClassUrl.Url url) { // Build the arguments to the PLINK process: port number, user and the host // Use the default SSH values if the url did not provide them string args = " -P " + (url.Port > 0 ? url.Port.ToString() : "22") + " -l " + (string.IsNullOrEmpty(url.User) ? "anonymous" : url.User) + " " + url.Host; // plink does everything through its stderr stream ExecResult result = Exec.Run(pathPlink, args); App.PrintLogMessage(result.stderr, MessageType.Error); }
/// <summary> /// Executes a shell command /// </summary> public static string ExecuteShellCommand(string cmd, string args) { App.PrintStatusMessage("Shell execute: " + cmd + " " + args, MessageType.Command); // WAR: Shell execute is platform-specific if (IsMono()) { return(Exec.Run(cmd, args).ToString()); } else { args = "/c " + cmd + " " + args; return(Exec.Run("cmd.exe", args).ToString()); } }
/// <summary> /// User clicked on the Install button.. We need to find the latest msysgit build, /// download it and run it /// </summary> private void BtInstallClick(object sender, EventArgs e) { string installerFile = Path.GetTempFileName(); // Junk name so we can safely call 'Delete' try { // msysgit is hosted at https://github.com/msysgit/msysgit/releases // and the files can be downloaded at the subfolder 'download': FormDownload msysgit = new FormDownload("Download msysgit", @"https://github.com/msysgit/msysgit/releases", @"(?<file>Git-[1-2]+.[0-9]+.[0-9]+-\S+.exe)", "/download/"); // If the download succeeded, run the installer file if (msysgit.ShowDialog() == DialogResult.OK) { installerFile = msysgit.TargetFile; ExecResult ret = Exec.Run(installerFile, String.Empty); if (ret.retcode == 0) { // Check if the git executable is at the expected location now if (File.Exists(gitPath)) { PathToGit = textBoxPath.Text = gitPath; } } DialogResult = DialogResult.OK; } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); } finally { // Make sure we don't leave temporary files around File.Delete(installerFile); } }