Skip to content

Instantly share code, notes, and snippets.

@seeflanigan
Created September 16, 2010 21:46
Show Gist options
  • Save seeflanigan/583227 to your computer and use it in GitHub Desktop.
Save seeflanigan/583227 to your computer and use it in GitHub Desktop.

Results

Of different approaches to sorting a .gitignore file

Initial .gitignore

.ackrc
*.log
coverage/*
*flymake*
public/system/*
config/database.yml
.rvmrc

Calling /usr/bin/sort on a .gitignore

$ sort .gitignore 
.ackrc
config/database.yml
coverage/*
*flymake*
*.log
public/system/*
.rvmrc

Calling ruby sort_gitignore.rb from a path containing a .gitignore

$ cat .gitignore 
*.log
*flymake*
.ackrc
.rvmrc
config/database.yml
coverage/*
public/system/*
#EOF
#!/usr/bin/env ruby
## Should transform a .gitignore file like this:
##
#
# tmp
# *.sql.bz2
# coverage/*
# *flymake*
# public/system/*
#
##
## Into this:
##
#
# *.sql.bz2
# *flymake*
# coverage/*
# public/system/*
# tmp
#
require 'ftools'
require 'tempfile'
lines = File.read(".gitignore")
entries = lines.split("\n")
tempfile = Tempfile.new(".gitignore.swp")
tempfile.write entries.sort.join("\n")
tempfile.write "\n#EOF\n"
tempfile.rewind
File.copy(tempfile.path, ".gitignore")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment