Skip to content

Instantly share code, notes, and snippets.

@Falkor
Last active June 16, 2023 17:01
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Falkor/848c82daa63710b6c132bb42029b30ef to your computer and use it in GitHub Desktop.
Save Falkor/848c82daa63710b6c132bb42029b30ef to your computer and use it in GitHub Desktop.
#!/bin/bash
# Time-stamp: <Tue 2021-08-31 14:26 svarrette>
################################################################################
# Pre-commit hook to avoid accidentally adding unencrypted files with
# [git-crypt](https://www.agwa.name/projects/git-crypt/)
# Fix to [Issue #45](https://github.com/AGWA/git-crypt/issues/45)
#
# Usage:
# $ cd /path/to/repository
# $ git-crypt init
# $ curl <url/to/this/raw/gist> -o .git/hooks/pre-commit
# $ chmod +x .git/hooks/pre-commit
#
# Otherwise, you might want to add it as a git submodule, using:
# $ git submodule add https://gist.github.com/848c82daa63710b6c132bb42029b30ef.git config/hooks/pre-commit.git-crypt
# $ cd .git/hooks
# $ ln -s ../../config/hooks/pre-commit.git-crypt/pre-commit.git-crypt.sh pre-commit
#
# The latest version of this script can be found as a GIST on:
# https://gist.github.com/Falkor/848c82daa63710b6c132bb42029b30ef
#
################################################################################
# MIT License
#
# Copyright (c) 2016-2021 S. Varrette <Sebastien.Varrette@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
################################################################################
#
if [ -d .git-crypt ]; then
STAGED_FILES=$(git diff --cached --name-status | awk '$1 != "D" { print $2 }' | xargs echo)
if [ -n "${STAGED_FILES}" ]; then
git-crypt status ${STAGED_FILES} &>/dev/null
if [[ $? -ne 0 ]]; then
git-crypt status -e ${STAGED_FILES}
echo '/!\ You should have first unlocked your repository BEFORE staging the above file(s)'
echo '/!\ Proceed now as follows:'
echo -e "\t git unstage ${STAGED_FILES}"
echo -e "\t git crypt unlock"
echo -e "\t git add ${STAGED_FILES}"
exit 1
fi
fi
fi
@zacharykeeton
Copy link

Line 28 should be, echo -e "\t git-crypt unlock"

@mkesper
Copy link

mkesper commented Aug 31, 2021

Could you please clarify the license of this gist? We'd like to post it as a full pre-commit hook repo on Github.
My suggestion would be to use a simple license like https://opensource.org/licenses/mit-license.php

@Falkor
Copy link
Author

Falkor commented Aug 31, 2021

You're right @mkesper -- It's done. Enjoy ;)

@mkesper
Copy link

mkesper commented Aug 31, 2021

Thanks a lot!

@approximate
Copy link

Line 36 should be if [ -d .git/git-crypt ]; then -- I assume you want to make sure that git-crypt was ever enabled for the repo?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment