Never been to TextSnippets 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!)

« Newer Snippets
Older Snippets »
35 total  XML / RSS feed 

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>

Start Subversion Repository with Initial Import

// description of your code here

On the server (cannot be done remotely)
svnadmin create /var/svn/repository_name


On the client with the files to check in
svn import svn+ssh://myserver.com/var/svn/repository_name

You MUST use full path if using svn+ssh. If using HTTP, full path is not necessary.

Then erase the entire directory, and checkout the files from Subversion in order to make the current directory a working copy. Be careful and copy the directory to a backup directory before deleting all files and checking out a working copy.
cd ..
mkdir bak_directory
cp -R my_directory bak_directory
cd my_directory
rm -rf *
svn co svn+ssh://myserver.com/var/svn/repository_name .

Don't forget that period at the end of the checkout (co) statement. It will create the directory if you forget the . (current directory) optional parameter which will result in my_directory/my_directory/[your files]

Access subversion repository on Textdrive via ssh

I found it really difficult to get svn via svn+ssh geting to work because I made two erorrs:
I used not my main user. However, only the main user can get shell access necessary for svn via ssh.
I used a wrong path.

In order to get things going set up subversion following the usual directives in the knowledge base and then access your repository like this:
(OS X terminal here)
% svn co svn+ssh://mainuser@yourdomain.tld/home/mainuser/svn/repositoryname/

This should do the trick.

Remember not to use ssh and http(s) at the same time. It's either or.

Syncing Subversion

When there are directories and files that are in a subversion respository, but don't need to be under source control, you must delete them both in the repository and locally.

To delete it locally:

svn delete <dir> --force


To delete it at repository:
svn delete http://cru.textdriven.com/svn/<project_dir>/ -m ""


Then perform your commit as per normal:
svn commit -m ""

wdiff wrapper for svn

#/bin/sh

wdiff $6 $7

Add all new files to Subversion

This solution is shell-agnostic, unlike http://textsnippets.com/posts/show/450, and cleaner than to do a --force. I recommend adding it to a alias in your shell, i.e "sall".

svn st | grep "^?" | awk -F "      " '{print $2}' | xargs svn add


Can be modified to do a svn delete on files you accidentally rm -rf'ed:

svn st | grep "^!" | awk -F "      " '{print $2}' | xargs svn delete

SVN ignore for all assets that don't begin by "_"

svn propset svn:ignore "[!_]*" .

sup: bash script for universal SCM syncing (svn, svk, cvs, darcs, etc.)

I really can never remember if the project I'm in uses svn or cvs or if it's my own local svk mirror, etc. etc. Thus was born 'sup'!

Save and drop into a location that's in your PATH (I use ~/bin, YMMV)

#!/bin/bash
# sup -- a quick bash script to sync w/ various SCM tools
# @author   Jamie Wilkinson 

HERE=$(pwd)

## subversion
if [ -e ".svn" ]; then
        svn up
## cvs
elif [ -e "CVS" ]; then
        cvs up -dP
## darcs
elif [ -e "_darcs" ]; then
        darcs pull --all
## svk
elif [ -n "`grep $HERE ~/.svk/config`" ]; then
    svk up
## git
elif [ -e ".git" ]; then
    git pull
## perforce
#   todo
## arch
#   todo
## bzr
#   todo

fi

Recursively remove .svn directories (ruby script)

This snippet traverses a directory tree and removes .svn directories.

require 'find'
require 'fileutils'
Find.find('./') do |path|
  if File.basename(path) == '.svn'
    FileUtils.remove_dir(path, true)
    Find.prune
  end
end

Rails Subversion First Checkout

// I use this for checking out a new rails project

svn checkout http://studicious.com/svn/trunk %1
svn remove log/*
svn commit -m "Removed log files"
svn propset svn:ignore "*.log" log/
svn update log/
svn commit -m "Ignoring log files"
svn remove tmp/*
svn propset svn:ignore "*" tmp/
svn update tmp/
svn commit -m "Ignoring temp folder"

Backup from textdrive to strongspace

#!/bin/sh
# backup script for textdrive to strongspace based on these articles:
# http://help.textdrive.com/index.php?pg=kb.page&id=113
# http://forum.textdrive.com/viewtopic.php?id=10126

# change this!
USER=txd_username
HOME=/users/home/$USER
USER_SS=strongspace_username
PWD_MYSQL=somepassword # chmod 700 this file

# Backup database 
# copy this line for more databases or 
# use --all-databases if you are the main user
# Don't forget to change the database name
/usr/local/bin/mysqldump --opt --skip-add-locks --user=$USER --password=$PWD_MYSQL database1 | gzip > $HOME/backups/database1_`date "+%Y-%m-%d"`.gz
/usr/local/bin/mysqldump --opt --skip-add-locks --user=$USER --password=$PWD_MYSQL database2 | gzip > $HOME/backups/database2_`date "+%Y-%m-%d"`.gz

# Backup subversion (Only works with FSFS)
cd $HOME
tar -z -c -f $HOME/backups/svn_`date "+%Y-%m-%d"`.tar.gz svn

# Add custom dirs here, if you need it, just like the svn example above
# I just keep everything I need in subversion

# Delete old backups
cd $HOME/backups/
/usr/bin/find *.gz -mtime +8 -delete

# Send it to strongspace
/usr/local/bin/rsync -azq --delete -e "ssh -i $HOME/.ssh/ss" $HOME/backups/*.gz $USER_SS@$USER_SS.strongspace.com:/home/$USER_SS/txd-backup/

create a rails app from rails trunk, optionally committing to a subversion repository

I name this script railstrunk on my machine. So, usage would be:

railstrunk app_name


That creates a rails application under app_name.

If app_name is a subversion working copy, rails is set as an external and generated files are committed, and some ignore properties are set.

Here's how I'd go about creating a versioned rails app:

mkdir -p happy_app happy_app/trunk happy_app/tags happy_app/branches 
svn import happy_app http://myserver/myrepos/happy_app -m "Layout for happy_app"
rm -rf happy_app
svn co http://myserver/myrepos/happy_app/trunk happy_app
railstrunk happy_app


$rails_dir is set at the beginning of script, and it should be a path to a working copy of rails trunk on your machine. It's merely used to speed things up, it's not necessary.

Notice I remove the silly public/index.html from the generated app.

And now the code:

#!/bin/bash

rails_dir=~/code/ruby/rails

if [ $# != 1 ]; then
  echo "Usage: $0 app_name"
  echo
  echo "Creates a rails application under app_name."
  echo
  echo "If app_name is a subversion working copy, rails is set as an external"
  echo "and generated files are committed."
  echo
  echo "If $rails_dir exists, things are sped up by either symlinking it"
  echo "(for non-versioned apps) or copying it to the vendor dir."
  echo
  echo "$rails_dir should be a rails trunk working copy."
  exit
fi

dir=$1
mkdir -p $dir
cd $dir

if [ -n "`ls`" ]; then
  echo "Can't create app: $dir is not empty." >&2
  exit 1
fi

if [ -d $rails_dir ]; then
  if [ "`svn info $rails_dir 2>/dev/null | grep URL:`" != "URL: http://dev.rubyonrails.org/svn/rails/trunk" ]; then
    echo "$rails_dir is not a rails trunk working copy. Not going to use it." >&2
  elif [ -n "`svn st $rails_dir`" ]; then
    echo "$rails_dir is modified. Not going to use it." >&2
  else
    use_rails_dir=1
  fi
fi

if [ -z "`svn info 2>/dev/null`" ]; then
  mkdir vendor
  if [ -n "$use_rails_dir" ]; then
    ln -s $rails_dir vendor/rails
    cd vendor/rails
    svn up
    cd ../..
  else
    svn co http://dev.rubyonrails.org/svn/rails/trunk vendor/rails
  fi
  ruby vendor/rails/railties/bin/rails .
  rm public/index.html
else
  svn mkdir vendor
  svn ps svn:externals 'rails http://dev.rubyonrails.org/svn/rails/trunk' vendor
  svn ci -m 'set rails external to rails trunk'
  [ -n "$use_rails_dir" ] && cp -r $rails_dir vendor/rails
  svn up
  ruby vendor/rails/railties/bin/rails .
  rm public/index.html
  rm log/*
  mv config/database.yml config/database.yml.sample
  svn add . --force
  svn ps svn:ignore '*' tmp/cache tmp/pids tmp/sessions tmp/sockets
  svn ps svn:ignore 'database.yml' config
  svn ps svn:ignore '*.log' log
  svn ci -m "- created rails app
- moved database.yml to database.yml.sample
- deleted public/index.html
- ignored logs and tmp"
  cp config/database.yml.sample config/database.yml
fi

Remove missing subversion files

// automatically remove files missing from subversion

svn status | grep '\!' | awk '{print $2;}' | xargs svn rm 

update a working copy and all externals in parallel

If you have an app in subversion with many externals, it may take a bit too long to update it, as updates happen one after another.

This bit of script updates the app and each external in parallel, making it oh so much faster.


#!/usr/local/bin/ruby

puts(
  ( `svn pl -R`.scan(/\S.*'(.*)':\n((?:  .*\n)+)/)\
    .inject({}) { |h, (d, p)| h[d] = p.strip.split(/\s+/); h }\
    .select { |d, ps| ps.include? 'svn:externals' }\
    .map { |xd, ps| [xd, `svn pg svn:externals #{xd}`] }\
    .map { |xd, exts| exts.strip.split(/\s*\n/).map { |l| xd + '/' + l.split(/\s+/).first } }\
    .inject { |a, b| a + b }\
    .map { |d| "cd #{d} && svn up 2>&1" } \
    << 'svn up . --ignore-externals 2>&1'
  )\
  .map { |cmd| [cmd, Thread.new { `#{cmd}` }] }\
  .map { |cmd, thread| "#{cmd}\n#{thread.value}" }.join("\n")
)


(note: if you add an external or change an external property in another way, you'll need to run the standard svn up once)

Rake task that adds all unversioned files to the repository

task :add_all_to_svn do
  `svn status --show-updates`.each { |l|
    l = l.split
    system("svn add #{l.last}") if l.first == "?" # File is not under version control
  }
end
« Newer Snippets
Older Snippets »
35 total  XML / RSS feed