代码之家  ›  专栏  ›  技术社区  ›  Fallenreaper

git log--name status head^..head显示Mac上以前的提交,而不是Windows上的提交?

  •  1
  • Fallenreaper  · 技术社区  · 6 年前

    我在我的Mac上做开发,但我有一个Windows虚拟机,我最终运行了一些构建。很简单。我让詹金斯每天拉并建立应用程序。

    在这个脚本中,我需要获取一些元数据。我需要获取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 显示提交,但是 头^..头 没有返回任何结果。
    2. grep不是windows命令提示符中的有效命令。
    3. 剪切不是windows命令提示符中的有效命令。

    我可能会找到grep和削减替代品。

    有人能解释为什么提交不会出现在windows上吗?是否有一个可靠的内置python实用程序可以处理这一切?我对运行直接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分钟的时间间隔上。

    这很容易在python或bash中完成。

    1 回复  |  直到 6 年前
        1
  •  1
  •   torek    6 年前

    下面是一个通用的配方,可以通过 subprocess.Popen 或者以明显的方式用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 并确保短名称将查找适当的hashit通常在脚本中更明智地保持 满的 尽可能长的命名,将其传递给其他类似于理解此模式的脚本,以免找到 标签 命名的 xyz 当还有一个 分支 命名的 XYZ公司 )

    (如果您使用的是python库,那么它可能有一个迭代器,其函数类似于 每个参考的git (第三章)

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

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

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

       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 ,分支名为 名称 是的。这个 %(refname:short) 格式化 每个参考的git 会剥去 refs/heads/ refs/remotes/ 一部分,但如前所述,过早这样做可能是不明智的。如果python脚本能够处理完整的引用名称,那将是最好的。

    else if there were deleted branches since the last time it was run:
    

    这也需要内存,以及一些做集合减法的方法(保存的引用集减去当前引用集等于删除集)。shell脚本中的内存表示外部文件;python代码中的内存也可以是外部文件,使用 pickle 或者json格式之类的。

      run_another_python_script with the branch
    else
       nothing happens because this script already ran once.