Meanwhile, Sally is fixin’ to go ahead and add a feature that was requested by the sales team: If the user chooses the lucky number 7 as the red ball, the chances of winning are doubled. Since she is starting a new task, she decides to begin with pull and update to make sure she has the latest code.
lottery sally$ git pull remote: Counting objects: 10, done. remote: Compressing objects: 100% (4/4), done. remote: Total 6 (delta 2), reused 0 (delta 0) Unpacking objects: 100% (6/6), done. From http://server.futilisoft.com:8000/lottery 37c09ff..b19f36c master -> origin/master Updating 37c09ff..b19f36c Fast-forward lottery.c | 19 +++++++++++++++++++ 1 files changed, 19 insertions(+), 0 deletions(-) lottery sally$ git show commit b19f36cf4dddc2f70a597a0b558cf3be3de205b4 Merge: 7895c84 37c09ff Author: Harry <harry@futilisoft.com> Date: Sat Jun 11 14:02:28 2011 +0200 Merge branch 'master' of http://server.futilisoft.com:8000/lottery
Then she implements the lucky 7 feature in two shakes of a lamb’s tail by
adding just a few lines
of new code to main()
.
lottery sally$ git diff index 22bf053..8548299 100644 --- a/lottery.c +++ b/lottery.c @@ -44,6 +44,11 @@ int result = calculate_result(white_balls, power_ball); + if (7 == power_ball) + { + result = result * 2; + } + printf("%d percent chance of winning\n", result); return 0;
And commits her change. And pushes it too.
lottery sally$ git commit -a -m "lucky 7" [master b77378f] lucky 7 1 files changed, 5 insertions(+), 0 deletions(-) lottery sally$ git push Counting objects: 5, done. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 314 bytes, done. Total 3 (delta 1), reused 0 (delta 0) Unpacking objects: 100% (3/3), done. To http://server.futilisoft.com:8000/lottery b19f36c..b77378f master -> master
Meanwhile, Harry has realised his last change had a bug. He modified
calculate_result()
to return -1 for invalid arguments but he forgot to modify
the caller to handle the error. As a consequence, entering a ball number that is
out of range causes the program to behave improperly.
lottery harry$ ./a.out 61 2 3 4 5 42 -1 percent chance of winning
The percent chance of winning certainly can’t be a negative number, now can it? So Harry adds an extra check for this case.
lottery harry$ git diff diff --git a/lottery.c b/lottery.c index 22bf053..aad5995 100644 --- a/lottery.c +++ b/lottery.c @@ -44,6 +44,12 @@ int result = calculate_result(white_balls, power_ball); + if (result < 0) + { + fprintf(stderr, "Invalid arguments\n"); + return -1; + } + printf("%d percent chance of winning\n", result); return 0;
And proceeds to commit and push the fix.
lottery harry$ git commit -a -m "propagate error code" [master 2460684] propagate error code 1 files changed, 6 insertions(+), 0 deletions(-) lottery harry$ git push To http://server.futilisoft.com:8000/lottery ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to 'http://server.futilisoft.com:8000/lottery' To prevent you from losing history, non-fast-forward updates were rejected Merge the remote changes (e.g. 'git pull') before pushing again. See the 'Note about fast-forwards' section of 'git push --help' for details. .
Blimey! Sally must have pushed a new changeset already. Harry once again needs to pull and merge to combine Sally’s changes with his own.
lottery harry$ git pull remote: Counting objects: 5, done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 1), reused 0 (delta 0) Unpacking objects: 100% (3/3), done. From http://server.futilisoft.com:8000/lottery b19f36c..b77378f master -> origin/master Auto-merging lottery.c CONFLICT (content): Merge conflict in lottery.c Automatic merge failed; fix conflicts and then commit the result.
The merge didn’t go quite as smoothly this time.
Harry wonders if anyone would notice if he were to sneak off to the pub. Apparently there
was a conflict.
Harry decides to
open up lottery.c
in his editor to examine the situation.
... int result = calculate_result(white_balls, power_ball); <<<<<<< HEAD if (result < 0) { fprintf(stderr, "Invalid arguments\n"); return -1; ======= if (7 == power_ball) { result = result * 2; >>>>>>> b77378f6eb0af44468be36a085c3fe06a80e0322 } printf("%d percent chance of winning\n", result); return 0; ...
Git has included both Harry’s code and Sally’s code with conflict markers to delimit things. What we want is to include both blocks of code. Sally’s new code can simply be included right after Harry’s error checking.
... int result = calculate_result(white_balls, power_ball); if (result < 0) { fprintf(stderr, "Invalid arguments\n"); return -1; } if (7 == power_ball) { result = result * 2; } printf("%d percent chance of winning\n", result); return 0; ...
That should take care of the problem. Harry compiles the code to make sure and then commits the merge.
lottery harry$ git status -s UU lottery.c ?? a.out lottery harry$ git status # On branch master # Your branch and 'origin/master' have diverged, # and have 1 and 1 different commit(s) each, respectively. # # Unmerged paths: # (use "git add/rm <file>..." as appropriate to mark resolution) # # both modified: lottery.c # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # a.out no changes added to commit (use "git add" and/or "git commit -a")
lottery harry$ git commit -a -m "merge" [master 05f316d] merge
And then to retry the push.
lottery harry$ git push Counting objects: 10, done. Compressing objects: 100% (4/4), done. Writing objects: 100% (6/6), 573 bytes, done. Total 6 (delta 2), reused 0 (delta 0) Unpacking objects: 100% (6/6), done. To http://server.futilisoft.com:8000/lottery b77378f..05f316d master -> master
And… that’s the last wicket.