This page looks best with JavaScript enabled

Detecting changes in a git repository using ANT

 ·  β˜• 2 min read  ·  ✍️ FJTC

I’ve been using Apache Ant to write build scripts to automate the build and publishing of project. In order to make sure this script will run if and only if the repository contains no local modifications, I added the following target:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
    <target name="checkGitChanges" unless="debug"> 
        <exec executable="git" outputproperty="git.status"> 
            <arg value="status"/> 
            <arg value="-s"/> 
        </exec> 
        <fail message="Changes detected in the repository."> 
            <condition> 
                <not> 
                    <equals arg1="${git.status}" arg2=""/> 
                </not> 
            </condition> 
        </fail> 
    </target>

In order to activate it, add it as the first dependency of your main target. If you want to skip this target in order to test it, just run ant with the parameter -Ddebug.

The idea behind this target is simple, first, I run β€œgit status -s” and put the output inside the property git.status. If the repository is not modified, the command git status -s is supposed to generate no output, thus the property git.status will be empty. Right after the execution of git status -s, it checks the output of the command. If it is not empty, the script must fail with the message Changes detected in the repository.

The most important advantage of this simple target is that it uses only Git itself and the core tasks of Ant. No additional Ant tasks needed.

Share on

FJTC
WRITTEN BY
FJTC
Co-Founder of InterlockLedger Network