如何使用 PowerShell 查找特定文件的 git 日志或提交历史记录

How to find the git log or commit history for a specific file using PowerShell

我想使用 powershell 查找特定 file/path 的提交历史记录。根据最后的提交消息,如果它被删除,我正在调用一个已删除的函数。它调用 rest API 将其从 Azure 中删除,否则它将转到下一个。

这是我正在尝试做的事情

  $commited_file_name = $([io.path]::GetFileNameWithoutExtension($_.Line)) #This gives Me the filename

  $path = $_.Line #This Gives me the complete Path. They change it dynamically based on the committed file in the Azure Devops Repo

通过使用上面的行,我正在传递以下 git 命令来获取特定文件的提交历史记录。我的大部分文件都是 Json 个文件。

通过引用这两个Find when a file was deleted in Git and How to find a deleted file in the project commit history。因为我找不到如何通过 powershell 做到这一点。

这是我做的。

      #Tried to get the commit history using a Path
      $commit_log = git log --full-history -1 [$path]
      WriteDebug "Commit Log:$commit_log" #[However this is not returning anything ]

      #Then tried to get the commit history using a Filename
      $commit_file = git log --all --full-history -1 -- "**/$commited_file_name.json.*"
      WriteDebug  "Commit File history:$commit_file" #[This is also not returing anything]
      
      #And it is failing at this point and invoked the DotheNextTask function even if the last commit of the file was deleted.

      $filter_commit = $("$commit_file" | grep -c "Deleted")
      if ("$filter_commit" -ne 0 ) {
        Deletefile
      }
      else{
        DotheNextTask
      }

有什么地方我做得不对吗?或者我在这里遗漏了什么。

我已经更新了代码并对其进行了测试。现在它正在显示输出。

请注意,如果您使用的是 PowerShell,则必须对连字符进行转义:git log '--' [file path].

$commit_file = git log --all --full-history -1 $("**/$commited_file_name.*")
WriteDebug  "Commit File history:$commit_file" #[Fetches previous commit history from all branches for that specific file]