public bool Checkout(Api.Git.Repository repository, string branchName) { using (var repo = new LibGit2Sharp.Repository(repository.Path)) { Branch branch; // Check if local branch exists if (repo.Branches.Any(b => b.FriendlyName == branchName)) { branch = Commands.Checkout(repo, branchName); } else { // Create local branch to remote branch tip and set its upstream branch to remote var upstreamBranch = repo.Branches.FirstOrDefault(b => string.Equals(b.UpstreamBranchCanonicalName, "refs/heads/" + branchName, StringComparison.OrdinalIgnoreCase)); if (upstreamBranch is null) { return(false); } _ = repo.CreateBranch(branchName, upstreamBranch.Tip); this.SetUpstream(repository, branchName, upstreamBranch.FriendlyName); branch = Commands.Checkout(repo, branchName); } return(branch.FriendlyName == branchName); } }
public bool Checkout(Api.Git.Repository repository, string branchName) { using (var repo = new LibGit2Sharp.Repository(repository.Path)) { string realBranchName = branchName; Branch branch; // Check if local branch exists if (repo.Branches.Any(b => b.FriendlyName == branchName)) { branch = Commands.Checkout(repo, branchName); } else { // Create local branch to remote branch tip and set its upstream branch to remote var upstreamBranch = repo.Branches.FirstOrDefault(b => b.FriendlyName.EndsWith(branchName)); branch = repo.CreateBranch(branchName, upstreamBranch.Tip); this.SetUpstream(repository, branchName, upstreamBranch.FriendlyName); branch = Commands.Checkout(repo, branchName); } return(branch.FriendlyName == branchName); } }
/// <summary> /// Runs the given git command, and returns a reader for STDOUT. NOTE: The returned value MUST be disposed! /// </summary> public TextReader CommandOutputPipe(Api.Git.Repository repository, params string[] command) { AssertValidCommand(command); var process = Start(repository, command, RedirectStdout); return(new ProcessStdoutReader(this, process)); }
/// <summary> /// Runs the given git command, and returns the contents of its STDOUT. /// </summary> public string Command(Api.Git.Repository repository, params string[] command) { string retVal = null; CommandOutputPipe(repository, stdout => retVal = stdout.ReadToEnd(), command); return(retVal); }
/// <summary> /// Runs the given git command, and returns the first line of its STDOUT. /// </summary> public string CommandOneline(Api.Git.Repository repository, params string[] command) { string retVal = null; CommandOutputPipe(repository, output => retVal = output, command); return(retVal); }
private Api.Git.Repository ReadRepositoryWithRetries(string repoPath, int maxRetries) { Api.Git.Repository repository = null; int currentTry = 1; while (repository == null && currentTry <= maxRetries) { try { repository = ReadRepositoryInternal(repoPath); } catch (LockedFileException) { if (currentTry >= maxRetries) { throw; } else { System.Threading.Thread.Sleep(500); } } currentTry++; } return(repository); }
public bool Checkout(Api.Git.Repository repository, string branchName) { using (var repo = new LibGit2Sharp.Repository(repository.Path)) { var branch = Commands.Checkout(repo, branchName); return(branch.FriendlyName == branchName); } }
public void Pull(Api.Git.Repository repository) { var arguments = _appSettingsService.PruneOnFetch ? new string[] { "pull", "--prune" } : new string[] { "pull", }; _gitCommander.Command(repository, arguments); }
/// <summary> /// Runs the given git command, and redirects STDOUT to the provided action. /// </summary> public void CommandOutputPipe(Api.Git.Repository repository, Action <string> handleOutput, params string[] command) { Time(command, () => { AssertValidCommand(command); var output = Start(repository, command, RedirectStdout); handleOutput(output); }); }
public void CommandInputOutputPipe(Api.Git.Repository repository, Action <TextWriter, TextReader> interact, params string[] command) { Time(command, () => { AssertValidCommand(command); var process = Start(repository, command, And <ProcessStartInfo>(RedirectStdin, RedirectStdout)); interact(NewStreamWithEncoding(process.StandardInput, _encoding), process.StandardOutput); Close(process); }); }
/// <summary> /// Runs the given git command, and redirects STDOUT to the provided action. /// </summary> public void CommandOutputPipe(Api.Git.Repository repository, Action <TextReader> handleOutput, params string[] command) { Time(command, () => { AssertValidCommand(command); var process = Start(repository, command, RedirectStdout); handleOutput(process.StandardOutput); Close(process); }); }
public void CommandInputPipe(Api.Git.Repository repository, Action <TextWriter> action, params string[] command) { Time(command, () => { AssertValidCommand(command); var process = Start(repository, command, RedirectStdin); action(NewStreamWithEncoding(process.StandardInput, _encoding)); Close(process); }); }
protected virtual GitProcess Start(Api.Git.Repository repository, string[] command, Action <ProcessStartInfo> initialize) { var startInfo = new ProcessStartInfo(); startInfo.FileName = "git"; startInfo.WorkingDirectory = repository.Path; SetArguments(startInfo, command); startInfo.CreateNoWindow = true; startInfo.UseShellExecute = false; startInfo.EnvironmentVariables["GIT_PAGER"] = "cat"; RedirectStderr(startInfo); initialize(startInfo); Trace.WriteLine("Starting process: " + startInfo.FileName + " " + startInfo.Arguments + " on " + repository.Name, "git command"); var process = new GitProcess(Process.Start(startInfo)); process.ConsumeStandardError(); return(process); }
private void SetUpstream(Api.Git.Repository repository, string localBranchName, string upstreamBranchName) { _gitCommander.Command(repository, "branch", $"--set-upstream-to={upstreamBranchName}", localBranchName); }
public void Push(Api.Git.Repository repository) { _gitCommander.Command(repository, "push"); }
private GitProcess Start(Api.Git.Repository repository, string[] command) { return(Start(repository, command, x => { })); }
public void Fetch(Api.Git.Repository repository) { _gitCommander.Command(repository, "fetch"); }
/// <summary> /// Runs the given git command, and passes STDOUT through to the current process's STDOUT. /// </summary> public void CommandNoisy(Api.Git.Repository repository, params string[] command) { CommandOutputPipe(repository, output => Trace.TraceInformation(output), command); }
protected virtual string Start(Api.Git.Repository repository, string[] command, Action <ProcessStartInfo> initialize) { var timeout = (int)TimeSpan.FromSeconds(10).TotalMilliseconds; var psi = new ProcessStartInfo(); psi.FileName = "git"; psi.WorkingDirectory = repository.Path; SetArguments(psi, command); psi.CreateNoWindow = true; psi.UseShellExecute = false; psi.EnvironmentVariables["GIT_PAGER"] = "cat"; RedirectStderr(psi); initialize(psi); var output = new StringBuilder(); var error = new StringBuilder(); using (var outputWaitHandle = new AutoResetEvent(initialState: false)) using (var errorWaitHandle = new AutoResetEvent(initialState: false)) using (var process = new Process()) { process.StartInfo = psi; process.OutputDataReceived += (sender, e) => { if (e.Data == null) { try { outputWaitHandle.Set(); } catch (ObjectDisposedException) { // if the wait handle was disposed, // we can ignore the call to .Set() } } else { output.AppendLine(e.Data); } }; process.ErrorDataReceived += (sender, e) => { if (e.Data == null) { try { errorWaitHandle.Set(); } catch (ObjectDisposedException) { // if the wait handle was disposed, // we can ignore the call to .Set() } } else { error.AppendLine(e.Data); } }; try { process.Start(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); if (process.WaitForExit(timeout) && outputWaitHandle.WaitOne(timeout) && errorWaitHandle.WaitOne(timeout)) { // Process completed. Check process.ExitCode here. return(output.ToString()); } // Timed out. return(error?.ToString() ?? "Unknown error"); } finally { if (!process.WaitForExit((int)TimeSpan.FromSeconds(10).TotalMilliseconds)) { throw new GitCommandException("Command did not terminate."); } if (process.ExitCode != 0) { throw new GitCommandException(string.Format("Command exited with error code: {0}\n{1}", process.ExitCode, error?.ToString() ?? "Unknown error")); } } } }