Skip to content

Commit

Permalink
Merge pull request #53 from lindexi/t/lindexi/AutoSyncGitRepo
Browse files Browse the repository at this point in the history
更新 Git 逻辑
  • Loading branch information
lindexi authored Nov 24, 2021
2 parents e3d89b6 + 867baa8 commit b94e2cf
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 77 deletions.
149 changes: 74 additions & 75 deletions package/GitCommand/GitCommand/Git.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace Lindexi.Src.GitCommand
Expand All @@ -15,109 +16,74 @@ static Git()
#endif
}

/// <inheritdoc />
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 = ".")
/// <summary>
/// 两个版本修改的文件
/// </summary>
/// <param name="source">可以传入commit或分支</param>
/// <param name="target">可以传入commit或分支</param>
public List<GitDiffFile> DiffFile(string source, string target)
{
file = file.Replace(Repo.FullName, "");
if (file.StartsWith("\\"))
{
file = file.Substring(1);
}
var gitDiffFileList = new List<GitDiffFile>();

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<string> temp = new List<string>()
{
"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;
}

/// <summary>
/// 两个版本修改的文件
/// </summary>
/// <param name="source">可以传入commit或分支</param>
/// <param name="target">可以传入commit或分支</param>
public List<GitDiffFile> DiffFile(string source, string target)
public int GetGitCommitRevisionCount()
{
var gitDiffFileList = new List<GitDiffFile>();
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);
}
Expand Down Expand Up @@ -151,7 +117,7 @@ public void Clean()

public void FetchAll()
{
Control("fetch --all");
Control("fetch --all --tags");
}

public DirectoryInfo Repo { get; }
Expand All @@ -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);
}
}

/// <summary>
/// 是否需要写入日志
/// </summary>
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;//乱码
Expand Down Expand Up @@ -250,29 +224,54 @@ 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;

return output + "\r\n";
}


/// <summary>
/// 默认命令的超时时间
/// </summary>
public TimeSpan DefaultCommandTimeout { set; get; } = TimeSpan.FromMinutes(1);

/// <summary>
/// 切换到某个 commit 或分支
/// </summary>
public void Checkout(string commit)
{
Control($"checkout {commit}");
Checkout(commit, false);
}

/// <summary>
/// 创建新分支
/// 切换到某个 commit 或分支
/// </summary>
/// <param name="commit"></param>
/// <param name="shouldHard">是否需要强行切换,加上 -f 命令</param>
public void Checkout(string commit, bool shouldHard)
{
var command = $"checkout {commit}";

if (shouldHard)
{
command += " -f";
}

Control(command);
}

/// <summary>
/// 创建新分支,使用 checkout -b <paramref name="branchName"/> 命令
/// </summary>
/// <param name="branchName"></param>
public void CheckoutNewBranch(string branchName)
{
Control($"checkout -b {branchName}");
}
}

public class GitDiffFile
{
/// <inheritdoc />
Expand Down
2 changes: 1 addition & 1 deletion package/GitCommand/GitCommand/GitCommand.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="dotnetCampus.SourceYard" Version="0.1.19359-alpha">
<PackageReference Include="dotnetCampus.SourceYard" Version="0.1.19369-alpha">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="dotnetCampus.SourceYard" Version="0.1.19359-alpha">
<PackageReference Include="dotnetCampus.SourceYard" Version="0.1.19369-alpha">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down

0 comments on commit b94e2cf

Please sign in to comment.