3

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

Zoe
  • 27,060
  • 21
  • 118
  • 148
alestar
  • 341
  • 5
  • 15
  • Are you absolutely certain that is the correct sha1 key and your current working directory is in the git repository you think you are? Can you execute the command by hand? – Thorbjørn Ravn Andersen Jan 10 '17 at 00:01

1 Answers1

1

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.

Chad Nouis
  • 6,861
  • 1
  • 27
  • 28
  • Hi, Chad that is definitely the answer I can not vote yet, but you got it right. I found out the hard way but you are right, the output of `exec` does insert a new line at the end of the file, therefore causing the problem. I figure it out when I manually copy paste the exact SHA ID into the `arg line`. Also, thanks for the advice, I didn't know that was the case for `failonerror` and `failonerror`. FYI I am not able to c – alestar Jan 11 '17 at 01:06