#TIL : Fastly conflict detector script

Last month, I built a CI solution for our project and adding a conflict detector to our build commands. This script runned so slow because it will check all application files (and our application codebase has many of css, js files).

This was the script

1
2
3
4
5
6
7
8
9
#!/bin/bash

grep -rli --exclude=conflict_detector.sh --exclude-dir={.git,vendor,node_modules} "<<<<<<< HEAD" .

if [ $? -eq 0 ]; then
exit 1
else
exit 0
fi

Today, I think why don’t we just check recently updated files (in the latest commit) ??? Then I have this new script

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/bash

# New way :D
CHANGED_FILES=$(git log --pretty=format: --name-only HEAD^..HEAD | sort | uniq)

for f in $CHANGED_FILES
do
if grep --exclude=conflict_detector.sh -q "<<<<<<< HEAD" $f
then
exit 1
fi
done

exit 0

conflict_detector.sh is the filename of script, we exclude it from check to make sure changing this file doesn’t make it failed.

You can use this approach to check linter, coding standard or run preprocessor ;)

Result (in my context) :

  • Old script : 12 seconds
  • New script : ~ 50ms (200 times faster)

be Automated, be Fast, but be Careful !

#TIL : using git hooks to improve working flow

We can improve our team workflow by defining some git hooks that trigger on specified events.
You can read all events and their usecases here : https://www.digitalocean.com/community/tutorials/how-to-use-git-hooks-to-automate-development-and-deployment-tasks

This is what I implemented to my today-i-learned repo. I used pre-commit to update Table of Contents in the README.md file, so every content in my repo will be updated on Github repo page.

1
$ ln pre-commit .git/hooks/pre-commit

pre-commit file :

1
2
3
4
5
6
#!/bin/sh

echo 'Running pre-commit hook'

python til_update_readme.py
git add README.md

So it will run a Python script that update new TOC and then add the file to git.

Automation ! Automation ! AND …. Automation !!! 🤖