| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- using GitLib = LibGit2Sharp;
- namespace Common
- {
- public class GitHelper
- {
- private readonly string _repoPath;
- public GitHelper(string repoPath)
- {
- _repoPath = Path.GetFullPath(repoPath);
- if (!Directory.Exists(_repoPath))
- Directory.CreateDirectory(_repoPath);
- }
- // 1. 是否是 Git 仓库(替代 IsGitRepo)
- public bool IsGitRepository()
- {
- // 静态方法:Discover
- return GitLib.Repository.Discover(_repoPath) != null;
- }
- // 2. 初始化仓库(git init)
- public bool InitRepository()
- {
- // if (IsGitRepository()) return true;
- GitLib.Repository.Init(_repoPath);
- return true;
- }
- // 3. 克隆远程仓库
- public bool Clone(string remoteUrl, string userName, string token, string targetBranch = null)
- {
- try
- {
- var cloneOpt = new GitLib.CloneOptions();
- if (!string.IsNullOrEmpty(targetBranch))
- cloneOpt.BranchName = targetBranch;
- // 配置凭证回调(解决认证报错)
- cloneOpt.FetchOptions.CredentialsProvider = (url, user, credType) =>
- {
- return new GitLib.UsernamePasswordCredentials
- {
- Username = userName,
- Password = token
- };
- };
- GitLib.Repository.Clone(remoteUrl, _repoPath, cloneOpt);
- return true;
- }
- catch (Exception ex)
- {
- Console.WriteLine($"克隆仓库失败:{ex.Message}");
- return false;
- }
- }
- // 4. Stage 文件(git add .)→ 用 Commands.Stage
- public bool StageAll()
- {
- if (!IsGitRepository()) return false;
- using var repo = new GitLib.Repository(_repoPath);
- GitLib.Commands.Stage(repo, "*"); // 静态方法,不是实例方法
- return true;
- }
- // 5. Commit(git commit -m)
- public bool Commit(string message, string name = "Admin", string email = "admin@example.com")
- {
- if (!IsGitRepository()) return false;
- using var repo = new GitLib.Repository(_repoPath);
- var sig = new GitLib.Signature(name, email, DateTime.Now);
- repo.Commit(message, sig, sig, new GitLib.CommitOptions()); // 实例方法,存在
- return true;
- }
- // 6. Pull(git pull)→ 用 Commands.Pull
- public bool PullCode(string userName, string token, string userNameCommit = "TestUser", string userEmail = "test@demo.com")
- {
- try
- {
- using var repo = new GitLib.Repository(_repoPath);
- var sign = new GitLib.Signature(userNameCommit, userEmail, DateTime.Now);
- var pullOpt = new GitLib.PullOptions
- {
- FetchOptions = new GitLib.FetchOptions
- {
- // 拉取凭证(解决私有仓库认证)
- CredentialsProvider = (url, user, credType) =>
- {
- return new GitLib.UsernamePasswordCredentials
- {
- Username = userName,
- Password = token
- };
- }
- }
- };
- GitLib.Commands.Pull(repo, sign, pullOpt);
- return true;
- }
- catch (Exception ex)
- {
- Console.WriteLine($"拉取代码失败:{ex.Message}");
- return false;
- }
- }
- // 7. Push(git push)
- public bool Push(string userName, string token, string branch = "main")
- {
- if (!IsGitRepository()) return false;
- using var repo = new GitLib.Repository(_repoPath);
- var cred = new GitLib.UsernamePasswordCredentials
- {
- Username = userName,
- Password = token // GitHub/Gitee 用 Token
- };
- var pushOpts = new GitLib.PushOptions
- {
- CredentialsProvider = (_, _, _) => cred
- };
- var targetBranch = repo.Branches[branch] ?? throw new Exception("分支不存在");
- repo.Network.Push(targetBranch, pushOpts);
- return true;
- }
- }
- }
|