Skip to content

Instantly share code, notes, and snippets.

@attakei
Last active December 25, 2015 05:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save attakei/98eb484cf013afd0d1ba to your computer and use it in GitHub Desktop.
Save attakei/98eb484cf013afd0d1ba to your computer and use it in GitHub Desktop.
Get gitignore from github/gitignore

Fetch and merge gitignore from Github

It is tool to fetch gitignore contents from https://github.com/github/gitignore, merge fetched contents and generate local .gitignore file.

Usage

example)

$ curl https://gist.githubusercontent.com/attakei/98eb484cf013afd0d1ba/raw/f98b7ef8d87732363d2ccdb76ec9ef35151163d2/get_gitignore.py > get_gitignore
$ chmod +x get_gitignore
$ ./get_gitignoe Python
#!/usr/bin/env python
from __future__ import unicode_literals
import sys
import os
import argparse
PY_VERSION = sys.version_info[0]
if PY_VERSION == 3:
from http.request import urlopen
elif PY_VERSION == 2:
from urllib2 import urlopen
else:
print('error')
exit(1)
URL_BASE = 'https://raw.githubusercontent.com/github/gitignore/master/{}.gitignore'
parser = argparse.ArgumentParser('Get gitignore file from github')
parser.add_argument(
'lang', metavar='LANG', type=str, nargs='+',
help='use gitignore languages',
)
def main(argv):
args = parser.parse_args(argv)
ignore_contents = []
for lang in args.lang:
resp = urlopen(URL_BASE.format(lang))
ignore_contents.append((lang, resp.read()))
ignore_path = os.path.join(os.getcwd(), '.gitignore')
with open(ignore_path, 'w') as fp:
write_global_header(fp)
for (lang, content) in ignore_contents:
write_lang_heaer(fp, lang)
fp.write(content)
fp.write('\n')
def write_global_header(fp):
fp.write(
"""####################
# Generated by https://gist.github.com/attakei/98eb484cf013afd0d1ba
####################
"""
)
def write_lang_heaer(fp, lang):
fp.write(
"""########
# {}
########
""".format(lang)
)
if __name__ == '__main__':
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment