Saturday, October 26, 2013

How to create and use a local Subversion repository

From time to time it may be useful to create an ad hoc local Subversion repository. Let's say, for example, that I have some configuration files in a directory structure under /etc/foo. I'd like to make some changes to these, but I'm worried that I might break something. It would be nice to be able to do svn revert [filename], or even svn revert . -R.

Before I present one possible scenario for accomplishing this, here are two important Subversion resources:

Here's how I might create and use a local Subversion repository:
  1. Install Subversion
    # zypper install --no-recommends subversion
  2. Create a repository. In this case, we don't want the repository to be permanent -- it's just a one-time thing -- so we put it in /var/tmp
    # svnadmin create /var/tmp/foo
    # ls -l /var/tmp/foo   # take a look at what we just created
  3. Import /etc/foo into the repository.
    # cd /etc/foo
    # svn import . file:///var/tmp/foo -m "Initial import"
    Adding         bubba.html
    Adding         subdir
    Adding         subdir/bubba2
    Committed revision 1.
  4. At this point, I have the files from /etc/foo in the Subversion repository, /var/tmp/foo -- what's next? Good question. Since there is no working copy yet, I have to make one using svn co. But where? I have two options: either live dangerously by overwriting the existing /etc/foo with the checkout, or play it on the safe side by making a separate working copy somewhere else -- say, in /home/smithfarm/foo. Assuming the program using /etc/foo for its configuration can be told to instead take the configuration from /home/smithfarm/foo, the latter option would enable me to first test my changes in /home/smithfarm/foo and later, when I am satisfied with them, "put them into production" by moving them to /etc/foo. I'm usually a pretty cautious person, so I go with the latter option:
    # svn co file:///var/tmp/foo /home/smithfarm/foo
    A    /home/smithfarm/foo/bubba.html
    A    /home/smithfarm/foo/subdir
    A    /home/smithfarm/foo/subdir/bubba2
    Checked out revision 1.
  5. Now I make some changes to the working copy:
    # cd /home/smithfarm/foo
    # vim bubba.html
    # vim newfile.conf
    # svn add newfile.conf
    A         newfile.conf
    # svn status
    M       bubba.html
    A       newfile.conf
    # svn diff
    Index: bubba.html
    --- bubba.html  (revision 1)
    +++ bubba.html  (working copy)
    @@ -1,4 +1,4 @@
     This is a test.
    -This <% "nothing but a <friggin> test" | h %>.
    +This <% "nothing but a <smashin> test" | h %>.
    Index: newfile.conf
    --- newfile.conf        (revision 0)
    +++ newfile.conf        (working copy)
    @@ -0,0 +1 @@
  6. Now, before I go off committing the changes, I first test them (!)
    # test_the_changes

  7. If I'm not satisfied with my change to bubba.html and want to revert it, I do:
    # svn revert bubba.html
    # cat bubba.html

  8. And commit the changes:
    # svn co -m "First round of changes"
    Sending        bubba.html
    Adding         newfile.conf
    Transmitting file data ..
    Committed revision 2.

  9. Now I tell my program (foo) to take its configuration from /home/smithfarm/foo and test my changes. If I don't like the result, I can always put the configuration back to /etc/foo (which has not been touched). Or I can revert changes like this:
    # cd /home/smithfarm/foo
    # svn revert bubba.html

  10. If I already committed the changes, and I would like to revert to the previous commit just for bubba.html, I do:
    # svn merge -r COMMITTED:PREV bubba.html    # type it exactly like this

  11. If I'm not sure which commit I want to return to, I first look at the log:
    # svn log
    and then choose a revision number to return to. If the revision number to return to is 3, the command would look like this:
    # svn merge -r COMMITTED:3 bubba.html

  12. In any case, when I'm done I will surely want to delete both the working copy and the repository itself. This is a simple case of rm -rf (as always, use with care):
    # rm -rf /home/smithfarm/foo
    # rm -rf /var/tmp/foo

No comments:

Post a Comment