如何在ant中运行git结帐?

How to run git checkout in ant?

我一直在阅读这篇 post 到 git 的特定日期结帐。我已经能够获得我针对结帐的特定提交的修订提交 SHA 代码,但是当我尝试实际 运行 git 结帐命令行时,我得到了错误:

error: pathspec 'de957d59f5ebef20f34155456b8ab46f127dc345 ' did not match any file(s) known to git.

不确定那是什么意思。我正在 运行 在我的 windows 7 机器

上从 ant 1.94 执行此命令

ant 命令脚本如下所示:

 <target name="git.revlist" description="Revision list of repo for a particular timeframe" >
    <exec executable="git" dir="${run.repo.dir}" failifexecutionfails="true" output="${output_commit_sha_file}" >
        <arg line="rev-list -n 1 --before=${snapshot_before_date} ${repo_branch}"/>
    </exec>
    <loadfile property="output_commit_sha" srcfile="${output_commit_sha_file}"  />
    <exec executable="git" dir="${run.repo.dir}" failifexecutionfails="true" >
        <arg line="checkout ${output_commit_sha}"/>
    </exec> 
 </target>

第一次执行实际上成功地检索了 SHA (de957d59f5ebef20f34155456b8ab46f127dc345) 代码,但是当尝试将其用于第二次执行任务命令参数时,它通过了上述错误。

任何 ideas/recommendations 我在这里遗漏的内容。就像我提到的,我确实有几个看起来像这样的任务命令行,用于执行其他任务,比如 git clonegit log,但是这个似乎缺少一些关键的东西。

提前致谢

在错误消息中,我注意到结束引号前有一个 space:

pathspec 'de957d59f5ebef20f34155456b8ab46f127dc345 '
                                                  ^ a space

我相信 <exec>output 属性会在输出文件的末尾插入一个换行符。 <loadfile> 稍后将换行符转换为 space.

为了避免处理 space,考虑使用 outputproperty 而不是 outputgit rev-list 的结果保存到 Ant 属性 中:

<exec executable="git" dir="${run.repo.dir}" outputproperty="output_commit_sha">
    <arg line="rev-list -n 1 --before=${snapshot_before_date} ${repo_branch}"/>
</exec>
<exec executable="git" dir="${run.repo.dir}">
    <arg line="checkout ${output_commit_sha}"/>
</exec>

上面的版本很好,因为它避免了必须创建一个文件来存储 git rev-list 的结果。它还删除了对 <loadfile>.

的调用

顺便说一下,您可能想使用 failonerror="true" 而不是 failifexecutionfails="true"failifexecutionfails 默认为 true,因此可以省略。然而,failonerror 默认为 false。将 failonerror="true" 添加到 <exec> 通常是一件好事。