如何在 Git 日志中包含当前文件夹名称

How to include current folder name in Git log

我正在编写一个脚本来遍历某个文件夹及其子文件夹中的所有 Git 个存储库并执行 git log。 脚本的输出最终保存到 CSV 日志文件中。

现在我想在 git-log 结果中包含当前文件夹名称,但我在 git-log 文档中找不到如何执行此操作。

我当前的 git-log 命令如下所示:

git log --branches=master --no-merges --pretty=format:"%H;%an;%ad;%s" --date=format:'%Y-%m-%d %H:%M'

知道怎么做吗?

我认为您为任务使用了错误的工具。当前目录是环境的 属性,而不是存储库; git log不是很在意

因此,您应该获取脚本中的当前目录,并以某种方式将其放入 git log 的输出中。

如果您需要在 git log 返回的每一行上都有 repo 目录,那么您可以简单地将 $(pwd) 压缩到格式字符串中:

git log --branches=master --no-merges --pretty=format:"%H;%an;%ad;%s;$(pwd)" --date=format:'%Y-%m-%d %H:%M'

请注意,如果当前路径包含 % 会产生不良结果,因为它是被 git log 解释的特殊字符。

为避免这种情况发生,您可以在将路径插入格式之前使用 sed 转义 % 字符:

dir=$(pwd | sed s/%/%%/g)
git log --branches=master --no-merges --pretty=format:"%H;%an;%ad;%s;$dir" --date=format:'%Y-%m-%d %H:%M'

如果当前路径包含 "; 仍然会产生问题,因为它们对于 shell 和 CSV 格式都是特殊字符。如果需要,您也可以尝试在 sed 命令中引用它们。

我不确定我是否理解正确,但您是否在搜索 --dirstat ?:

这将在提交消息的底部为每个更改的目录添加更改的百分比。来自联机帮助页:

   --dirstat[=<param1,param2,...>]
       Output the distribution of relative amount of changes for each sub-directory. The behavior of --dirstat can be customized by
       passing it a comma separated list of parameters. The defaults are controlled by the diff.dirstat configuration variable (see git-
       config(1)). The following parameters are available: