Skip to content

Commit 8b39df3

Browse files
committed
feature: git command logs
Signed-off-by: leo <[email protected]>
1 parent 928a0ad commit 8b39df3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+1040
-573
lines changed

src/App.Utils.cs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace SourceGit
2+
{
3+
public static class CommandExtensions
4+
{
5+
public static T Use<T>(this T cmd, Models.ICommandLog log) where T : Commands.Command
6+
{
7+
cmd.Log = log;
8+
return cmd;
9+
}
10+
}
11+
}

src/Commands/Archive.cs

+2-13
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,12 @@
1-
using System;
2-
3-
namespace SourceGit.Commands
1+
namespace SourceGit.Commands
42
{
53
public class Archive : Command
64
{
7-
public Archive(string repo, string revision, string saveTo, Action<string> outputHandler)
5+
public Archive(string repo, string revision, string saveTo)
86
{
97
WorkingDirectory = repo;
108
Context = repo;
119
Args = $"archive --format=zip --verbose --output=\"{saveTo}\" {revision}";
12-
TraitErrorAsOutput = true;
13-
_outputHandler = outputHandler;
1410
}
15-
16-
protected override void OnReadline(string line)
17-
{
18-
_outputHandler?.Invoke(line);
19-
}
20-
21-
private readonly Action<string> _outputHandler;
2211
}
2312
}

src/Commands/AssumeUnchanged.cs

+7-68
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,14 @@
1-
using System.Collections.Generic;
2-
using System.Text.RegularExpressions;
3-
4-
namespace SourceGit.Commands
1+
namespace SourceGit.Commands
52
{
6-
public partial class AssumeUnchanged
3+
public class AssumeUnchanged : Command
74
{
8-
[GeneratedRegex(@"^(\w)\s+(.+)$")]
9-
private static partial Regex REG_PARSE();
10-
11-
class ViewCommand : Command
12-
{
13-
public ViewCommand(string repo)
14-
{
15-
WorkingDirectory = repo;
16-
Args = "ls-files -v";
17-
RaiseError = false;
18-
}
19-
20-
public List<string> Result()
21-
{
22-
Exec();
23-
return _outs;
24-
}
25-
26-
protected override void OnReadline(string line)
27-
{
28-
var match = REG_PARSE().Match(line);
29-
if (!match.Success)
30-
return;
31-
32-
if (match.Groups[1].Value == "h")
33-
{
34-
_outs.Add(match.Groups[2].Value);
35-
}
36-
}
37-
38-
private readonly List<string> _outs = new List<string>();
39-
}
40-
41-
class ModCommand : Command
42-
{
43-
public ModCommand(string repo, string file, bool bAdd)
44-
{
45-
var mode = bAdd ? "--assume-unchanged" : "--no-assume-unchanged";
46-
47-
WorkingDirectory = repo;
48-
Context = repo;
49-
Args = $"update-index {mode} -- \"{file}\"";
50-
}
51-
}
52-
53-
public AssumeUnchanged(string repo)
5+
public AssumeUnchanged(string repo, string file, bool bAdd)
546
{
55-
_repo = repo;
56-
}
7+
var mode = bAdd ? "--assume-unchanged" : "--no-assume-unchanged";
578

58-
public List<string> View()
59-
{
60-
return new ViewCommand(_repo).Result();
9+
WorkingDirectory = repo;
10+
Context = repo;
11+
Args = $"update-index {mode} -- \"{file}\"";
6112
}
62-
63-
public void Add(string file)
64-
{
65-
new ModCommand(_repo, file, true).Exec();
66-
}
67-
68-
public void Remove(string file)
69-
{
70-
new ModCommand(_repo, file, false).Exec();
71-
}
72-
73-
private readonly string _repo;
7413
}
7514
}

src/Commands/Blame.cs

+11-9
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,17 @@ public Blame(string repo, string file, string revision)
2121

2222
public Models.BlameData Result()
2323
{
24-
var succ = Exec();
25-
if (!succ)
24+
var rs = ReadToEnd();
25+
if (!rs.IsSuccess)
26+
return _result;
27+
28+
var lines = rs.StdOut.Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries);
29+
foreach (var line in lines)
2630
{
27-
return new Models.BlameData();
31+
ParseLine(line);
32+
33+
if (_result.IsBinary)
34+
break;
2835
}
2936

3037
if (_needUnifyCommitSHA)
@@ -42,13 +49,8 @@ public Models.BlameData Result()
4249
return _result;
4350
}
4451

45-
protected override void OnReadline(string line)
52+
private void ParseLine(string line)
4653
{
47-
if (_result.IsBinary)
48-
return;
49-
if (string.IsNullOrEmpty(line))
50-
return;
51-
5254
if (line.IndexOf('\0', StringComparison.Ordinal) >= 0)
5355
{
5456
_result.IsBinary = true;

src/Commands/Branch.cs

+11-6
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,32 @@ public static string ShowCurrent(string repo)
1111
return cmd.ReadToEnd().StdOut.Trim();
1212
}
1313

14-
public static bool Create(string repo, string name, string basedOn)
14+
public static bool Create(string repo, string name, string basedOn, Models.ICommandLog log)
1515
{
1616
var cmd = new Command();
1717
cmd.WorkingDirectory = repo;
1818
cmd.Context = repo;
1919
cmd.Args = $"branch {name} {basedOn}";
20+
cmd.Log = log;
2021
return cmd.Exec();
2122
}
2223

23-
public static bool Rename(string repo, string name, string to)
24+
public static bool Rename(string repo, string name, string to, Models.ICommandLog log)
2425
{
2526
var cmd = new Command();
2627
cmd.WorkingDirectory = repo;
2728
cmd.Context = repo;
2829
cmd.Args = $"branch -M {name} {to}";
30+
cmd.Log = log;
2931
return cmd.Exec();
3032
}
3133

32-
public static bool SetUpstream(string repo, string name, string upstream)
34+
public static bool SetUpstream(string repo, string name, string upstream, Models.ICommandLog log)
3335
{
3436
var cmd = new Command();
3537
cmd.WorkingDirectory = repo;
3638
cmd.Context = repo;
39+
cmd.Log = log;
3740

3841
if (string.IsNullOrEmpty(upstream))
3942
cmd.Args = $"branch {name} --unset-upstream";
@@ -43,25 +46,27 @@ public static bool SetUpstream(string repo, string name, string upstream)
4346
return cmd.Exec();
4447
}
4548

46-
public static bool DeleteLocal(string repo, string name)
49+
public static bool DeleteLocal(string repo, string name, Models.ICommandLog log)
4750
{
4851
var cmd = new Command();
4952
cmd.WorkingDirectory = repo;
5053
cmd.Context = repo;
5154
cmd.Args = $"branch -D {name}";
55+
cmd.Log = log;
5256
return cmd.Exec();
5357
}
5458

55-
public static bool DeleteRemote(string repo, string remote, string name)
59+
public static bool DeleteRemote(string repo, string remote, string name, Models.ICommandLog log)
5660
{
5761
bool exists = new Remote(repo).HasBranch(remote, name);
5862
if (exists)
59-
return new Push(repo, remote, $"refs/heads/{name}", true).Exec();
63+
return new Push(repo, remote, $"refs/heads/{name}", true).Use(log).Exec();
6064

6165
var cmd = new Command();
6266
cmd.WorkingDirectory = repo;
6367
cmd.Context = repo;
6468
cmd.Args = $"branch -D -r {remote}/{name}";
69+
cmd.Log = log;
6570
return cmd.Exec();
6671
}
6772
}

src/Commands/Checkout.cs

+4-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.Text;
43

54
namespace SourceGit.Commands
@@ -12,19 +11,15 @@ public Checkout(string repo)
1211
Context = repo;
1312
}
1413

15-
public bool Branch(string branch, Action<string> onProgress)
14+
public bool Branch(string branch)
1615
{
1716
Args = $"checkout --recurse-submodules --progress {branch}";
18-
TraitErrorAsOutput = true;
19-
_outputHandler = onProgress;
2017
return Exec();
2118
}
2219

23-
public bool Branch(string branch, string basedOn, Action<string> onProgress)
20+
public bool Branch(string branch, string basedOn)
2421
{
2522
Args = $"checkout --recurse-submodules --progress -b {branch} {basedOn}";
26-
TraitErrorAsOutput = true;
27-
_outputHandler = onProgress;
2823
return Exec();
2924
}
3025

@@ -62,19 +57,10 @@ public bool FileWithRevision(string file, string revision)
6257
return Exec();
6358
}
6459

65-
public bool Commit(string commitId, Action<string> onProgress)
60+
public bool Commit(string commitId)
6661
{
6762
Args = $"checkout --detach --progress {commitId}";
68-
TraitErrorAsOutput = true;
69-
_outputHandler = onProgress;
7063
return Exec();
7164
}
72-
73-
protected override void OnReadline(string line)
74-
{
75-
_outputHandler?.Invoke(line);
76-
}
77-
78-
private Action<string> _outputHandler;
7965
}
8066
}

src/Commands/Clone.cs

+2-14
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
using System;
2-
3-
namespace SourceGit.Commands
1+
namespace SourceGit.Commands
42
{
53
public class Clone : Command
64
{
7-
private readonly Action<string> _notifyProgress;
8-
9-
public Clone(string ctx, string path, string url, string localName, string sshKey, string extraArgs, Action<string> ouputHandler)
5+
public Clone(string ctx, string path, string url, string localName, string sshKey, string extraArgs)
106
{
117
Context = ctx;
128
WorkingDirectory = path;
13-
TraitErrorAsOutput = true;
149
SSHKey = sshKey;
1510
Args = "clone --progress --verbose ";
1611

@@ -21,13 +16,6 @@ public Clone(string ctx, string path, string url, string localName, string sshKe
2116

2217
if (!string.IsNullOrEmpty(localName))
2318
Args += localName;
24-
25-
_notifyProgress = ouputHandler;
26-
}
27-
28-
protected override void OnReadline(string line)
29-
{
30-
_notifyProgress?.Invoke(line);
3119
}
3220
}
3321
}

src/Commands/Command.cs

+10-10
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,22 @@ public enum EditorType
3232
public string SSHKey { get; set; } = string.Empty;
3333
public string Args { get; set; } = string.Empty;
3434
public bool RaiseError { get; set; } = true;
35-
public bool TraitErrorAsOutput { get; set; } = false;
35+
public Models.ICommandLog Log { get; set; } = null;
3636

3737
public bool Exec()
3838
{
3939
var start = CreateGitStartInfo();
4040
var errs = new List<string>();
4141
var proc = new Process() { StartInfo = start };
4242

43+
Log?.AppendLine($"$ git {Args}\n");
44+
4345
proc.OutputDataReceived += (_, e) =>
4446
{
45-
if (e.Data != null)
46-
OnReadline(e.Data);
47+
if (e.Data == null)
48+
return;
49+
50+
Log?.AppendLine(e.Data);
4751
};
4852

4953
proc.ErrorDataReceived += (_, e) =>
@@ -54,8 +58,7 @@ public bool Exec()
5458
return;
5559
}
5660

57-
if (TraitErrorAsOutput)
58-
OnReadline(e.Data);
61+
Log?.AppendLine(e.Data);
5962

6063
// Ignore progress messages
6164
if (e.Data.StartsWith("remote: Enumerating objects:", StringComparison.Ordinal))
@@ -97,6 +100,7 @@ public bool Exec()
97100
if (RaiseError)
98101
Dispatcher.UIThread.Post(() => App.RaiseException(Context, e.Message));
99102

103+
Log?.AppendLine(string.Empty);
100104
return false;
101105
}
102106

@@ -114,6 +118,7 @@ public bool Exec()
114118

115119
int exitCode = proc.ExitCode;
116120
proc.Close();
121+
Log?.AppendLine(string.Empty);
117122

118123
if (!CancellationToken.IsCancellationRequested && exitCode != 0)
119124
{
@@ -162,11 +167,6 @@ public ReadToEndResult ReadToEnd()
162167
return rs;
163168
}
164169

165-
protected virtual void OnReadline(string line)
166-
{
167-
// Implemented by derived class
168-
}
169-
170170
private ProcessStartInfo CreateGitStartInfo()
171171
{
172172
var start = new ProcessStartInfo();

src/Commands/Commit.cs

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ public Commit(string repo, string message, bool amend, bool signOff)
1111

1212
WorkingDirectory = repo;
1313
Context = repo;
14-
TraitErrorAsOutput = true;
1514
Args = $"commit --allow-empty --file=\"{_tmpFile}\"";
1615
if (amend)
1716
Args += " --amend --no-edit";

0 commit comments

Comments
 (0)