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

此生再无相见时 提交于 2019-12-25 02:24:26

问题


I've followed this solution to fetch the files, which are modified in the last commit.

The solutions will be like

$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
    }
}

With this, I can able to get the modified files from the last commit, but in my case, there may be N- new commit(s).

So I'm seeing a way to achieve this with change the cmd like

2 Commits

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

3 commits

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

like so on.,

However, I couldn't able to find a way to get the no of new commits in the Build definition

Update 1

My TFS Get Sources always check out the corresponding branch with the latest commit 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

Which also makes difficult the find the old commit id's as well


回答1:


I guess you're looking for git rev-list

Let's say you perform the following steps:

  • You are on master
  • You create a hotfix branch: git checkout -b hotfix_1
  • You modify and commit some files, its hash is f3de9ae73e1fb06c23506c
  • You modify and commit again. latest hash is 4985ead3183df8388cf8e4

At this point you're two commits ahead of master, so doing

git rev-list HEAD ^master

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

Which in this case prints

4985ead3183df8388cf8e4
f3de9ae73e1fb06c23506c

(because it makes sense to list them in reverse chronological order).

However this method does only make sense when you're comparing branches that haven't diverged. Otherwise, you will only get the number of commits between your HEAD and the last common ancestor, which does not mean the master branch hasn't had any new commits since your current HEAD diverged.



来源:https://stackoverflow.com/questions/52232468/how-to-find-the-number-of-new-commitsgit-during-the-tfs-build

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!