Skip to content

Instantly share code, notes, and snippets.

@taylor
Created March 23, 2012 04:56
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save taylor/2166959 to your computer and use it in GitHub Desktop.
Git script for creating and modifying .gitignore files
#!/bin/bash
## REPO -- https://github.com/github/gitignore
## DOWNLOADS
DL_IGNORE_URL="https://raw.github.com/github/gitignore/master/"
IGNORE_TREE_URL="https://api.github.com/repos/github/gitignore/git/trees/master"
CACHE_IGNORE_LIST="$HOME/.cache/gitignore_list"
CACHE_IGNORE_NAMES="$HOME/.cache/gitignore_list_names"
function init_cache() {
if [ ! -d $HOME/.cache ] ; then
mkdir -p $HOME/.cache 2> /dev/null
fi
if [ ! -f $CACHE_IGNORE_LIST ] ; then
curl -o $CACHE_IGNORE_LIST -qs $IGNORE_TREE_URL
fi
}
function get_ignore_names() {
grep -E 'path":.*\.gitignore' $CACHE_IGNORE_LIST \
| sed 's/.* "\([^"]*\).gitignore",/\1/' \
> $CACHE_IGNORE_NAMES
}
function list_ignore_names() {
if [ ! -f $CACHE_IGNORE_NAMES -o $CACHE_IGNORE_NAMES -ot $CACHE_IGNORE_LIST ] ; then
get_ignore_names
fi
cat $CACHE_IGNORE_NAMES
}
function display_default_gitignore() {
cat <<-EOM | sed 's/^ *//'
*~
*.lock
.*.swp
*.log
tmp/
db/*
*.DS_Store
*#
.svn
.pyc
EOM
}
function get_ignore_file() {
curl -qs "${DL_IGNORE_URL}/${1}.gitignore"
}
function show_gitignore() {
if [ "$1" = "" ] ; then
display_default_gitignore
else
match=$(grep -i "$1" "$CACHE_IGNORE_NAMES")
r=$?
if [ "$r" = 0 ] ; then
get_ignore_file "$match"
else
echo "Could not find that name. See the list command"
fi
fi
}
function add_ignores() {
for i in "$@"
do
echo $i | tee -a ".gitignore"
done
}
#### Execution Starts Here
if [ ! "$1" = "init" -a ! "$1" = "list" -a ! "$1" = "add" ] ; then
echo "usage: git ignore <init [TYPE]|list|add <pattern> [pattern2] [...]>"
exit 1
fi
init_cache
case $1 in
"init")
show_gitignore "$2"
;;
"list")
list_ignore_names
;;
"add")
if [ "$2" = "" ] ; then
echo "Must give at least one pattern to ignore"
exit 1
fi
shift
add_ignores "$@"
;;
*)
echo "Unknown command $1"
;;
esac
@wwalker
Copy link

wwalker commented Mar 23, 2012

echo -e '_~\n.DS_Store\n.svn_.o*.so*.pyc' > .gitignore

@wwalker
Copy link

wwalker commented Mar 23, 2012

That's an amazing list. I'd forgotten how many languages there were since I only use a few now.

@taylor
Copy link
Author

taylor commented Mar 27, 2012

This may not be appropriate for directly including in the gitignore repo but I thought it may be interesting for people (@Aqua-Ye, @xpaulbettsx) interested in the @github ignore templates...

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