Git merge marker

How to Check for Git Merge Conflicts in Travis CI

We use Travis for Continuous Integration (CI).  In other words, we pay a bot called Travis to check our code before we deploy it live.  Travis checks for things like PHP syntax errors and runs all of our PHP unit tests to make sure that the new code change doesn’t break anything.

What Travis checks is configurable by editing the .travis.yml file.  The travis configuration file is a YML file (hint: if Travis won’t run, you may have a syntax error in your YML, try running the file though a YML file validator).

In the travis configuration file, you can write linux commands that you’d like Travis to run.   Therefore, anything you can do in a bash shell, you should be able to tell Travis to do.

None Zero Return Status

If your Travis script calls set -e or you source a file that calls this (for example, if you use the travis script in WP Dev Lib), then any bash command that ends with a non 0 return status will cause Travis to fail.

This is what tripped me up, and what I ultimately did was create a bash function for checking for merge conflicts (see code below).  By the way, the keyword “Declare” doesn’t work in Travis, due to how Travis runs the commands.  Creating a function lets you control what the return status is for that function, and solves the issue that a non-0 return status for a variable assignment will cause Travis to fail.

Why have Travis Check for Merge Markers?

While git merge markers in PHP files will cause syntax errors, merge markers in static build files (CSS / JS files, for example), can have fatal consequences, but do not get caught by the PHP syntax check.  This really only applies if you commit your build files to the git repository.

It’s not unheard of that people think they have resolved Git merge conflicts, but really, they haven’t resolved them correctly.  Some tools like the Github GUI have a safety check to prevent this issue, but using Git command line, it’s totally possible to make this mistake.

If the merge markers are present in critical files, the website or application won’t run.  This is something that should cause a Travis failure.

How to add a check for Git Merge Markers

The simplest way I found to accomplish this was to create a new bash file.  We’ll call it travis-functions.sh

This file contains a function that uses grep to search for the merge markers and then exits with a non-0 exit code if Git merge markers are found.  We exclude checking some directories like node_modules and also we have to exclude checking the file containing this check.  My file looks like this:

function checkconflicts() {
  local found_merge_markers="$(grep -EHlr --exclude=travis-functions.sh --exclude-dir={node_modules,vendor} '<<<<<<< HEAD|>>>>>>>' .)"
    if [ -n "$found_merge_markers" ]; then
      echo "git merge conflict markers found!"
      echo $found_merge_markers
      exit 1
    fi;
}

Update the exclude-dir argument to whatever makes sense given your project.  These are the directories that we won’t be checking for merge markers.

To run this check, you have to source the file and run the function in your .travis.yml file.  This can be done like this:

  # Fail on Merge Conflict markers
  - source travis-functions.sh
  - checkconflicts

That’s everything!  Hope this helps you!

Related Links

Travis CI: Common Build Problems

Leave a Reply