optimize(Commit): detect selected file type before reading its content

This commit is contained in:
leo 2020-08-12 14:24:38 +08:00
parent 8628e2af2d
commit 42c933ab54

View file

@ -11,6 +11,7 @@ namespace SourceGit.Git {
public class Commit { public class Commit {
private static readonly string GPGSIG_START = "gpgsig -----BEGIN PGP SIGNATURE-----"; private static readonly string GPGSIG_START = "gpgsig -----BEGIN PGP SIGNATURE-----";
private static readonly string GPGSIG_END = " -----END PGP SIGNATURE-----"; private static readonly string GPGSIG_END = " -----END PGP SIGNATURE-----";
private static readonly Regex REG_TESTBINARY = new Regex(@"^\-\s+\-\s+.*$");
/// <summary> /// <summary>
/// Object in commit. /// Object in commit.
@ -227,6 +228,11 @@ namespace SourceGit.Git {
var count = 0; var count = 0;
var binary = false; var binary = false;
repo.RunCommand($"diff 4b825dc642cb6eb9a060e54bf8d69288fbee4904 {SHA} --numstat -- \"{file}\"", line => {
if (REG_TESTBINARY.IsMatch(line)) binary = true;
});
if (!binary) {
var errs = repo.RunCommand($"show {SHA}:\"{file}\"", line => { var errs = repo.RunCommand($"show {SHA}:\"{file}\"", line => {
if (binary) return; if (binary) return;
@ -243,14 +249,15 @@ namespace SourceGit.Git {
data.Add(line); data.Add(line);
}); });
if (errs != null) App.RaiseError(errs);
}
if (!binary && count > 1000) { if (!binary && count > 1000) {
data.Add("..."); data.Add("...");
data.Add($"Total {count} lines. Hide {count-1000} lines."); data.Add($"Total {count} lines. Hide {count-1000} lines.");
} }
isBinary = binary; isBinary = binary;
if (errs != null) App.RaiseError(errs);
return string.Join("\n", data); return string.Join("\n", data);
} }