Skip to content

Commit 291361d

Browse files
committed
refactor: use StringBuilder instead of concating string with operator +
Signed-off-by: leo <longshuang@msn.cn>
1 parent f53291c commit 291361d

File tree

13 files changed

+120
-82
lines changed

13 files changed

+120
-82
lines changed

src/Commands/Apply.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
1-
namespace SourceGit.Commands
1+
using System.Text;
2+
3+
namespace SourceGit.Commands
24
{
35
public class Apply : Command
46
{
57
public Apply(string repo, string file, bool ignoreWhitespace, string whitespaceMode, string extra)
68
{
79
WorkingDirectory = repo;
810
Context = repo;
9-
Args = "apply ";
11+
12+
var builder = new StringBuilder(1024);
13+
builder.Append("apply ");
14+
1015
if (ignoreWhitespace)
11-
Args += "--ignore-whitespace ";
16+
builder.Append("--ignore-whitespace ");
1217
else
13-
Args += $"--whitespace={whitespaceMode} ";
18+
builder.Append("--whitespace=").Append(whitespaceMode).Append(' ');
19+
1420
if (!string.IsNullOrEmpty(extra))
15-
Args += $"{extra} ";
16-
Args += $"{file.Quoted()}";
21+
builder.Append(extra).Append(' ');
22+
23+
Args = builder.Append(file.Quoted()).ToString();
1724
}
1825
}
1926
}

src/Commands/CherryPick.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace SourceGit.Commands
1+
using System.Text;
2+
3+
namespace SourceGit.Commands
24
{
35
public class CherryPick : Command
46
{
@@ -7,14 +9,16 @@ public CherryPick(string repo, string commits, bool noCommit, bool appendSourceT
79
WorkingDirectory = repo;
810
Context = repo;
911

10-
Args = "cherry-pick ";
12+
var builder = new StringBuilder(1024);
13+
builder.Append("cherry-pick ");
1114
if (noCommit)
12-
Args += "-n ";
15+
builder.Append("-n ");
1316
if (appendSourceToMessage)
14-
Args += "-x ";
17+
builder.Append("-x ");
1518
if (!string.IsNullOrEmpty(extraParams))
16-
Args += $"{extraParams} ";
17-
Args += commits;
19+
builder.Append(extraParams).Append(' ');
20+
21+
Args = builder.Append(commits).ToString();
1822
}
1923
}
2024
}

src/Commands/Clone.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace SourceGit.Commands
1+
using System.Text;
2+
3+
namespace SourceGit.Commands
24
{
35
public class Clone : Command
46
{
@@ -7,15 +9,16 @@ public Clone(string ctx, string path, string url, string localName, string sshKe
79
Context = ctx;
810
WorkingDirectory = path;
911
SSHKey = sshKey;
10-
Args = "clone --progress --verbose ";
1112

13+
var builder = new StringBuilder(1024);
14+
builder.Append("clone --progress --verbose ");
1215
if (!string.IsNullOrEmpty(extraArgs))
13-
Args += $"{extraArgs} ";
14-
15-
Args += $"{url} ";
16-
16+
builder.Append(extraArgs).Append(' ');
17+
builder.Append(url).Append(' ');
1718
if (!string.IsNullOrEmpty(localName))
18-
Args += localName;
19+
builder.Append(localName);
20+
21+
Args = builder.ToString();
1922
}
2023
}
2124
}

src/Commands/Command.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ protected ProcessStartInfo CreateGitStartInfo(bool redirect)
190190
start.Environment.Add("LC_ALL", "C");
191191
}
192192

193-
var builder = new StringBuilder();
193+
var builder = new StringBuilder(2048);
194194
builder
195195
.Append("--no-pager -c core.quotepath=off -c credential.helper=")
196196
.Append(Native.OS.CredentialHelper)

src/Commands/Fetch.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
1-
using System.Threading.Tasks;
1+
using System.Text;
2+
using System.Threading.Tasks;
23

34
namespace SourceGit.Commands
45
{
56
public class Fetch : Command
67
{
78
public Fetch(string repo, string remote, bool noTags, bool force)
89
{
9-
_remoteKey = $"remote.{remote}.sshkey";
10+
_remote = remote;
1011

1112
WorkingDirectory = repo;
1213
Context = repo;
13-
Args = "fetch --progress --verbose ";
14-
15-
if (noTags)
16-
Args += "--no-tags ";
17-
else
18-
Args += "--tags ";
1914

15+
var builder = new StringBuilder(512);
16+
builder.Append("fetch --progress --verbose ");
17+
builder.Append(noTags ? "--no-tags " : "--tags ");
2018
if (force)
21-
Args += "--force ";
19+
builder.Append("--force ");
20+
builder.Append(remote);
2221

23-
Args += remote;
22+
Args = builder.ToString();
2423
}
2524

2625
public Fetch(string repo, Models.Branch local, Models.Branch remote)
2726
{
28-
_remoteKey = $"remote.{remote.Remote}.sshkey";
27+
_remote = remote.Remote;
2928

3029
WorkingDirectory = repo;
3130
Context = repo;
@@ -34,10 +33,10 @@ public Fetch(string repo, Models.Branch local, Models.Branch remote)
3433

3534
public async Task<bool> RunAsync()
3635
{
37-
SSHKey = await new Config(WorkingDirectory).GetAsync(_remoteKey).ConfigureAwait(false);
36+
SSHKey = await new Config(WorkingDirectory).GetAsync($"remote.{_remote}.sshkey").ConfigureAwait(false);
3837
return await ExecAsync().ConfigureAwait(false);
3938
}
4039

41-
private readonly string _remoteKey;
40+
private readonly string _remote;
4241
}
4342
}

src/Commands/InteractiveRebase.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace SourceGit.Commands
1+
using System.Text;
2+
3+
namespace SourceGit.Commands
24
{
35
public class InteractiveRebase : Command
46
{
@@ -7,10 +9,13 @@ public InteractiveRebase(string repo, string basedOn, bool autoStash)
79
WorkingDirectory = repo;
810
Context = repo;
911
Editor = EditorType.RebaseEditor;
10-
Args = "rebase -i --autosquash ";
12+
13+
var builder = new StringBuilder(512);
14+
builder.Append("rebase -i --autosquash ");
1115
if (autoStash)
12-
Args += "--autostash ";
13-
Args += basedOn;
16+
builder.Append("--autostash ");
17+
18+
Args = builder.Append(basedOn).ToString();
1419
}
1520
}
1621
}

src/Commands/Pull.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Threading.Tasks;
1+
using System.Text;
2+
using System.Threading.Tasks;
23

34
namespace SourceGit.Commands
45
{
@@ -10,12 +11,14 @@ public Pull(string repo, string remote, string branch, bool useRebase)
1011

1112
WorkingDirectory = repo;
1213
Context = repo;
13-
Args = "pull --verbose --progress ";
1414

15+
var builder = new StringBuilder(512);
16+
builder.Append("pull --verbose --progress ");
1517
if (useRebase)
16-
Args += "--rebase=true ";
18+
builder.Append("--rebase=true ");
19+
builder.Append(remote).Append(' ').Append(branch);
1720

18-
Args += $"{remote} {branch}";
21+
Args = builder.ToString();
1922
}
2023

2124
public async Task<bool> RunAsync()

src/Commands/Push.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Threading.Tasks;
1+
using System.Text;
2+
using System.Threading.Tasks;
23

34
namespace SourceGit.Commands
45
{
@@ -10,18 +11,20 @@ public Push(string repo, string local, string remote, string remoteBranch, bool
1011

1112
WorkingDirectory = repo;
1213
Context = repo;
13-
Args = "push --progress --verbose ";
1414

15+
var builder = new StringBuilder(1024);
16+
builder.Append("push --progress --verbose ");
1517
if (withTags)
16-
Args += "--tags ";
18+
builder.Append("--tags ");
1719
if (checkSubmodules)
18-
Args += "--recurse-submodules=check ";
20+
builder.Append("--recurse-submodules=check ");
1921
if (track)
20-
Args += "-u ";
22+
builder.Append("-u ");
2123
if (force)
22-
Args += "--force-with-lease ";
24+
builder.Append("--force-with-lease ");
2325

24-
Args += $"{remote} {local}:{remoteBranch}";
26+
builder.Append(remote).Append(' ').Append(local).Append(':').Append(remoteBranch);
27+
Args = builder.ToString();
2528
}
2629

2730
public Push(string repo, string remote, string refname, bool isDelete)
@@ -30,12 +33,14 @@ public Push(string repo, string remote, string refname, bool isDelete)
3033

3134
WorkingDirectory = repo;
3235
Context = repo;
33-
Args = "push ";
3436

37+
var builder = new StringBuilder(512);
38+
builder.Append("push ");
3539
if (isDelete)
36-
Args += "--delete ";
40+
builder.Append("--delete ");
41+
builder.Append(remote).Append(' ').Append(refname);
3742

38-
Args += $"{remote} {refname}";
43+
Args = builder.ToString();
3944
}
4045

4146
public async Task<bool> RunAsync()

src/Commands/QueryCommits.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ namespace SourceGit.Commands
88
{
99
public class QueryCommits : Command
1010
{
11-
public QueryCommits(string repo, string limits, bool needFindHead = true)
11+
public QueryCommits(string repo, string limits, bool markMerged = true)
1212
{
1313
WorkingDirectory = repo;
1414
Context = repo;
1515
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}";
16-
_findFirstMerged = needFindHead;
16+
_markMerged = markMerged;
1717
}
1818

1919
public QueryCommits(string repo, string filter, Models.CommitSearchMethod method, bool onlyCurrentBranch)
@@ -51,7 +51,7 @@ public QueryCommits(string repo, string filter, Models.CommitSearchMethod method
5151
WorkingDirectory = repo;
5252
Context = repo;
5353
Args = builder.ToString();
54-
_findFirstMerged = false;
54+
_markMerged = false;
5555
}
5656

5757
public async Task<List<Models.Commit>> GetResultAsync()
@@ -63,6 +63,7 @@ public QueryCommits(string repo, string filter, Models.CommitSearchMethod method
6363
proc.StartInfo = CreateGitStartInfo(true);
6464
proc.Start();
6565

66+
var findHead = false;
6667
while (await proc.StandardOutput.ReadLineAsync().ConfigureAwait(false) is { } line)
6768
{
6869
var parts = line.Split('\0');
@@ -79,13 +80,12 @@ public QueryCommits(string repo, string filter, Models.CommitSearchMethod method
7980
commit.Subject = parts[7];
8081
commits.Add(commit);
8182

82-
if (commit.IsMerged && !_isHeadFound)
83-
_isHeadFound = true;
83+
findHead |= commit.IsMerged;
8484
}
8585

8686
await proc.WaitForExitAsync().ConfigureAwait(false);
8787

88-
if (_findFirstMerged && !_isHeadFound && commits.Count > 0)
88+
if (_markMerged && !findHead && commits.Count > 0)
8989
{
9090
var set = await new QueryCurrentBranchCommitHashes(WorkingDirectory, commits[^1].CommitterTime)
9191
.GetResultAsync()
@@ -109,7 +109,6 @@ public QueryCommits(string repo, string filter, Models.CommitSearchMethod method
109109
return commits;
110110
}
111111

112-
private bool _findFirstMerged = false;
113-
private bool _isHeadFound = false;
112+
private bool _markMerged = false;
114113
}
115114
}

src/Commands/QueryRevisionObjects.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using System.Diagnostics;
3+
using System.Text;
34
using System.Text.RegularExpressions;
45
using System.Threading.Tasks;
56

@@ -14,10 +15,13 @@ public QueryRevisionObjects(string repo, string sha, string parentFolder)
1415
{
1516
WorkingDirectory = repo;
1617
Context = repo;
17-
Args = $"ls-tree {sha}";
1818

19+
var builder = new StringBuilder(1024);
20+
builder.Append("ls-tree ").Append(sha);
1921
if (!string.IsNullOrEmpty(parentFolder))
20-
Args += $" -- {parentFolder.Quoted()}";
22+
builder.Append(" -- ").Append(parentFolder.Quoted());
23+
24+
Args = builder.ToString();
2125
}
2226

2327
public async Task<List<Models.Object>> GetResultAsync()

0 commit comments

Comments
 (0)