Clone, Add, Status, Commit

By this time Harry is done dossing about and is ready to start coding.

Since this is Harry’s first time using Mercurial, he first sets up his .hgrc file with a user string that will be used to identify his commits in the log.

[ui]
username = Harry <harry@futilisoft.com>

Now he needs to get his own repository instance.

~ harry$ hg clone http://server.futilisoft.com:8000/ ./lottery
no changes found
updating to branch default
0 files updated, 0 files merged, 0 files removed, 0 files unresolved

Note that Mercurial doesn’t have a Checkout command. It keeps the repository instance within the administrative area of the working copy. This means you can only have one working copy for each repository instance.

Harry wonders if Sally has already done anything in the new repository.

~ harry$ cd lottery

lottery harry$ ls -al
total 0
drwxr-xr-x   3 harry  staff  102 May 17 07:55 .
drwxr-xr-x  21 harry  staff  714 May 17 07:55 ..
drwxr-xr-x   8 harry  staff  272 May 17 07:55 .hg

Apparently not. Nothing here but the .hg administrative area. Jolly good then. It’s time to start coding. He opens his text editor and creates the starting point for their product.

#include <stdio.h>
#include <stdlib.h>

int calculate_result(int white_balls[5], int power_ball)
{
    return 0;
}

int main(int argc, char** argv)
{
    if (argc != 7)
    {
        fprintf(stderr, "Usage: %s power_ball (5 white balls)\n", argv[0]);
        return -1;
    }

    int power_ball = atoi(argv[1]);

    int white_balls[5];
    for (int i=0; i<5; i++)
    {
        white_balls[i] = atoi(argv[2+i]);
    }

    int result = calculate_result(white_balls, power_ball);

    printf("%d percent chance of winning\n", result);

    return 0;
}

Typical of most initial implementations, this is missing a lot of features. But it’s a good place to begin. Before committing his code, he wants to make sure it compiles and runs.

lottery harry$ gcc -std=c99 lottery.c 

lottery harry$ ls -l
total 32
-rwxr-xr-x  1 harry  staff  8904 May 17 07:56 a.out
-rw-r--r--  1 harry  staff   555 May 17 07:56 lottery.c

lottery harry$ ./a.out
Usage: ./a.out power_ball (5 white balls)

lottery harry$ ./a.out 42 1 2 3 4 5
0 percent chance of winning

Righto. Time to store this file in the repository. First Harry needs to add the file to the pending changeset.

lottery harry$ hg add lottery.c

Harry uses the status operation to make sure the pending changeset looks proper.

lottery harry$ hg status
A lottery.c
? a.out

Mercurial is complaining because it doesn’t know what to do about that a.out file. Stiff upper lip and all that. That’s a compiled executable, which should not be stored in a version control repository. He can just ignore that.[23] Now it’s time to commit the file.

lottery harry$ hg commit -m "initial implementation"


[23] Or he could add a.out to his .hgignore file.