GitHelper.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using GitLib = LibGit2Sharp;
  2. namespace Common
  3. {
  4. public class GitHelper
  5. {
  6. private readonly string _repoPath;
  7. public GitHelper(string repoPath)
  8. {
  9. _repoPath = Path.GetFullPath(repoPath);
  10. if (!Directory.Exists(_repoPath))
  11. Directory.CreateDirectory(_repoPath);
  12. }
  13. // 1. 是否是 Git 仓库(替代 IsGitRepo)
  14. public bool IsGitRepository()
  15. {
  16. // 静态方法:Discover
  17. return GitLib.Repository.Discover(_repoPath) != null;
  18. }
  19. // 2. 初始化仓库(git init)
  20. public bool InitRepository()
  21. {
  22. // if (IsGitRepository()) return true;
  23. GitLib.Repository.Init(_repoPath);
  24. return true;
  25. }
  26. // 3. 克隆远程仓库
  27. public bool Clone(string remoteUrl, string userName, string token, string targetBranch = null)
  28. {
  29. try
  30. {
  31. var cloneOpt = new GitLib.CloneOptions();
  32. if (!string.IsNullOrEmpty(targetBranch))
  33. cloneOpt.BranchName = targetBranch;
  34. // 配置凭证回调(解决认证报错)
  35. cloneOpt.FetchOptions.CredentialsProvider = (url, user, credType) =>
  36. {
  37. return new GitLib.UsernamePasswordCredentials
  38. {
  39. Username = userName,
  40. Password = token
  41. };
  42. };
  43. GitLib.Repository.Clone(remoteUrl, _repoPath, cloneOpt);
  44. return true;
  45. }
  46. catch (Exception ex)
  47. {
  48. Console.WriteLine($"克隆仓库失败:{ex.Message}");
  49. return false;
  50. }
  51. }
  52. // 4. Stage 文件(git add .)→ 用 Commands.Stage
  53. public bool StageAll()
  54. {
  55. if (!IsGitRepository()) return false;
  56. using var repo = new GitLib.Repository(_repoPath);
  57. GitLib.Commands.Stage(repo, "*"); // 静态方法,不是实例方法
  58. return true;
  59. }
  60. // 5. Commit(git commit -m)
  61. public bool Commit(string message, string name = "Admin", string email = "admin@example.com")
  62. {
  63. if (!IsGitRepository()) return false;
  64. using var repo = new GitLib.Repository(_repoPath);
  65. var sig = new GitLib.Signature(name, email, DateTime.Now);
  66. repo.Commit(message, sig, sig, new GitLib.CommitOptions()); // 实例方法,存在
  67. return true;
  68. }
  69. // 6. Pull(git pull)→ 用 Commands.Pull
  70. public bool PullCode(string userName, string token, string userNameCommit = "TestUser", string userEmail = "test@demo.com")
  71. {
  72. try
  73. {
  74. using var repo = new GitLib.Repository(_repoPath);
  75. var sign = new GitLib.Signature(userNameCommit, userEmail, DateTime.Now);
  76. var pullOpt = new GitLib.PullOptions
  77. {
  78. FetchOptions = new GitLib.FetchOptions
  79. {
  80. // 拉取凭证(解决私有仓库认证)
  81. CredentialsProvider = (url, user, credType) =>
  82. {
  83. return new GitLib.UsernamePasswordCredentials
  84. {
  85. Username = userName,
  86. Password = token
  87. };
  88. }
  89. }
  90. };
  91. GitLib.Commands.Pull(repo, sign, pullOpt);
  92. return true;
  93. }
  94. catch (Exception ex)
  95. {
  96. Console.WriteLine($"拉取代码失败:{ex.Message}");
  97. return false;
  98. }
  99. }
  100. // 7. Push(git push)
  101. public bool Push(string userName, string token, string branch = "main")
  102. {
  103. if (!IsGitRepository()) return false;
  104. using var repo = new GitLib.Repository(_repoPath);
  105. var cred = new GitLib.UsernamePasswordCredentials
  106. {
  107. Username = userName,
  108. Password = token // GitHub/Gitee 用 Token
  109. };
  110. var pushOpts = new GitLib.PushOptions
  111. {
  112. CredentialsProvider = (_, _, _) => cred
  113. };
  114. var targetBranch = repo.Branches[branch] ?? throw new Exception("分支不存在");
  115. repo.Network.Push(targetBranch, pushOpts);
  116. return true;
  117. }
  118. }
  119. }