0

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?

2 Answers 2

2

You could also check the result of:

git rev-parse --verify HEAD

(after this thread:)

  1. It's a plumbing command, and so less likely to change its behavior versus "git diff".
  2. 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 current HEAD 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.

Sign up to request clarification or add additional context in comments.

Comments

0

The trivial from the top of my head is

$ git log | egrep -c  "^commit"
1493

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.