Branch

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

lottery sally$ git checkout -b no_boys_allowed
Switched to a new branch 'no_boys_allowed'

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$ git commit -a -m "add -favorite and cleanup some other stuff"
[no_boys_allowed 02f9797] add -favorite and cleanup some other stuff
 1 files changed, 43 insertions(+), 18 deletions(-)

lottery sally$ git push
Everything up-to-date

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

lottery sally$ git push origin no_boys_allowed
Counting objects: 7, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 705 bytes, done.
Total 4 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (4/4), done.
To http://server.futilisoft.com:8000/lottery
 * [new branch]      no_boys_allowed -> no_boys_allowed