Skip to content

Commit

Permalink
RevisionReader: Parse raw commit body
Browse files Browse the repository at this point in the history
* Parse raw body %B instead of combining %s%n%n%b to avoid extra processing
in both Git and GE.
Simplify the handling.

* Do not allocate body for older commits
Previously a temporary variable was allocated also if body was
not stored in the commit message (to save memory, for older commits).

* Only store commit body if multiline message
If the body is the same as the subject, do not store the message
This also avoids loading the commit body for older commits
if the commit is selected

* Align usage of commit body retrieval for other than RevisionReader
  * "Previous commit messages" used %s%b instead of %B
  * Align parsing of raw body and subject

* ObjectId Inline local parsing functions

* Use ReadOnlySpan<char> in RevisionReader
Reduce string allocations

* Use HighPerformance StringBuffer
  • Loading branch information
gerhardol committed Jul 5, 2021
1 parent 380f6ef commit c8396d8
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 639 deletions.
19 changes: 13 additions & 6 deletions GitCommands/Git/GitModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -950,11 +950,18 @@ public GitRevision GetRevision(ObjectId? objectId = null, bool shortFormat = fal
}
else
{
string message = ProcessDiffNotes(10);
string message = ProcessDiffNotes(startIndex: 10);

// commit message is not re-encoded by git when format is given
revision.Body = ReEncodeCommitMessage(message, revision.MessageEncoding);
revision.Subject = revision.Body?.Substring(0, revision.Body.IndexOfAny(new[] { '\r', '\n' })) ?? "";
// See also RevisionReader for parsing commit body
string body = ReEncodeCommitMessage(message, revision.MessageEncoding);
revision.Body = body;

ReadOnlySpan<char> span = (body ?? "").AsSpan();
int endSubjectIndex = span.IndexOf('\n');
revision.Subject = endSubjectIndex >= 0
? span.Slice(0, endSubjectIndex).TrimEnd().ToString()
: body ?? "";
}

if (loadRefs)
Expand Down Expand Up @@ -3507,7 +3514,7 @@ public string GetFileText(ObjectId id, Encoding encoding)
"-z",
$"-n {count}",
revision,
"--pretty=format:%e%n%s%n%n%b",
"--pretty=format:%e%n%B",
{ !string.IsNullOrEmpty(authorPattern), string.Concat("--author=\"", authorPattern, "\"") }
};

Expand Down Expand Up @@ -3884,7 +3891,7 @@ public bool IsRunningGitProcess()
// there was a bug: Git before v1.8.4 did not recode commit message when format is given
// Lossless encoding is used, because LogOutputEncoding might not be lossless and not recoded
// characters could be replaced by replacement character while re-encoding to LogOutputEncoding
public string ReEncodeCommitMessage(string s, string? toEncodingName)
public string? ReEncodeCommitMessage(string s, string? toEncodingName)
{
Encoding? encoding;
try
Expand All @@ -3896,7 +3903,7 @@ public string ReEncodeCommitMessage(string s, string? toEncodingName)
return s + "\n\n! Unsupported commit message encoding: " + toEncodingName + " !";
}

return ReEncodeStringFromLossless(s, encoding);
return ReEncodeStringFromLossless(s, encoding)?.Trim();
}

public Encoding? GetEncodingByGitName(string? encodingName)
Expand Down
1 change: 1 addition & 0 deletions GitCommands/GitCommands.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

<ItemGroup>
<PackageReference Include="JetBrains.Annotations" />
<PackageReference Include="Microsoft.Toolkit.HighPerformance" />
<PackageReference Include="Newtonsoft.Json" />
<PackageReference Include="System.IO.Abstractions" />
<PackageReference Include="System.Reactive.Linq" />
Expand Down

0 comments on commit c8396d8

Please sign in to comment.