private static IList <ReflogRecordData> ParseResult1(GitOutput output, Dictionary <Hash, RevisionData> cache) { if (output.Output.Length < 40) { return(new ReflogRecordData[0]); } var parser = new GitParser(output.Output); int index = 0; var list = new List <ReflogRecordData>(); while (!parser.IsAtEndOfString) { var selector = parser.ReadLine(); if (selector.Length == 0) { break; } var message = parser.ReadLine(); var sha1 = parser.ReadHash(skip: 1); RevisionData rev; if (!cache.TryGetValue(sha1, out rev)) { rev = new RevisionData(sha1); cache.Add(sha1, rev); } parser.ParseRevisionData(rev, cache); list.Add(new ReflogRecordData(index++, message, rev)); } return(list); }
/// <summary>Check latest gitter version on this chanel.</summary> /// <returns>Latest gitter version.</returns> public Version CheckVersion() { Version result = null; var cmd = new LsRemoteCommand( LsRemoteCommand.Heads(), LsRemoteCommand.Tags(), new CommandParameter(_url)); GitOutput output; try { output = GitProcess.Execute(new GitInput(cmd)); } catch { return null; } if(output.ExitCode != 0) { return null; } var parser = new GitParser(output.Output); string branchSHA1 = null; while(!parser.IsAtEndOfString) { var sha1 = parser.ReadString(40, 1); var refname = parser.ReadLine(); if(branchSHA1 == null) { if(refname == GitConstants.LocalBranchPrefix + _branch) { branchSHA1 = sha1; } } else { if(sha1 == branchSHA1 && refname.Length > GitConstants.TagPrefix.Length + 1 && refname.StartsWith(GitConstants.TagPrefix) && refname[GitConstants.TagPrefix.Length] == 'v') { var s = GitConstants.TagPrefix.Length + 1; var e = refname.Length - 1; while(s < refname.Length && !char.IsDigit(refname[s])) ++s; while(e > 0 && !char.IsDigit(refname[e])) --e; if(e > s && Version.TryParse(refname.Substring(s, e - s + 1), out result)) { break; } else { result = null; } } } } return result; }
/// <summary>Clone existing repository.</summary> /// <param name="parameters"><see cref="CloneRepositoryParameters"/>.</param> /// <exception cref="ArgumentNullException"><paramref name="parameters"/> == <c>null</c>.</exception> public Task InvokeAsync(CloneRepositoryParameters parameters, IProgress <OperationProgress> progress, CancellationToken cancellationToken) { Verify.Argument.IsNotNull(parameters, nameof(parameters)); var command = _commandFactory(parameters, true); if (!Directory.Exists(parameters.Path)) { Directory.CreateDirectory(parameters.Path); } progress?.Report(new OperationProgress(Resources.StrsConnectingToRemoteHost.AddEllipsis())); var errorMessages = default(List <string>); var stdOutReceiver = new NullReader(); var stdErrReceiver = new NotifyingAsyncTextReader(); stdErrReceiver.TextLineReceived += (s, e) => { if (!string.IsNullOrWhiteSpace(e.Text)) { var parser = new GitParser(e.Text); var operationProgress = parser.ParseProgress(); progress?.Report(operationProgress); if (operationProgress.IsIndeterminate) { if (!string.IsNullOrWhiteSpace(operationProgress.ActionName)) { if (errorMessages == null) { errorMessages = new List <string>(); } errorMessages.Add(operationProgress.ActionName); } } else { errorMessages?.Clear(); } } }; return(_commandExecutor .ExecuteCommandAsync(command, stdOutReceiver, stdErrReceiver, CommandExecutionFlags.None, cancellationToken) .ContinueWith( t => { var exitCode = TaskUtility.UnwrapResult(t); if (exitCode != 0) { var errorMessage = errorMessages != null && errorMessages.Count != 0 ? string.Join(Environment.NewLine, errorMessages) : string.Format(CultureInfo.InvariantCulture, "git process exited with code {0}", exitCode); throw new GitException(errorMessage); } }, cancellationToken, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default)); }
/// <summary>Returns git version.</summary> /// <returns>git version.</returns> private Version QueryVersion() { var gitOutput = CommandExecutor.ExecuteCommand( new Command("--version"), CommandExecutionFlags.None); gitOutput.ThrowOnBadReturnCode(); var parser = new GitParser(gitOutput.Output); return(parser.ReadVersion()); }
public async Task <IList <ReferencePushResult> > InvokeAsync(PushParameters parameters, IProgress <OperationProgress> progress, CancellationToken cancellationToken) { Verify.Argument.IsNotNull(parameters, nameof(parameters)); var command = _commandFactory(parameters, true); progress?.Report(new OperationProgress(Resources.StrsConnectingToRemoteHost.AddEllipsis())); var errorMessages = default(List <string>); var stdOutReceiver = new AsyncTextReader(); var stdErrReceiver = new NotifyingAsyncTextReader(); stdErrReceiver.TextLineReceived += (s, e) => { if (!string.IsNullOrWhiteSpace(e.Text)) { var parser = new GitParser(e.Text); var operationProgress = parser.ParseProgress(); progress?.Report(operationProgress); if (operationProgress.IsIndeterminate) { if (errorMessages == null) { errorMessages = new List <string>(); } errorMessages.Add(operationProgress.ActionName); } else { errorMessages?.Clear(); } } }; var processExitCode = await _commandExecutor .ExecuteCommandAsync( command, stdOutReceiver, stdErrReceiver, CommandExecutionFlags.None, cancellationToken) .ConfigureAwait(continueOnCapturedContext: false); if (processExitCode != 0) { var errorMessage = errorMessages != null && errorMessages.Count != 0 ? string.Join(Environment.NewLine, errorMessages) : string.Format(CultureInfo.InvariantCulture, "git process exited with code {0}", processExitCode); throw new GitException(errorMessage); } return(_resultsParser(stdOutReceiver.GetText())); }
public static Version CheckVersion(string gitExe) { var stdErrReceiver = new AsyncTextReader(); var stdOutReceiver = new AsyncTextReader(); var executor = new GitProcessExecutor(gitExe); var exitCode = executor.Execute(new GitInput(new Command("--version")), stdOutReceiver, stdErrReceiver); var output = new GitOutput(stdOutReceiver.GetText(), stdErrReceiver.GetText(), exitCode); output.ThrowOnBadReturnCode(); var parser = new GitParser(output.Output); return(parser.ReadVersion()); }
private static IList <StashedStateData> ParseResult1(GitOutput output, Dictionary <Hash, RevisionData> cache) { int index = 0; var parser = new GitParser(output.Output); var res = new List <StashedStateData>(); while (!parser.IsAtEndOfString) { var sha1 = new Hash(parser.String, parser.Position); var rev = new RevisionData(sha1); parser.Skip(41); parser.ParseRevisionData(rev, cache); var state = new StashedStateData(index, rev); res.Add(state); ++index; } return(res); }
private string ParseTagMessage(Command command, GitOutput output) { Assert.IsNotNull(output); output.ThrowOnBadReturnCode(); var parser = new GitParser(output.Output); while (!parser.IsAtEndOfLine) { parser.SkipLine(); } parser.SkipLine(); if (parser.RemainingSymbols > 1) { var message = parser.ReadStringUpTo(parser.Length - 1); const char c = 'ďż˝'; if (message.ContainsAnyOf(c)) { output = _commandExecutor.ExecuteCommand(command, Encoding.Default, CommandExecutionFlags.None); output.ThrowOnBadReturnCode(); parser = new GitParser(output.Output); while (!parser.IsAtEndOfLine) { parser.SkipLine(); } parser.SkipLine(); if (parser.RemainingSymbols > 1) { message = parser.ReadStringUpTo(parser.Length - 1); } else { message = string.Empty; } } return(message); } else { return(string.Empty); } }
public IList <ReflogRecordData> Invoke(QueryReflogParameters parameters) { Verify.Argument.IsNotNull(parameters, "parameters"); var command = _commandFactory(parameters); var output = _commandExecutor.ExecuteCommand(command, CommandExecutionFlags.None); output.ThrowOnBadReturnCode(); var cache = new Dictionary <Hash, RevisionData>(Hash.EqualityComparer); var list = ParseResult1(output, cache); // get real commit parents command = GetCommand2(parameters); output = _commandExecutor.ExecuteCommand(command, CommandExecutionFlags.None); output.ThrowOnBadReturnCode(); var parser = new GitParser(output.Output); parser.ParseCommitParentsFromRaw(list.Select(rrd => rrd.Revision), cache); return(list); }
public RevisionData ParseSingleRevision(QueryRevisionParameters parameters, GitOutput output) { Assert.IsNotNull(parameters); Assert.IsNotNull(output); if(output.ExitCode != 0) { var revName = parameters.SHA1.ToString(); if(IsUnknownRevisionError(output.Error, revName)) { throw new UnknownRevisionException(revName); } output.Throw(); } var parser = new GitParser(output.Output); var rev = new RevisionData(parameters.SHA1); parser.ParseRevisionData(rev, null); return rev; }
public IList<RevisionGraphData> ParseRevisionGraph(GitOutput output) { Assert.IsNotNull(output); output.ThrowOnBadReturnCode(); var parser = new GitParser(output.Output); var result = new List<RevisionGraphData>(); while(!parser.IsAtEndOfString) { var sha1 = parser.ReadString(40, 1); int end = parser.FindNullOrEndOfString(); int numParents = (end - parser.Position + 1) / 41; if(numParents == 0) { parser.Position = end + 1; result.Add(new RevisionGraphData(sha1, new string[0])); } else { var parents = new List<string>(numParents); for(int i = 0; i < numParents; ++i) { parents.Add(parser.ReadString(40, 1)); } result.Add(new RevisionGraphData(sha1, parents)); } } return result; }
public IList<ConfigParameterData> ParseQueryConfigResults(QueryConfigParameters parameters, GitOutput output) { Assert.IsNotNull(parameters); Assert.IsNotNull(output); if(output.ExitCode != 0 && parameters.ConfigFile != ConfigFile.Other) { return new ConfigParameterData[0]; } HandleConfigResults(output); var res = new List<ConfigParameterData>(); var parser = new GitParser(output.Output); while(!parser.IsAtEndOfString) { var name = parser.ReadStringUpTo(parser.FindNewLineOrEndOfString(), 1); var value = parser.ReadStringUpTo(parser.FindNullOrEndOfString(), 1); if(parameters.ConfigFile != ConfigFile.Other) { res.Add(new ConfigParameterData(name, value, parameters.ConfigFile)); } else { res.Add(new ConfigParameterData(name, value, parameters.FileName)); } } return res; }
public ObjectCountData ParseObjectCountData(GitOutput output) { Assert.IsNotNull(output); output.ThrowOnBadReturnCode(); int count = 0; int size = 0; int inPack = 0; int packs = 0; int sizePack = 0; int prunePackable = 0; int garbage = 0; var parser = new GitParser(output.Output); while(!parser.IsAtEndOfString) { if(parser.CheckValueAndSkip("count: ")) { int.TryParse(parser.ReadLine(), NumberStyles.Integer, CultureInfo.InvariantCulture, out count); } else if(parser.CheckValueAndSkip("size: ")) { int.TryParse(parser.ReadLine(), NumberStyles.Integer, CultureInfo.InvariantCulture, out size); } else if(parser.CheckValueAndSkip("in-pack: ")) { int.TryParse(parser.ReadLine(), NumberStyles.Integer, CultureInfo.InvariantCulture, out inPack); } else if(parser.CheckValueAndSkip("packs: ")) { int.TryParse(parser.ReadLine(), NumberStyles.Integer, CultureInfo.InvariantCulture, out packs); } else if(parser.CheckValueAndSkip("size-pack: ")) { int.TryParse(parser.ReadLine(), NumberStyles.Integer, CultureInfo.InvariantCulture, out sizePack); } else if(parser.CheckValueAndSkip("prune-packable: ")) { int.TryParse(parser.ReadLine(), NumberStyles.Integer, CultureInfo.InvariantCulture, out prunePackable); } else if(parser.CheckValueAndSkip("garbage: ")) { int.TryParse(parser.ReadLine(), NumberStyles.Integer, CultureInfo.InvariantCulture, out garbage); } else { parser.SkipLine(); } } return new ObjectCountData(count, size, inPack, packs, sizePack, prunePackable, garbage); }
public IList<NoteData> ParseNotes(GitOutput output) { Assert.IsNotNull(output); output.ThrowOnBadReturnCode(); var res = new List<NoteData>(); var notes = output.Output; if(notes.Length > 81) { var parser = new GitParser(notes); while(!parser.IsAtEndOfString) { var noteSHA1 = parser.ReadString(40, 1); var objectSHA1 = parser.ReadString(40, 1); res.Add(new NoteData(noteSHA1, objectSHA1, null)); } } return res; }
public IList<string> ParseFilesToClean(GitOutput output) { Assert.IsNotNull(output); output.ThrowOnBadReturnCode(); var files = output.Output; var res = new List<string>(); var parser = new GitParser(files); while(!parser.IsAtEndOfString) { if(parser.CheckValue("Would remove ")) { parser.Skip(13); res.Add(parser.DecodeEscapedString(parser.FindNewLineOrEndOfString(), 1)); } else { parser.SkipLine(); } } return res; }
public RevisionData ParseDereferenceOutput(DereferenceParameters parameters, GitOutput output) { Assert.IsNotNull(parameters); Assert.IsNotNull(output); if(output.ExitCode != 0) { if(IsUnknownRevisionError(output.Error, parameters.Reference)) { throw new UnknownRevisionException(parameters.Reference); } if(IsBadObjectError(output.Error, parameters.Reference)) { throw new UnknownRevisionException(parameters.Reference); } output.Throw(); } if(parameters.LoadRevisionData) { var parser = new GitParser(output.Output); return parser.ParseRevision(); } else { var hash = new Hash(output.Output); return new RevisionData(hash); } }
public BranchesData ParseBranches(QueryBranchesParameters parameters, GitOutput output) { Assert.IsNotNull(parameters); Assert.IsNotNull(output); output.ThrowOnBadReturnCode(); var parser = new GitParser(output.Output); return parser.ParseBranches(parameters.Restriction, parameters.AllowFakeBranch); }
public IList<UserData> ParseUsers(GitOutput output) { Assert.IsNotNull(output); output.ThrowOnBadReturnCode(); var res = new List<UserData>(); var parser = new GitParser(output.Output); while(!parser.IsAtEndOfString) { var tab = parser.FindNoAdvance('\t'); string commitsCountStr = parser.ReadStringUpTo(tab, 1); int commitsCount = int.Parse(commitsCountStr, NumberStyles.Integer, CultureInfo.InvariantCulture); var eol = parser.FindLfLineEnding(); var emailSeparator = parser.String.LastIndexOf(" <", eol - 1, eol - tab - 1); string name = parser.ReadStringUpTo(emailSeparator, 2); string email = parser.ReadStringUpTo(eol - 1, 2); var userData = new UserData(name, email, commitsCount); res.Add(userData); } return res; }
public Task <IList <ReferencePushResult> > InvokeAsync(PushParameters parameters, IProgress <OperationProgress> progress, CancellationToken cancellationToken) { Verify.Argument.IsNotNull(parameters, "parameters"); var command = _commandFactory(parameters, true); if (progress != null) { progress.Report(new OperationProgress(Resources.StrsConnectingToRemoteHost.AddEllipsis())); } List <string> errorMessages = null; var stdOutReceiver = new AsyncTextReader(); var stdErrReceiver = new NotifyingAsyncTextReader(); stdErrReceiver.TextLineReceived += (s, e) => { if (!string.IsNullOrWhiteSpace(e.Text)) { var parser = new GitParser(e.Text); var operationProgress = parser.ParseProgress(); if (progress != null) { progress.Report(operationProgress); } if (operationProgress.IsIndeterminate) { if (errorMessages == null) { errorMessages = new List <string>(); } errorMessages.Add(operationProgress.ActionName); } else { if (errorMessages != null) { errorMessages.Clear(); } } } }; return(_commandExecutor .ExecuteCommandAsync( command, stdOutReceiver, stdErrReceiver, CommandExecutionFlags.None, cancellationToken) .ContinueWith(task => { int exitCode = TaskUtility.UnwrapResult(task); if (exitCode != 0) { string errorMessage; if (errorMessages != null && errorMessages.Count != 0) { errorMessage = string.Join(Environment.NewLine, errorMessages); } else { errorMessage = string.Format(CultureInfo.InvariantCulture, "git process exited with code {0}", exitCode); } throw new GitException(errorMessage); } else { return _resultsParser(stdOutReceiver.GetText()); } }, cancellationToken, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default)); }
public static Version CheckVersion(string gitExe) { var stdErrReceiver = new AsyncTextReader(); var stdOutReceiver = new AsyncTextReader(); var executor = new GitProcessExecutor(gitExe); var exitCode = executor.Execute(new GitInput(new Command("--version")), stdOutReceiver, stdErrReceiver); var output = new GitOutput(stdOutReceiver.GetText(), stdErrReceiver.GetText(), exitCode); output.ThrowOnBadReturnCode(); var parser = new GitParser(output.Output); return parser.ReadVersion(); }
public Task <IList <ReflogRecordData> > InvokeAsync(QueryReflogParameters parameters, IProgress <OperationProgress> progress, CancellationToken cancellationToken) { Verify.Argument.IsNotNull(parameters, "parameters"); var command1 = _commandFactory(parameters); var command2 = GetCommand2(parameters); var tcs = new TaskCompletionSource <object>(); if (cancellationToken.CanBeCanceled) { cancellationToken.Register(() => tcs.TrySetCanceled()); } int completedTasks = 0; var task1 = _commandExecutor.ExecuteCommandAsync(command1, CommandExecutionFlags.None, cancellationToken); var task2 = _commandExecutor.ExecuteCommandAsync(command2, CommandExecutionFlags.None, cancellationToken); task1.ContinueWith( t => tcs.TrySetCanceled(), cancellationToken, TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnCanceled, TaskScheduler.Default); task2.ContinueWith( t => tcs.TrySetCanceled(), cancellationToken, TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnCanceled, TaskScheduler.Default); task1.ContinueWith( t => tcs.TrySetException(t.Exception), cancellationToken, TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnFaulted, TaskScheduler.Default); task2.ContinueWith( t => tcs.TrySetException(t.Exception), cancellationToken, TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnFaulted, TaskScheduler.Default); task1.ContinueWith( t => { if (Interlocked.Increment(ref completedTasks) == 2) { tcs.TrySetResult(null); } }, cancellationToken, TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.Default); task2.ContinueWith( t => { if (Interlocked.Increment(ref completedTasks) == 2) { tcs.TrySetResult(null); } }, cancellationToken, TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.Default); return(tcs.Task. ContinueWith( t => { TaskUtility.PropagateFaultedStates(t); var output1 = TaskUtility.UnwrapResult(task1); output1.ThrowOnBadReturnCode(); var output2 = TaskUtility.UnwrapResult(task2); output2.ThrowOnBadReturnCode(); var cache = new Dictionary <Hash, RevisionData>(Hash.EqualityComparer); var list = ParseResult1(output1, cache); var parser = new GitParser(output2.Output); parser.ParseCommitParentsFromRaw(list.Select(rrd => rrd.Revision), cache); return list; }, cancellationToken, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default)); }