0

I'm integrating some filterchains in my Ant build script which add build date, program version, and git head sha to the source files when packaging, however I'm not sure how to determine if the source repo is "clean", as-in if there are no changes since the HEAD commit in the current source-to-be-built.

To tag the HEAD commit, I'm using the git.revision target made in this question: How to lookup the latest git commit hash from an ant build script

I'd like to do something similar but indicate repo status, ie. "SOURCE: @STATUS@" which would filter to: "SOURCE: CLEAN" / "SOURCE: DIRTY", or similar.

Just running a git status command in an exec from ant returns a lot of stuff and makes it harder to work with. Is there an easier way?

Community
  • 1
  • 1
SnakeDoc
  • 13,611
  • 17
  • 65
  • 97

1 Answers1

1

My preferred way to tackle this problem is to never change one of my source files (under revision control). Instead ANT creates a "build" or "target" directory which is added to the ".gitignore" file so that git does not care about its content.

├── build.xml
├── .gitignore
├── src
│   ├── main
│   │   ├── java
│   │   └── resources
│   └── test
│       └── java
└── build
    ├── classes
    ├── jars
    └── resources

The advantages of this approach is:

  1. Any file generated by the build will be ignored by git
  2. Simple to create a "clean" target in ANT to refresh the workspace.

An example of my standard "clean" target

<target name="clean">
  <deleted dir="build"/>
</target>

To ensure a clean build one runs the following

ant clean build

Finally, if you're using a CI server like Jenkins (and I highly recommend you do) there is often plugin support, for example:

Provides functionality for deleting the entire project workspace or selected files.

Hope this helps.

Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
  • I do already have a setup like this. The goal I"m trying to reach is if a developer makes a local build on their system with some "WIP" patch, I want the source distribution to indicate so, ie. source is not clean. 9 times out of 10, it will just say that the source is clean, but if there was some change made to it prior to compilation and not checked into the repo, I want it indicated. – SnakeDoc Jan 19 '15 at 17:59
  • @SnakeDoc Ahh, that's something different. I apologise in advance, I'm highly opinionated in this matter: All changes shared with other developers, **must** be committed into the SCM. Otherwise, they don't really exist :-) To do this in a shared code environment use a CI server like Jenkins to control all code pushes to your shared repository (and link to the changes in GIT). A really advanced solution would be to add a code review review capability like Gerrit. It's a killer GIT app when devs are closely collaborating on the same codebase. – Mark O'Connor Jan 19 '15 at 18:02
  • Hmm, perhaps I'm barking up the wrong tree then. I was really just trying to provide a visual queue that the source file you're looking at was definitively built from the commit sha printed in the file's header, and not just indicate that the sha was the last commit but we don't know if some other change happened locally (without a commit) before this build was run. Using a CI would solve some of that reliability sure... we use Jenkins already and it does fetch a clean HEAD for each build. – SnakeDoc Jan 19 '15 at 18:08
  • 1
    @SnakeDoc No I gotcha! I saw a very clever approach to this problem outlined by the following link. Using the git log (which can be matched back the SHA commit) to determine the build number in your semantic numbering scheme. Clever and worth a read: http://www.hurryupandwait.io/blog/using-git-to-version-stamp-chef-artifacts However, please note I'm not a fan of builds based on uncommitted source. That's why my Jenkins build is always performed against "clean" source – Mark O'Connor Jan 19 '15 at 18:16
  • 1
    @SnakeDoc Page is not responsive. Here's a cached copy: http://webcache.googleusercontent.com/search?q=cache:Kg0qkdObXUIJ:www.hurryupandwait.io/blog/using-git-to-version-stamp-chef-artifacts+&cd=1&hl=en&ct=clnk&gl=uk&client=ubuntu – Mark O'Connor Jan 19 '15 at 18:16