2

In Travis-ci I'm using this command.

git push origin HEAD:${TRAVIS_PULL_REQUEST_BRANCH:-$TRAVIS_BRANCH}

This all works perfectly fine unless there is nothing to commit (response: Everything up-to-date).

This is a perfectly fine response in my case, but it results in: exited with 1. This is something I would like to avoid. Regardless if it actually is able to commit or if all is up to date I would like the response to be exit 0.

How can I do this?

UPDATE:
As mentioned by @Mort in the comments, || true works, but now I always have a positive exit status. I would like to only change the Git exit code for Everything up-to-date.

Bob van Luijt
  • 7,153
  • 12
  • 58
  • 101
  • 4
    `git push origin HEAD:${TRAVIS_PULL_REQUEST_BRANCH:-$TRAVIS_BRANCH} || true` ? – Mort Oct 31 '17 at 13:43
  • 1
    See e.g. https://unix.stackexchange.com/questions/118217/chmod-silent-mode-how-force-exit-code-0-in-spite-of-error, this is by no means specific to Travis or Git. – jonrsharpe Oct 31 '17 at 13:44
  • Possible duplicate of [How to return exit code 0 from a failed command](https://stackoverflow.com/questions/36130299/how-to-return-exit-code-0-from-a-failed-command) – jonrsharpe Oct 31 '17 at 13:45
  • @Mort yes, that works, I did update the question though :-) – Bob van Luijt Oct 31 '17 at 13:48
  • `git push` *should* exit 0 on successful push, *including* "Everything up-to-date". What version of Git are you using? – torek Oct 31 '17 at 17:11
  • Hi @torek, not sure. It is in Travis-ci: https://travis-ci.org/weaviate/weaviate/jobs/295304839 – Bob van Luijt Oct 31 '17 at 17:14
  • 1
    Opening `Build system information` shows `git version 2.14.1`, which is plenty modern and should not have that kind of bug. But I see on line 982: `The command "git commit -m " updated nightly builds with travis"" exited with 1.` It's not the push that's failing, it's the commit that's failing. – torek Oct 31 '17 at 17:23
  • Ooooo, of course, thanks! Would adding `—force` be the right way to go in this siuation? – Bob van Luijt Oct 31 '17 at 17:25

1 Answers1

11

Your question isn't unique to Travis, and with that in mind, this has already been answered on this page. But I will re-post my own version of the most popular (and best) answer there, here, as I don't have enough reputation to mark this as a duplicate.

So, you're having the classic exited with a 1 problem because you tried to commit when there was nothing to commit. But you still want it to exited with a 1 if there is an actual problem. Just do this: don't run the commit command at all if you find that there is nothing to commit. You will check beforehand if there are any commits to be had, like so:

git diff-index --quiet HEAD || git commit -am 'Jolly ol commit message' 

It's pretty slick.

JCollier
  • 1,102
  • 2
  • 19
  • 31
  • 1
    Helped me build this useful alias `git fire` : `git config --global alias.fire '!set -x; git add -A && (git diff-index --quiet HEAD || git commit -m "${*:-WIP}") && git push'` – a505999 Jan 23 '22 at 18:44