Script for adding all new files in a SVN repository

Published 26 July, 2007 in Linux , Programming - 0 Comments

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:

7 lines of Bash
  1. # Add all new files
  2. svnadd path/to/repository/
  3. # Add all new files except those matching the regexp
  4. svnadd -s “.*.txt|tmp/.*” path/to/repository/
  5. # Add all new files except those matching the regexp in
  6. # the file defined by the “-f” flag
  7. 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.

77 lines of Bash
  1. #!/bin/bash
  2. ##########################################################################
  3. #
  4. # Script for adding all new files in a directory structure to an SVN
  5. # repository.
  6. #
  7. # copyright © 2007 Pontus Östlund <spam@poppa.se>
  8. #
  9. # The svnadd script is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or (at your
  12. # option) any later version.
  13. #
  14. # The svnadd script is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  17. # Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program; if not, write to the Free Software Foundation,
  21. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. #
  23. ##########################################################################
  24. while getopts “s:f:h” opts
  25. do
  26. case $opts in
  27. s) skip=“$OPTARG”;;
  28. f) if [ -f “$OPTARG” ]; then
  29. skip=$(head -n 1 $OPTARG)
  30. else
  31. echo “$OPTARG doesn’t exist!”
  32. fi
  33. ;;
  34. h) help=1;;
  35. esac
  36. done
  37. shift $(($OPTIND1))
  38. if [ -z $1 ] || [ “$help” ]; then
  39. echo “About: svnadd enables you to add all new files in [dir] to a “
  40. “subversion repository”
  41. echo “Usage: svnadd [flags] [dir]”
  42. echo
  43. echo ” -s <pattern> Regexp pattern for files to skip”
  44. echo ” -f <path> Path to file containing regexp for files to skip”
  45. echo ” The regexp should be placed in line one!”
  46. echo ” [dir] The path to the repository”
  47. echo
  48. echo “Example: svnadd svnsrc/”
  49. echo “Example: svnadd -s ‘tmp/.*.xml’ svnsrc/”
  50. echo “Example: svnadd -f regexp.txt svnsrc/”
  51. echo
  52. exit 0
  53. fi
  54. dir=$1
  55. if [ ! -d “$dir” ]; then
  56. echo “$dir is not a directory”
  57. exit 1
  58. fi
  59. while read status file; do
  60. if [ “$status” = “?” ]; then
  61. res=$( echo “$file” | egrep -o “$skip” )
  62. if [ -z “$res” ]; then
  63. svn add “$file”
  64. fi
  65. fi
  66. done<<EOF
  67. $( svn st “$dir” )
  68. EOF
  69. exit 0

Download

svnadd 17:31, Sat 17 October 2009 :: 2.1 kB