public bool Clone(string url, string path, out string repoClonedPath, StdInputStreamCallbackMethod writeUsernameCallback, StdInputStreamCallbackMethod writePasswordCallback) { lock (this) { StreamWriter stdInWriter = null; void getStdInputStreamCallback(StreamWriter writer) { stdInWriter = writer; } string repoClonedPathTemp = null; void stdCallback(string line) { if (line.StartsWith("Cloning into")) { var match = Regex.Match(line, @"Cloning into '(.*)'\.\.\."); if (match.Success) { repoClonedPathTemp = match.Groups[1].Value; } } } void stdErrorCallback(string line) { if (line.StartsWith("Username for")) { if (writeUsernameCallback == null || !writeUsernameCallback(stdInWriter)) { stdInWriter.WriteLine(""); } } else if (line.StartsWith("Password for")) { if (writePasswordCallback == null || !writePasswordCallback(stdInWriter)) { stdInWriter.WriteLine(""); } } } var result = RunExe("git", string.Format("clone \"{0}\"", url), workingDirectory: path, getStdInputStreamCallback: getStdInputStreamCallback, stdCallback: stdCallback, stdErrorCallback: stdErrorCallback); lastResult = result.output; lastError = result.errors; repoClonedPath = repoClonedPathTemp; return(string.IsNullOrEmpty(lastError)); } }
public bool Clone(string url, string destination, out string repoPath, StdInputStreamCallbackMethod writeUsernameCallback, StdInputStreamCallbackMethod writePasswordCallback) { lock (this) { try { // clone if (!repository.Clone(url, destination, out repoPath, writeUsernameCallback, writePasswordCallback)) { throw new Exception(repository.lastError); } repoPath = Path.Combine(destination, repoPath); return(true); } catch (Exception e) { DebugLog.LogError("Clone error: " + e.Message); repoPath = null; return(false); } } }
private (string output, string errors) RunExe ( string exe, string arguments, string workingDirectory = null, StdInputStreamCallbackMethod stdInputStreamCallback = null, GetStdInputStreamCallbackMethod getStdInputStreamCallback = null, GetStdOutputStreamCallbackMethod getStdOutputStreamCallback = null, StdCallbackMethod stdCallback = null, StdCallbackMethod stdErrorCallback = null, bool stdResultOn = true, bool stdErrorResultOn = true, string stdOutToFilePath = null, Stream stdOutToStream = null ) { if (RunExeDebugLineCallback != null) { RunExeDebugLineCallback(string.Format("GitCommander: {0} {1}", exe, arguments)); } if (stdCallback != null) { stdResultOn = false; } string output = "", errors = ""; using (var process = new Process()) { // setup start info process.StartInfo.FileName = exe; process.StartInfo.Arguments = arguments; process.StartInfo.WorkingDirectory = workingDirectory == null ? repoPath : workingDirectory; process.StartInfo.RedirectStandardInput = stdInputStreamCallback != null || getStdInputStreamCallback != null; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.StartInfo.CreateNoWindow = true; process.StartInfo.UseShellExecute = false; process.StartInfo.StandardOutputEncoding = Encoding.UTF8; process.StartInfo.StandardErrorEncoding = Encoding.UTF8; var outDataReceived = new StdCallbackMethod(delegate(string line) { if (stdResultOn) { if (output.Length != 0) { output += Environment.NewLine; } output += line; } if (stdCallback != null) { stdCallback(line); } if (line.StartsWith("warning:")) { if (StdWarningCallback != null) { StdWarningCallback(line); } } else { if (StdCallback != null) { StdCallback(line); } } }); process.OutputDataReceived += delegate(object sender, DataReceivedEventArgs e) { string line = e.Data; if (line == null) { return; } outDataReceived(line); }; process.ErrorDataReceived += delegate(object sender, DataReceivedEventArgs e) { string line = e.Data; if (line == null) { return; } // valid true error string lineLower = line.ToLower(); bool isError = false; foreach (var prefix in errorPrefixes) { if (lineLower.StartsWith(prefix)) { isError = true; break; } } // if not error use normal stdout callbacks if (!isError) { outDataReceived(line); return; } // invoke error callbacks if (stdErrorResultOn) { if (errors.Length != 0) { errors += Environment.NewLine; } errors += line; } if (stdErrorCallback != null) { stdErrorCallback(line); } if (StdErrorCallback != null) { StdErrorCallback(line); } }; // start process process.Start(); // return streams if (getStdInputStreamCallback != null) { getStdInputStreamCallback(process.StandardInput); } if (getStdOutputStreamCallback != null) { getStdOutputStreamCallback(process.StandardOutput); } // begin line reads process.BeginErrorReadLine(); if (stdOutToFilePath == null && stdOutToStream == null && getStdOutputStreamCallback == null) { process.BeginOutputReadLine(); } // write input if (stdInputStreamCallback != null) { while (!stdInputStreamCallback(process.StandardInput)) { Thread.Sleep(1); } process.StandardInput.Flush(); process.StandardInput.Close(); } // write output if (getStdOutputStreamCallback == null) { if (stdOutToFilePath != null) { stdOutToFilePath = Path.Combine(repoPath, stdOutToFilePath); using (var stdOutStream = new FileStream(stdOutToFilePath, FileMode.Create, FileAccess.Write, FileShare.None)) { process.StandardOutput.BaseStream.CopyTo(stdOutStream); process.StandardOutput.BaseStream.Flush(); stdOutStream.Flush(); } } else if (stdOutToStream != null) { process.StandardOutput.BaseStream.CopyTo(stdOutToStream); process.StandardOutput.BaseStream.Flush(); stdOutToStream.Flush(); } } // wait for process to finish process.WaitForExit(); } return(output, errors); }