在Cake脚本的GitPull方法中获取修改文件的详细信息。

4
2个回答

3
首先需要声明一下,由于在 Cake.Git 和 Libgit2sharp 中之前有与合并相关的问题,您需要升级到 Cake.Git 版本为0.14.0或更高版本才能使本回答有效。
获取更改文件最可靠的方法无论是快进合并还是其他方式都是:
  1. 拉取之前获取提交
  2. 进行拉取
  3. 如果仓库不是最新的,则获取拉取后提交
  4. 对比拉取前后提交之间的差异
使用 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 集合,该集合具有以下属性。

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() 的重载,因此此脚本的输出将类似于下面这样
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

但由于它是一个类型化对象,你当然可以在程序中做更多的事情。


1
在执行GitPull方法时,您可以尝试使用(非Cake解决方案) git pull之后(即获取加合并)。
git log --stat

或者,如同在FETCH_HEAD玩耍中所提到的

git log --name-only ..FETCH_HEAD

我没有看到Cake GitLog方法支持这些选项,所以你可以尝试解析以下结果:

var result = GitLog("c:/temp/cake", 1);

(这是由git pull生成的最后一次合并提交)

@JeevaS 确保您的进程在 Git 存储库路径中执行:https://dev59.com/2XVD5IYBdhLWcg3wBWzd#114937 - VonC
我已将Git仓库设置为当前目录。当我执行git clean -xdf命令时,它可以正常工作,甚至git pull stat也可以正常工作。但是在尝试写日志时遇到了上述问题。 - Jeeva S
@JeevaS var startInfo = new ProcessStartInfo(); startInfo.WorkingDirectory = // 工作目录 // 设置其他属性 Process proc = Process.Start(startInfo); - VonC
尝试了以下代码,但问题仍然存在:var startInfo = new ProcessStartInfo(); startInfo.WorkingDirectory = @"G:\Gitlab\sfinput-xaml"; startInfo.Arguments= "pull --stat >../GitLogs/log.txt"; startInfo.FileName = "Git"; Process proc = Process.Start(startInfo); - Jeeva S
面对“fatal: Invalid refspec G:/GitLogs/log.txt”问题 - Jeeva S
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接