3

我已使用以下 ant 详细信息来检索签出分支的最新提交 ID,使用此方法我应该注意哪些注意事项?

是否存在我无法检索预期值的极端情况?

<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}"/>
4

2 回答 2

4

您可以读取 的内容.git/HEAD,然后读取从中获得的文件的内容。

您将遇到的警告是,您从上述步骤获得的 SHA-1 可能位于一个包文件中(git 将多个更改压缩在一起以节省空间的方式)。我建议使用 git 而不是自己尝试操作 .git 文件夹的内容。

于 2013-01-18T15:55:12.783 回答
2

JGIT可用于在您的构建中提供 git 客户端

例子

$ ant clone print-latest-commit-id
..
..
clone-repo:
[git-clone] Cloning repository https://github.com/myproj/myrepo.git

resolve:

get-commit-id:

print-commit-id:
     [echo] head.commitId = 9e3e8358f2b31507b13f5def69627da422e1656b

构建.xml

<project name="build" default="clone" xmlns:ivy="antlib:org.apache.ivy.ant">

    <target name="resolve" description="Resolve 3rd party dependencies">
        <ivy:cachepath pathid="build.path">
            <dependency org="org.codehaus.groovy" name="groovy-all" rev="2.1.0-rc-2" conf="default"/>
            <dependency org="org.eclipse.jgit" name="org.eclipse.jgit.ant" rev="2.1.0.201209190230-r" conf="default"/>
            <exclude org="org.apache.ant"/>
        </ivy:cachepath>
    </target>

    <target name="clone-repo" depends="resolve" description="Pull code from SCM repository">
        <taskdef resource="org/eclipse/jgit/ant/ant-tasks.properties" classpathref="build.path"/>

        <git-clone uri="https://github.com/myproj/myrepo.git" dest="build/repo" branch="???"/>
    </target>

    <target name="get-commit-id" depends="resolve" description="Print git log">
        <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>

        <groovy>
            import org.eclipse.jgit.api.Git

            Git git = Git.open(new File("build/repo"))

            properties["head.commitId"] = git.log().call().first().name()
        </groovy>
    </target>

    <target name="print-commit-id" depends="get-commit-id" description="Print commit id">
        <echo message="head.commitId = ${head.commitId}"/>
    </target>

    <target name="clean" description="Cleanup build files">
        <delete dir="build"/>
    </target>

</project>

笔记:

  • Apache ivy用于管理 3rd 方 jars
  • 我更喜欢将Groovy用于嵌入式脚本
于 2013-01-19T04:36:13.727 回答