Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Remove .svn files recursively

find . -type d -name '.svn' -exec rm -rf {} \;

recursevly remove .svn files from an existing repo

If you need to take a project out of scm, you can run this bash script from the top folder - and it will remove all .svn folders recursevly (sp)

Works well for
- changing an svn checkout into and svn export
- switching from svn to git

find . -type d -name '.svn' -exec rm -rf {} \;

find foo in directories, but foo on .svn directories

// search for foo in all .php files in the dir tree, but skip .svn directories

find . -path .svn -prune -o -name *.php | xargs grep foo

bash shell code to replace text in multiple files, multiple dirs, (but not in SVN dirs)

find . -name          *.php -exec sed -i 's/oldtext/newtext/g' {} \;
find . -name .svn -prune -o -exec sed -i 's/oldtext/newtext/g' {} \;


My understanding:

find files starting with . (local directory)

-name .svn -prune means if you find .svn for a filename (directory name), skip it.

-o OR

-exec execute

sed -i sed is the stream editor. -i means change the files in place.

's/new/old/g' Substitute old text for new text Globally.

{} sends the filenames that find found to sed

\; terminates the -exec from find, and \ escapes it so the shell won't think it means to end its command.


Subversion Configuration Script for Your Rails Application

// Configure SVN for your rails app.

#!/bin/sh
svn remove log/*
svn commit -m"removing log files" 
svn propset svn:ignore "*.log" log/
svn update log/
svn commit -m 'Ignoring all files in /log/ ending in .log'
svn move config/database.yml config/database.example
svn commit -m 'Moving database.yml to database.example to provide a template for anyone who checks out the code'
svn propset svn:ignore "database.yml" config/
svn update config/
svn commit -m 'Ignoring database.yml'
svn remove tmp/*
svn propset svn:ignore "*" tmp/
svn update tmp/
svn commit -m "ignore tmp/ content from now" 
svn propset svn:ignore ".htaccess" config/
svn update config/
svn commit -m 'Ignoring .htaccess'
svn propset svn:ignore "dispatch.fcgi" config/
svn update config/
svn commit -m 'Ignoring dispatch.fcgi'

Add missing empty directories in .svn

Fix errors like
svn: Your .svn/text-base directory may be missing or corrupt; run 'svn cleanup' and try again
svn: Can't open file 'blabla/.svn/text-base/entries': No such file or directory

require 'pathname'

def recurse(dir)
  for child in dir.children
    next unless child.directory?              # ignore files
    next if child.basename.to_s == '.svn'     # ignore .svn directory
    next unless (child + '.svn').exist?       # ignore unadded directories
    text_base_dir = child + '.svn/text-base'  # path to text-base dir
    next if text_base_dir.exist?              # ignore existing text-base dirs
    text_base_dir.mkdir
    puts text_base_dir
    recurse child
  end
end

recurse Pathname.new('.')

moving subversion server to another machine

dump the repository to a text file
svnadmin dump repositoryPath > repository.dumpfile


create the new repository on the new machine
cd /path/to/new-repository-parent-directory
svnadmin create repository-name
svnadmin load repository-name < repository.dumpfile


transfer your local svn project to the new machine. Use absolute paths!
svn switch --relocate oldurl newurl


Example:

svn switch --relocate http://myoldcrapserver.com/svn/myfunkyproj svn+ssh://mykickingnewserver.com/var/svn/myfunkyproj

export certain revision of file from svn

Easier to just cat the revision you want and copy it over rather than try to check out a certain revision.

svn cat -r [REV] mydir/myfile > mydir/myfile

Protect .svn directories using htaccess

// block access to .svn dirs
// should be done server-wide if you can (another snippet)

<IfModule mod_rewrite.c>
  RewriteRule ^(.*/)?\.svn/ - [F,L]
  ErrorDocument 403 "Access Forbidden"
</IfModule>

Protect .svn directories server-wide (Apache)

// protect ".svn" and "CVS" dirs (could add more)
// for server-wide protection; goes in httpd.conf
// there's a separate snippet for .htaccess-based code

<DirectoryMatch "^/.*/(\.svn|CVS)/">
  Order deny,allow
  Deny from all 
</DirectoryMatch>