简体   繁体   中英

How to run git checkout in ant?

I have been reading this post to git checkout by a specific date. I have been able to obtain the revision commit SHA code to the specific commit that I am targeting for the checkout, but when I am trying to actually run the git checkout command line I got the error:

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

Not sure what that means. I am running this command from ant 1.94 on my windows 7 machine

The ant command script looks as follow:

 <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>

Where the first execution actually retrieve the SHA (de957d59f5ebef20f34155456b8ab46f127dc345) code successfully, but when trying to use that for the second execution tasks command arguments, it throughs the above error.

Any ideas/recommendations on what I am missing here. Like I mentioned, I do have several task command lines that look like this and are used to perform other tasks, like git clone and git log , but this one seems to be missing something crucial.

Thanks in advance

In the error message, I noticed a space before the end quote:

pathspec 'de957d59f5ebef20f34155456b8ab46f127dc345 '
                                                  ^ a space

I believe the output attribute of <exec> inserts a newline at the end of the output file. <loadfile> later converts the newline into a space.

To avoid dealing with the space, consider saving the results of git rev-list to an Ant property by using outputproperty instead of output :

<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>

The above version is nice because it avoids having to create a file that stores the results of git rev-list . It also removes a call to <loadfile> .

By the way, you may want to use failonerror="true" instead of failifexecutionfails="true" . failifexecutionfails is true by default, so that can be omitted. failonerror , however, is false by default. Adding failonerror="true" to <exec> is usually a good thing to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM