git log --name-status HEAD ^ .. HEAD在Mac上显示先前的提交,但在Windows上不显示吗?

时间:2018-07-09 18:46:49

标签: python windows git

我正在Mac上进行开发,但是我有Windows VM,最终运行了一些构建。非常简单。我有詹金斯(Jenkins)拉并每天构建应用程序。

在此脚本中,我需要获取一些元数据。我需要获取git服务器上分支的列表,如果有的话(因为分支可能没有提交),则需要最新提交的元数据。特别是提交者的电子邮件。

通常在我的Mac上,它很简单:

git branch --all > branches
for b in $(cat branches);
do
  git checkout $b
  git log --name-status HEAD^..HEAD | grep Author | cut -d"<" -f2 | cut -d">" -f1 > email
  echo $b > branch
  python buildScript.py $(cat email) $(cat branch)
  ; done
rm email branch
mv branches old_branches

我注意到的问题是:

  1. 在分支的新分支上,git log --name-status显示了提交,但是 HEAD ^ .. HEAD 未返回任何结果。
  2. grep在Windows Cmd提示中不是有效的cmd。
  3. cut在Windows Cmd提示中不是有效的cmd。

我可能会找到grep并削减替代方案。

有人可以解释为什么提交不会显示在Windows上吗?有没有内置的Python Utility可以在其中处理所有问题?我对运行直接bash没有任何问题,但是我认为python可以更加干净,因为我已经在运行python脚本来构建...

编辑: 我想要的最终状态是一个bash脚本(或python脚本),其操作如下:

loop over all branches.
if branch is new since the last time the script was run, and has at least 1 commit
   run_a_python_script with the committers email and that branch.
else if there were deleted branches since the last time it was run:
   run_another_python_script with the branch
else
   nothing happens because this script already ran once.

我将其设置为每10分钟一次cron。

这很容易在Python或Bash中完成。

1 个答案:

答案 0 :(得分:1)

这是一个通用配方,可以通过subprocess.Popen在Python中实现,或者以明显的方式在sh / bash中实现。

loop over all branches

使用git for-each-ref。如果您在具有分支机构的服务器上,则为git for-each-ref refs/heads;如果您在将分支重命名为远程跟踪名称(origin/master等)的客户端上,则为git for-each-ref refs/remotes/origin。如果需要,请使用for-each-ref的格式指令生成适当的简称(但请注意the gitrevisions documentation中的六步查找过程,并确保该简称将查找适当的哈希值-通常更明智些)在脚本中尽量保留 full 名称,并将其传递给类似理解该模式的其他脚本,以免找到名为{{的 tag 1}},同时还有一个名为xyz分支

(如果您使用的是Python库,则它可能在引用上具有一个迭代器,其功能类似于xyz。)

git for-each-ref

根据定义,没有至少一个提交就不能存在Git分支。原因是分支 name 只是一个(1)提交哈希ID的名称。

“自上次运行以来,它是新的”,当然需要某种记忆。

if branch is new since the last time the script was run, and has at least 1 commit

给出参考名称 run_a_python_script with the committers email and that branch. ,以查找作者的电子邮件(这就是您在原始代码中所做的事情):

$ref

要查找提交者的电子邮件(请注意,每个提交都有单独的“作者”和“提交者”,尽管在大多数情况下两者通常是相同的),请使用git log --no-walk --pretty=format:%ae $ref 而不是%ce

如果引用的格式为%ae,则分支命名为 refs/heads/name ;如果其格式为name,则分支名为 refs/remotes/origin/name name中的%(refname:short)格式将剥离git for-each-refrefs/heads/部分,但是如前所述,这样做为时过早是不明智的。如果Python脚本可以处理完整的引用名称,那将是最佳选择。

refs/remotes/

这也需要内存,并且需要进行某种方式的集减(保存的参考集减去当前参考集等于删除的集)。 Shell脚本中的内存表示一个外部文件;使用else if there were deleted branches since the last time it was run: 或JSon格式或其他格式,Python代码中的内存也可以是外部文件。

pickle