private bool SimpleLFSInvoke(string args, StdCallbackMethod stdCallback = null, StdCallbackMethod stdErrorCallback = null) { var result = repository.RunExe("git", "lfs " + args, stdCallback: stdCallback, stdErrorCallback: stdErrorCallback); repository.lastResult = result.output; repository.lastError = result.errors; return(string.IsNullOrEmpty(repository.lastError)); }
private bool SimpleGitInvoke(string args, StdCallbackMethod stdCallback = null, StdCallbackMethod stdErrorCallback = null) { lock (this) { var result = RunExe("git", args, stdCallback: stdCallback, stdErrorCallback: stdErrorCallback); lastResult = result.output; lastError = result.errors; return(string.IsNullOrEmpty(lastError)); } }
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); }