void dataReceived(string line) { if (line == null) { return; } if (line == COMMIT_BEGIN) { // a new commit finalizes the last revision finishRevision(); nextStep = ReadStep.Commit; } switch (nextStep) { case ReadStep.Commit: // Sanity check if (line == COMMIT_BEGIN) { revision = new GitRevision(); } else { // Bail out until we see what we expect return; } break; case ReadStep.Hash: revision.Guid = line; for (int i = heads.Count - 1; i >= 0; i--) { if (heads[i].Guid == revision.Guid) { revision.Heads.Add(heads[i]); //Only search for a head once, remove it from list heads.Remove(heads[i]); } } break; case ReadStep.Parents: revision.ParentGuids = line.Split(splitChars, StringSplitOptions.RemoveEmptyEntries); break; case ReadStep.Tree: revision.TreeGuid = line; break; case ReadStep.AuthorName: revision.Author = line; break; case ReadStep.AuthorDate: { DateTime dateTime; DateTime.TryParse(line, out dateTime); revision.AuthorDate = dateTime; } break; case ReadStep.CommitterName: revision.Committer = line; break; case ReadStep.CommitterDate: { DateTime dateTime; DateTime.TryParse(line, out dateTime); revision.CommitDate = dateTime; } break; case ReadStep.CommitMessage: //We need to recode the commit message because of a bug in Git. //We cannot let git recode the message to Settings.Encoding which is //needed to allow the "git log" to print the filename in Settings.Encoding if (logoutputEncoding == null) { logoutputEncoding = GitCommandHelpers.GetLogoutputEncoding(); } if (!logoutputEncoding.Equals(Settings.Encoding)) { revision.Message = logoutputEncoding.GetString(Settings.Encoding.GetBytes(line)); } else { revision.Message = line; } break; case ReadStep.FileName: revision.Name = line; break; } nextStep++; }
/// <summary> /// Creates a CommitInformation object from raw commit info data from git. The string passed in should be /// exact output of a log or show command using --format=raw. /// </summary> /// <param name="rawData">Raw commit data from git.</param> /// <returns>CommitInformation object populated with parsed info from git string.</returns> public static CommitInformation CreateFromRawData(string rawData) { var lines = new List <string>(rawData.Split('\n')); var commit = lines.Single(l => l.StartsWith(COMMIT_LABEL)); var guid = commit.Substring(COMMIT_LABEL.Length); lines.Remove(commit); // TODO: we can use this to add more relationship info like gitk does if wanted var tree = lines.Single(l => l.StartsWith(TREE_LABEL)); var treeGuid = tree.Substring(TREE_LABEL.Length); lines.Remove(tree); // TODO: we can use this to add more relationship info like gitk does if wanted List <string> parentLines = lines.FindAll(l => l.StartsWith(PARENT_LABEL)); var parentGuids = parentLines.Select(parent => parent.Substring(PARENT_LABEL.Length)).ToArray(); lines.RemoveAll(parentLines.Contains); var authorInfo = lines.Single(l => l.StartsWith(AUTHOR_LABEL)); var author = GetPersonFromAuthorInfoLine(authorInfo, AUTHOR_LABEL.Length); var authorDate = GetTimeFromAuthorInfoLine(authorInfo); lines.Remove(authorInfo); var committerInfo = lines.Single(l => l.StartsWith(COMMITTER_LABEL)); var committer = GetPersonFromAuthorInfoLine(committerInfo, COMMITTER_LABEL.Length); var commitDate = GetTimeFromAuthorInfoLine(committerInfo); lines.Remove(committerInfo); var message = new StringBuilder(); foreach (var line in lines) { message.AppendFormat("{0}\n", line); } var body = "\n\n" + message.ToString().TrimStart().TrimEnd() + "\n\n"; //We need to recode the commit message because of a bug in Git. //We cannot let git recode the message to Settings.Encoding which is //needed to allow the "git log" to print the filename in Settings.Encoding Encoding logoutputEncoding = GitCommandHelpers.GetLogoutputEncoding(); if (logoutputEncoding != Settings.Encoding) { body = logoutputEncoding.GetString(Settings.Encoding.GetBytes(body)); } var header = FillToLenght(Strings.GetAuthorText() + ":", COMMITHEADER_STRING_LENGTH) + author + "\n" + FillToLenght(Strings.GetAuthorDateText() + ":", COMMITHEADER_STRING_LENGTH) + GitCommandHelpers.GetRelativeDateString(DateTime.UtcNow, authorDate.UtcDateTime) + " (" + authorDate.LocalDateTime.ToString("ddd MMM dd HH':'mm':'ss yyyy") + ")\n" + FillToLenght(Strings.GetCommitterText() + ":", COMMITHEADER_STRING_LENGTH) + committer + "\n" + FillToLenght(Strings.GetCommitterDateText() + ":", COMMITHEADER_STRING_LENGTH) + GitCommandHelpers.GetRelativeDateString(DateTime.UtcNow, commitDate.UtcDateTime) + " (" + commitDate.LocalDateTime.ToString("ddd MMM dd HH':'mm':'ss yyyy") + ")\n" + FillToLenght(Strings.GetCommitHashText() + ":", COMMITHEADER_STRING_LENGTH) + guid; header = RemoveRedundancies(header); var commitInformation = new CommitInformation(header, body); return(commitInformation); }