Reputation: 3810
I have a script that i need to act differently based on if there are any, that is one or more, commits. What is the best way to do this?
In pseudo code it would look something like this..
#!/bin/bash
if [[ `git log_count` == "0" ]]; then
echo "No commits exist for this repo."
# Do stuff..
else
echo "One or more commits do exist!"
# Do other stuff
fi
Any ideas?
Upvotes: 0
Views: 645
Reputation: 1328712
You could also check the result of:
git rev-parse --verify HEAD
(after this thread:)
- It's a plumbing command, and so less likely to change its behavior versus "
git diff
".- The seems more obvious to me.
rev-parse --verify
is meant to ask "is this a valid object name?"It is slightly different from your
show-ref
.
Yours asks "is there anything in refs/heads in this repository?"
Mine asks "does the currentHEAD
exist?"In practice, they are both reasonable tests, since once you have a branch, it is very difficult to get
HEAD
to point to something invalid short of editing manually to some bogus value. But you might prefer one over the other depending on what you are trying to say.
Upvotes: 2
Reputation: 2779
The trivial from the top of my head is
$ git log | egrep -c "^commit"
1493
Upvotes: 0