-
Notifications
You must be signed in to change notification settings - Fork 0
/
GitService.cs
57 lines (47 loc) · 1.5 KB
/
GitService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System.IO;
using CliWrap;
using CliWrap.Buffered;
using NLog;
namespace RunningLog;
public class GitService(string repoDir)
{
public string RepoDir { get; set; } = repoDir;
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
public async Task<string> GetGitStatus()
{
var result = await Cli.Wrap("git")
.WithArguments("status --porcelain")
.WithWorkingDirectory(RepoDir)
.ExecuteBufferedAsync();
return result.StandardOutput.Trim();
}
public async Task ExecuteGitCommand(string arguments)
{
await Cli.Wrap("git")
.WithArguments(arguments)
.WithWorkingDirectory(RepoDir)
.WithStandardOutputPipe(PipeTarget.ToDelegate(s => _logger.Debug(s)))
.WithStandardErrorPipe(PipeTarget.ToDelegate(s => _logger.Debug(s)))
.ExecuteAsync();
}
public async Task CommitChanges(string message)
{
await ExecuteGitCommand($"commit -a -m \"{message}\"");
}
public async Task PushChanges()
{
await ExecuteGitCommand("push");
}
public async Task<string> GetUnpushedCommits()
{
var result = await Cli.Wrap("git")
.WithArguments("log @{u}..HEAD --oneline")
.WithWorkingDirectory(RepoDir)
.ExecuteBufferedAsync();
return result.StandardOutput.Trim();
}
public bool IsGitRepository()
{
return Directory.Exists(Path.Combine(RepoDir, ".git"));
}
}