Skip to content

Commit

Permalink
完成更多方法
Browse files Browse the repository at this point in the history
  • Loading branch information
lindexi committed May 11, 2024
1 parent 8b95e73 commit a0e77f1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
14 changes: 14 additions & 0 deletions package/GitCommand/GitCommand.Tests/GitTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System.IO;
using System.Threading;

using Lindexi.Src.GitCommand;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using MSTest.Extensions.Contracts;

namespace GitCommand.Tests
Expand Down Expand Up @@ -44,5 +47,16 @@ public void GetCurrentCommit()
Assert.AreEqual(false, currentCommit.Contains('\n'));
});
}

[ContractTestCase]
public void GetGitCommitRevisionCount()
{
"获取当前的 git 的 commit 数量,可以获取成功".Test(() =>
{
var git = new Git(new DirectoryInfo("."));
var count = git.GetGitCommitRevisionCount();
Assert.AreNotEqual(0, count);
});
}
}
}
24 changes: 21 additions & 3 deletions package/GitCommand/GitCommand/Git.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ public string[] GetLogCommit()
public string GetCurrentCommit()
{
var (success, output) = ExecuteCommand("rev-parse HEAD");

if (!success)
{
return string.Empty;
}

return output.Trim('\n');
}

Expand All @@ -78,10 +84,14 @@ public string GetCurrentCommit()
/// <returns></returns>
public int GetGitCommitRevisionCount()
{
var control = Control("rev-list --count HEAD");
var str = control.Split("\n", StringSplitOptions.RemoveEmptyEntries).Select(temp => temp.Replace("\r", "")).Where(temp => !string.IsNullOrEmpty(temp)).Reverse().FirstOrDefault();
var (success, output) = ExecuteCommand("rev-list --count HEAD");

if (!success)
{
return -1;
}

if (int.TryParse(str, out var count))
if (int.TryParse(output, out var count))
{
return count;
}
Expand Down Expand Up @@ -452,6 +462,14 @@ public string GetCurrentBranch()
exitCode = errorList.Count > 0 ? -1 : 0;
}

if (outputList.Count > 0)
{
if (outputList[^1] is null)
{
outputList.RemoveAt(outputList.Count - 1);
}
}

var output = string.Join('\n', outputList);
return (exitCode == 0, output);
}
Expand Down

0 comments on commit a0e77f1

Please sign in to comment.