c# - 需要在cake脚本的GitPull方法中获取修改文件的详细信息

标签 c# git libgit2sharp cakebuild

您好,我正在使用 GitPull 方法将更改 pull 入存储库。

从下面的链接引用

http://cakebuild.net/api/Cake.Git/GitAliases/CC1AE32F

我需要在执行 GitPull 方法时获取更新文件的日志。

是否有任何方法可以使用下面的页面获取这些详细信息,或者建议使用其他方法在蛋糕中执行上述操作。

http://cakebuild.net/dsl/git/

最佳答案

首先是一个免责声明,因为之前在 Cake.Git/Libgit2sharp 中存在 merge 问题,您需要升级到 0.14.0 或更高版本的 Cake.Git使这个答案起作用。

无论是否进行快进 merge ,可靠地获取更改文件的最简单方法是:

  1. pull 前获取提交
  2. pull
  3. 如果 repo 不是最新的, pull 后获取提交
  4. 在 pull 提交之前和之后做一个差异

Cake.Git 的做法是

  1. GitLogTip
  2. GitPull
  3. 如果pullResult . Status != GitMergeStatus . UpToDate然后 GitLogTip
  4. GitDiff

这可能看起来像下面这样

#addin nuget:?package=Cake.Git&version=0.14.0

DirectoryPath repoDir = MakeAbsolute(Directory("./Cake_Git"));

string  name    = "John Doe",
        email   = "john@doe.com";

var beforePullCommit = GitLogTip(repoDir);

var pullResult = GitPull(repoDir, name, email);

if (pullResult.Status!=GitMergeStatus.UpToDate)
{
    var afterPullCommit = GitLogTip(repoDir);

    var diff = GitDiff(repoDir, beforePullCommit.Sha, afterPullCommit.Sha);

    foreach(var file in diff)
    {
        Information("{0}", file);
    }
}

GitDiff返回 GitDiffFile 的 ICollection具有这些属性的 s。

Name        Value           Summary
Exists      bool            The file exists in the new side of the diff.
OldExists   bool            The file exists in the old side of the diff.
OldPath     string          The old path.
Path        string          The new path.
Status      GitChangeKind   The kind of change that has been done
                            (added, deleted, modified ...).

并且有一个 ToString() 覆盖 sp 这个脚本的输出看起来像这样

Path: ReleaseNotes.md, OldPath: ReleaseNotes.md, Status: Modified, Exists: True, OldExists: True
Path: src\Cake.Git\Cake.Git.csproj, OldPath: src\Cake.Git\Cake.Git.csproj, Status: Modified, Exists: True, OldExists: True
Path: src\Cake.Git\GitMergeResult.cs, OldPath: src\Cake.Git\GitMergeResult.cs, Status: Modified, Exists: True, OldExists: True
Path: src\Cake.Git\packages.config, OldPath: src\Cake.Git\packages.config, Status: Modified, Exists: True, OldExists: True
Path: src\SolutionInfo.cs, OldPath: src\SolutionInfo.cs, Status: Modified, Exists: True, OldExists: True

但由于它是一个类型化对象,您当然可以通过编程方式进行更多操作。

关于c# - 需要在cake脚本的GitPull方法中获取修改文件的详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42918647/

相关文章:

proxy - 使用 LibGit2Sharp 抛出企业防火墙背后的 Git 网络操作

c# - 为使用 SSH.NET SshClient.RunCommand 执行的命令 (cli) 提供输入/子命令

c# - 具有相同签名的委托(delegate)

c# - 缺少属性时如何强制 System.Text.Json 序列化程序抛出异常?

git - 为什么我不能将更改推送到这个最新的 Git 子树?

git - 如何找到具有特定父级的所有提交?

c# - Libnoise XNA translate perlin 导致失真

git - 在 Git Bash 上运行 "git difftool"会打开 Visual Studio 的多个空实例

Git 扩展 - 如何将 master merge 到分支

c# - 如何在 LibGit2Sharp 中执行 git diff --name-status origin/master...HEAD ?