Skip to content
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
5 changes: 3 additions & 2 deletions src/Commands/Blame.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace SourceGit.Commands
{
Expand All @@ -19,9 +20,9 @@ public Blame(string repo, string file, string revision)
_result.File = file;
}

public Models.BlameData Result()
public async Task<Models.BlameData> ResultAsync()
{
var rs = ReadToEnd();
var rs = await ReadToEndAsync();
if (!rs.IsSuccess)
return _result;

Expand Down
34 changes: 22 additions & 12 deletions src/Commands/Branch.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text;
using System.Threading.Tasks;

namespace SourceGit.Commands
{
Expand All @@ -13,7 +14,16 @@ public static string ShowCurrent(string repo)
return cmd.ReadToEnd().StdOut.Trim();
}

public static bool Create(string repo, string name, string basedOn, bool force, Models.ICommandLog log)
public static async Task<string> ShowCurrentAsync(string repo)
{
var cmd = new Command();
cmd.WorkingDirectory = repo;
cmd.Context = repo;
cmd.Args = "branch --show-current";
return (await cmd.ReadToEndAsync()).StdOut.Trim();
}

public static async Task<bool> CreateAsync(string repo, string name, string basedOn, bool force, Models.ICommandLog log)
{
var builder = new StringBuilder();
builder.Append("branch ");
Expand All @@ -28,20 +38,20 @@ public static bool Create(string repo, string name, string basedOn, bool force,
cmd.Context = repo;
cmd.Args = builder.ToString();
cmd.Log = log;
return cmd.Exec();
return await cmd.ExecAsync();
}

public static bool Rename(string repo, string name, string to, Models.ICommandLog log)
public static async Task<bool> RenameAsync(string repo, string name, string to, Models.ICommandLog log)
{
var cmd = new Command();
cmd.WorkingDirectory = repo;
cmd.Context = repo;
cmd.Args = $"branch -M {name} {to}";
cmd.Log = log;
return cmd.Exec();
return await cmd.ExecAsync();
}

public static bool SetUpstream(string repo, string name, string upstream, Models.ICommandLog log)
public static async Task<bool> SetUpstreamAsync(string repo, string name, string upstream, Models.ICommandLog log)
{
var cmd = new Command();
cmd.WorkingDirectory = repo;
Expand All @@ -53,31 +63,31 @@ public static bool SetUpstream(string repo, string name, string upstream, Models
else
cmd.Args = $"branch {name} -u {upstream}";

return cmd.Exec();
return await cmd.ExecAsync();
}

public static bool DeleteLocal(string repo, string name, Models.ICommandLog log)
public static async Task<bool> DeleteLocalAsync(string repo, string name, Models.ICommandLog log)
{
var cmd = new Command();
cmd.WorkingDirectory = repo;
cmd.Context = repo;
cmd.Args = $"branch -D {name}";
cmd.Log = log;
return cmd.Exec();
return await cmd.ExecAsync();
}

public static bool DeleteRemote(string repo, string remote, string name, Models.ICommandLog log)
public static async Task<bool> DeleteRemoteAsync(string repo, string remote, string name, Models.ICommandLog log)
{
bool exists = new Remote(repo).HasBranch(remote, name);
bool exists = await new Remote(repo).HasBranchAsync(remote, name);
if (exists)
return new Push(repo, remote, $"refs/heads/{name}", true) { Log = log }.Exec();
return await new Push(repo, remote, $"refs/heads/{name}", true) { Log = log }.ExecAsync();

var cmd = new Command();
cmd.WorkingDirectory = repo;
cmd.Context = repo;
cmd.Args = $"branch -D -r {remote}/{name}";
cmd.Log = log;
return cmd.Exec();
return await cmd.ExecAsync();
}
}
}
26 changes: 14 additions & 12 deletions src/Commands/Checkout.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace SourceGit.Commands
{
Expand All @@ -11,7 +12,7 @@ public Checkout(string repo)
Context = repo;
}

public bool Branch(string branch, bool force)
public async Task<bool> BranchAsync(string branch, bool force)
{
var builder = new StringBuilder();
builder.Append("checkout --progress ");
Expand All @@ -20,10 +21,10 @@ public bool Branch(string branch, bool force)
builder.Append(branch);

Args = builder.ToString();
return Exec();
return await ExecAsync();
}

public bool Branch(string branch, string basedOn, bool force, bool allowOverwrite)
public async Task<bool> BranchAsync(string branch, string basedOn, bool force, bool allowOverwrite)
{
var builder = new StringBuilder();
builder.Append("checkout --progress ");
Expand All @@ -35,17 +36,17 @@ public bool Branch(string branch, string basedOn, bool force, bool allowOverwrit
builder.Append(basedOn);

Args = builder.ToString();
return Exec();
return await ExecAsync();
}

public bool Commit(string commitId, bool force)
public async Task<bool> CommitAsync(string commitId, bool force)
{
var option = force ? "--force" : string.Empty;
Args = $"checkout {option} --detach --progress {commitId}";
return Exec();
return await ExecAsync();
}

public bool UseTheirs(List<string> files)
public async Task<bool> UseTheirsAsync(List<string> files)
{
var builder = new StringBuilder();
builder.Append("checkout --theirs --");
Expand All @@ -56,10 +57,10 @@ public bool UseTheirs(List<string> files)
builder.Append("\"");
}
Args = builder.ToString();
return Exec();
return await ExecAsync();
}

public bool UseMine(List<string> files)
public async Task<bool> UseMineAsync(List<string> files)
{
var builder = new StringBuilder();
builder.Append("checkout --ours --");
Expand All @@ -69,14 +70,15 @@ public bool UseMine(List<string> files)
builder.Append(f);
builder.Append("\"");
}

Args = builder.ToString();
return Exec();
return await ExecAsync();
}

public bool FileWithRevision(string file, string revision)
public async Task<bool> FileWithRevisionAsync(string file, string revision)
{
Args = $"checkout --no-overlay {revision} -- \"{file}\"";
return Exec();
return await ExecAsync();
}
}
}
45 changes: 39 additions & 6 deletions src/Commands/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

namespace SourceGit.Commands
{
Expand Down Expand Up @@ -32,7 +33,39 @@ public enum EditorType
public bool RaiseError { get; set; } = true;
public Models.ICommandLog Log { get; set; } = null;

public bool Exec()
public ReadToEndResult ReadToEnd()
{
var start = CreateGitStartInfo();
var proc = new Process() { StartInfo = start };

try
{
proc.Start();
}
catch (Exception e)
{
return new ReadToEndResult()
{
IsSuccess = false,
StdOut = string.Empty,
StdErr = e.Message,
};
}

var rs = new ReadToEndResult()
{
StdOut = proc.StandardOutput.ReadToEnd(),
StdErr = proc.StandardError.ReadToEnd(),
};

proc.WaitForExit();
rs.IsSuccess = proc.ExitCode == 0;
proc.Close();

return rs;
}

public async Task<bool> ExecAsync()
{
Log?.AppendLine($"$ git {Args}\n");

Expand Down Expand Up @@ -74,7 +107,7 @@ public bool Exec()

proc.BeginOutputReadLine();
proc.BeginErrorReadLine();
proc.WaitForExit();
await proc.WaitForExitAsync(CancellationToken);

if (dummy != null)
{
Expand Down Expand Up @@ -103,7 +136,7 @@ public bool Exec()
return true;
}

public ReadToEndResult ReadToEnd()
public async Task<ReadToEndResult> ReadToEndAsync()
{
var start = CreateGitStartInfo();
var proc = new Process() { StartInfo = start };
Expand All @@ -124,11 +157,11 @@ public ReadToEndResult ReadToEnd()

var rs = new ReadToEndResult()
{
StdOut = proc.StandardOutput.ReadToEnd(),
StdErr = proc.StandardError.ReadToEnd(),
StdOut = await proc.StandardOutput.ReadToEndAsync(CancellationToken),
StdErr = await proc.StandardError.ReadToEndAsync(CancellationToken),
};

proc.WaitForExit();
await proc.WaitForExitAsync(CancellationToken);
rs.IsSuccess = proc.ExitCode == 0;
proc.Close();

Expand Down
5 changes: 3 additions & 2 deletions src/Commands/Commit.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System.Threading.Tasks;

namespace SourceGit.Commands
{
Expand All @@ -18,9 +19,9 @@ public Commit(string repo, string message, bool signOff, bool amend, bool resetA
Args += resetAuthor ? " --amend --reset-author --no-edit" : " --amend --no-edit";
}

public bool Run()
public async Task<bool> RunAsync()
{
var succ = Exec();
var succ = await ExecAsync();

try
{
Expand Down
15 changes: 15 additions & 0 deletions src/Commands/CompareRevisions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace SourceGit.Commands
{
Expand Down Expand Up @@ -43,6 +44,20 @@ public CompareRevisions(string repo, string start, string end, string path)
return _changes;
}

public async Task<List<Models.Change>> ResultAsync()
{
var rs = await ReadToEndAsync();
if (!rs.IsSuccess)
return _changes;

var lines = rs.StdOut.Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
ParseLine(line);

_changes.Sort((l, r) => Models.NumericSort.Compare(l.Path, r.Path));
return _changes;
}

private void ParseLine(string line)
{
var match = REG_FORMAT().Match(line);
Expand Down
35 changes: 33 additions & 2 deletions src/Commands/Config.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace SourceGit.Commands
{
Expand Down Expand Up @@ -51,7 +52,37 @@ public string Get(string key)
return ReadToEnd().StdOut.Trim();
}

public bool Set(string key, string value, bool allowEmpty = false)
public async Task<Dictionary<string, string>> ListAllAsync()
{
Args = "config -l";

var output = await ReadToEndAsync();
var rs = new Dictionary<string, string>();
if (output.IsSuccess)
{
var lines = output.StdOut.Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
{
var idx = line.IndexOf('=', StringComparison.Ordinal);
if (idx != -1)
{
var key = line.Substring(0, idx).Trim();
var val = line.Substring(idx + 1).Trim();
rs[key] = val;
}
}
}

return rs;
}

public async Task<string> GetAsync(string key)
{
Args = $"config {key}";
return (await ReadToEndAsync()).StdOut.Trim();
}

public async Task<bool> SetAsync(string key, string value, bool allowEmpty = false)
{
var scope = _isLocal ? "--local" : "--global";

Expand All @@ -60,7 +91,7 @@ public bool Set(string key, string value, bool allowEmpty = false)
else
Args = $"config {scope} {key} \"{value}\"";

return Exec();
return await ExecAsync();
}

private bool _isLocal = false;
Expand Down
5 changes: 3 additions & 2 deletions src/Commands/CountLocalChangesWithoutUntracked.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;

namespace SourceGit.Commands
{
Expand All @@ -11,9 +12,9 @@ public CountLocalChangesWithoutUntracked(string repo)
Args = "--no-optional-locks status -uno --ignore-submodules=all --porcelain";
}

public int Result()
public async Task<int> ResultAsync()
{
var rs = ReadToEnd();
var rs = await ReadToEndAsync();
if (rs.IsSuccess)
{
var lines = rs.StdOut.Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries);
Expand Down
Loading