A Shell Script for renaming an hg project and files

This semester I’ve been taking a “Web Databases” class, for a number of the assignments we’ve been progressively building up a simple digital library site. Each project we needed to change the name of the files from something like “blandau-p5-home.html” to “blandau-p6-home.html”.
I was using Mercurial for managing my version control, and needed a way to quickly manage moving over the files from one name to another. In comes the shell script! I needed to clone the Mercurial repository over, then rename all the files to the new name. Here’s the code:

v=''
# process the option flags
while getopts ":vh" opt; do
   case $opt in
      v  ) v='-v ' ;;
      h  ) echo "usage: renameproj.sh  [-v] [-h] old new" ;;
      \? ) echo "usage: renameproj.sh  [-v] [-h] old new" ;;
   esac
done
shift $(($OPTIND - 1))

# Get the old and new project names
old=$1
new=$2

# Function to rename files
renamepfiles(){
v=''
# process the option flags
while getopts ":vo:n:" opt; do
   case $opt in
      v  ) v='-v ' ;;
      o  ) old=$OPTARG ;;
      n  ) new=$OPTARG ;;
      \? ) echo "usage: renamepfiles  [-v] [-o oldproject] [-n newproject] args..." ;;
   esac
done
shift $(($OPTIND - 1))

# Make the move
for filename do
   oldfile=${filename}
   # the next line transforms the old filename into the new one by replacing the
   # old porject name with the new one.
   newfile=${filename/$old/$new}
   # first rename it with the move command
   mv -i ${v}$oldfile $newfile
   # then rename it with the hg command
   hg ${v}rename -A $oldfile $newfile
done
}

# perform an mercurial clone of the project
hg ${v}clone $old $new
# change the working directory to our new project
cd ${new}

# call the function over all the files with the "old" project name in their name
renamepfiles ${v}-n $new -o $old $(find . -name "*$old*" -maxdepth 2 -print | xargs echo -n)
echo "- Project Moved"
Posted
Views