Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
654 views
in Technique[技术] by (71.8m points)

version control - How can I get the latest commit ID of the currently checked out branch in git, using ANT, without having an active git installation?

I've used the following ant details to retrieve the checked out branch's latest commit ID, what caveats should I be concerned about using this method?

Are there edge cases where I wouldn't retrieve the expected values?

<scriptdef name="substring" language="javascript">
    <attribute name="text" />
    <attribute name="start" />
    <attribute name="end" />
    <attribute name="property" />
    <![CDATA[
       var text = attributes.get("text");
       var start = attributes.get("start");
       var end = attributes.get("end") || (text.length() - 1);
       project.setProperty(attributes.get("property"), text.substring(start, end));
     ]]>
</scriptdef>

<loadfile property="head.branch" srcfile="${basedir}/.git/HEAD" />
<substring text="${head.branch}" start="5" property="branch" />
<loadfile property="head.commitId" srcfile="${basedir}/.git/${branch}"/>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

you can read the contents of .git/HEAD, then read the contents of the file that you get from that.

The caveat that you will run into is that the SHA-1 that you get from the above steps may be in a pack file (the way git compresses multiple changes together to save space). I would recommend using git instead of trying to manipulate the .git folder contents yourself.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...