-
Notifications
You must be signed in to change notification settings - Fork 0
/
git_course.sh
1478 lines (1193 loc) · 33.2 KB
/
git_course.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#/bin/bash
LINE_WIDTH=120
TEXT_MARGINS=5
curr_dir=$( pwd )
repo_root="$curr_dir/git_pc"
TITLE_NUMBER=( 0 0 0 )
print_header()
{
clear
welcome_msg="GIT Course"
l_spaces=$(( (${LINE_WIDTH} - 6 - ${#welcome_msg})/2 ))
r_spaces=$(( ${LINE_WIDTH} - 6 - ${#welcome_msg} - ${l_spaces} ))
printf "%0.s=" $(seq 1 ${LINE_WIDTH}); echo
printf "==="; printf "%0.s " $(seq 1 ${l_spaces}); printf "${welcome_msg}"; printf "%0.s " $(seq 1 ${r_spaces}); printf "==="; echo
printf "%0.s=" $(seq 1 ${LINE_WIDTH}); echo; echo
}
hr()
{
echo; printf "%0.s-" $(seq 1 ${LINE_WIDTH}); echo; echo
}
print_section_title()
{
title="$1"
TITLE_NUMBER[0]=$(( ${TITLE_NUMBER[0]} + 1 ))
TITLE_NUMBER[1]=0
TITLE_NUMBER[2]=0
printf "%0.s " $(seq 1 ${TEXT_MARGINS}); printf " %d. %s\n\n" "${TITLE_NUMBER[0]}" "${title}"
}
print_lesson_title()
{
title="$1"
TITLE_NUMBER[1]=$(( ${TITLE_NUMBER[1]} + 1 ))
TITLE_NUMBER[2]=0
printf "%0.s " $(seq 1 ${TEXT_MARGINS}); printf " %d.%d. %s\n\n" "${TITLE_NUMBER[0]}" "${TITLE_NUMBER[1]}" "${title}"
}
print_task_title()
{
title="$1"
TITLE_NUMBER[2]=$(( ${TITLE_NUMBER[2]} + 1 ))
printf "%0.s " $(seq 1 ${TEXT_MARGINS}); printf " %d.%d.%d. %s\n\n" "${TITLE_NUMBER[0]}" "${TITLE_NUMBER[1]}" "${TITLE_NUMBER[2]}" "${title}"
}
print_text_block()
{
text=( $@ )
counter=${LINE_WIDTH}
chars_per_line=$(( $LINE_WIDTH - $TEXT_MARGINS*2 ))
first="1"
for word in ${text[@]}
do
if [[ $(( ${counter} + ${#word} )) -gt ${chars_per_line} ]]
then
if [ -z "$first" ]
then
echo
fi
printf "%0.s " $(seq 1 ${TEXT_MARGINS})
counter=0
first=""
fi
printf "%s " "${word}"
counter=$(( ${counter} + ${#word} + 1 ))
done
echo
}
print_line()
{
printf "%0.s " $(seq 1 ${TEXT_MARGINS}); echo "$1"
}
print_help_text()
{
echo
print_text_block "Actions:"
print_line " h - get hints for current task"
print_line " r - reset task"
print_line " s - skip current task"
print_line " q - quit GIT Practice Course"
echo
print_line " ? - show this help text"
echo
}
hint()
{
text="$@"
print_text_block "- hint: ${text}"
}
task_pass()
{
msg="$1"
printf "%0.s " $(seq 1 ${TEXT_MARGINS})
echo "+ ${msg}"
}
task_fail()
{
msg="$1"
printf "%0.s " $(seq 1 ${TEXT_MARGINS})
echo "- ${msg}"
}
press_any_key()
{
call="$1"
msg="-- Press any key --"
l_spaces=$(( ${LINE_WIDTH} - ${#msg} ))
echo
printf "%0.s " $(seq 1 ${l_spaces}); printf "%s" "${msg}"; echo
read -n 1 -s -r inp
if [[ "$inp" == "h" ]] && [[ -n "$call" ]]
then
${call}_hint
press_any_key $call
elif [[ "$inp" == "s" ]] && [[ -n "$call" ]]
then
${call}_solution
elif [[ "$inp" == "r" ]] && [[ -n "$call" ]]
then
${call}_clean
press_any_key $call
elif [[ "$inp" == "?" ]]
then
print_help_text
press_any_key $call
elif [[ "$inp" == "q" ]]
then
exit
fi
}
get_commit_message()
{
pushd ${repo_root} > /dev/null
git log --pretty=%B -n 1 | xargs
popd > /dev/null
}
get_commited_files()
{
pushd ${repo_root} > /dev/null
git show | grep "+++ " | grep -oP "[a-zA-Z0-9]+\.[a-zA-Z0-9]+"
popd > /dev/null
}
get_current_branch()
{
pushd ${repo_root} > /dev/null
git rev-parse --abbrev-ref HEAD
popd > /dev/null
}
about_git()
{
print_header
print_text_block "Git is a free and open source distributed version control" \
"system designed to handle everything from small to very large projects" \
"with speed and efficiency."
hr
}
before_course()
{
print_text_block "Before we start with GIT Course, open an" \
"additional terminal in which you will practice git commands."; echo
print_text_block "When you are done with each task, just press any key" \
"to check the result of your task."
print_text_block "In case that something is not correct, you will get a" \
"chance to repeat the task."; echo
print_text_block "You can also use special actions at any moment."; echo
print_help_text
press_any_key
}
congratulations()
{
print_header
print_text_block "Congratulations !!!"; echo
print_text_block "You've successfully completed GIT Basics."
print_text_block "You are now ready to use git on your own."
hr
}
#################
# 1. GIT Basics #
#################
GIT_BASICS=(
git_init
git_commit
git_branch
git_rebase
git_cherry_pick
)
#################
# 1.1 git init #
#################
git_init_clean()
{
rm -rf ${repo_root} > /dev/null
}
git_init_hint()
{
hint "mkdir ${repo_root}"
hint "cd ${repo_root}"
hint "git init"
}
git_init_solution()
{
mkdir -p "${repo_root}"
pushd "${repo_root}" > /dev/null
git init -q > /dev/null
popd > /dev/null
}
git_init()
{
print_header
print_section_title "GIT Basics"
print_lesson_title "Init"
while [ 1 ]
do
print_text_block "To work with git, we first need a git repository. Your" \
"first task is to make a new directory ${repo_root}"; echo
print_text_block "When you have a new directory created, initialize it as a" \
"git repository with 'git init'."
press_any_key git_init
if [ ! -d "${repo_root}" ]
then
task_fail "New directory created"
continue
fi
task_pass "New directory created"
if [ ! -d "${repo_root}/.git" ]
then
task_fail "GIT repository initialize"
continue
fi
task_pass "GIT repository initialized"
break
done
}
##################
# 1.2 git commit #
##################
git_commit_create_files()
{
pushd ${repo_root} > /dev/null
echo "# GIT Practice Course" > README.md
echo "This is a test file." >> README.md
cat > hello.c << EOF
#include <stdlib.h>
#include <stdio.h>
int main()
{
printf("Hello World!");
return 0;
}
EOF
cat > hello2.c << EOF
#include <stdlib.h>
#include <stdio.h>
int main()
{
printf("Hello again...");
return 0;
}
EOF
popd > /dev/null
}
git_commit_clean()
{
pushd ${repo_root} > /dev/null
rm -rf ./* > /dev/null
rm -rf ./.git > /dev/null
git_init_solution
git_commit_create_files
}
git_commit_hint()
{
right_comm_msg="initial commit"
add_files="README.md"
hint "git add ${add_files}"
hint "git commit -m '${right_comm_msg}'"
}
git_commit_solution()
{
git_commit_clean
right_comm_msg="initial commit"
add_files="README.md"
pushd ${repo_root} > /dev/null
git add ${add_files}
git commit -m "${right_comm_msg}" > /dev/null
popd > /dev/null
}
git_commit_2_prepare()
{
wrong_comm_msg="hello world"
add_files=( "hello.c" )
pushd ${repo_root} > /dev/null
git add ${add_files[@]}
git commit --author="John Doe <>" -m "${wrong_comm_msg}" > /dev/null
popd > /dev/null
}
git_commit_2_clean()
{
git_commit_solution
git_commit_2_prepare
}
git_commit_2_hint()
{
hint "Add hello2.c to stage, amend commit and change commit message"
}
git_commit_2_solution()
{
git_commit_2_clean
right_comm_msg="Adding hello.c and hello2.c files"
add_files=( "hello2.c" )
pushd ${repo_root} > /dev/null
git add ${add_files[@]} > /dev/null
git commit --amend -m "${right_comm_msg}" > /dev/null
popd > /dev/null
}
git_commit()
{
print_lesson_title "Commits"
print_text_block "Now when the new repository is ready, I'll prepare a few" \
"new files. Before you start, type 'git status' to see what is new in " \
"the repository."; echo
print_text_block "There you should see README.md, hello.c and hello2.c files."; echo
git_commit_create_files
press_any_key
# Task 1
print_task_title "First commit"
comm_msg="initial commit"
files=( "README.md" )
while [ 1 ]
do
print_text_block "Your first task here is to add ${files} to the stage and" \
"then commit it with the message '${comm_msg}'."; echo
print_text_block "NOTE: Before you commit a new file, check repository" \
"status (git status) to be sure that you are committing the right changes."
press_any_key git_commit
u_comm_msg=$( get_commit_message )
u_files=($( get_commited_files ))
if [[ "${comm_msg}" != "${u_comm_msg}" ]]
then
task_fail "Wrong commit message"
continue
elif [[ "${files[@]}" != "${u_files[@]}" ]]
then
task_fail "Wrong files commited"
continue
fi
task_pass "First commit created"
break
done
echo
print_text_block "After committing your changes, you can check repository " \
"history with 'git log'."
print_text_block "There you'll find a few important information"; echo
print_line "commit 7e63...3456 GIT SHA is the unique ID of every commit"
print_line "Author: $USER Commit author"
print_line "Date: Thu Aug 25 ... Date and time when the commit is created"
print_line " initial commit Change description"
echo
press_any_key
# Task 2
print_task_title "Second commit"
comm_msg="Adding hello.c and hello2.c files"
files=( "hello.c" "hello2.c" )
git_commit_2_prepare
while [ 1 ]
do
print_text_block "Your second task will be to fix my mistake."
print_text_block "If you want to add more changes, change commit message or" \
"both, you can do that with 'git commit --amend'."; echo
print_text_block "I've committed only one of two hello.c files with a " \
"commit message that is not very descriptive."
print_text_block "Add hello2.c file and change my commit message to '${comm_msg}'."
press_any_key git_commit_2
u_comm_msg=$( get_commit_message )
u_files=($( get_commited_files ))
if [[ "${comm_msg}" != "${u_comm_msg}" ]]
then
task_fail "Wrong commit message"
continue
elif [[ "${files[@]}" != "${u_files[@]}" ]]
then
task_fail "Wrong files committed"
continue
fi
task_pass "Second commit created"
break
done
}
##################
# 1.3 git branch #
##################
git_branch_clean()
{
git_commit_2_solution
}
git_branch_hint()
{
right_new_branch="implement_sort"
hint "git checkout -b ${right_new_branch}"
}
git_branch_solution()
{
git_branch_clean
right_new_branch="implement_sort"
pushd ${repo_root} > /dev/null
git checkout -q -b "${right_new_branch}"
popd > /dev/null
}
git_branch_2_prepare()
{
pushd ${repo_root} > /dev/null
cat > sort.c << EOF
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
char *list[argc - 1];
char *tmp = NULL;
int i, j;
int len = argc - 1;
for (i = 1; i < argc; ++i)
list[i - 1] = argv[i];
for (i = 0; i < len - 1; ++i) {
for (j = i + 1; j < len; ++j) {
if (strcmp(list[i], list[j]) < 0) {
tmp = list[i];
list[i] = list[j];
list[j] = tmp;
}
}
}
for (i = 0; i < len; ++i)
printf("%s ", list[i]);
printf("\n");
return 0;
}
EOF
popd > /dev/null
}
git_branch_2_clean()
{
git_branch_solution
git_branch_2_prepare
}
git_branch_2_hint()
{
hint "Add sort.c to stash and commit changes"
}
git_branch_2_solution()
{
git_branch_2_clean
right_comm_msg="Sorting application added"
add_files=( "sort.c" )
pushd ${repo_root} > /dev/null
git add ${add_files[@]}
git commit -m "${right_comm_msg}" > /dev/null
popd > /dev/null
}
git_branch_3_clean()
{
git_branch_2_solution
}
git_branch_3_hint()
{
hint "Checkout master branch and use 'git merge ${right_new_branch}' to merge changes"
}
git_branch_3_solution()
{
git_branch_3_clean
right_new_branch="implement_sort"
pushd ${repo_root} > /dev/null
git checkout -q master
git merge ${right_new_branch} > /dev/null
popd > /dev/null
}
git_branch()
{
print_lesson_title "Branches"
print_text_block "Branching means you diverge from the main line of" \
"development and continue to do work without messing with that main line."
print_text_block "Main line is often called 'master' or 'main' branch. It" \
"is also common to have a 'dev' branch. When all changes work on the" \
"development branch, changes are merged to the master branch."; echo
press_any_key
# Task 1
print_task_title "New branch"
print_text_block "It is good to make a new local branch every time you are" \
"starting with a new task. In that way, you'll be able to switch" \
"between tasks without copy/pasting the code or saving it outside the" \
"repository."; echo
new_branch="implement_sort"
while [ 1 ]
do
print_text_block "Your task is to create a new branch '${new_branch}' that" \
"will diverge from current (and latest) commit on the master branch" \
"and position yourself to a new branch."; echo
print_text_block "You can do that in multiple ways:"
print_line " git branch ${new_branch}; git checkout ${new_branch}"
print_line " - or -"
print_line " git checkout -b ${new_branch}"
press_any_key git_branch
pushd ${repo_root} > /dev/null
u_branch=$( get_current_branch )
new_branch_exists=$( git branch | grep -o "${new_branch}" )
popd > /dev/null
if [[ -z "${new_branch_exists}" ]]
then
task_fail "Create ${new_branch} branch"
continue
elif [[ "${new_branch}" != "${u_branch}" ]]
then
task_fail "Checkout ${new_branch} branch"
continue
fi
task_pass "Branch ${new_branch} created"
break
done
echo; echo
# Task 2
comm_msg="Sorting application added"
files=( "sort.c" )
print_task_title "Adding a feature"
git_branch_2_prepare
while [ 1 ]
do
print_text_block "Now when we have a new branch, we can write a new feature" \
"and commit changes. I've already prepared a new file, you just need to" \
"commit it with the message '${comm_msg}'."
press_any_key git_branch_2
u_branch=$( get_current_branch )
u_comm_msg=$( get_commit_message )
u_files=($( get_commited_files ))
if [[ "${new_branch}" != "${u_branch}" ]]
then
task_fail "Checkout ${new_branch} branch"
continue
elif [[ "${comm_msg}" != "${u_comm_msg}" ]]
then
task_fail "Wrong commit message"
continue
elif [[ "${files[@]}" != "${u_files[@]}" ]]
then
task_fail "Wrong files commited"
continue
fi
task_pass "Commit added to new branch"
break
done
echo; echo
# Task 3
print_task_title "Merge"
print_text_block "If you now check git log, you'll notice that your " \
"implement_sort branch is one commit ahead of the master branch."; echo
while [ 1 ]
do
print_text_block "We have tested our changes and we are ready to merge" \
"changes to the master branch."
print_text_block "To do that, you'll need to checkout master branch and" \
"merge ${new_branch} to it."
press_any_key git_branch_3
u_branch=$( get_current_branch )
u_comm_msg=$( get_commit_message )
if [[ "master" != "${u_branch}" ]]
then
task_fail "Checkout master branch"
continue
elif [[ "${comm_msg}" != "${u_comm_msg}" ]]
then
task_fail "Merge ${new_branch}"
continue
fi
task_pass "Sort feature is merged to master branch"
break
done
}
##################
# 1.4 git rebase #
##################
git_rebase_prepare()
{
pushd ${repo_root} > /dev/null
curr_branch=$( get_current_branch )
git checkout -q master
git reset -q --hard HEAD^
cat > sum.c << EOF
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv)
{
unsigned int i, j;
int sum = 0;
for (i = 1; i < argc; ++i)
sum += atoi(argv[i]);
printf("%d\n", sum);
return 0;
}
EOF
git add sum.c
git commit --author="John Doe <>" -m "Sum calculator added" > /dev/null
git checkout -q ${curr_branch}
popd > /dev/null
}
git_rebase_clean()
{
git_branch_3_solution
git_rebase_prepare
}
git_rebase_hint()
{
right_new_branch="implement_sort"
hint "git checkout ${right_new_branch}"
hint "git rebase master"
hint "git checkout master"
hint "git merge ${right_new_branch}"
}
git_rebase_solution()
{
git_rebase_clean
right_new_branch="implement_sort"
pushd ${repo_root} > /dev/null
git checkout -q ${right_new_branch}
git rebase -q master > /dev/null
git checkout -q master
git merge ${right_new_branch} > /dev/null
popd > /dev/null
}
git_rebase_2_clean()
{
git_rebase_solution
}
git_rebase_2_hint()
{
hint "Create a new branch and checkout to it"
hint "Make changes to sort.c code"
hint "Commit your changes"
}
git_rebase_2_solution()
{
git_rebase_2_clean
right_new_branch="update_sort"
pushd ${repo_root} > /dev/null
git checkout -q -b ${right_new_branch}
sed -i 's/< 0/> 0/' sort.c
git add sort.c
git commit -m "sort.c updated to sort ascending" > /dev/null
popd > /dev/null
}
git_rebase_3_prepare()
{
pushd ${repo_root} > /dev/null
curr_branch=$( get_current_branch )
git checkout -q master
cat > sort.c << EOF
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
char *list[argc - 1];
char *tmp = NULL;
int i, j;
int len = argc - 1;
for (i = 1; i < argc; ++i)
list[i - 1] = argv[i];
for (i = 0; i < len - 1; ++i) {
for (j = i + 1; j < len; ++j) {
if (strcmp(list[i], list[j]) < 0) {
tmp = list[i];
list[i] = list[j];
list[j] = tmp;
}
}
}
for (i = len - 1; i >= 0; --i)
printf("%s ", list[i]);
printf("\n");
return 0;
}
EOF
git add sort.c
git commit --author="John Doe <>" -m "random update" > /dev/null
git checkout -q ${curr_branch}
popd > /dev/null
}
git_rebase_3_clean()
{
git_rebase_2_solution
git_rebase_3_prepare
}
git_rebase_3_hint()
{
hint "Rebase changes"
hint "Change last for loop from 'for (i = len - 1; i >= 0; --i)' to 'for (i = 0; i < len; ++i)'"
hint "Test sort app"
hint "Merge changes to master"
}
git_rebase_3_solution()
{
git_rebase_3_clean
right_new_branch="update_sort"
pushd ${repo_root} > /dev/null
git rebase -q master
sed -i 's/for (i = len - 1; i >= 0; --i)/for (i = 0; i < len; ++i)/' sort.c
git add sort.c
git commit --amend -m "sort.c updated to sort ascending" > /dev/null
git checkout -q master
git merge ${right_new_branch} > /dev/null
popd > /dev/null
}
git_rebase()
{
print_lesson_title "Rebase"
print_text_block "In your last task you've successfully merged your changes" \
"to the master branch, but that's not always the case, especially when" \
"you are not working alone."; echo
print_text_block "We'll go back in time in a parallel universe. Where" \
"somebody else already merged his/her changes for another task."
print_text_block "I'll fetch and pull the latest changes on the master" \
"branch from an imaginary remote server, so you can see these in your" \
"git tree."
print_text_block "Before doing anything, check gitk command (if you are" \
"using terminal, use 'git log --all --graph --decorate --oneline')."
git_rebase_prepare
press_any_key
print_text_block "You can see two branches in gitk. Your feature branch" \
"${new_branch} and master branch that has one commit more than before."; echo
print_text_block "Now you have two options. In the first option, you'll do" \
"exactly the same thing as before and just merge ${new_branch} to the" \
"master branch. If you do that, git will make another merge commit and" \
"connect everything. Do that and check gitk."
press_any_key
# Task 1
git_rebase_clean
while [ 1 ]
do
print_text_block "But, there's a second and better option. Before you merge" \
"your commit, you should rebase your commit to the last commit in the" \
"master branch. You will do that with 'git rebase master'."
print_text_block "After rebasing check git log/gitk and merge your change" \
"to master."
press_any_key git_rebase
u_branch=$( get_current_branch )
u_comm_msg=$( get_commit_message )
if [[ "master" != "${u_branch}" ]]
then
task_fail "Merge changes to the master branch"
continue
elif [[ "${comm_msg}" != "${u_comm_msg}" ]]
then
task_fail "Merge ${new_branch}"
continue
fi
task_pass "Sort feature is rebased and merged to master branch"
break
done
echo; echo
# Task 2
new_branch="update_sort"
print_text_block "It's all fun and games when everybody works on different" \
"files and nobody makes a mess in your little universe."
print_text_block "Now, let's see what happens in the real world."; echo
print_text_block "We implemented a sorting app, but somebody changed the" \
"app description and it should sort words ascending instead of descending."
git_rebase_2_clean
while [ 1 ]
do
print_text_block "Make a new branch '${new_branch}' from the master, change" \
"sort app to work as expected, and commit changes (do not merge it just yet)."
print_text_block "NOTE: use 'gcc -o sort sort.c' to compile sort.c file and" \
"test it before committing change."
press_any_key git_rebase_2
pushd ${repo_root} > /dev/null
u_branch=$( get_current_branch )
sort_result="$( gcc -o ./sort sort.c; ./sort c d a )"
u_comm_msg=$( get_commit_message )
u_files=$( get_commited_files )
popd > /dev/null
if [[ "${new_branch}" != "${u_branch}" ]]
then
task_fail "Checkout ${new_branch} branch"
continue
elif [[ "a c d " != "${sort_result}" ]]
then
task_fail "Fix sort.c file"
continue
elif [[ "sort.c" != "${u_files}" ]] || [[ "Sorting application added" == "${u_comm_msg}" ]]
then
task_fail "Commit just sort.c file"
continue
fi
task_pass "You fixed sort.c functionality"
break
done
echo; echo
# Task 3
print_text_block "Now, I'll again fetch and pull the latest changes for" \
"the master branch and you check what's happening."
print_text_block "For a start, checkout the master branch and merge your" \
"change to it."
git_rebase_3_prepare
press_any_key
print_text_block "Now try to test the sort app if it works as expected."; echo
press_any_key