Version-controlled edit via Bash script (Debian)

As I am currently setting up a new server, I thought it would be a good idea to keep most of the configuration files (e.g. /etc) under version control. RCS comes in handy to achieve just that, but laziness requires some kind of script to make its use convenient 😉

So I sat down and wrote this little Bash script for my Debian system (Ubuntu and Knoppix should work as well then). Please note that you may have to change the shebang line, you also may have to adjust the command to call the file editor. I am using the nice “alternatives”-mechanism of Debian by calling editor instead of any specific editor. For example I’ve found myself disliking vi, while others swear an oath that vi is the best editor around. In any case, this problem should not exist if you have set your favorite editor.

If you have no clue what I am talking about, use:

man update-alternatives

Alright, now that you have set your favorite editor for sure, go and make sure to install the rcs and diff packages by issuing (as super-user):

apt-get update && apt-get install rcs diff

… either you’ll have it installed or you will install them with these commands. There shouldn’t be any unsatisfied dependencies on any “normal” system apart from those packages themselves (and even diff would be on most systems already).

Now all prerequisites should be installed on your system and you will be able to run the script. I have put it into /usr/sbin/vedit, because I only want it to be used by the super-user, but your mileage may vary, so feel free to put it anywhere you like.

The script is called vedit for “version-controlled edit” and does the following:

  • Checks the command line parameters
  • Creates a temporary folder to put the “work copy” into
  • Launches the editor you set with update-alternatives (default on Etch is nano)
  • After the editor quits it checks whether the file is already under version control. If so and if the “work copy” doesn’t differ from the original, the old version is kept and no further action takes place.
  • If the file is under version control and was changed, the file is being checked out and locked and the contents are being overwritten (cat with redirect) as to retain any permissions and ownership. The cleanup() routine of the script handles the checkin and unlocking.
  • If the file isn’t under version control, the RCS folder is created in the folder which contains the file that has been edited and the initial checkin takes place, followed by an immediate checkout (because the initial checkin removes the original file).

Note: there is a loophole. Because the locking of the file fetches the latest revision, any changes that have been made using a tool other than vedit may get lost. I may eventually fix this and put it up here as an update if I see fit.

Limitation: Currently only one file can be edited at a time. This may eventually be fixed as well.

// Oliver

Download vedit.tar.gz (MD5: 7e1417d673aee327ae0433f98f900ce2). The script is subject to the ZLIB license (i.e. no attribution required).

PS: Obviously it would also be possible to use an alias in your .bashrc to tie nano and editor to this script and check beforehand, whether the edited file is in /etc/ and then call either the ordinary editor or the version-controlled one.
Update #1: Fixed one glitch concerning full versus relative paths of edited files and fixed the loophole previously described.

This entry was posted in EN, Linux, Programming, Software. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *