CHAPTER 34
Automatic RCS

The RCS (Revision Control System) is a set of programs used for source-code version control. It is used heavily by programmers to maintain source files and track source-code changes. The script in this chapter1 was created in an effort to control configuration files on a system without having to perform the manual check-in and check-out process used with the RCS. This is done by replacing your system editor with this script, which in turn calls your editor with the appropriate RCS commands.

The script demonstrates a few useful techniques: it grabs command-line parameters, it calls the script through a link, and finally it demonstrates the use of the check-in and check-out commands in the RCS. Many more features and options are available in the RCS, but they are mostly outside the scope of this discussion.

This script isn't intended to be called specifically as an add-on utility; it is intended as a replacement for the original editor for your environment. I wrote it for use with vi, although other editors could be replaced in the same manner. You would first rename the original vi or vim binary in /usr/bin to something like /usr/bin/vim-rcs. When you move this script to its new location, you would replace the original /usr/bin/vi (or vim) with your script so that when you call vi/vim, you will be running this script instead. The $VI variable refers to the new location of the original vi/vim binary so the script knows where you moved it. This works even when calling vi (now the script) with a wild card such as *, because the shell will expand the wild card before executing the call.

We start out with some configurable system variables. The file locations defined with these variables may vary from installation to installation, so you'll need to make the appropriate changes for your site.


Note  You must have the RCS installed on your system; it may not be installed by default.


__________

1. The script is based on one given to me by my colleague Brian Grell.

#!/bin/sh
CI=/usr/bin/ci
CO=/usr/bin/co
TEST=/usr/bin/test
BASENAME=/bin/basename
ME=`$BASENAME $0`
DIRNAME=/usr/bin/dirname

The ME variable is assigned the name with which the script was invoked. If you named this script vi so that it would replace the vi binary, ME would be set to vi. We assign the ME variable its value by using basename to remove any leading path used to call the script. Later in this chapter we will come back to this variable and the way it is used. For now, note that when installing the script on your system, you will also need to create a soft link in a directory in your PATH variable; that soft link should be called vir and point to the script.

When a script is run, environment variables are set that don't directly relate to the script itself since they are part of the shell executing the script and its environment. One example is the variable $@, which is set to the values of all the positional parameters (starting from 1) that were passed to the script. (As you have seen, positional parameter 0 is assigned the name of the script.)

ALLTHEFILES=$@

To get a little more specific, consider the following:

runme a b c d

In this example, the value of $0 would be runme, $1 would be a, $2 would be b, and so on. $@ would be set to the combination of all of the positional parameters: a b c d.

This is necessary in our example script because you might want to call your editor with a wild card, such as filename*, or you might want to call the editor to edit a list of files. The variable ALLTHEFILES is set for this purpose.

In the following code we start the main loop that iterates through the list of files passed to the script. First we determine the path and filename of the current file.

VI=/usr/bin/vim-rcs
for file in $ALLTHEFILES
do
  # Get some basic info about the file
  PATH=`$DIRNAME $file`
  FILENAME=`$BASENAME $file`

Now the script should determine whether the file has already been checked in under the RCS. If it has, we check it out. Then we edit the file as usual.

  $TEST -f "$PATH/RCS/$FILENAME,v" -o -f "$PATH/$FILENAME,v" && $CO -l $file
  $VI $file

After each file has been edited, we want to see how the script was called to determine whether it needs to be checked back in. Recall that a soft link to the script was created when the script was installed. If the script was called with this vir soft link instead of with a vi/vim command, which we can find out by examining $ME, then we can assume that even if the file was not under RCS control before, it needs to be checked in so it will be available in the future. The script checks to see if the RCS-controlled version of the file exists. If the controlled version does not exist, the script checks the file into the RCS with the initial check-in (-i) switch; otherwise it checks it in as normal.

  if [ "$ME" = "vir" ]
  then
    $TEST ! -f "$PATH/RCS/$FILENAME,v" -o ! -f "$PATH/$FILENAME,v" && $CI -i $file
  else
    $TEST -f "$PATH/RCS/$FILENAME,v" -o -f "$PATH/$FILENAME,v" && $CI -u $file
  fi
done

You can see that by determining how the script was invoked (using the soft link or using the name of the file), you avoid having to force all of the files you edit into the RCS. Chapter 13 contains further discussion of the technique of using the syntax of the invocation to select a script's behavior.

You could upgrade this script by defining directories for which it is assumed that all files inside those directories should be checked into the RCS even if they weren't originally. (The /etc directory would be a good candidate for this treatment.) Files residing in other directories would be edited as usual, without any version control.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.17.28