refactor: use StringBuilder instead of concating string with operator +

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo
2025-12-04 16:28:49 +08:00
parent f53291c124
commit 291361df5c
13 changed files with 120 additions and 82 deletions

View File

@@ -1,4 +1,6 @@
namespace SourceGit.Commands
using System.Text;
namespace SourceGit.Commands
{
public class Apply : Command
{
@@ -6,14 +8,19 @@
{
WorkingDirectory = repo;
Context = repo;
Args = "apply ";
var builder = new StringBuilder(1024);
builder.Append("apply ");
if (ignoreWhitespace)
Args += "--ignore-whitespace ";
builder.Append("--ignore-whitespace ");
else
Args += $"--whitespace={whitespaceMode} ";
builder.Append("--whitespace=").Append(whitespaceMode).Append(' ');
if (!string.IsNullOrEmpty(extra))
Args += $"{extra} ";
Args += $"{file.Quoted()}";
builder.Append(extra).Append(' ');
Args = builder.Append(file.Quoted()).ToString();
}
}
}

View File

@@ -1,4 +1,6 @@
namespace SourceGit.Commands
using System.Text;
namespace SourceGit.Commands
{
public class CherryPick : Command
{
@@ -7,14 +9,16 @@
WorkingDirectory = repo;
Context = repo;
Args = "cherry-pick ";
var builder = new StringBuilder(1024);
builder.Append("cherry-pick ");
if (noCommit)
Args += "-n ";
builder.Append("-n ");
if (appendSourceToMessage)
Args += "-x ";
builder.Append("-x ");
if (!string.IsNullOrEmpty(extraParams))
Args += $"{extraParams} ";
Args += commits;
builder.Append(extraParams).Append(' ');
Args = builder.Append(commits).ToString();
}
}
}

View File

@@ -1,4 +1,6 @@
namespace SourceGit.Commands
using System.Text;
namespace SourceGit.Commands
{
public class Clone : Command
{
@@ -7,15 +9,16 @@
Context = ctx;
WorkingDirectory = path;
SSHKey = sshKey;
Args = "clone --progress --verbose ";
var builder = new StringBuilder(1024);
builder.Append("clone --progress --verbose ");
if (!string.IsNullOrEmpty(extraArgs))
Args += $"{extraArgs} ";
Args += $"{url} ";
builder.Append(extraArgs).Append(' ');
builder.Append(url).Append(' ');
if (!string.IsNullOrEmpty(localName))
Args += localName;
builder.Append(localName);
Args = builder.ToString();
}
}
}

View File

@@ -190,7 +190,7 @@ namespace SourceGit.Commands
start.Environment.Add("LC_ALL", "C");
}
var builder = new StringBuilder();
var builder = new StringBuilder(2048);
builder
.Append("--no-pager -c core.quotepath=off -c credential.helper=")
.Append(Native.OS.CredentialHelper)

View File

@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Text;
using System.Threading.Tasks;
namespace SourceGit.Commands
{
@@ -6,26 +7,24 @@ namespace SourceGit.Commands
{
public Fetch(string repo, string remote, bool noTags, bool force)
{
_remoteKey = $"remote.{remote}.sshkey";
_remote = remote;
WorkingDirectory = repo;
Context = repo;
Args = "fetch --progress --verbose ";
if (noTags)
Args += "--no-tags ";
else
Args += "--tags ";
var builder = new StringBuilder(512);
builder.Append("fetch --progress --verbose ");
builder.Append(noTags ? "--no-tags " : "--tags ");
if (force)
Args += "--force ";
builder.Append("--force ");
builder.Append(remote);
Args += remote;
Args = builder.ToString();
}
public Fetch(string repo, Models.Branch local, Models.Branch remote)
{
_remoteKey = $"remote.{remote.Remote}.sshkey";
_remote = remote.Remote;
WorkingDirectory = repo;
Context = repo;
@@ -34,10 +33,10 @@ namespace SourceGit.Commands
public async Task<bool> RunAsync()
{
SSHKey = await new Config(WorkingDirectory).GetAsync(_remoteKey).ConfigureAwait(false);
SSHKey = await new Config(WorkingDirectory).GetAsync($"remote.{_remote}.sshkey").ConfigureAwait(false);
return await ExecAsync().ConfigureAwait(false);
}
private readonly string _remoteKey;
private readonly string _remote;
}
}

View File

@@ -1,4 +1,6 @@
namespace SourceGit.Commands
using System.Text;
namespace SourceGit.Commands
{
public class InteractiveRebase : Command
{
@@ -7,10 +9,13 @@
WorkingDirectory = repo;
Context = repo;
Editor = EditorType.RebaseEditor;
Args = "rebase -i --autosquash ";
var builder = new StringBuilder(512);
builder.Append("rebase -i --autosquash ");
if (autoStash)
Args += "--autostash ";
Args += basedOn;
builder.Append("--autostash ");
Args = builder.Append(basedOn).ToString();
}
}
}

View File

@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Text;
using System.Threading.Tasks;
namespace SourceGit.Commands
{
@@ -10,12 +11,14 @@ namespace SourceGit.Commands
WorkingDirectory = repo;
Context = repo;
Args = "pull --verbose --progress ";
var builder = new StringBuilder(512);
builder.Append("pull --verbose --progress ");
if (useRebase)
Args += "--rebase=true ";
builder.Append("--rebase=true ");
builder.Append(remote).Append(' ').Append(branch);
Args += $"{remote} {branch}";
Args = builder.ToString();
}
public async Task<bool> RunAsync()

View File

@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Text;
using System.Threading.Tasks;
namespace SourceGit.Commands
{
@@ -10,18 +11,20 @@ namespace SourceGit.Commands
WorkingDirectory = repo;
Context = repo;
Args = "push --progress --verbose ";
var builder = new StringBuilder(1024);
builder.Append("push --progress --verbose ");
if (withTags)
Args += "--tags ";
builder.Append("--tags ");
if (checkSubmodules)
Args += "--recurse-submodules=check ";
builder.Append("--recurse-submodules=check ");
if (track)
Args += "-u ";
builder.Append("-u ");
if (force)
Args += "--force-with-lease ";
builder.Append("--force-with-lease ");
Args += $"{remote} {local}:{remoteBranch}";
builder.Append(remote).Append(' ').Append(local).Append(':').Append(remoteBranch);
Args = builder.ToString();
}
public Push(string repo, string remote, string refname, bool isDelete)
@@ -30,12 +33,14 @@ namespace SourceGit.Commands
WorkingDirectory = repo;
Context = repo;
Args = "push ";
var builder = new StringBuilder(512);
builder.Append("push ");
if (isDelete)
Args += "--delete ";
builder.Append("--delete ");
builder.Append(remote).Append(' ').Append(refname);
Args += $"{remote} {refname}";
Args = builder.ToString();
}
public async Task<bool> RunAsync()

View File

@@ -8,12 +8,12 @@ namespace SourceGit.Commands
{
public class QueryCommits : Command
{
public QueryCommits(string repo, string limits, bool needFindHead = true)
public QueryCommits(string repo, string limits, bool markMerged = true)
{
WorkingDirectory = repo;
Context = repo;
Args = $"log --no-show-signature --decorate=full --format=%H%x00%P%x00%D%x00%aN±%aE%x00%at%x00%cN±%cE%x00%ct%x00%s {limits}";
_findFirstMerged = needFindHead;
_markMerged = markMerged;
}
public QueryCommits(string repo, string filter, Models.CommitSearchMethod method, bool onlyCurrentBranch)
@@ -51,7 +51,7 @@ namespace SourceGit.Commands
WorkingDirectory = repo;
Context = repo;
Args = builder.ToString();
_findFirstMerged = false;
_markMerged = false;
}
public async Task<List<Models.Commit>> GetResultAsync()
@@ -63,6 +63,7 @@ namespace SourceGit.Commands
proc.StartInfo = CreateGitStartInfo(true);
proc.Start();
var findHead = false;
while (await proc.StandardOutput.ReadLineAsync().ConfigureAwait(false) is { } line)
{
var parts = line.Split('\0');
@@ -79,13 +80,12 @@ namespace SourceGit.Commands
commit.Subject = parts[7];
commits.Add(commit);
if (commit.IsMerged && !_isHeadFound)
_isHeadFound = true;
findHead |= commit.IsMerged;
}
await proc.WaitForExitAsync().ConfigureAwait(false);
if (_findFirstMerged && !_isHeadFound && commits.Count > 0)
if (_markMerged && !findHead && commits.Count > 0)
{
var set = await new QueryCurrentBranchCommitHashes(WorkingDirectory, commits[^1].CommitterTime)
.GetResultAsync()
@@ -109,7 +109,6 @@ namespace SourceGit.Commands
return commits;
}
private bool _findFirstMerged = false;
private bool _isHeadFound = false;
private bool _markMerged = false;
}
}

View File

@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
@@ -14,10 +15,13 @@ namespace SourceGit.Commands
{
WorkingDirectory = repo;
Context = repo;
Args = $"ls-tree {sha}";
var builder = new StringBuilder(1024);
builder.Append("ls-tree ").Append(sha);
if (!string.IsNullOrEmpty(parentFolder))
Args += $" -- {parentFolder.Quoted()}";
builder.Append(" -- ").Append(parentFolder.Quoted());
Args = builder.ToString();
}
public async Task<List<Models.Object>> GetResultAsync()

View File

@@ -1,4 +1,6 @@
namespace SourceGit.Commands
using System.Text;
namespace SourceGit.Commands
{
public class Rebase : Command
{
@@ -6,10 +8,13 @@
{
WorkingDirectory = repo;
Context = repo;
Args = "rebase ";
var builder = new StringBuilder(512);
builder.Append("rebase ");
if (autoStash)
Args += "--autostash ";
Args += basedOn;
builder.Append("--autostash ");
Args = builder.Append(basedOn).ToString();
}
}
}

View File

@@ -1,4 +1,6 @@
namespace SourceGit.Commands
using System.Text;
namespace SourceGit.Commands
{
public class Revert : Command
{
@@ -6,9 +8,16 @@
{
WorkingDirectory = repo;
Context = repo;
Args = $"revert -m 1 {commit} --no-edit";
var builder = new StringBuilder(512);
builder
.Append("revert -m 1 ")
.Append(commit)
.Append(" --no-edit");
if (!autoCommit)
Args += " --no-commit";
builder.Append(" --no-commit");
Args = builder.ToString();
}
}
}

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace SourceGit.Commands
@@ -65,26 +66,20 @@ namespace SourceGit.Commands
public async Task<bool> AddAsync(string fullpath, string name, bool createNew, string tracking)
{
Args = "worktree add ";
var builder = new StringBuilder(1024);
builder.Append("worktree add ");
if (!string.IsNullOrEmpty(tracking))
Args += "--track ";
builder.Append("--track ");
if (!string.IsNullOrEmpty(name))
{
if (createNew)
Args += $"-b {name} ";
else
Args += $"-B {name} ";
}
Args += $"{fullpath.Quoted()} ";
builder.Append(createNew ? "-b " : "-B ").Append(name).Append(' ');
builder.Append(fullpath.Quoted()).Append(' ');
if (!string.IsNullOrEmpty(tracking))
Args += tracking;
builder.Append(tracking);
else if (!string.IsNullOrEmpty(name) && !createNew)
Args += name;
builder.Append(name);
Args = builder.ToString();
return await ExecAsync().ConfigureAwait(false);
}