Manage revision of small scripts

We all did small scripts, sometimes useful, sometimes absolutely not. But anyway, those are kind of work that we never think important enough to be put on any revision control system. That's true, spend minutes, even hour to create a repository for it if you're not used to, never been a good idea. But with Mercurial, you maybe have a very good solution. And it even works for a big project, trust me !

Background

Mercurial has been created in 2005, mostly in python and is a distributed revision control tool. Let say that this tool doesn't have a central repository but as many as you want. What's the point ? For me, the point was that it allows you to quickly transform one of you working folder in a repository. I also use it to create one repository per feature and keep committing changes until merging a functionality, one after another, with the deployment repository.

How to ?

First, you will have to install it of course. If you have ubuntu, lucky you :

sudo apt-get install mercurial
otherwise... lucky you, it's in python. If you have recent version of python, you just need to:
easy_install -U mercurial

and you're all set. Ok you have all those great scripts that you want to protect with mercurial. The command will be hg... why hg ? because it's the chemical symbol of mercury! First, I will put my name in my $HOME/.hgrc so it will be used to indicate the author on each revision:


[ui]
username= Brice LEROY <bbrriiccee@example.com>
[extensions]
hgext.fetch=
hgext.record=
just to make you familiar with mercurial, those are the usual command:
  • hg init: initialize the current folder.
  • hg add filename: add a file.
  • hg remove filename: remove a file.
  • hg commit [filename]: commit the changes on the repository or only on the file(s) specified.
  • hg fetch repository: pull the changes from another repository and try to commit them.
  • hg status [file]: show the status of the current repository or file.
  • hg clone repository destination: clone the repository to the destination.
  • hg log [-l x]: show history, up to x record if specified.
  • hg update r x: jump to the revision number x.
  • hg serve [-p port]: start a web server to browse the repository on port 8000 by default.

I will give you a quick look on how to transform a simple folder in a revision controlled one:


brice@django:~$ cd script/
brice@django:~/script$ ls
prog1.py  prog2.py
brice@django:~/script$ hg init
brice@django:~/script$ hg status
? prog1.py
? prog2.py
brice@django:~/script$ hg add prog1.py
brice@django:~/script$ hg add prog2.py
brice@django:~/script$ hg commit -m"Prog1 and Prog2 to demonstrate hg"
brice@django:~/script$ hg status
brice@django:~/script$ hg log
changeset:   0:ef9e6782682b
tag:         tip
user:        Brice LEROY <bbrriiccee@example.com>
date:        Mon Jan 19 20:11:32 2009 -0800
summary:     Prog1 and Prog2 to demonstrate hg

brice@django:~/script$ cat prog1.py
#!/usr/bin/python

print "hello world"


brice@django:~/script$ echo "print 'Hello again'" >> prog1.py 
brice@django:~/script$ hg status
M prog1.py
brice@django:~/script$ hg diff
diff -r ef9e6782682b prog1.py
--- a/prog1.py Mon Jan 19 20:11:32 2009 -0800
+++ b/prog1.py Mon Jan 19 20:13:44 2009 -0800
@@ -3,3 +3,4 @@ print "hello world"
 print "hello world"
 
 
+print 'Hello again'
brice@django:~/script$ hg commit -m"Adding a second hello to prog1"
brice@django:~/script$ hg status
brice@django:~/script-copy$ cd ..
brice@django:~$ hg clone script script-copy
2 files updated, 0 files merged, 0 files removed, 0 files unresolved
brice@django:~$ cd script-copy/
brice@django:~/script-copy$ ls
prog1.py  prog2.py
brice@django:~/script-copy$ hg log
changeset:   1:2cbe7040af32
tag:         tip
user:        Brice LEROY <bbrriiccee@example.com>
date:        Mon Jan 19 20:14:42 2009 -0800
summary:     Adding a second hello to prog1

changeset:   0:ef9e6782682b
user:        Brice LEROY <bbrriiccee@example.com>
date:        Mon Jan 19 20:11:32 2009 -0800
summary:     Prog1 and Prog2 to demonstrate hg
brice@django:~/script-copy$ echo "print 'Hello again again'" >> prog1.py
brice@django:~/script-copy$ hg status
M prog1.py
brice@django:~/script-copy$ hg commit -m"Adding another hello to prog1"
brice@django:~/script-copy$ cd ../script
brice@django:~/script$ hg log
changeset:   1:2cbe7040af32
tag:         tip
user:        Brice LEROY <bbrriiccee@example.com>
date:        Mon Jan 19 20:14:42 2009 -0800
summary:     Adding a second hello to prog1

changeset:   0:ef9e6782682b
user:        Brice LEROY <bbrriiccee@example.com>
date:        Mon Jan 19 20:11:32 2009 -0800
summary:     Prog1 and Prog2 to demonstrate hg

brice@django:~/script$ hg fetch ../script-copy/
pulling from ../script-copy/
searching for changes
adding changesets
adding manifests
adding file changes
added 1 changesets with 1 changes to 1 files
1 files updated, 0 files merged, 0 files removed, 0 files unresolved
brice@django:~/script$ hg log
changeset:   2:2c40f514d0b4
tag:         tip
user:        Brice LEROY <bbrriiccee@example.com>
date:        Mon Jan 19 20:17:58 2009 -0800
summary:     Adding another hello to prog1

changeset:   1:2cbe7040af32
user:        Brice LEROY <bbrriiccee@example.com>
date:        Mon Jan 19 20:14:42 2009 -0800
summary:     Adding a second hello to prog1

changeset:   0:ef9e6782682b
user:        Brice LEROY <bbrriiccee@example.com>
date:        Mon Jan 19 20:11:32 2009 -0800
summary:     Prog1 and Prog2 to demonstrate hg
brice@django:~/script$ hg update -r 1
1 files updated, 0 files merged, 0 files removed, 0 files unresolved
brice@django:~/script$ cat prog1.py 
#!/usr/bin/python

print "hello world"


print 'Hello again'
brice@django:~/script$ hg update -r 2
1 files updated, 0 files merged, 0 files removed, 0 files unresolved
brice@django:~/script$ cat prog1.py 
#!/usr/bin/python

print "hello world"


print 'Hello again'
print 'Hello again again'

Tagged : hg , mercurial , source , versioning

1 Response

  1. Cool! That's a clveer way of looking at it!

Leave a Reply

captcha