-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6-git.sh
More file actions
2218 lines (1680 loc) · 56.8 KB
/
6-git.sh
File metadata and controls
2218 lines (1680 loc) · 56.8 KB
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/sh
. .lib/shell-compat-test.sh
_DURATION=25
_LSN_VERSION=1.0.1
# Put tutorial library files into $PATH if they are not already added
if [[ -d "$PWD/.lib" && ":$PATH:" != *":$PWD/.lib:"* ]]; then
PATH=$PWD/.lib:$PATH
fi
# The number of the next project
_A=0
# Name of the starter code repo
_REPO_NAME=GASRATS-Demo
# This function is named `_Git` to avoid clashing with Zsh's `_git`
_Git() { (( $# == 0 )) && echo $(blu Git) || echo $(blu $*); }
source ansi-terminal-ctl.sh
source progress.sh
if [[ -n $_TUTR ]]; then
source editors+viewers.sh
source generic-error.sh
source git.sh
source noop.sh
source open.sh
source platform.sh
# the number of Git subcommands taught in this lesson
_SUBCMDS=10
_subcmd() { rev "Git subcommand $1/$_SUBCMDS:${_Z} $(_Git $2)"; }
_local() { (( $# == 0 )) && echo $(ylw local) || echo $(ylw $*); }
_remote() { (( $# == 0 )) && echo $(mgn remote) || echo $(mgn $*); }
_origin() { (( $# == 0 )) && echo $(red origin) || echo $(red $*); }
# origin of the starter code repo
_SSH_REPO_URL=git@github.com:SmallSatGasTeam/$_REPO_NAME
# Open the current Git repo's origin web page
browse_repo() {
_tutr_git_repo_https_url
if [[ -n $REPLY ]]; then
_tutr_open $REPLY
_tutr_warn echo "Opening $REPLY in your web browser..."
else
_tutr_warn echo "Failed to find this repo's origin URL!"
fi
}
fi
_repo_warning() {
cat <<-:
The repository $(path $_REPO_NAME) already exists in the
parent directory. Because this lesson involves cloning this repository,
it should not already exist.
If you have not yet finished the shell-tutor, you may not wish to delete
your work. In that case, it's probably best to not re-run this lesson.
If you want to start over, use $(cmd rm -rf) to delete $(path $_REPO_NAME)
and everything in it. From here you can run this command:
$(cmd rm -rf ../$_REPO_NAME)
Otherwise, you can rename the directory with $(cmd "mv OLD NEW").
After moving or removing the repository, this lesson can be restarted.
If you are just looking for a quick refresher, please refer to the
$(_Git) instructions online at https://github.com/SmallSatGasTeam/GASNotes/blob/main/Git%20Commands.md.
:
}
_tutr_lesson_statelog_global() {
_TUTR_STATE_CODE= # We don't have a meaningful and general state code yet...
_TUTR_STATE_TEXT=$(_tutr_git_default_text_statelog $_REPO_PATH)
}
setup() {
source screen-size.sh 80 30
source assert-program-exists.sh
_tutr_assert_program_exists ssh
_tutr_assert_program_exists git
source ssh-connection-test.sh
_ssh_key_is_missing_msg() {
cat <<-MSG
${_Y} ______
${_Y}---' __) ${_Z}Your SSH key is missing!
${_Y} __) ${_Z}You can fix it yourself by running lesson 5 again.
${_Y} __)
${_Y} ____) ${_Z}Run this command:
${_Y}---. ( ${_Z} $(cmd MENU=yes ./tutorial.sh)
${_Y} '. \\ ${_Z}Then choose ${_W}5-ssh-key.sh
${_Y} \\_)
${_Y} ${_Z}Contact $_EMAIL if you need assistance.
MSG
}
_tutr_assert_ssh_connection_is_okay
export _BASE="$PWD"
# Because I can't count on GNU Coreutils realpath(1) or readlink(1) on
# all systems, get parent dir's real name the old fashioned way
export _PARENT=$(cd .. && pwd)
export _REPO_PATH=$_PARENT/$_REPO_NAME
# Exit if the starter code repo already exists
if [[ -d "$_REPO_PATH/.git" ]]; then
_tutr_err _repo_warning
return 1
fi
}
prologue() {
[[ -z $DEBUG ]] && clear
echo
cat <<-PROLOGUE
$(_tutr_progress)
Shell Lesson #6: The Git Version Control System
In this lesson you will learn how to
* Prepare Git on your computer
* Ask Git for help about its commands
* Clone a Git repository onto your computer
* Check the status of your repository
* Change a file and commit it to the repository
* View the Git log
* Submit your homework to GitHub
This lesson takes around $_DURATION minutes.
PROLOGUE
_tutr_pressenter
}
# 0. Learn about the 'help' command
git_help_prologue() {
cat <<-:
$(_Git) is a system of programs that manage a $(_Git repository) of source code.
Wh-what does that mean? Patience, and it will all make sense.
You don't know it yet, but $(_Git) will become one of your best friends as
you write code. $(_Git) has a bit of a... reputation... for not being very
easy to learn.
We'll take things nice and slow.
:
_tutr_pressenter
cat <<-:
To keep things simple, everything that you do with $(_Git) happens with the
$(cmd git) command.
The first argument to $(cmd git) is the name of a $(bld subcommand).
After the subcommand you may give other arguments to complete the
command. The syntax is:
$(cmd "git SUBCOMMAND [arguments...]")
I will teach you $(rev $_SUBCMDS) subcommands to get started with $(_Git).
Later on, when you are more comfortable, you will add more subcommands
to your repertoire.
The most important subcommand is $(cmd help). If you forget everything else
you know about $(_Git), you can figure it out again with this subcommand.
Run $(cmd git help) now.
:
}
git_help_test() {
_tutr_generic_test -c git -a help
}
git_help_hint() {
_tutr_generic_hint $1 git
echo "Run $(cmd git help) to proceed"
}
git_help_epilogue() {
_tutr_pressenter
cat <<-:
${_Y} _
Phew! That's a lot of help! ${_Y} ( |
${_Y} ___\\ \\
It's okay if this seems overwhelming right now. ${_Y} (__() \`-|
Soon, much of it will make complete sense to you. ${_Y} (___() |
${_Y} (__() |
Trust me! ${_Y} (_()__.--|
:
_tutr_pressenter
}
# 1. practice getting help
git_help_help_prologue() {
cat <<-:
$(cmd git help) shows a small sampling of possible subcommands that $(_Git) can
run, but it doesn't tell you $(bld how) to use them. You can obtain detailed
help about a specific subcommand by giving $(cmd git help) that subcommand's
name as an argument.
:
if [[ $_PLAT = MINGW ]]; then
cat <<-:
Since you are on Windows, $(_Git) may display the subcommand's manual page
in your browser instead of the console.
If nothing appears in your console, just wait a bit longer for your
browser to pop up.
Otherwise, the information will be presented just like a man page.
As before, use $(kbd q) to quit the man page viewer.
:
else
cat <<-:
That subcommand's manual page will appear in the terminal, just the same
as the other manual pages you've read previously. As before, use $(kbd q) to
quit the man page viewer.
:
fi
cat <<-:
Begin by viewing the manual for the $(cmd help) subcommand:
$(cmd git help help)
:
}
git_help_help_test() {
_tutr_generic_test -c git -a help -a help
}
git_help_help_hint() {
_tutr_generic_hint $1 git
cat <<-:
Read the help for git's $(cmd help) subcommand:
$(cmd git help help)
:
if [[ $_PLAT = MINGW ]]; then
cat <<-:
This command may open git's $(cmd help) page in your browser instead of
the console.
:
fi
}
git_help_help_epilogue() {
cat <<-:
Whenever you are unsure how to use a $(_Git) subcommand, remember to run
$(cmd git help SUBCOMMAND)
to learn how to use it.
$(_subcmd 1 help)
Now you can begin in earnest!
:
_tutr_pressenter
}
# 2. Launch a command shell and use the `git config` command to set up your
# user name and email address.
git_config_rw() {
git config --global --unset user.name
git config --global --unset user.email
}
git_config_ff() {
git config --global user.name "The Cheat"
git config --global user.email Cheatachu72@homestarrunner.com
}
git_config_pre() {
git config --get user.name >/dev/null
_HAS_NAME=$?
git config --get user.email >/dev/null
_HAS_EMAIL=$?
if (( _HAS_NAME == 0 && _HAS_EMAIL == 0 )); then
printf "\x1b[1;32mTutor\x1b[0m: Git knows your name and email address, which means that\n"
printf "\x1b[1;32mTutor\x1b[0m: you already know about the $(cmd git config) subcommand.\n"
printf "\x1b[1;32mTutor\x1b[0m:\n"
printf "\x1b[1;32mTutor\x1b[0m: This isn't your first time at the rodeo, is it?\n"
printf "\x1b[1;32mTutor\x1b[0m:\n"
printf "\x1b[1;32mTutor\x1b[0m: Anyhow, I think you've earned this:\n"
printf "\x1b[1;32mTutor\x1b[0m:\n"
printf "\x1b[1;32mTutor\x1b[0m: $(_subcmd 2 config)\n"
printf "\x1b[1;32mTutor\x1b[0m:\n"
_tutr_pressenter
fi
}
git_config_prologue() {
cat <<-:
Use the $(cmd git config) command to set up your name and email address.
$(_Git) needs to know who you are so that when you make commits it can
record who was responsible.
The command to set your user name is like this:
$(cmd 'git config --global user.name "Danny Boy"')
And the command for your email goes like:
$(cmd 'git config --global user.email "danny.boy@houseofpain.com"')
Of course, use your own name and email address.
:
}
git_config_test() {
git config --get user.name >/dev/null
_HAS_NAME=$?
git config --get user.email >/dev/null
_HAS_EMAIL=$?
if [[ ${_CMD[0]} = git && ${_CMD[1]} = config && ${_CMD[@]} != *--global* ]]; then return 97
elif (( $_HAS_NAME == 0 && $_HAS_EMAIL == 0 )); then return 0
elif (( $_HAS_NAME == 0 )); then return 99
elif (( $_HAS_EMAIL == 0 )); then return 98
elif [[ ${_CMD[0]} = git && ${_CMD[1]} = config ]]; then return 96
else _tutr_generic_test -c git -a config -d "$_BASE"
fi
}
git_config_hint() {
case $1 in
99)
cat <<-:
Good! Now set your email address under the $(bld user.email) setting.
:
;;
98)
cat <<-:
Almost there! Now configure your name in the $(bld user.name) setting.
:
;;
97)
cat <<-:
The $(cmd "--global") option to the $(cmd git config) command is important
because it records that setting across your entire computer.
Without it, you'll need to run $(cmd git config) every time you create
a new $(_Git) repository. And that's just a lot of unnecessary work.
Try that command again, but add $(cmd "--global") right after
$(cmd git config) and before the name of the setting.
:
;;
96)
cat <<-:
The commands you must use look like this:
$(cmd 'git config --global user.name "Danny Boy"')
$(cmd 'git config --global user.email "danny.boy@houseofpain.com"')
Of course, you should use your own name and email address.
:
;;
*)
_tutr_generic_hint $1 git "$_BASE"
;;
esac
}
git_config_epilogue() {
cat <<-:
$(_subcmd 2 config)
Because you gave $(cmd git config) the $(cmd '--global') option you will not need to
perform this step in the future. From now on, the $(cmd git) program on
this computer knows who you are.
If you made a typo when entering your name or email, or want to change
them, you may do so at any time. You'll just use these same commands
again.
If you install $(_Git) on another computer, it will remind you to run
this set up routine when you first run $(cmd git).
:
_tutr_pressenter
}
# 3. see if we're presently within a git repo
git_status0_prologue() {
cat <<-:
One of the most important aspects of $(_Git) is that it facilitates sharing
your project with other programmers. $(_Git) has been called "the social
network for code".
There are three key concepts related to sharing projects:
* The directory of files which make up a project and is managed by $(_Git)
is called a $(bld repository) (or $(bld repo) for short)
* $(bld cloning) downloads a $(_Git) repository onto your computer
* $(bld pushing) uploads your repository to another computer
You've already cloned at least one repository; that's how this shell
tutorial came to be on your computer. In a moment you are going to
clone another repository from the internet.
:
_tutr_pressenter
cat <<-:
Before you clone a repository, it is wise to ensure that your shell's
CWD is $(bld not) already inside a $(_Git) repository. Things get confusing really
fast when one $(_Git) repository is nested in another.
$(_Git "Git's") $(cmd status) subcommand provides information about repositories:
* When this command $(blu succeeds) it means that your shell's CWD
$(bld is) in a repository.
* When this command $(red fails) it typically means that you are $(bld not)
already inside a repository.
Run $(cmd git status) to see which is the case for you.
:
}
git_status0_test() {
_tutr_generic_test -i -c git -a status -d "$_BASE"
}
git_status0_hint() {
_tutr_generic_hint $1 git "$_BASE"
cat <<-:
Run $(cmd git status)
:
}
git_status0_epilogue() {
if (( $_RES == 0 )); then
cat <<-:
This output indicates that you are presently in a repository.
:
else
cat <<-:
Huh, it looks like you're not in a repository right now.
That's unexpected, but not a problem.
:
fi
cat <<-:
$(_subcmd 3 status)
:
_tutr_pressenter
}
cd_dotdot0_rw() {
cd "$_BASE"
}
cd_dotdot0_ff() {
cd "$_PARENT"
}
# 4. cd .. to escape this git repository
cd_dotdot0_prologue() {
echo Go up and out of this directory.
}
cd_dotdot0_test() {
if [[ "$PWD" = "$_PARENT" ]]; then return 0
else _tutr_generic_test -c cd -a .. -d "$_PARENT"
fi
}
cd_dotdot0_hint() {
_tutr_generic_hint $1 cd "$_PARENT"
cat <<-:
Run $(cmd cd ..) to leave this directory for its parent.
:
}
# 5. Ensure that you're not presently within a git repository by running `git status`.
git_status1_prologue() {
cat <<-:
You are now in the parent directory of the shell tutorial repository.
But what if this directory is also a $(_Git) repository? You had better run
$(cmd git status) to find out.
When $(cmd git status) is used outside of a repository it reports a $(red fatal)
error. Usually one wishes to avoid $(red fatal) errors, but in this case
an error message is $(cyn Good News).
:
}
git_status1_test() {
_tutr_generic_test -f -c git -a status -d "$_PARENT"
}
git_status1_hint() {
[[ -n $DEBUG ]] && echo "_test returns '$1'" # DELETE ME
case $1 in
$STATUS_WIN)
cat <<-:
This directory really shouldn't be a $(_Git) repository.
Run $(cmd tutor bug) and email the output to $_EMAIL
before proceeding.
:
;;
*)
_tutr_generic_hint $1 git "$_PARENT"
cat <<-:
Run $(cmd git status) to find out if you are still inside a repository.
:
;;
esac
}
git_status1_epilogue() {
_tutr_pressenter
if (( $_RES == 0 )); then
cat <<-:
Hmm, you're still inside a repo here?
It might cause you trouble if you proceed.
Please contact $_EMAIL for help.
:
else
cat <<-:
This is exactly what you want to see when you $(bld "shouldn't") be in a $(_Git)
repository.
:
fi
cat <<-:
It never hurts to run $(cmd git status). You really can't use it too much!
:
_tutr_pressenter
}
# 6. Clone the git repository containing the starter code from GitHub onto
# your computer using the `git clone` command.
git_clone_rw() {
command rm -rf "$_REPO_PATH"
}
git_clone_ff() {
git clone $_SSH_REPO_URL
}
git_clone_pre() {
# See if the starter code repo already exists
if [[ -d "$_REPO_PATH/.git" ]]; then
_tutr_err _repo_warning
return 1
fi
}
git_clone_prologue() {
cat <<-:
Now you will $(bld clone) the starter code for the GASRATS-Demo.
$(bld Cloning) a repo makes a new directory on your computer into which the
repo's information is downloaded.
The syntax of this command is
$(cmd "git clone URL [DIRECTORY]")
$(cmd URL) is the location of another $(_Git) repo known as the $(_remote). Most
often the $(_remote) repo is out on the internet, but it can also be another
directory on your computer. When a $(_remote) repo is cloned from the web,
the URL argument begins with $(cmd git@) or $(cmd https://).
If you leave off the optional $(cmd DIRECTORY) argument, $(_Git) chooses the name
of the new directory for you.
Use $(cmd git clone) to clone the $(_remote) repo at the URL
$(path $_SSH_REPO_URL)
Leave off the optional $(cmd DIRECTORY) argument for now; you can rename the
repo after this tutorial.
:
}
git_clone_test() {
_tutr_generic_test -c git -a clone -a "^https://github.com/SmallSatGasTeam/GASRATS-Demo$|^git@github.com:SmallSatGasTeam/GASRATS-Demo$" -d "$_PARENT"
}
git_clone_hint() {
case $1 in
$STATUS_FAIL)
cat <<-:
$(cmd git clone) failed unexpectedly.
If the above error message includes the phrases $(red fatal: unable to access) and
$(red Connection refused), that indicates an issue with your network
connection. Ensure that you are connected to the internet and try again.
If you are using campus WiFi, make sure you are connected to $(bld Eduroam)
and $(red not) $(blu USU Guest).
If the error persists or is different, please contact $_EMAIL
for help. Copy the full command and all of its output.
:
;;
*)
_tutr_generic_hint $1 git "$_PARENT"
cat <<-:
To clone this repo run
$(cmd git clone $_SSH_REPO_URL)
:
;;
esac
}
git_clone_epilogue() {
if [[ $_RES -eq 0 ]]; then
cat <<-:
That is all normal output for the $(cmd clone) subcommand.
$(_subcmd 4 clone)
:
_tutr_pressenter
else
cat <<-:
Hmm... something went wrong while cloning that repository.
Copy the text in the terminal and prepare a bug report for Erik.
:
fi
}
# 7. Enter the newly cloned repository
cd_into_repo_rw() {
cd "$_PARENT"
}
cd_into_repo_ff() {
cd "$_REPO_PATH"
_ORIG_URL=$(git remote get-url origin)
}
cd_into_repo_prologue() {
cat <<-:
$(cmd git clone) created a new directory called $(path $_REPO_NAME)
and populated it with files from the internet.
This directory is a new $(_Git) repository.
Why not $(cmd cd) inside and take a look around?
:
}
cd_into_repo_test() {
if [[ "$PWD" = "$_REPO_PATH" ]]; then return 0
elif _tutr_noop; then return $NOOP
else _tutr_generic_test -c cd -a $_REPO_NAME -d "$_REPO_PATH"
fi
}
cd_into_repo_hint() {
_tutr_generic_hint $1 cd "$_REPO_PATH"
cat <<-:
Enter the new repo with the $(cmd cd) command:
$(cmd cd $_REPO_NAME)
:
}
cd_into_repo_post() {
if [[ $_RES -ne 0 ]]; then
_tutr_die printf "'Then send it to $_EMAIL.'"
fi
_ORIG_URL=$(git remote get-url origin)
}
# 8. See what a clean, newly cloned repo looks like with 'git status'
git_status2_prologue() {
cat <<-:
You can see what files and directories are here with $(cmd ls). After the
last lesson the layout of this repository should be familiar.
Now that you're back inside of a $(_Git) repository you can run $(cmd git status)
again to see what state the repository is in. Since you just barely
cloned it down from the internet, this repo should be in a clean state.
Run $(cmd git status) to proceed.
:
}
git_status2_test() {
if _tutr_noop; then return $NOOP
else _tutr_generic_test -c git -a status -d "$_REPO_PATH"
fi
}
git_status2_hint() {
_tutr_generic_hint $1 git "$_REPO_PATH"
cat <<-:
Run $(cmd git status) to proceed.
:
}
git_status2_epilogue() {
_tutr_pressenter
cat <<-:
This is what a clean repository looks like. By $(bld clean) I mean that
there is no difference between the files in this $(_local local repo) and the
files in the $(_remote remote repo).
Let me explain what this message is telling you.
$(bld On branch main)
This message reminds you that you are working on the 'main' (A.K.A.
default) branch. For the time being all of your work will be on this
branch. You'll learn more about branches later in the semester.
$(bld "Your branch is up to date with 'origin/main'.")
The files in the 'main' branch of this $(_local local repo) are the same
as the files on the $(_remote "remote repo's") 'main' branch. $(_Git) doesn't
automatically go out to the internet to check, though; this
information was up-to-date as of your $(cmd git clone) command.
$(bld "nothing to commit, working tree clean")
$(bld Working tree) refers to the source code files in this $(_local local repo).
Since you have not changed anything, they are exactly as $(_Git) remembers
them.
:
_tutr_pressenter
}
# Open "README.md" in an editor and change the file in some way.
# Return to your command shell ask git about the status of your repository.
edit_readme0_rw() {
git restore "$_REPO_PATH/README.md"
}
edit_readme0_ff() {
cat <<-: >> "$_REPO_PATH/README.md"
,,,
Kilroy was here (o o)
----------------------------------------------------ooO-(_)-Ooo-------
:
sed -i -e 'y/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM/' "$_REPO_PATH/README.md"
}
# The next step also wants the user to run 'git status'
edit_readme0_pre() {
_CMD=()
}
edit_readme0_prologue() {
cat <<-:
$(_Git) is much more than just a slick way to download code from the 'net.
The thing you will do the most with $(_Git) is take snapshots (A.K.A.
$(bld commits)) of your project while you work. $(bld Commits) record the state of
files in your project at various points in time. When you make a
mistake or paint yourself into a corner you can turn the project back to
an earlier commit and try again. $(_Git) is the ultimate $(bld undo button) that
transcends all other tools.
To make a commit you first need to change $(bld something) in the $(_local repository).
There is a file here called $(path README.md). Open this file in $(cyn Nano), change
something, and save it. $(_Git) will be able to tell that you changed this
file. Run $(cmd git status) to see what $(_Git) says about it.
It really doesn't matter what you do to $(path README.md); you can even
remove the whole file. Knock yourself out!
:
}
edit_readme0_test() {
_README_UNCHANGED=99
if [[ "$PWD" != "$_REPO_PATH" ]]; then return $WRONG_PWD
elif [[ ${_CMD[0]} = git && ${_CMD[1]} = status ]]; then return $NOOP
elif _tutr_file_unstaged README.md; then return 0
elif ! _tutr_file_changed README.md; then return $_README_UNCHANGED
else _tutr_generic_test -c git -a status
fi
}
edit_readme0_hint() {
case $1 in
$NOOP)
return
;;
$WRONG_PWD)
_tutr_minimal_chdir_hint "$_REPO_PATH"
;;
$_README_UNCHANGED)
cat <<-:
You won't see anything interesting until you change $(path README.md).
Go ahead and open it in $(cyn Nano) and make a mess of it. You can't
really hurt anything here!
:
;;
*)
_tutr_generic_hint $1 git "$_REPO_PATH"
cat <<-:
Open $(path README.md) in $(cyn Nano), change it and save it.
Or, just delete the file. Whatever floats your boat.
:
;;
esac
}
edit_readme0_epilogue() {
[[ ! -f "$_REPO_PATH/README.md" ]] && echo "Not messing around, are we?"
}
# Run 'git status' to see what our edited/deleted file looks like
git_status3_prologue() {
cat <<-:
See what $(cmd git status) has to say about your handiwork.
:
}
git_status3_test() {
_tutr_generic_test -c git -a status -d "$_REPO_PATH"
}
git_status3_hint() {
_tutr_generic_hint $1 git "$_REPO_PATH"
cat <<-:
Run $(cmd git status) to see what $(_Git) makes of your change to $(path README.md)
:
}
git_status3_epilogue() {
_tutr_pressenter
cat <<-:
You will see this message a lot, so you had better know what it means.
$(bld On branch main)
You are still on the main branch.
$(bld Your branch is up to date with $(_remote "'origin/main'"))
The $(_local "local repo's") main branch is not different from the main branch
on the $(_remote remote repo) named $(_origin). The $(_origin) repo is the one you
cloned from.
$(bld "Changes not staged for commit")
This is where $(_Git) lists what files have changed. $(_Git) knows when a
file is created, modified or deleted. Right before it displays the
changed files, it suggests commands you might run:
* $(cmd git add) accepts the changes
* $(cmd git restore) discards the changes, A.K.A. the $(bld undo button)
Whenever you screw up, $(_Git "Git's") ready to fix it!
The most important thing to remember is that $(cmd git status) suggests one
or more commands that move your project along. Whenever you are unsure
about what to do next, just run $(cmd git status)!
:
_tutr_pressenter
}
# Use git restore (or checkout) to discard this change
git_restore_rw() {
cat <<-: >> "$_REPO_PATH/README.md"
,,,
Kilroy was here (o o)
----------------------------------------------------ooO-(_)-Ooo-------
:
sed -i -e 'y/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM/' "$_REPO_PATH/README.md"
}
git_restore_ff() {
git restore "$_REPO_PATH/README.md"
}
git_restore_prologue() {
cat <<-:
Practice fixing a mistake with $(_Git "Git's") $(cmd restore) subcommand.
The syntax of this subcommand is
$(cmd git restore FILE...)
Use $(cmd git restore) to discard the change to $(path README.md) you made. This
will put $(path README.md) back in its original state, no matter what you did
to it.
:
}
git_restore_test() {
# TODO: handle the case where the user runs `git add README.md`
# and stages their change instead of reverting it
if [[ "$PWD" != "$_REPO_PATH" ]]; then return $WRONG_PWD
elif [[ -z $(git status --porcelain=v1) ]]; then return 0
elif _tutr_noop; then return $NOOP
else _tutr_generic_test -c git -a restore -a README.md -d "$_REPO_PATH"
fi
}
git_restore_hint() {
_tutr_generic_hint $1 git "$_REPO_PATH"
cat <<-:
Use $(cmd git restore) to undo the change you made to $(path README.md).
$(cmd git restore README.md)
:
}
git_restore_epilogue() {
cat <<-:
Excellent!
:
_tutr_pressenter
}
# 11. Run 'git status' to prove that 'git restore' really worked
git_status4_prologue() {
cat <<-:
All better now! Run $(cmd git status) to see for yourself. The state of the
working tree should be $(bld clean).
:
}
git_status4_test() {
if _tutr_noop; then return $NOOP
else _tutr_generic_test -c git -a status -d "$_REPO_PATH"