Monday, March 30, 2009

A piece of the Mercurial guide for students

If you are still interested on this Mercurial guide for students, we will be releasing it pretty soon (since our assignment is due this Friday).

One of the parts that I am writing is explaining many concepts by using simple examples. If you want to have a look at it you can do so by reading in the next section of this post.

Another part of what I will be writing is how to setup your own infrastructure since you can't always rely on school to set things up for you. In my case scenario, I chose our school's server called "matrix" in which we have ssh access and has python 2.5 installed.
  • ssh into the server
  • wget http://www.selenic.com/mercurial/release/mercurial-1.2.1.tar.gz
  • tar -xzvf mercurial-1.2.1.tar.gz
  • cd mercurial-1.2.1
  • python setup.py build
  • python setup.py install --prefix="${HOME}"
  • echo "export PYTHONPATH="/${HOME}/lib/python2.5/site-packages" > ~/.bashrc
Unfortunately, I have tried to repeat this installation on my matrix account and it is not happening. I will do a follow up blog post regarding this issue.





Create the repository
First thing you have to create is the repository which will hold the project:
  1. open the command prompt
  2. create a folder where you will have your project(s)
  3. inside the folder type this command
    hg init
You will now see a folder called ".hg" (the period in front of a folder indicates a hidden folder in Linux) this folder contains the metadata of your repository.

Create the first file
You have many ways to create files but if you are still in the command line do the following:
notepad readme.txt
Modify the file and save it. Back in the command line type the following:
hg status
The output of this will show you the following:
C:\Documents and Settings\student\hg_project>hg status
? readme.txt
The question mark indicates that there is a file that is not part of the VCS system.
If you try an "hg commit", you will get a message saying that nothing has changed.
If you try an "hg diff", you will see that there is no output

To add this file for the next commit do the following:
C:\Documents and Settings\student\hg_project>hg add readme.txt

C:\Documents and Settings\student\hg_project>hg status
A readme.txt

C:\Documents and Settings\student\hg_project>hg diff
diff -r 000000000000 readme.txt
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/readme.txt Wed Mar 25 19:02:48 2009 -0400
@@ -0,0 +1,1 @@
+hey there
\ No newline at end of file
If we now look at the status of the repository we can see that the file has been "added" indicated by the "A" in front of the file name. You can also see that "hg diff" shows the differences of your working copy.

NOTE: The file is not part of the repo yet. It is only part of your local working copy.

Let's now commit the file into the repository, to do so:
C:\Documents and Settings\student\hg_project>hg commit -u me@nowhere.com -m "adding readm
e.txt file"

C:\Documents and Settings\student\hg_project>hg log
changeset: 0:b85f2cde9d27
tag: tip
user: me@nowhere.com
date: Wed Mar 25 19:07:09 2009 -0400
summary: adding readme.txt file
We indicate who the user that is doing the commit is with the parameter -u (stands for user) and we add a message by using the parameter -m (stands for message) and indicate the message between double quotes.

With the command "hg log" we can see that the changeset is number 0 (let's ignore for now the number after the colon TODO), the user who commited the changeset (which we provided) , the date and the summary (which we provided as well).

Modifying multiple files but only one changeset
TODO

Back out a changeset
TODO

Clone a repository living in your local file system

If you want to have more than one working copy of a repository you do it like this (note the quotes):
hg clone "C:\Documents and Settings\student\hg_project"
This might be nonsense but it might help you to work on separate bugs.

NOTE: this second working copy takes less space than the first one. Mercurial did not really created copies of your first repository but created symbolic links to them. These files will only really be created if you make changes to them. TODO QUOTE

If we do changes in the readme.txt file from the second working copy and we do a commit:
C:\Documents and Settings\student\other_working_copy\hg_project>hg status
M readme.txt

C:\Documents and Settings\student\other_working_copy\hg_project>hg commit -u sec
ond@nowhere.com -m "This is a commit from the second working copy"

C:\Documents and Settings\student\other_working_copy\hg_project>hg log
changeset: 1:522aef5bea8a
tag: tip
user: second@nowhere.com
date: Wed Mar 25 19:25:07 2009 -0400
summary: This is a commit from the second working copy

changeset: 0:b85f2cde9d27
user: Me_me
date: Wed Mar 25 19:07:09 2009 -0400
summary: adding readme.txt file
You can note that the


Who did it and what changes for a certain changeset
If you want to know who did what in a certain changeset, you can do so by dumping the header and diffs for one or more changesets. In the following example we use the export command and indicate changesets 0 and 1.
C:\Documents and Settings\student\other_working_copy\hg_project>hg export 0 1
# HG changeset patch
# User Me_me
# Date 1238022429 14400
# Node ID b85f2cde9d2702abb2df3d6d44cfb212d00f3a48
# Parent 0000000000000000000000000000000000000000
adding readme.txt file

diff -r 000000000000 -r b85f2cde9d27 readme.txt
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/readme.txt Wed Mar 25 19:07:09 2009 -0400
@@ -0,0 +1,1 @@
+hey there
\ No newline at end of file
# HG changeset patch
# User second@nowhere.com
# Date 1238023507 14400
# Node ID 522aef5bea8adfb62ccaacfca1d35c4f2fe90044
# Parent b85f2cde9d2702abb2df3d6d44cfb212d00f3a48
This is a commit from the second working copy

diff -r b85f2cde9d27 -r 522aef5bea8a readme.txt
--- a/readme.txt Wed Mar 25 19:07:09 2009 -0400
+++ b/readme.txt Wed Mar 25 19:25:07 2009 -0400
@@ -1,1 +1,2 @@
-hey there
\ No newline at end of file
+hey there
+modification from second working copy
\ No newline at end of file





Creative Commons License
This work by Zambrano Gasparnian, Armen is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.