Skip to content

Commit 6ae8c7c

Browse files
committed
code_style: move class extensions to App.Extensions.cs to use these extensions anywhere
Signed-off-by: leo <longshuang@msn.cn>
1 parent 9c03921 commit 6ae8c7c

33 files changed

+266
-265
lines changed

src/Commands/StringExtensions.cs renamed to src/App.Extensions.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using System;
1+
using System;
22

3-
namespace SourceGit.Commands
3+
namespace SourceGit
44
{
55
public static class StringExtensions
66
{
@@ -14,4 +14,13 @@ public static string Escaped(this string value)
1414
return value.Replace("\"", "\\\"", StringComparison.Ordinal);
1515
}
1616
}
17+
18+
public static class CommandExtensions
19+
{
20+
public static T Use<T>(this T cmd, Models.ICommandLog log) where T : Commands.Command
21+
{
22+
cmd.Log = log;
23+
return cmd;
24+
}
25+
}
1726
}

src/Commands/Branch.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,55 @@ namespace SourceGit.Commands
55
{
66
public class Branch : Command
77
{
8-
public Branch(string repo)
8+
public Branch(string repo, string name)
99
{
1010
WorkingDirectory = repo;
1111
Context = repo;
12+
_name = name;
1213
}
1314

14-
public async Task<bool> CreateAsync(string name, string basedOn, bool force)
15+
public async Task<bool> CreateAsync(string basedOn, bool force)
1516
{
1617
var builder = new StringBuilder();
1718
builder.Append("branch ");
1819
if (force)
1920
builder.Append("-f ");
20-
builder.Append(name);
21+
builder.Append(_name);
2122
builder.Append(" ");
2223
builder.Append(basedOn);
2324

2425
Args = builder.ToString();
2526
return await ExecAsync().ConfigureAwait(false);
2627
}
2728

28-
public async Task<bool> RenameAsync(string name, string to)
29+
public async Task<bool> RenameAsync(string to)
2930
{
30-
Args = $"branch -M {name} {to}";
31+
Args = $"branch -M {_name} {to}";
3132
return await ExecAsync().ConfigureAwait(false);
3233
}
3334

34-
public async Task<bool> SetUpstreamAsync(string name, string upstream)
35+
public async Task<bool> SetUpstreamAsync(string upstream)
3536
{
3637
if (string.IsNullOrEmpty(upstream))
37-
Args = $"branch {name} --unset-upstream";
38+
Args = $"branch {_name} --unset-upstream";
3839
else
39-
Args = $"branch {name} -u {upstream}";
40+
Args = $"branch {_name} -u {upstream}";
4041

4142
return await ExecAsync().ConfigureAwait(false);
4243
}
4344

44-
public async Task<bool> DeleteLocalAsync(string name)
45+
public async Task<bool> DeleteLocalAsync()
4546
{
46-
Args = $"branch -D {name}";
47+
Args = $"branch -D {_name}";
4748
return await ExecAsync().ConfigureAwait(false);
4849
}
4950

50-
public async Task<bool> DeleteRemoteAsync(string remote, string name)
51+
public async Task<bool> DeleteRemoteAsync(string remote)
5152
{
52-
Args = $"branch -D -r {remote}/{name}";
53+
Args = $"branch -D -r {remote}/{_name}";
5354
return await ExecAsync().ConfigureAwait(false);
5455
}
56+
57+
private readonly string _name;
5558
}
5659
}

src/Commands/Discard.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ public static async Task AllAsync(string repo, bool includeIgnored, Models.IComm
3838
App.RaiseException(repo, $"Failed to discard changes. Reason: {e.Message}");
3939
}
4040

41-
await new Reset(repo, "HEAD", "--hard") { Log = log }.ExecAsync().ConfigureAwait(false);
41+
await new Reset(repo, "HEAD", "--hard").Use(log).ExecAsync().ConfigureAwait(false);
4242

4343
if (includeIgnored)
44-
await new Clean(repo) { Log = log }.ExecAsync().ConfigureAwait(false);
44+
await new Clean(repo).Use(log).ExecAsync().ConfigureAwait(false);
4545
}
4646

4747
/// <summary>
@@ -81,7 +81,7 @@ public static async Task ChangesAsync(string repo, List<Models.Change> changes,
8181
{
8282
var pathSpecFile = Path.GetTempFileName();
8383
await File.WriteAllLinesAsync(pathSpecFile, restores).ConfigureAwait(false);
84-
await new Restore(repo, pathSpecFile, false) { Log = log }.ExecAsync().ConfigureAwait(false);
84+
await new Restore(repo, pathSpecFile, false).Use(log).ExecAsync().ConfigureAwait(false);
8585
File.Delete(pathSpecFile);
8686
}
8787
}

src/Commands/GitFlow.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ public static async Task<bool> InitAsync(string repo, string master, string deve
2121
init.WorkingDirectory = repo;
2222
init.Context = repo;
2323
init.Args = "flow init -d";
24-
init.Log = log;
25-
return await init.ExecAsync().ConfigureAwait(false);
24+
return await init.Use(log).ExecAsync().ConfigureAwait(false);
2625
}
2726

2827
public static async Task<bool> StartAsync(string repo, Models.GitFlowBranchType type, string name, Models.ICommandLog log)
@@ -47,8 +46,7 @@ public static async Task<bool> StartAsync(string repo, Models.GitFlowBranchType
4746
return false;
4847
}
4948

50-
start.Log = log;
51-
return await start.ExecAsync().ConfigureAwait(false);
49+
return await start.Use(log).ExecAsync().ConfigureAwait(false);
5250
}
5351

5452
public static async Task<bool> FinishAsync(string repo, Models.GitFlowBranchType type, string name, bool squash, bool push, bool keepBranch, Models.ICommandLog log)
@@ -85,8 +83,7 @@ public static async Task<bool> FinishAsync(string repo, Models.GitFlowBranchType
8583
finish.WorkingDirectory = repo;
8684
finish.Context = repo;
8785
finish.Args = builder.ToString();
88-
finish.Log = log;
89-
return await finish.ExecAsync().ConfigureAwait(false);
86+
return await finish.Use(log).ExecAsync().ConfigureAwait(false);
9087
}
9188
}
9289
}

src/Commands/LFS.cs

Lines changed: 43 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,70 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.IO;
3+
using System.Text;
44
using System.Text.RegularExpressions;
55
using System.Threading.Tasks;
66

77
namespace SourceGit.Commands
88
{
9-
public partial class LFS
9+
public partial class LFS : Command
1010
{
1111
[GeneratedRegex(@"^(.+)\s+([\w.]+)\s+\w+:(\d+)$")]
1212
private static partial Regex REG_LOCK();
1313

14-
private class SubCmd : Command
15-
{
16-
public SubCmd(string repo, string args, Models.ICommandLog log)
17-
{
18-
WorkingDirectory = repo;
19-
Context = repo;
20-
Args = args;
21-
Log = log;
22-
}
23-
24-
public async Task<Result> ReadAsync()
25-
{
26-
return await ReadToEndAsync().ConfigureAwait(false);
27-
}
28-
}
29-
3014
public LFS(string repo)
3115
{
32-
_repo = repo;
16+
WorkingDirectory = repo;
17+
Context = repo;
3318
}
3419

35-
public bool IsEnabled()
20+
public async Task<bool> InstallAsync()
3621
{
37-
var path = Path.Combine(_repo, ".git", "hooks", "pre-push");
38-
if (!File.Exists(path))
39-
return false;
40-
41-
var content = File.ReadAllText(path);
42-
return content.Contains("git lfs pre-push");
22+
Args = "lfs install --local";
23+
return await ExecAsync().ConfigureAwait(false);
4324
}
4425

45-
public async Task<bool> InstallAsync(Models.ICommandLog log)
26+
public async Task<bool> TrackAsync(string pattern, bool isFilenameMode)
4627
{
47-
return await new SubCmd(_repo, "lfs install --local", log).ExecAsync().ConfigureAwait(false);
48-
}
28+
var builder = new StringBuilder();
29+
builder.Append("lfs track ");
30+
builder.Append(isFilenameMode ? "--filename " : string.Empty);
31+
builder.Append(pattern.Quoted());
4932

50-
public async Task<bool> TrackAsync(string pattern, bool isFilenameMode, Models.ICommandLog log)
51-
{
52-
var opt = isFilenameMode ? "--filename" : "";
53-
return await new SubCmd(_repo, $"lfs track {opt} {pattern.Quoted()}", log).ExecAsync().ConfigureAwait(false);
33+
Args = builder.ToString();
34+
return await ExecAsync().ConfigureAwait(false);
5435
}
5536

56-
public async Task FetchAsync(string remote, Models.ICommandLog log)
37+
public async Task FetchAsync(string remote)
5738
{
58-
await new SubCmd(_repo, $"lfs fetch {remote}", log).ExecAsync().ConfigureAwait(false);
39+
Args = $"lfs fetch {remote}";
40+
await ExecAsync().ConfigureAwait(false);
5941
}
6042

61-
public async Task PullAsync(string remote, Models.ICommandLog log)
43+
public async Task PullAsync(string remote)
6244
{
63-
await new SubCmd(_repo, $"lfs pull {remote}", log).ExecAsync().ConfigureAwait(false);
45+
Args = $"lfs pull {remote}";
46+
await ExecAsync().ConfigureAwait(false);
6447
}
6548

66-
public async Task PushAsync(string remote, Models.ICommandLog log)
49+
public async Task PushAsync(string remote)
6750
{
68-
await new SubCmd(_repo, $"lfs push {remote}", log).ExecAsync().ConfigureAwait(false);
51+
Args = $"lfs push {remote}";
52+
await ExecAsync().ConfigureAwait(false);
6953
}
7054

71-
public async Task PruneAsync(Models.ICommandLog log)
55+
public async Task PruneAsync()
7256
{
73-
await new SubCmd(_repo, "lfs prune", log).ExecAsync().ConfigureAwait(false);
57+
Args = "lfs prune";
58+
await ExecAsync().ConfigureAwait(false);
7459
}
7560

7661
public async Task<List<Models.LFSLock>> GetLocksAsync(string remote)
7762
{
63+
Args = $"lfs locks --remote={remote}";
64+
65+
var rs = await ReadToEndAsync().ConfigureAwait(false);
7866
var locks = new List<Models.LFSLock>();
79-
var cmd = new SubCmd(_repo, $"lfs locks --remote={remote}", null);
80-
var rs = await cmd.ReadAsync().ConfigureAwait(false);
67+
8168
if (rs.IsSuccess)
8269
{
8370
var lines = rs.StdOut.Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries);
@@ -99,23 +86,23 @@ public async Task PruneAsync(Models.ICommandLog log)
9986
return locks;
10087
}
10188

102-
public async Task<bool> LockAsync(string remote, string file, Models.ICommandLog log)
89+
public async Task<bool> LockAsync(string remote, string file)
10390
{
104-
return await new SubCmd(_repo, $"lfs lock --remote={remote} {file.Quoted()}", log).ExecAsync().ConfigureAwait(false);
91+
Args = $"lfs lock --remote={remote} {file.Quoted()}";
92+
return await ExecAsync().ConfigureAwait(false);
10593
}
10694

107-
public async Task<bool> UnlockAsync(string remote, string file, bool force, Models.ICommandLog log)
95+
public async Task<bool> UnlockAsync(string remote, string file, bool force)
10896
{
109-
var opt = force ? "-f" : "";
110-
return await new SubCmd(_repo, $"lfs unlock --remote={remote} {opt} {file.Quoted()}", log).ExecAsync().ConfigureAwait(false);
97+
var builder = new StringBuilder();
98+
builder
99+
.Append("lfs unlock --remote=")
100+
.Append(remote)
101+
.Append(force ? " -f " : " ")
102+
.Append(file.Quoted());
103+
104+
Args = builder.ToString();
105+
return await ExecAsync().ConfigureAwait(false);
111106
}
112-
113-
public async Task<bool> UnlockAsync(string remote, long id, bool force, Models.ICommandLog log)
114-
{
115-
var opt = force ? "-f" : "";
116-
return await new SubCmd(_repo, $"lfs unlock --remote={remote} {opt} --id={id}", log).ExecAsync().ConfigureAwait(false);
117-
}
118-
119-
private readonly string _repo;
120107
}
121108
}

src/Commands/MergeTool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public MergeTool(string repo, int type, string exec, string file)
1212

1313
_merger = Models.ExternalMerger.Supported.Find(x => x.Type == type);
1414
_exec = exec;
15-
_file = string.IsNullOrEmpty(file) ? "" : $"{file.Quoted()}";
15+
_file = string.IsNullOrEmpty(file) ? string.Empty : file.Quoted();
1616
}
1717

1818
public async Task<bool> OpenAsync()

src/Commands/Tag.cs

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,59 @@
11
using System.IO;
2+
using System.Text;
23
using System.Threading.Tasks;
34

45
namespace SourceGit.Commands
56
{
6-
public static class Tag
7+
public class Tag : Command
78
{
8-
public static async Task<bool> AddAsync(string repo, string name, string basedOn, Models.ICommandLog log)
9+
public Tag(string repo, string name)
910
{
10-
var cmd = new Command();
11-
cmd.WorkingDirectory = repo;
12-
cmd.Context = repo;
13-
cmd.Args = $"tag --no-sign {name} {basedOn}";
14-
cmd.Log = log;
15-
return await cmd.ExecAsync().ConfigureAwait(false);
11+
WorkingDirectory = repo;
12+
Context = repo;
13+
_name = name;
1614
}
1715

18-
public static async Task<bool> AddAsync(string repo, string name, string basedOn, string message, bool sign, Models.ICommandLog log)
16+
public async Task<bool> AddAsync(string basedOn)
1917
{
20-
var param = sign ? "--sign -a" : "--no-sign -a";
21-
var cmd = new Command();
22-
cmd.WorkingDirectory = repo;
23-
cmd.Context = repo;
24-
cmd.Args = $"tag {param} {name} {basedOn} ";
25-
cmd.Log = log;
18+
Args = $"tag --no-sign {_name} {basedOn}";
19+
return await ExecAsync().ConfigureAwait(false);
20+
}
21+
22+
public async Task<bool> AddAsync(string basedOn, string message, bool sign)
23+
{
24+
var builder = new StringBuilder();
25+
builder
26+
.Append("tag ")
27+
.Append(sign ? "--sign -a " : "--no-sign -a ")
28+
.Append(_name)
29+
.Append(' ')
30+
.Append(basedOn);
2631

2732
if (!string.IsNullOrEmpty(message))
2833
{
2934
string tmp = Path.GetTempFileName();
3035
await File.WriteAllTextAsync(tmp, message);
31-
cmd.Args += $"-F {tmp.Quoted()}";
36+
builder.Append(" -F ").Append(tmp.Quoted());
3237

33-
var succ = await cmd.ExecAsync().ConfigureAwait(false);
38+
Args = builder.ToString();
39+
var succ = await ExecAsync().ConfigureAwait(false);
3440
File.Delete(tmp);
3541
return succ;
3642
}
3743

38-
cmd.Args += $"-m {name}";
39-
return await cmd.ExecAsync().ConfigureAwait(false);
44+
builder.Append(" -m ");
45+
builder.Append(_name);
46+
47+
Args = builder.ToString();
48+
return await ExecAsync().ConfigureAwait(false);
4049
}
4150

42-
public static async Task<bool> DeleteAsync(string repo, string name, Models.ICommandLog log)
51+
public async Task<bool> DeleteAsync()
4352
{
44-
var cmd = new Command();
45-
cmd.WorkingDirectory = repo;
46-
cmd.Context = repo;
47-
cmd.Args = $"tag --delete {name}";
48-
cmd.Log = log;
49-
return await cmd.ExecAsync().ConfigureAwait(false);
53+
Args = $"tag --delete {_name}";
54+
return await ExecAsync().ConfigureAwait(false);
5055
}
56+
57+
private readonly string _name;
5158
}
5259
}

src/Models/DiffOption.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ public override string ToString()
106106

107107
builder.Append("-- ");
108108
if (!string.IsNullOrEmpty(_orgPath))
109-
builder.Append($"\"{_orgPath}\" ");
110-
builder.Append($"\"{_path}\"");
109+
builder.Append($"{_orgPath.Quoted()} ");
110+
builder.Append(_path.Quoted());
111111

112112
return builder.ToString();
113113
}

0 commit comments

Comments
 (0)