【问题标题】:How to find the git log or commit history for a specific file using PowerShell如何使用 PowerShell 查找特定文件的 git 日志或提交历史记录
【发布时间】:2022-01-10 09:18:18
【问题描述】:

我想使用 powershell 查找特定文件/路径的提交历史记录。根据最后的提交消息,如果它被删除,我正在调用一个已删除的函数。它会调用一个 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 GitHow 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 提交信息。在这里,我将 PowerShell 变量作为参数值传递给 git 命令,但它什么也没返回。

标签: git powershell git-log


【解决方案1】:

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

请注意,如果您使用的是 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]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-14
    • 1970-01-01
    • 2014-05-05
    • 2018-11-01
    • 1970-01-01
    • 2011-05-01
    • 2012-02-21
    • 2017-05-03
    相关资源
    最近更新 更多