39

I have a Jenkins job that builds from a github.com repository's master branch with Maven (mvn clean install), then checks for license headers in Java files and missing NOTICE files, and adds them if necessary (mvn license:format notice:generate). Sometimes this will result in changed or added files, sometimes not.

Whenever there have any changes been made (by the license plugin) I want to push the changes to the github repo.

Now I'm having trouble figuring out how best to achieve that. I've added a shell build step after the Maven license plugin where I execute git commands:

git add . # Just in case any NOTICE files have been added
git commit -m "Added license headers"

git add . alone works, i.e., doesn't break the build, even if no files have been added. However, git commit breaks the build, if there aren't any changes at all.

I'm not concerned about pushing back to github, as I believe the Git Publisher post-build action can do this for me. Can someone please point me in the right direction for the git commit?

2
  • isn't there a way to get the timestamp of the latest change? Feb 26, 2014 at 11:36
  • @ratchetfreak Thanks. You mean as in a) save the timestamp(s) before the license plug-in is run, and compare to timestamp(s) after it has been run? Sounds a bit expensive to me. Unless someone knows better :).
    – s.d
    Feb 26, 2014 at 11:44

3 Answers 3

113
git diff --quiet && git diff --staged --quiet || git commit -am 'Added license headers'

This command do exactly what is required, 'git commit only if there are changes', while the commands in the other answers do not: they only ignore any error of git commit.

4
  • 1
    It would be helpful to add parentheses here in case I don't know the bash operator precedence off the top of my head Dec 17, 2019 at 9:10
  • 5
    In case there is a git add before this (like in the question) it should be sufficient to just run git diff --staged --quiet || git commit -m '…'. Otherwise, git diff --quiet HEAD || git commit -am '…' simplifies this solution. Feb 26, 2021 at 10:55
  • 3
    git add . && git diff --staged --quiet || git commit -m '...' also works on a newly initialized repo
    – dinvlad
    Oct 25, 2021 at 5:07
  • this fails for me in github actions on ubuntu with "fatal: ambiguous argument '||': unknown revision or path not in the working tree."
    – Timon
    May 1 at 10:21
33

You can also just catch the error code with an OR operator:

git commit -m "Added license headers" || echo "No changes to commit"
2
  • 1
    This is very nice for the CI setting. Leaves something useful in the logs to look for. Dec 17, 2019 at 9:12
  • 1
    This is what I needed to resolve git issues in jenkins. Jun 4, 2021 at 15:40
5

To stop the build from breaking on the shell build step returning exit code 1 at any one point, e.g., when trying to make a git commit although there is nothing to commit, you can simply wrap the respective commands into an echo.

echo `git add -A && git commit -m "Added license headers"`

Now, whether there are untracked files to add to the index or not, and whether the working tree is dirty or clean, echo will return exit code 0, as there will be some string to be echoed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.