Branch

Sally wants more privacy. She decides to create her own named branch.

lottery sally$ hg branch no_boys_allowed
marked working directory as branch no_boys_allowed

In its very early days, Mercurial was designed to support branching by having one repository instance per branch. This approach turned out to not be flexible enough, so the developers added named branches, a way of associating a name with a line of development. This approach allows multiple branches to exist within a single repository instance.

Now that Sally is working in her own branch, she feels much more productive. She adds support for the “favorite” option. When a user is playing her favorite numbers, her chances of winning should be doubled. In doing this, she had to rework the way command-line args are parsed. And she removes an atoi() call she missed last time. And she restructures all the error checking into one place.

So main() now looks like this:

int main(int argc, char** argv)
{
    int balls[6];
    int count_balls = 0;
    int favorite = 0;

    for (int i=1; i<argc; i++)
    {
        const char* arg = argv[i];

        if ('-' == arg[0])
        {
            if (0 == strcmp(arg, "-favorite"))
            {
                favorite = 1;
            }
            else
            {
                goto usage_error;
            }
        }
        else
        {
            char* endptr = NULL;
            long val = strtol(arg, &endptr, 10);
            if (*endptr)
            {
                goto usage_error;
            }
            balls[count_balls++] = (int) val;
        }
    }

    if (6 != count_balls)
    {
        goto usage_error;
    }

    int power_ball = balls[5];

    int result = calculate_result(balls, power_ball);

    if (result < 0)
    {
        goto usage_error;
    }

    if (LUCKY_NUMBER == power_ball)
    {
        result = result * 2;
    }

    if (favorite)
    {
        result = result * 2;
    }

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

    return 0;

usage_error:
    fprintf(stderr, "Usage: %s [-favorite] (5 white balls) power_ball\n", argv[0]);
    return -1;
}

She commits her changes, knowing that the commit will succeed because she is working in her private branch.

lottery sally$ hg commit -m "add -favorite and cleanup some other stuff"

lottery sally$ hg push
pushing to http://server.futilisoft.com:8000/
searching for changes
abort: push creates new remote branches: no_boys_allowed!
(use 'hg push --new-branch' to create new remote branches)

Hey! What’s the problem here? Ah, Mercurial just wants Sally to be more explicit about the fact that she’s creating a new branch.

lottery sally$ hg push --new-branch
pushing to http://server.futilisoft.com:8000/
searching for changes
remote: adding changesets
remote: adding manifests
remote: adding file changes
remote: added 1 changesets with 1 changes to 1 files