diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000000000000000000000000000000000000..827f946a44f6f7bd7dd3209bab5e922e59e202c7 --- /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 0000000000000000000000000000000000000000..8a9223714581622ec9b9c7dc6e6a1016ebabea7f --- /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 0000000000000000000000000000000000000000..3b4d00804dcf9b04eed359b8d8b6c4a4a7503591 --- /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 0000000000000000000000000000000000000000..32789ced82282edf227fcd375a5e6ceb85036ca3 --- /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 0000000000000000000000000000000000000000..2725e3521d243c5d754900106e5dff9b05586cc0 --- /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 0000000000000000000000000000000000000000..9f5359b5b09b18a7a2f16b58272d3c93389bc077 --- /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" +}