如何在 TFS 构建期间查找新提交数 (git)

How to find the number of new commits(git) during the TFS Build

我已经按照这个 来获取在上次提交中修改的文件。

解决方案就像

$files=$(git diff HEAD HEAD~ --name-only)
echo $files
$temp=$files -split ' '
$count=$temp.Length
echo "Total changed $count files"
New-Item -ItemType directory -Path $(Build.ArtifactStagingDirectory)\files
For ($i=0; $i -lt $temp.Length; $i++)
{
  $name=$temp[$i]
  echo "this is $name file"
  if (Test-Path "$(Build.SourcesDirectory)$name")
    {
      Copy-Item $(Build.SourcesDirectory)$name $(Build.ArtifactStagingDirectory)\files
    }
}

有了这个,我可以从上次提交中获取修改后的文件,但在我的情况下,可能有 N- 新提交。

所以我看到了一种通过更改 cmd 来实现此目的的方法,例如

2 次提交

$files=$(git diff HEAD HEAD~2 --name-only)

3 次提交

$files=$(git diff HEAD HEAD~3 --name-only)

等等,

但是,我无法找到在构建定义中获取新提交数量的方法

更新 1

我的 TFS Get Sources 总是检查具有最新提交 ID 的相应分支

2018-09-08T06:05:35.8623084Z ##[command]git checkout --progress --force e88c5a4bf29a539c515ca0e5fea104799426026e
2018-09-08T06:05:36.3681977Z Previous HEAD position was 40ac471... Updated xxxxxxx

这也使得查找旧提交 ID 变得困难

我猜你正在寻找 git rev-list

假设您执行以下步骤:

  • 你是大师
  • 您创建了一个修补程序分支:git checkout -b hotfix_1
  • 你修改并提交了一些文件,它的哈希是f3de9ae73e1fb06c23506c
  • 您修改并再次提交。最新的哈希值是 4985ead3183df8388cf8e4

此时你比 master 提前两次提交,所以这样做

git rev-list HEAD ^master

表示:"list those commits that are on my history and not in master's history".

在这种情况下打印

4985ead3183df8388cf8e4
f3de9ae73e1fb06c23506c

(因为按时间倒序列出它们是有意义的)。

但是这种方法只有在比较没有分歧的分支时才有意义。否则,你只会得到你的 HEAD 和最后一个共同祖先之间的提交次数,这并不意味着 master 分支自你当前的 HEAD 分歧以来没有任何新的提交。