2

So I am making a script that fetches the latest tag from the main remote repository and merges it into my local repos with my custom changes, for about 10-20 repos, in a single script. The merge part looks something like this:

git fetch -q https://android.googlesource.com/platform/$REPO_PATH $TAG
git merge -q --no-ff FETCH_HEAD
if [ $? -eq 0 ]; then
    echo -e "$REPO_PATH merged succesfully"
else
    echo -e "\n$REPO_PATH has merge conflict(s)\n"
fi

The point of this is that I want to know which all commits merged succesfully (and needs a push to my custom git). But sometimes the repos are already up to date (i.e. a new tag in the remote repo without any new commits), and in that case the "merged succesfully" message shows even though nothing has really been merged.

I only want to know which all repos underwent a merge and needs pushing to my git, and also want to know which all have merge conflicts (this works fine in my script though). All I have noticed is that when git does a real merge, it opens up nano asking for the commit message. But I have no idea how to detect merges using that. Thanks in advance.

Adithya
  • 25
  • 3
  • You can suppress the commit message during a merge. https://stackoverflow.com/a/36189488/989920 – evolutionxbox Dec 05 '18 at 16:27
  • no, thats not the point. please read the last para – Adithya Dec 05 '18 at 16:29
  • That is why I commented, not answered. Why does it matter which merged properly? If it's up-to-date a push won't do anything anyway. – evolutionxbox Dec 05 '18 at 16:35
  • no, as I mentioned, this script deals with over 10 repos and I dont wanna cd into each of them and do a git push to check whether I need to push it or not – Adithya Dec 05 '18 at 16:45
  • Why bother checking? Just push in the repos which didn't fail merging (not "up-to-date"). – evolutionxbox Dec 05 '18 at 16:58
  • in that case I wouldnt have asked the question here, I know that I can check myself – Adithya Dec 05 '18 at 17:00
  • If a git repo is in a merge (due to conflicts) there will be an `index.lock` file in the `.git` folder. If the merge doesn't need to happen ("already up-to-date"), then push anyway as it won't make a difference. – evolutionxbox Dec 05 '18 at 17:04

1 Answers1

6

Before you start the merge, store the hash of the current branch head:

git rev-parse HEAD

Then check it again after the merge command runs, if they are the same, the merge was unable to complete.

You can determine the status by checking the status code as well:

  | Match      | No Match                    |
  --------------------------------------------
0 | Up to Date | Merged                      |
  --------------------------------------------
1 | Conflict   | Something is horribly wrong |
  --------------------------------------------
LightBender
  • 4,046
  • 1
  • 15
  • 31