Meanwhile, Harry has been coding as well. He heard somebody say that it’s best to compile with all the warnings turned on, so he decides to give it a try.
lottery harry$ gcc -std=c99 -Wall -Wextra -Werror lottery.c cc1: warnings being treated as errors lottery.c:5: warning: unused parameter 'white_balls' lottery.c:5: warning: unused parameter 'power_ball'
I say! The code has some warnings. The calculate_result()
function
isn’t using its parameters. Harry looks at the situation and realises the
problem immediately: That function should be checking its arguments for
validity! The power ball can be from 1 to 39 inclusive. The white balls can be
1 to 59 inclusive. So he implements the error checking.
int calculate_result(int white_balls[5], int power_ball) { for (int i=0; i<5; i++) { if ( (white_balls[i] < 1) || (white_balls[i] > 59) ) { return -1; } } if ( (power_ball < 1) || (power_ball > 39) ) { return -1; } return 0; }
Grand. Let’s see if it compiles.
lottery harry$ gcc -std=c99 -Wall -Wextra -Werror lottery.c lottery harry$ ls -l total 32 -rwxr-xr-x 1 harry staff 8904 May 17 08:17 a.out -rw-r--r-- 1 harry staff 843 May 17 08:16 lottery.c
Quite. But Harry decides to diff before committing, just for good measure.
lottery harry$ git diff diff --git a/lottery.c b/lottery.c index e59c732..6b1d76a 100644 --- a/lottery.c +++ b/lottery.c @@ -4,6 +4,25 @@ int calculate_result(int white_balls[5], int power_ball) { + for (int i=0; i<5; i++) + { + if ( + (white_balls[i] < 1) + || (white_balls[i] > 59) + ) + { + return -1; + } + } + + if ( + (power_ball < 1) + || (power_ball > 39) + ) + { + return -1; + } + return 0; }
Good show. Time to commit the change.
lottery harry$ git commit -a -m "fix some warnings" [master 7895c84] fix some warnings 1 files changed, 19 insertions(+), 0 deletions(-)
No problems there. This time he remembers that he needs to push his changes to the server.
But Sally has been working at the same time and she had her change ready to commit and push first.
lottery sally$ git commit -a -m "change order of the command line args to be \ more like what the user will expect" [master 37c09ff] change order of the command line args to be more like what the user ... 1 files changed, 3 insertions(+), 3 deletions(-) lottery sally$ git push Counting objects: 6, done. Compressing objects: 100% (4/4), done. Writing objects: 100% (6/6), 397 bytes, done. Total 6 (delta 1), reused 0 (delta 0) To http://server.futilisoft.com:8000/lottery bcb39be..7895c84 master -> master
So Harry tries to push his changes.
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.
What’s all this then? Git is not allowing Harry to push his change because Sally has already pushed something to the master branch.
Harry uses pull to bring in changes.
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 + 7895c84...37c09ff master -> origin/master (forced update) Auto-merging lottery.c Merge made by recursive. lottery.c | 6 +++--- 1 files changed, 3 insertions(+), 3 deletions(-)
I don’t like the way Harry did this. He used git pull, which did the merge and committed it without giving Harry a chance to review. Not cool. Harry should have used git pull --no-commit. |
Now the merge is done.
lottery harry$ git status -s ?? a.out
Everything seems to be proper good.
lottery harry$ git show -c 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 diff --combined lottery.c index 6b1d76a,adf47a7..22bf053 --- a/lottery.c +++ b/lottery.c @@@ -3,25 -3,6 +3,25 @@@ int calculate_result(int white_balls[5], int power_ball) { + for (int i=0; i<5; i++) + { + if ( + (white_balls[i] < 1) + || (white_balls[i] > 59) + ) + { + return -1; + } + } + + if ( + (power_ball < 1) + || (power_ball > 39) + ) + { + return -1; + } + return 0; } @@@ -29,16 -10,16 +29,16 @@@ int main(int argc, char** argv { if (argc != 7) { - fprintf(stderr, "Usage: %s power_ball (5 white balls)\n", argv[0]); + fprintf(stderr, "Usage: %s (5 white balls) power_ball\n", argv[0]); return -1; } - int power_ball = atoi(argv[1]); + int power_ball = atoi(argv[6]); int white_balls[5]; for (int i=0; i<5; i++) { - white_balls[i] = atoi(argv[2+i]); + white_balls[i] = atoi(argv[1+i]); } int result = calculate_result(white_balls, power_ball);
The merge commit is done. Apparently Git was able to merge Sally’s changes directly into Harry’s modified copy of the file without any conflicts. Smashing! Let’s see if it compiles.
lottery harry$ gcc -std=c99 -Wall -Wextra -Werror lottery.c lottery harry$ ls -l total 32 -rwxr-xr-x 1 harry staff 8904 May 17 08:34 a.out -rw-r--r-- 1 harry staff 843 May 17 08:28 lottery.c
Very well then. So Harry is ready to push the merge.
lottery harry$ git push Counting objects: 10, done. Compressing objects: 100% (4/4), done. Writing objects: 100% (6/6), 717 bytes, done. Total 6 (delta 2), reused 0 (delta 0) Unpacking objects: 100% (6/6), done. To http://server.futilisoft.com:8000/lottery 37c09ff..b19f36c master -> master