Skip to content

code_style: general cleanup #1543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ indent_size = 2
# Shell scripts
[*.sh]
end_of_line = lf

[*.{cmd,bat}]
end_of_line = crlf

Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ jobs:
strategy:
matrix:
include:
- name : Windows x64
- name: Windows x64
os: windows-2022
runtime: win-x64
- name : Windows ARM64
- name: Windows ARM64
os: windows-2022
runtime: win-arm64
- name : macOS (Intel)
- name: macOS (Intel)
os: macos-13
runtime: osx-x64
- name : macOS (Apple Silicon)
- name: macOS (Apple Silicon)
os: macos-latest
runtime: osx-arm64
- name : Linux
- name: Linux
os: ubuntu-latest
runtime: linux-x64
container: ubuntu:20.04
- name : Linux (arm64)
- name: Linux (arm64)
os: ubuntu-latest
runtime: linux-arm64
container: ubuntu:20.04
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/format-check.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
name: Format Check
on:
push:
branches: [ develop ]
branches: [develop]
pull_request:
branches: [develop]
workflow_dispatch:
workflow_call:

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/localization-check.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Localization Check
on:
push:
branches: [ develop ]
branches: [develop]
paths:
- 'src/Resources/Locales/**'
workflow_dispatch:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: windows-2022
strategy:
matrix:
runtime: [ win-x64, win-arm64 ]
runtime: [win-x64, win-arm64]
steps:
- name: Checkout sources
uses: actions/checkout@v4
Expand Down
41 changes: 17 additions & 24 deletions src/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ public static void SetTheme(string theme, string themeOverridesFile)
try
{
var resDic = new ResourceDictionary();
var overrides = JsonSerializer.Deserialize(File.ReadAllText(themeOverridesFile), JsonCodeGen.Default.ThemeOverrides);
using var stream = File.OpenRead(themeOverridesFile);
var overrides = JsonSerializer.Deserialize(stream, JsonCodeGen.Default.ThemeOverrides);
foreach (var kv in overrides.BasicColors)
{
if (kv.Key.Equals("SystemAccentColor", StringComparison.Ordinal))
Expand Down Expand Up @@ -449,31 +450,21 @@ private static bool TryLaunchAsRebaseTodoEditor(string[] args, out int exitCode)
if (!File.Exists(jobsFile))
return true;

var collection = JsonSerializer.Deserialize(File.ReadAllText(jobsFile), JsonCodeGen.Default.InteractiveRebaseJobCollection);
using var stream = File.OpenRead(jobsFile);
var collection = JsonSerializer.Deserialize(stream, JsonCodeGen.Default.InteractiveRebaseJobCollection);
using var writer = new StreamWriter(file);
foreach (var job in collection.Jobs)
{
switch (job.Action)
var code = job.Action switch
{
case Models.InteractiveRebaseAction.Pick:
writer.WriteLine($"p {job.SHA}");
break;
case Models.InteractiveRebaseAction.Edit:
writer.WriteLine($"e {job.SHA}");
break;
case Models.InteractiveRebaseAction.Reword:
writer.WriteLine($"r {job.SHA}");
break;
case Models.InteractiveRebaseAction.Squash:
writer.WriteLine($"s {job.SHA}");
break;
case Models.InteractiveRebaseAction.Fixup:
writer.WriteLine($"f {job.SHA}");
break;
default:
writer.WriteLine($"d {job.SHA}");
break;
}
Models.InteractiveRebaseAction.Pick => 'p',
Models.InteractiveRebaseAction.Edit => 'e',
Models.InteractiveRebaseAction.Reword => 'r',
Models.InteractiveRebaseAction.Squash => 's',
Models.InteractiveRebaseAction.Fixup => 'f',
_ => 'd'
};
writer.WriteLine($"{code} {job.SHA}");
}

writer.Flush();
Expand Down Expand Up @@ -506,7 +497,8 @@ private static bool TryLaunchAsRebaseMessageEditor(string[] args, out int exitCo

var origHead = File.ReadAllText(origHeadFile).Trim();
var onto = File.ReadAllText(ontoFile).Trim();
var collection = JsonSerializer.Deserialize(File.ReadAllText(jobsFile), JsonCodeGen.Default.InteractiveRebaseJobCollection);
using var stream = File.OpenRead(jobsFile);
var collection = JsonSerializer.Deserialize(stream, JsonCodeGen.Default.InteractiveRebaseJobCollection);
if (!collection.Onto.Equals(onto) || !collection.OrigHead.Equals(origHead))
return true;

Expand Down Expand Up @@ -626,7 +618,8 @@ private void Check4Update(bool manually = false)
try
{
// Fetch latest release information.
var client = new HttpClient() { Timeout = TimeSpan.FromSeconds(5) };
using var client = new HttpClient();
client.Timeout = TimeSpan.FromSeconds(5);
var data = await client.GetStringAsync("https://sourcegit-scm.github.io/data/version.json");

// Parse JSON into Models.Version.
Expand Down
19 changes: 8 additions & 11 deletions src/Commands/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public async Task<bool> ExecAsync()
{
Log?.AppendLine($"$ git {Args}\n");

var start = CreateGitStartInfo(true);
var errs = new List<string>();
var proc = new Process() { StartInfo = start };
using var proc = new Process();
proc.StartInfo = CreateGitStartInfo(true);

proc.OutputDataReceived += (_, e) => HandleOutput(e.Data, errs);
proc.ErrorDataReceived += (_, e) => HandleOutput(e.Data, errs);
Expand All @@ -67,7 +67,7 @@ public async Task<bool> ExecAsync()
{
proc.Start();

// It not safe, please only use `CancellationToken` in readonly commands.
// Not safe, please only use `CancellationToken` in readonly commands.
if (CancellationToken.CanBeCanceled)
{
dummy = proc;
Expand Down Expand Up @@ -110,11 +110,9 @@ public async Task<bool> ExecAsync()
}
}

int exitCode = proc.ExitCode;
proc.Close();
Log?.AppendLine(string.Empty);

if (!CancellationToken.IsCancellationRequested && exitCode != 0)
if (!CancellationToken.IsCancellationRequested && proc.ExitCode != 0)
{
if (RaiseError)
{
Expand All @@ -131,8 +129,8 @@ public async Task<bool> ExecAsync()

protected async Task<Result> ReadToEndAsync()
{
var start = CreateGitStartInfo(true);
var proc = new Process() { StartInfo = start };
using var proc = new Process();
proc.StartInfo = CreateGitStartInfo(true);

try
{
Expand All @@ -149,7 +147,6 @@ protected async Task<Result> ReadToEndAsync()
await proc.WaitForExitAsync(CancellationToken).ConfigureAwait(false);

rs.IsSuccess = proc.ExitCode == 0;
proc.Close();
return rs;
}

Expand Down Expand Up @@ -191,8 +188,8 @@ private ProcessStartInfo CreateGitStartInfo(bool redirect)
// Force using this app as git editor.
start.Arguments += Editor switch
{
EditorType.CoreEditor => $"-c core.editor=\"\\\"{selfExecFile}\\\" --core-editor\" ",
EditorType.RebaseEditor => $"-c core.editor=\"\\\"{selfExecFile}\\\" --rebase-message-editor\" -c sequence.editor=\"\\\"{selfExecFile}\\\" --rebase-todo-editor\" -c rebase.abbreviateCommands=true ",
EditorType.CoreEditor => $"""-c core.editor="\"{selfExecFile}\" --core-editor" """,
EditorType.RebaseEditor => $"""-c core.editor="\"{selfExecFile}\" --rebase-message-editor" -c sequence.editor="\"{selfExecFile}\" --rebase-todo-editor" -c rebase.abbreviateCommands=true """,
_ => "-c core.editor=true ",
};

Expand Down
6 changes: 2 additions & 4 deletions src/Commands/GenerateCommitMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,8 @@ await _service.ChatAsync(
});
}

responseBuilder.Append("\n");
summaryBuilder.Append("(file: ");
summaryBuilder.Append(change.Path);
summaryBuilder.Append(")\n");
responseBuilder.AppendLine();
summaryBuilder.Append("(file: ").Append(change.Path).AppendLine(")");
}

if (_cancelToken.IsCancellationRequested)
Expand Down
8 changes: 4 additions & 4 deletions src/Commands/QueryCommits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ public QueryCommits(string repo, string filter, Models.CommitSearchMethod method
break;
case 2:
_current.ParseDecorators(line);
if (_current.IsMerged && !_isHeadFounded)
_isHeadFounded = true;
if (_current.IsMerged && !_isHeadFound)
_isHeadFound = true;
break;
case 3:
_current.Author = Models.User.FindOrAdd(line);
Expand Down Expand Up @@ -110,7 +110,7 @@ public QueryCommits(string repo, string filter, Models.CommitSearchMethod method
if (start < rs.StdOut.Length)
_current.Subject = rs.StdOut.Substring(start);

if (_findFirstMerged && !_isHeadFounded && _commits.Count > 0)
if (_findFirstMerged && !_isHeadFound && _commits.Count > 0)
await MarkFirstMergedAsync().ConfigureAwait(false);

return _commits;
Expand Down Expand Up @@ -148,6 +148,6 @@ private async Task MarkFirstMergedAsync()
private List<Models.Commit> _commits = new List<Models.Commit>();
private Models.Commit _current = null;
private bool _findFirstMerged = false;
private bool _isHeadFounded = false;
private bool _isHeadFound = false;
}
}
8 changes: 2 additions & 6 deletions src/Commands/QueryFileContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ public static async Task<Stream> RunAsync(string repo, string revision, string f
var stream = new MemoryStream();
try
{
var proc = new Process() { StartInfo = starter };
proc.Start();
using var proc = Process.Start(starter);
await proc.StandardOutput.BaseStream.CopyToAsync(stream).ConfigureAwait(false);
await proc.WaitForExitAsync().ConfigureAwait(false);
proc.Close();

stream.Position = 0;
}
Expand All @@ -52,14 +50,12 @@ public static async Task<Stream> FromLFSAsync(string repo, string oid, long size
var stream = new MemoryStream();
try
{
var proc = new Process() { StartInfo = starter };
proc.Start();
using var proc = Process.Start(starter);
await proc.StandardInput.WriteLineAsync("version https://git-lfs.github.com/spec/v1").ConfigureAwait(false);
await proc.StandardInput.WriteLineAsync($"oid sha256:{oid}").ConfigureAwait(false);
await proc.StandardInput.WriteLineAsync($"size {size}").ConfigureAwait(false);
await proc.StandardOutput.BaseStream.CopyToAsync(stream).ConfigureAwait(false);
await proc.WaitForExitAsync().ConfigureAwait(false);
proc.Close();

stream.Position = 0;
}
Expand Down
8 changes: 2 additions & 6 deletions src/Commands/SaveChangesAsPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,10 @@ private static async Task<bool> ProcessSingleChangeAsync(string repo, Models.Dif

try
{
var proc = new Process() { StartInfo = starter };
proc.Start();
using var proc = Process.Start(starter);
await proc.StandardOutput.BaseStream.CopyToAsync(writer).ConfigureAwait(false);
await proc.WaitForExitAsync().ConfigureAwait(false);
var rs = proc.ExitCode == 0;
proc.Close();

return rs;
return proc.ExitCode == 0;
}
catch (Exception e)
{
Expand Down
4 changes: 1 addition & 3 deletions src/Commands/SaveRevisionFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,11 @@ private static async Task ExecCmdAsync(string repo, string args, string outputFi
{
try
{
var proc = new Process() { StartInfo = starter };
proc.Start();
using var proc = Process.Start(starter);
if (input != null)
await proc.StandardInput.WriteAsync(await new StreamReader(input).ReadToEndAsync());
await proc.StandardOutput.BaseStream.CopyToAsync(sw);
await proc.WaitForExitAsync();
proc.Close();
}
catch (Exception e)
{
Expand Down
6 changes: 2 additions & 4 deletions src/Commands/UnstageChangesForAmend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public UnstageChangesForAmend(string repo, List<Models.Change> changes)
_patchBuilder.Append(c.Path);
}

_patchBuilder.Append("\n");
_patchBuilder.AppendLine();
}
}

Expand All @@ -63,15 +63,13 @@ public async Task<bool> ExecAsync()

try
{
var proc = new Process() { StartInfo = starter };
proc.Start();
using var proc = Process.Start(starter);
await proc.StandardInput.WriteAsync(_patchBuilder.ToString());
proc.StandardInput.Close();

var err = await proc.StandardError.ReadToEndAsync().ConfigureAwait(false);
await proc.WaitForExitAsync().ConfigureAwait(false);
var rs = proc.ExitCode == 0;
proc.Close();

if (!rs)
App.RaiseException(_repo, err);
Expand Down
8 changes: 3 additions & 5 deletions src/Models/AvatarManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,9 @@ public void Start()
Bitmap img = null;
try
{
var client = new HttpClient() { Timeout = TimeSpan.FromSeconds(2) };
var task = client.GetAsync(url);
task.Wait();

var rsp = task.Result;
using var client = new HttpClient();
client.Timeout = TimeSpan.FromSeconds(2);
var rsp = client.GetAsync(url).Result;
if (rsp.IsSuccessStatusCode)
{
using (var stream = rsp.Content.ReadAsStream())
Expand Down
8 changes: 4 additions & 4 deletions src/Models/Commit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ public void ParseDecorators(string data)

Decorators.Sort((l, r) =>
{
if (l.Type != r.Type)
return (int)l.Type - (int)r.Type;
else
return NumericSort.Compare(l.Name, r.Name);
var delta = (int)l.Type - (int)r.Type;
if (delta != 0)
return delta;
return NumericSort.Compare(l.Name, r.Name);
});
}
}
Expand Down
1 change: 0 additions & 1 deletion src/Models/DiffOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ public DiffOption(string baseRevision, string targetRevision, Change change)
/// <summary>
/// Converts to diff command arguments.
/// </summary>
/// <returns></returns>
public override string ToString()
{
var builder = new StringBuilder();
Expand Down
Loading