如何在Ant中运行git checkout?

3
我一直在阅读这篇有关Git按特定日期进行checkout的帖子。我已经成功获得了要检出的特定提交的版本控制SHA代码,但是当我尝试运行git checkout命令时,我遇到了以下错误:

错误:路径规范'de957d59f5ebef20f34155456b8ab46f127dc345'未匹配任何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) 代码,但在尝试将其用于第二个执行任务的命令参数时,它会抛出上述错误。您有什么想法/建议吗?正如我所提到的,我有几个类似于此并用于执行其他任务的任务命令行,例如 "git clone" 和 "git log",但这个命令好像缺少了一些关键内容。
先感谢您。

你确定那是正确的SHA1密钥,并且你当前的工作目录在你认为的git仓库中吗?你能手动执行这个命令吗? - Thorbjørn Ravn Andersen
1个回答

1
在错误信息中,我注意到引号前有一个空格:
pathspec 'de957d59f5ebef20f34155456b8ab46f127dc345 '
                                                  ^ a space

我相信<exec>标签的output属性会在输出文件末尾插入一个换行符。稍后,<loadfile>标签将把换行符转换为空格。
为了避免处理空格,请考虑使用outputproperty替代output,将git 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,因此可以省略。然而,默认情况下failonerrorfalse。将failonerror="true"添加到<exec>通常是一个好习惯。

嗨,Chad,这绝对是我不能投票的答案,但你回答正确了。我通过艰难的方式发现了这一点,但你是对的,exec的输出确实在文件末尾插入了一个新行,因此导致了问题。当我手动复制粘贴确切的SHA ID到arg line时,我弄清楚了这个问题。另外,感谢您的建议,我不知道failonerrorfailonerror是这种情况。FYI,我无法c。 - alestar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接