diff --git a/src/Commands/Apply.cs b/src/Commands/Apply.cs index 189d4335..ca6ffe8d 100644 --- a/src/Commands/Apply.cs +++ b/src/Commands/Apply.cs @@ -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(); } } } diff --git a/src/Commands/CherryPick.cs b/src/Commands/CherryPick.cs index 0c82b9fd..d9d4faea 100644 --- a/src/Commands/CherryPick.cs +++ b/src/Commands/CherryPick.cs @@ -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(); } } } diff --git a/src/Commands/Clone.cs b/src/Commands/Clone.cs index efec264b..ec3c4594 100644 --- a/src/Commands/Clone.cs +++ b/src/Commands/Clone.cs @@ -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(); } } } diff --git a/src/Commands/Command.cs b/src/Commands/Command.cs index 0996a030..520f1970 100644 --- a/src/Commands/Command.cs +++ b/src/Commands/Command.cs @@ -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) diff --git a/src/Commands/Fetch.cs b/src/Commands/Fetch.cs index 9ec27656..9a599f82 100644 --- a/src/Commands/Fetch.cs +++ b/src/Commands/Fetch.cs @@ -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 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; } } diff --git a/src/Commands/InteractiveRebase.cs b/src/Commands/InteractiveRebase.cs index ebcadec1..7e4ca86b 100644 --- a/src/Commands/InteractiveRebase.cs +++ b/src/Commands/InteractiveRebase.cs @@ -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(); } } } diff --git a/src/Commands/Pull.cs b/src/Commands/Pull.cs index 93896c75..e121445b 100644 --- a/src/Commands/Pull.cs +++ b/src/Commands/Pull.cs @@ -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 RunAsync() diff --git a/src/Commands/Push.cs b/src/Commands/Push.cs index b822af46..44394dc4 100644 --- a/src/Commands/Push.cs +++ b/src/Commands/Push.cs @@ -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 RunAsync() diff --git a/src/Commands/QueryCommits.cs b/src/Commands/QueryCommits.cs index a92262d5..0e46ead8 100644 --- a/src/Commands/QueryCommits.cs +++ b/src/Commands/QueryCommits.cs @@ -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> 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; } } diff --git a/src/Commands/QueryRevisionObjects.cs b/src/Commands/QueryRevisionObjects.cs index e6463750..a7eaaa9e 100644 --- a/src/Commands/QueryRevisionObjects.cs +++ b/src/Commands/QueryRevisionObjects.cs @@ -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> GetResultAsync() diff --git a/src/Commands/Rebase.cs b/src/Commands/Rebase.cs index d08d55ad..f7f33ac5 100644 --- a/src/Commands/Rebase.cs +++ b/src/Commands/Rebase.cs @@ -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(); } } } diff --git a/src/Commands/Revert.cs b/src/Commands/Revert.cs index 2e7afd11..f42a62d0 100644 --- a/src/Commands/Revert.cs +++ b/src/Commands/Revert.cs @@ -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(); } } } diff --git a/src/Commands/Worktree.cs b/src/Commands/Worktree.cs index 1c3f8915..50ee9724 100644 --- a/src/Commands/Worktree.cs +++ b/src/Commands/Worktree.cs @@ -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 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); }