Skip to content

Instantly share code, notes, and snippets.

@rummelonp
Created December 14, 2012 16:47
Show Gist options
  • Save rummelonp/4286831 to your computer and use it in GitHub Desktop.
Save rummelonp/4286831 to your computer and use it in GitHub Desktop.
.gitignore のテンプレートを出力する git-ignore-template 作った

.gitignore のテンプレートを出力する git-ignore-template 作った

作った。

環境

Ruby の実行環境と json gem が必要。 因みに ruby 1.9.3 でしか動作確認してない。

インストール

git-ignore-template をパスの通ったところに置き実行権限を付与する。

curl -O https://gist.github.com/raw/4286831/git-ignore-template
chmod +x git-ignore-template
mv git-ignore-template /usr/local/bin

使い方

こんな感じで使える。

cd /path/to/my_project
git ignore-template Ruby Rails > .gitignore

引数を指定しないと使用可能な言語/環境が出力される。

$ git ignore-template
Usage: git ignore-template <language>...

Languages:
  Actionscript Android AppceleratorTitanium Autotools Bancha C C++ CFWheels CMake CSharp CakePHP Clojure CodeIgniter Compass Concrete5 Coq Delphi Django Drupal Erlang ExpressionEngine Finale ForceDotCom FuelPHP GWT Go Grails Haskell Java Jboss Jekyll Joomla Jython Kohana LaTeX Leiningen LemonStand Lilypond Lithium Magento Maven Node OCaml Objective-C Opa OracleForms Perl PlayFramework Python Qooxdoo Qt R Rails RhodesRhomobile Ruby Scala Sdcc SeamGen SketchUp SugarCRM Symfony Symfony2 SymphonyCMS Target3001 Tasm Textpattern TurboGears2 Unity VB.Net Waf Wordpress Yii ZendFramework gcov nanoc opencart

テンプレートを作成したい言語/環境をを指定(複数可能)するとその言語/環境の .gitignore のテンプレートが出力される。

$ git ignore-template Ruby Rails
## Ruby
*.gem
*.rbc
.bundle
.config
coverage
InstalledFiles
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp

# YARD artifacts
.yardoc
_yardoc
doc/

## Rails
*.rbc
*.sassc
.sass-cache
capybara-*.html
.rspec
/.bundle
/vendor/bundle
/log/*
/tmp/*
/db/*.sqlite3
/public/system/*
/coverage/
/spec/tmp/*
**.orig
rerun.txt
pickle-email-*.html

zsh で補完

zsh を使ってて git の補完が有効になってる場合は _git-ignore-template を $fpath の通ったところにおけば補完が効く。

curl -O https://gist.github.com/raw/4286831/_git-ignore-template
mv _git-ignore-template /usr/local/share/zsh/site-functions

因みに zsh 5.0.0 でしか動作確認してない。

その他

Github API v3 を使ってる。

Gitignore templates | GitHub API

後、API からの取得結果は $TMPDIR にキャッシュしてる。

#compdef git-ignore-template
#description generate .gitignore template
function __git_ignore_template_languages() {
local languages
languages=($(git ignore-template | grep -A1 Languages | grep -v Languages))
_wanted languages expl 'languages' compadd - $languages
}
function _git-ignore-template() {
_arguments '*:: :__git_ignore_template_languages'
}
#!/usr/bin/env ruby
require 'open-uri'
require 'json'
require 'uri'
GITHUB_API_ENDPOINT = 'https://api.github.com'
GITIGNORE_TEMPLATES_URI = File.join(GITHUB_API_ENDPOINT, '/gitignore/templates')
CACHE_DIR = ENV['TMPDIR'] || ENV['TMP'] || ENV['TEMP'] || '/tmp'
def cache_or_get(uri)
cache_name = URI.parse(uri).path.gsub(/\//, '_') + '.json'
cache_file = File.join(CACHE_DIR, cache_name)
if File.exists?(cache_file)
content = open(cache_file).read
else
content = open(uri).read
open(cache_file, 'w') { |f| f.puts content }
end
JSON.parse(content)
end
if ARGV.empty?
puts 'Usage: git ignore-template <language>...'
puts
puts 'Languages:'
languages = cache_or_get(GITIGNORE_TEMPLATES_URI)
puts ' ' + languages.join(' ')
else
ARGV.each do |lang|
begin
response = cache_or_get(File.join(GITIGNORE_TEMPLATES_URI, lang))
puts "## #{lang}"
puts response['source']
puts
rescue => e
$stderr.puts "#{lang}: #{e.message}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment