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 missing subversion files

This will remove versioned files from subversion that are missing from a working directory. i.e. the files displayed with a "!" after running 'svn st'

for d in `svn st | grep ^! | awk '{print $2}'`; do svn rm $d; done;

Simple perl script to merge SVN commits from a source branch to the current destination directory with a search string (Bugzilla id, for example) in the comments.

Today i have made my first PERL script!

For me it is very painful when it arrives the time to merge, into another branch, all the commits that i have done in the “trunk”. I have searched a little and did not find anything that could magically solve all my problems. I know that it’s better to create a separated branch when there are lot’s of commits, but there are some cases that a super-simple functionality can explode into a big ball of mud.

Practically the script merge all the commits of a bugzilla id to another branch. If someone knows a standard way to do this; please tell me!

The script take three inputs:

1. The starting revision ID to filter the search.
2. The SVN address of the source.
3. The search string to filter the results. Here you put your bugzilla bug id.

Commands that are executed when you launch the script:

1. Go to the directory of the destination branch.
2. To execute the script simply do:
3. svn_search_merge.pl 0 https://svn.example.com/main/trunk/ “1: “
4. Note that “1: ” is the bugzilla bug id. What happens next is:
5. svn log -r 1:HEAD https://svn.example.com/main/trunk/
6. With that command we get the log of all commits from the revision 1 to the HEAD. After it’s just matter of check if the string “1: ” is inside the log. Then we simply execute:
7. svn merge -r (ACTUAL_REVISION-1):ACTUAL_REVISION https://svn.example.com/main/trunk/

Source code of the script:
#!/usr/bin/perl

# Simple script to merge commits from a source branch to the current destination directory.
# http://mufumbo.wordpress.com/2008/05/10/simple-script-to-merge-commits-from-a-bugzilla-id/
#
# Example:
# $ cd my-branch-destination/
# $ svn_search_merge.pl 3000 https://svn.example.com/main/trunk/ "bug 673"
# Where 3000 is the starting revision and "bug 673" is the string to match in the comments.
#
use strict;
use warnings;

my $prev_revision = shift;
my $svnHost = shift;
my $searchStr = shift;

print "Starting Revision: $prev_revision\n";
print "SVN addr: $svnHost\n";
print "Search pattern: $searchStr\n";

my $buffer;
$buffer = `svn log -r $prev_revision:HEAD $svnHost`;
my $shouldContinue = "y";
LOGS: foreach my $changelog_entry (split(/----+/m, $buffer)) {
	if($changelog_entry =~ m/($searchStr)/) {
	        #my (undef, $info, undef, $comment) = split(/\n/, $changelog_entry);
	        #next unless $info =~ m/^r/;

		print "\n--------------------------------------------------";
		print $changelog_entry;
		my $revisionId = substr($changelog_entry, 2, 5);
		$revisionId =~ s/^\s+//;
		$revisionId =~ s/\s+$//;

		if ($shouldContinue ne 'a') {
			PROMPT: while(1) {
				print "\nShould continue with merge of revision '$revisionId'? (Yes,Always,Skip,Exit): ";
				$shouldContinue = <>;
				chomp($shouldContinue);

				last PROMPT if $shouldContinue eq 'y';
				last PROMPT if $shouldContinue eq 'a';
				next LOGS if $shouldContinue eq 's';
				die("User requested to stop.") if $shouldContinue eq 'e';
			}
		}
		else {
			print "\nAuto merging '$revisionId'\n";
		}

		my $pRevisionId = $revisionId-1;
		my $mergeBuffer = `svn merge -r $pRevisionId:$revisionId $svnHost`;
		print $mergeBuffer;
	}
}

Dump, filter, migrate subversion repo

svnadmin dump svn_repo > svn_repo.20080712.dumpfile

svndumpfilter exclude --drop-empty-revs --renumber-revs iogo1repo/projects/experientialforum/trunk/data iogo1repo/projects/experientialforum/trunk/www_joomla/ < svn_repo.20080712.dumpfile > svn_repo.small1.dumpfile

svndumpfilter include --preserve-revprops iogo1repo/projects/1stop iogo1repo/projects/AdminScripts iogo1repo/projects/db_utils iogo1repo/projects/esquebeauty iogo1repo/find-me-a-gift iogo1repo/projects/fmat iogo1repo/projects/furryanimallady iogo1repo/projects/galleriapangaea iogo1repo/projects/library iogo1repo/projects/moneybuckets iogo1repo/projects/netdreams_crm iogo1repo/projects/netdreams iogo1repo/projects/netdreams-cms iogo1repo/projects/nitroduck iogo1repo/projects/omwhs iogo1repo/projects/offthepress iogo1repo/projects/status iogo1repo/projects/sunshineestates iogo1repo/projects/timeorganizer iogo1repo/projects/the-vineyard iogo1repo/projects/x-tra-touch < svn_repo.small1.dumpfile > svn_repo.small2.dumpfile

svndumpfilter exclude --preserve-revprops iogo1repo/projects/library/fckeditor iogo1repo/projects/library/tinymce iogo1repo/projects/library/yui iogo1repo/projects/furryanimallady/trunk/www/product_images iogo1repo/projects/offthepress/trunk/www/incontact iogo1repo/projects/offthepress/trunk/www/admin/incontact < svn_repo.small2.dumpfile > svn_repo.small3.dumpfile

sed -e "s/iogo1repo\/projects\///" < svn_repo.small3.dumpfile > svn_repo.small4.dumpfile

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