Skip to content

Instantly share code, notes, and snippets.

@np
Created November 22, 2011 13:22
Show Gist options
  • Save np/1385648 to your computer and use it in GitHub Desktop.
Save np/1385648 to your computer and use it in GitHub Desktop.
git-ignore.d
#!/bin/bash
# vim: ft=sh
###########################################################################
# Copyright (c) 2011, Nicolas Pouillard
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
#
# * Neither the name of the copyright holders nor the names of other
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
###########################################################################
###########################################################################
## DOCUMENTATION
##
## git-ignore.d let's you split your .gitignore file into
## multiple files that are put in the .gitignore.d/ directory.
##
## Limitations:
## * You can no longer use the .gitignore file since it will be
## generated by git-ignore.d.
## * You have to call git-ignore.d to refresh the .gitignore
## file using the files from the .gitignore.d/ directory.
## We can deal with this limitation by calling git-ignore.d
## in various git hooks.
##
## Usage:
## # Make a .gitignore.d/ directory
## $ mkdir .gitignore.d
##
## # Move your .gitignore file into it.
## $ git mv .gitignore .gitignore.d/main
##
## # You can split .gitignore.d/main into multiple files.
## $ edit .gitignore.d/*
##
## # Run git-ignore.d to generate the .gitignore file
## $ git-ignore.d
##
## # The generated .gitignore file is actually a symlink to
## # .git/info/git-ignore.d-exclude so it is safe and cheap
## # to store the symlink.
## $ git add .gitignore
##
## # Commit
## $ git commit -m 'Switch to .gitignore.d'
##
## Use cases:
## * It may be just cleaner to have separate files for
## different sets of rules.
## * Parts of you ignore rules are generated, thus it is
## simpler to generate files in .gitignore.d/ instead of
## `patching' .gitignore.
## * You want to share common .gitignore rules among several
## repositories or directories. Symbolic links in the
## .gitignore.d/ directory is one solution.
###########################################################################
case "$1" in
"") ;;
-h|--help|-?)
grep '^##\( \|$\)' "$0" | sed -e 's/^## \?//'
exit 0;;
*)
echo "Unrecognized argument: use --help to get some help"
exit 1;;
esac
cd "$(git rev-parse --git-dir)/.."
die(){
echo error: "$@" >>/dev/stderr
exit 1
}
if [ "$(git rev-parse --is-inside-work-tree)" = false ]; then
die "Could not go in the git work-tree."
fi
EXCL=.git/info/git-ignore.d-exclude
rm -f $EXCL
echo "# Generated by git-ignore.d do not edit by hand." >$EXCL
if [ -d .gitignore.d ]; then
cat .gitignore.d/* >>$EXCL
else
echo "# No .gitignore.d/ directory." >>$EXCL
fi
chmod -w $EXCL
if [ -e .gitignore ]; then
if [ "$(readlink .gitignore)" != $EXCL ]; then
die "git-ignore.d expects to control .gitignore." \
"You can use any file in .gitignore.d/ instead."
fi
else
echo "Making .gitignore a symlink to $EXCL..." >>/dev/stderr
ln -s $EXCL .gitignore
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment