Git post merge hook to be notified when important/notable files change

Most of us work in projects where other devs screw up our code and introduce unintentional bugs. The right way to fix this is to have lots of integration/unit tests to catch them when they happen. However, sometimes you may not have all the integration tests you need. I wrote this git merge hook to be notified whenever I update my local repo and if any notable files have changed.

The .git/hooks/post-merge file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/bin/env ruby

changed_files = `git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD`.lines.map(&:chomp)
tracked_files = File.read("./tracked-files").lines.map(&:chomp)

notable_files = changed_files & tracked_files

if notable_files.empty?
  puts "----------NO NOTABLE CHANGES----------"
  exit(0)
end

puts "----------NOTABLE CHANGES:----------"
puts notable_files.join("\n")
puts "----------------------------------------"

system("git diff ORIG_HEAD HEAD -- #{notable_files.join(" ")}")

This expects you to have a file called “tracked-files” at the root of your repo.

Sample tracked-files

1
2
app/controllers/application_controller.rb
app/controllers/myawesome_controller.rb