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 »
24 total  XML / RSS feed 

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('.')

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>

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 ""

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

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

Patch / Diff Hunk Matching Regexp

This is tested in TextMate and I use it to select successive hunks of a patch file in diff -u format.

(?m)^@@.*?^(?=@@)

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

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)

Make a subversion release

Ruby script to make a release from SVN trunk.

Assumes a repository structure of:

/path/to/Trunk
/path/to/Releases/YYYY-MM-DD--XX

Should be easy enough to modify to suit your needs. Comments welcome :)

Example usage:
vi /usr/local/bin/release (and dump snippet into it)
chmod +x /usr/local/bin/release
cd /path/to/checked-out-trunk
release


#!/usr/bin/ruby

today = Time.now.strftime('%Y-%m-%d')

if `svn info`.to_a[1] !~ /^URL: (.*)\/Trunk$/
        puts "Repository invalid. Must be in Trunk."
        exit
end

repo_url = $1

repo = repo_url.split('/').last

release_url = repo_url + "/Releases"

if `svn ls #{release_url}`.to_a.last =~ /#{today}--(\d+)/
        num = $1.to_i + 1
else
        num = 1
end

release = "#{today}--#{num}"

cmd = "svn copy #{repo_url}/Trunk #{release_url}/#{release} -m 'copy #{repo} trunk to release #{release}'"
puts cmd
puts

print "Execute command? "

if gets.chomp.upcase != 'Y'
        puts "Nothing done."
        exit
end

`#{cmd}`
puts "Copied as #{release}."
puts "Execute on the remote machine:"
puts "svn switch #{release_url}/#{release}"

Add all new files to Subversion

Bash script to add all new files to Subversion

for i in `svn st | grep ? | awk -F "      " '{print $2}'`; do svn add $i; done

Bulk svn actions (usefull with rails)

Just run this code from inside your repository. This will do a bulk action like "svn add" on each file marked with the given filter (like '?') when doing "svn status"

Beware: this code might break with multi word filenames...

usage :
# ./svn_bulk ? add ---> this shows a preview of the actions
# ./svn_bulk ? add 1 ---> action !
#!/usr/bin/ruby
sign, action, doit = ARGV
sign = '\?' if sign == '?'
list = IO.popen("svn st")
list.each {|i|
if (i =~ /^#{sign}\s*(.*)/) 
cmd = "svn #{action} '"+$1+"'"
print cmd + "\n"
system(cmd) if doit
end
}

Svn obliterate (dump filter)

Taken and slightly adapted from www.robgonda.com/blog

svnadmin dump /path/to/repos > proj.dump
cat proj.dump | svndumpfilter exclude somefolder > cleanproj.dump
STOP SVN services
BACKUP /path/to/repos/conf /path/to/repos/hooks (all custom configuration for this repository)
DELETE /path/to/repos
svnadmin create /path/to/repos
RESTORE /path/to/repos/conf /path/to/repos/hooks
svnadmin load /path/to/repos < cleanproj.dump
RESTART SVN services

Deny access to .svn directories with lighttpd

$HTTP["url"] =~ "/\.svn/" {
    url.access-deny = ( "" )
}

Update All of the SVN Version Controlled Projects in a Directory

I have all of my projects in a directory like ~/PROJECTS. Each day when I come to work, I want to ensure that everything on my workstation is up to date. Rather than cd into each directory and issue "svn update" by hand, I kludged the following shell script together. Comments appreciated.


 for X in `find . -maxdepth 2 -type d -name ".svn" | cut -d / -f 2`; do svn update ./$X; done  

Back up Subversion repositories

Dump all Subversion repositories to a location of your choice. I run this as a backup_script from rsnapshot.

#!/bin/sh -e
###########################################################
# Back up Subversion repositories
###########################################################

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

# Subversion config
###########################################################

# My config
#SVN_DIR="/var/svn"
#BACKUP_DIR="."

SVN_DIR=""
BACKUP_DIR=""
SVN_REPOS=`ls $SVN_DIR`

# Dump repositories
###########################################################

for repos in $SVN_REPOS; do
    svnadmin dump --incremental --deltas --quiet $SVN_DIR/$repos \
        > $BACKUP_DIR/$repos
done

exit 0



Enable svn+ssh remote logins

Installing Subversion for local use is general an easy install, but allowing remote access to your svn repository over SSH can be problomatic dependent upon your OS and the means taken to install.

For Darwinports and Fink on OS X the install location has to be added to users $PATHs, but there are extra steps outlined here for use of the svn+ssh means of access:

http://subversion.tigris.org/faq.html#ssh-svnserve-location

A much easier alternate is to sym link the svn binaries to a place on the default PATH (used by the SSH login):

#For Darwinports
ln -s /opt/local/bin/sv* /usr/bin/

#For Fink
ln -s /sw/bin/sv* /usr/bin/


This links all the binaries at once.

Finding files to be added

svn add `svn st | grep '\?' | tr '\?' ' ' | xargs`

stop creating useless .ds_store files on your non-mac network share

http://docs.info.apple.com/article.html?artnum=301711

1. Open the Terminal.
2. Type:
defaults write com.apple.desktopservices DSDontWriteNetworkStores true

3. Press Return.
4. Restart the computer.

now you'll no longer fill the windows file server with annoying .ds_store files. you'll no longer unknowingly commit hundreds of irrelevent .ds_store files to your subversion repository mounted over webdav with auto-versioning. rejoice.
« Newer Snippets
Older Snippets »
24 total  XML / RSS feed