diff --git a/package/GitCommand/GitCommand/Git.cs b/package/GitCommand/GitCommand/Git.cs
index 19cd4a1..ea5b880 100644
--- a/package/GitCommand/GitCommand/Git.cs
+++ b/package/GitCommand/GitCommand/Git.cs
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
+using System.Linq;
using System.Text;
namespace Lindexi.Src.GitCommand
@@ -15,109 +16,74 @@ static Git()
#endif
}
+ ///
public Git(DirectoryInfo repo)
{
if (ReferenceEquals(repo, null)) throw new ArgumentNullException(nameof(repo));
if (!Directory.Exists(repo.FullName))
{
// 为什么不使用 repo.Exits 因为这个属性默认没有刷新,也就是在创建 DirectoryInfo 的时候文件夹不存在,那么这个值就是 false 即使后续创建了文件夹也不会刷新,需要调用 Refresh 才可以刷新,但是 Refresh 需要修改很多属性
+ // 详细请看 https://blog.walterlv.com/post/file-exists-vs-fileinfo-exists.html
throw new ArgumentException("必须传入存在的文件夹", nameof(repo));
}
Repo = repo;
}
- public string Add(string file = ".")
+ ///
+ /// 两个版本修改的文件
+ ///
+ /// 可以传入commit或分支
+ /// 可以传入commit或分支
+ public List DiffFile(string source, string target)
{
- file = file.Replace(Repo.FullName, "");
- if (file.StartsWith("\\"))
- {
- file = file.Substring(1);
- }
+ var gitDiffFileList = new List();
- string str = "add " + file;
- return Control(str);
+ return gitDiffFileList;
}
- private string ConvertDate(DateTime time)
+ public string[] GetLogCommit()
{
- //1. 一月 January (Jan)2. 二月 February (Feb)
- //3. 三月 March (Mar)
- //4. 四月 April (Apr)
- //5. 五月 May (May)
- //6. 六月 June (Jun)
- //7. 七月 July (Jul)
- //8. 八月 August (Aug)
- //9. 九月 September (Sep)
- //10. 十月 October (Oct)
- //11. 十一月 November (Nov)12. 十二月 December (Dec)
- List temp = new List()
- {
- "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug",
- "Sep","Oct","Nov","Dec"
- };
-
- //StringBuilder str = new StringBuilder();
- // git commit --date = "月 日 时间 年 +0800" - am "提交"
+ var file = Path.GetTempFileName();
+ Control($"log --pretty=format:\"%H\" > {file}");
- //git commit --date = "May 7 9:05:20 2016 +0800" - am "提交"
- return $"--date=\"{temp[time.Month - 1]} {time.Day} {time.Hour}:{time.Minute}:{time.Second} {time.Year} +0800\" ";
+ return File.ReadAllLines(file);
}
- public string Commit(string str = null, DateTime time = default(DateTime))
+ public string GetCurrentCommit()
{
- string commit = " commit";
- if (time != (default(DateTime)))
+ var file = Path.GetTempFileName();
+ Control($"rev-parse HEAD > \"{file}\"");
+ var commit = File.ReadAllText(file).Trim();
+ try
{
- commit += " " + ConvertDate(time);
+ File.Delete(file);
}
-
- if (string.IsNullOrEmpty(str))
+ catch (Exception)
{
- if (time == default(DateTime))
- {
- time = DateTime.Now;
- }
- str = time.Year + "年" + time.Month + "月" +
- time.Day + "日 " +
- time.Hour + ":" +
- time.Minute + ":" + time.Second;
+
}
- commit += " -m " + "\"" + str + "\"";
- //commit = FileStr() + commit;
- return Control(commit);
- }
- public string Tag(string tag)
- {
- var str = $"tag {tag}";
- return Control(str);
+ return commit;
}
- ///
- /// 两个版本修改的文件
- ///
- /// 可以传入commit或分支
- /// 可以传入commit或分支
- public List DiffFile(string source, string target)
+ public int GetGitCommitRevisionCount()
{
- var gitDiffFileList = new List();
+ 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();
- return gitDiffFileList;
- }
-
- public string[] GetLogCommit()
- {
- var file = Path.GetTempFileName();
- Control($"log --pretty=format:\"%h\" > {file}");
+ if (int.TryParse(str, out var count))
+ {
+ return count;
+ }
- return File.ReadAllLines(file);
+ return 0;
}
public string[] GetLogCommit(string formCommit, string toCommit)
{
var file = Path.GetTempFileName();
- Control($"log --pretty=format:\"%h\" {formCommit}..{toCommit} > {file}");
+ Control($"log --pretty=format:\"%H\" {formCommit}..{toCommit} > {file}");
return File.ReadAllLines(file);
}
@@ -151,7 +117,7 @@ public void Clean()
public void FetchAll()
{
- Control("fetch --all");
+ Control("fetch --all --tags");
}
public DirectoryInfo Repo { get; }
@@ -168,17 +134,25 @@ private string Control(string str)
return str;
}
- private static void WriteLog(string str)
+ private void WriteLog(string str)
{
- Console.WriteLine(str);
+ if (NeedWriteLog)
+ {
+ Console.WriteLine(str);
+ }
}
+ ///
+ /// 是否需要写入日志
+ ///
+ public bool NeedWriteLog { set; get; } = true;
+
private string FileStr()
{
return string.Format(GitStr, Repo.FullName);
}
- private static string Command(string str, string workingDirectory)
+ private string Command(string str, string workingDirectory)
{
// string str = Console.ReadLine();
//System.Console.InputEncoding = System.Text.Encoding.UTF8;//乱码
@@ -250,7 +224,7 @@ private static string Command(string str, string workingDirectory)
// line = reader.ReadLine();
//}
- p.WaitForExit(TimeSpan.FromMinutes(1).Milliseconds); //等待程序执行完退出进程
+ p.WaitForExit((int) DefaultCommandTimeout.TotalMilliseconds); //等待程序执行完退出进程
p.Close();
exited = true;
@@ -258,13 +232,39 @@ private static string Command(string str, string workingDirectory)
return output + "\r\n";
}
+
+ ///
+ /// 默认命令的超时时间
+ ///
+ public TimeSpan DefaultCommandTimeout { set; get; } = TimeSpan.FromMinutes(1);
+
+ ///
+ /// 切换到某个 commit 或分支
+ ///
public void Checkout(string commit)
{
- Control($"checkout {commit}");
+ Checkout(commit, false);
}
///
- /// 创建新分支
+ /// 切换到某个 commit 或分支
+ ///
+ ///
+ /// 是否需要强行切换,加上 -f 命令
+ public void Checkout(string commit, bool shouldHard)
+ {
+ var command = $"checkout {commit}";
+
+ if (shouldHard)
+ {
+ command += " -f";
+ }
+
+ Control(command);
+ }
+
+ ///
+ /// 创建新分支,使用 checkout -b 命令
///
///
public void CheckoutNewBranch(string branchName)
@@ -272,7 +272,6 @@ public void CheckoutNewBranch(string branchName)
Control($"checkout -b {branchName}");
}
}
-
public class GitDiffFile
{
///
diff --git a/package/GitCommand/GitCommand/GitCommand.csproj b/package/GitCommand/GitCommand/GitCommand.csproj
index d932224..0ea34a1 100644
--- a/package/GitCommand/GitCommand/GitCommand.csproj
+++ b/package/GitCommand/GitCommand/GitCommand.csproj
@@ -8,7 +8,7 @@
-
+
all
runtime; build; native; contentfiles; analyzers; buildtransitive
diff --git a/package/WhitmanRandomIdentifier/WhitmanRandomIdentifier.csproj b/package/WhitmanRandomIdentifier/WhitmanRandomIdentifier.csproj
index d94aa71..9f41fe6 100644
--- a/package/WhitmanRandomIdentifier/WhitmanRandomIdentifier.csproj
+++ b/package/WhitmanRandomIdentifier/WhitmanRandomIdentifier.csproj
@@ -8,7 +8,7 @@
true
-
+
all
runtime; build; native; contentfiles; analyzers; buildtransitive