Script for adding all new files in a SVN repository
When working with revision controlled stuff you sometimes add loads of new files to an already existing repository. Then when you are about to check in the next revision you have forgotten where all the new files are and you probably don’t feel like writing svn add thefile too many times. This is of course no problem if you’r using a decent SVN client – like Tortoise SVN on Windows – but I prefer the command line so the solution was writing a script that adds all new files in a directory structure to the SVN repository.
There are “one liners” for this but I wanted a bit more flexibility and ease of use so I wrote the a script that also lets you define regexp patterns for files you don’t want to add even if they exist in the directory structure.
This is how to use it:
- # Add all new files
- svnadd path/to/repository/
- # Add all new files except those matching the regexp
- svnadd -s ".*.txt|tmp/.*" path/to/repository/
- # Add all new files except those matching the regexp in
- # the file defined by the "-f" flag
- svnadd -f regexp.txt path/to/repository/
Quite easy! If you don’t feel like remembering the pattern for files to skip, put the regexp in a file and use the svn add thefile flag.
- #!/bin/bash
- ##########################################################################
- #
- # Script for adding all new files in a directory structure to an SVN
- # repository.
- #
- # copyright © 2007 Pontus Östlund <spam@poppa.se>
- #
- # The svnadd script is free software; you can redistribute it and/or
- # modify it under the terms of the GNU General Public License as published by
- # the Free Software Foundation; either version 2 of the License, or (at your
- # option) any later version.
- #
- # The svnadd script is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
- # Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program; if not, write to the Free Software Foundation,
- # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- #
- ##########################################################################
- while getopts "s:f:h" opts
- do
- case $opts in
- s) skip="$OPTARG";;
- f) if [ -f "$OPTARG" ]; then
- skip=$(head -n 1 $OPTARG)
- else
- echo "$OPTARG doesn't exist!"
- fi
- ;;
- h) help=1;;
- esac
- done
- shift $(($OPTIND - 1))
- if [ -z $1 ] || [ "$help" ]; then
- echo "About: svnadd enables you to add all new files in [dir] to a "
- "subversion repository"
- echo "Usage: svnadd [flags] [dir]"
- echo
- echo " -s <pattern> Regexp pattern for files to skip"
- echo " -f <path> Path to file containing regexp for files to skip"
- echo " The regexp should be placed in line one!"
- echo " [dir] The path to the repository"
- echo
- echo "Example: svnadd svnsrc/"
- echo "Example: svnadd -s 'tmp/.*.xml' svnsrc/"
- echo "Example: svnadd -f regexp.txt svnsrc/"
- echo
- exit 0
- fi
- dir=$1
- if [ ! -d "$dir" ]; then
- echo "$dir is not a directory"
- exit 1
- fi
- while read status file; do
- if [ "$status" = "?" ]; then
- res=$( echo "$file" | egrep -o "$skip" )
- if [ -z "$res" ]; then
- svn add "$file"
- fi
- fi
- done<<EOF
- $( svn st "$dir" )
- EOF
- exit 0


