From 4b8f77689150957817bbd9e72a5937f1bc1fbe18 Mon Sep 17 00:00:00 2001
From: Adar Nimrod <nimrod@shore.co.il>
Date: Tue, 13 Jun 2017 09:05:23 +0300
Subject: [PATCH] WIP.

---
 .pre-commit-config.yaml   | 17 +++++++++++++
 .pre-commit-hooks.yaml    |  8 ++++++
 .travis.yml               | 33 +++++++++++++++++++++++++
 hooks.yaml                |  1 +
 merge-conflict            |  9 +++++++
 tests/merge-conflict.bats | 51 +++++++++++++++++++++++++++++++++++++++
 6 files changed, 119 insertions(+)
 create mode 100644 .pre-commit-config.yaml
 create mode 100644 .pre-commit-hooks.yaml
 create mode 100644 .travis.yml
 create mode 120000 hooks.yaml
 create mode 100755 merge-conflict
 create mode 100755 tests/merge-conflict.bats

diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
new file mode 100644
index 0000000..827f946
--- /dev/null
+++ b/.pre-commit-config.yaml
@@ -0,0 +1,17 @@
+-   repo: https://github.com/pre-commit/pre-commit-hooks
+    sha: v0.7.1
+    hooks:
+    -   id: check-added-large-files
+    -   id: check-merge-conflict
+    -   id: check-yaml
+-   repo: https://www.shore.co.il/git/shell-pre-commit/
+    sha: v0.5.4
+    hooks:
+    -   id: shell-lint
+        files: &shellscripts 'merge-conflict'
+    -   id: shellcheck
+        files: *shellscripts
+-   repo: ./
+    sha: HEAD
+    hooks:
+    - id: merge-conflict
diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml
new file mode 100644
index 0000000..8a92237
--- /dev/null
+++ b/.pre-commit-hooks.yaml
@@ -0,0 +1,8 @@
+---
+- id: merge-conflict
+  name: merge conflicts
+  description: Checks for merge conflicts to the master branch.
+  language: script
+  entry: merge-conflict
+  files: '$^'
+  always_run: True
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..3b4d008
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,33 @@
+---
+language: python
+python: "3.6"
+dist: trusty
+sudo: false
+group: beta
+cache:
+  - pip
+  - directories:
+      - $HOME/.pre-commit
+      - $HOME/.cabal
+      - $HOME/.ghc
+
+addons:
+  apt:
+    packages:
+      - cabal-install
+      - ghc
+
+env:
+    PATH: "$HOME/bats/bin:$HOME/.cabal/bin:$PATH"
+
+install:
+  - cabal update && cabal install shellcheck
+  - pip install pre_commit | cat
+  - git clone --depth 1 https://github.com/sstephenson/bats.git "$HOME/bats"
+
+script:
+  - pre-commit run --all-files
+
+notifications:
+  on_failure: never
+  email: false
diff --git a/hooks.yaml b/hooks.yaml
new file mode 120000
index 0000000..32789ce
--- /dev/null
+++ b/hooks.yaml
@@ -0,0 +1 @@
+.pre-commit-hooks.yaml
\ No newline at end of file
diff --git a/merge-conflict b/merge-conflict
new file mode 100755
index 0000000..2725e35
--- /dev/null
+++ b/merge-conflict
@@ -0,0 +1,9 @@
+#!/bin/sh
+set -eu
+
+dest="${1:-master}"
+current="$(git symbolic-ref --short HEAD)"
+
+[ "$current" != "$dest" ] || exit
+
+git format-patch "$(git merge-base HEAD "$dest")..$dest" --stdout | git apply --check -
diff --git a/tests/merge-conflict.bats b/tests/merge-conflict.bats
new file mode 100755
index 0000000..9f5359b
--- /dev/null
+++ b/tests/merge-conflict.bats
@@ -0,0 +1,51 @@
+#!/usr/bin/env bats
+
+export PATH="$BATS_TEST_DIRNAME/../:$PATH"
+export repo="$BATS_TMPDIR/testrepo"
+
+@test "setup" {
+    if [ ! -f "$repo/file" ]
+    then
+        git init "$repo"
+        cd "$repo"
+        echo 1 > file
+        git -C "$repo" add "$repo/file"
+        git -C "$repo" commit -m "Initial commit."
+        git -C "$repo" checkout -b master2
+        git -C "$repo" checkout -b no-conflict
+        git -C "$repo" checkout -b conflict
+        echo 2 > "$repo/file"
+        git -C "$repo" commit file -m "Conflict with master."
+        git -C "$repo" checkout master
+        echo 3 > "$repo/file"
+        git -C "$repo" commit file -m "Conflict with the conflict branch."
+    fi
+}
+
+@test "Same branch" {
+    cd "$repo"
+    git checkout master || true
+    merge-conflict
+}
+
+@test "No conflict" {
+    cd "$repo"
+    git checkout no-conflict || true
+    merge-conflict
+}
+
+@test "Different than master" {
+    cd "$repo"
+    git checkout master || true
+    merge-conflict master2
+}
+
+@test "Conflict" {
+    cd "$repo"
+    git checkout conflict || true
+    ! merge-conflict
+}
+
+@test "teardown" {
+    rm -rf "$repo"
+}
-- 
GitLab