# git exercise. Single developer and local repository.
#
# In this demo/exercise we will develop a toy math-library
# with simple functions: `add`, `sub`, `mul` and `div`
# The functions are trivial, but to understand git, let's assume they were not.
# Each function should be committed seperately.
#
# -- Getting Started Up --
# Create a directory for this exercise and enter it.
File > Open Folder > create and open

# initialize a git repository in the directory
Source Control > Initialize Repository

# Create a file named simple_math.py with a single function
Explorer > New File

# Check how Source Control changed
- simple_math.py is listed under "Changes"
- marked as untracked (U)

# Click on the listed file and see changes displayed

# tell git to keep track of simple_math.py (put it in the staging area)
click on + symbol

# Check how Source Control changed
- simple_math.py is listed under "Staged Changes"
- marked as added (A)

# Now record commit to the repository.
- type a message ("My first commit. A big step for mankind.")
- click Commit

# this failed as we have not yet configured the user
# It seems, we need to open the terminal
View > Terminal

# Introduce yourself to git (set the variables for name and email):
git config --global user.name "Nicola Chiapolini"
git config --global user.email "nicola.chiapolini@physik.uzh.ch"

# Verify that git has correctly stored your config (see 'git help config')
git config user.name
git config user.email
# alternatively use `git config -l`

# Now try to commit again.
- the message should still be in the box
- click Commit

# Check how Source Control changed
- it's now empty (except for an annoying "Publish Branch")


# -- Modifying Files and Committing Changes --
# add a function `subtract(a,b)` calculating a-b to simple_math.py

# Check how Source Control changed
- simple_math.py is listed under "Changes" again
- marked as modified (M)

# Show the changes made to the file.
click on the file, changes are shown

# stage changes
click on + symbol

# Check how Source Control changed
- simple_math.py is listed under "Staged Changes" now
- marked as modified (M)

# commit changes
- leave meassage empty
- click commit

# a new tab "COMMIT_EDITMSG" is opened, entere a message
- entere a message ("my 2nd commit")
- save and close

# Check how Source Control changed
- no changes are listed anymore
- but at the bottom the two commits are displayed


# -- Using the Staging Area --
# Add a function `multiply(a,b)` to simple_math.py

# Check how Source Control changed
- simple_math.py is listed under "Changes"
- marked as modified (M)

# stage changes
click on + symbol

# Check how Source Control changed
- simple_math.py is listed under "Staged Changes"
- marked as modified (M)

# Show the changes made to the file.
click on the file, changes are shown

# we decide that we would prefer to call the function `mul` instead
- change the name of the function

# Check how Source Control changed
- simple_math.py is listed under "Changes" and under "Staged Changes"
- both entries marked as modified (M)

# look at the changes of the different versions.
click on the entries, changes are shown

# Update the staging area,
click on + symbol for the version under "Changes"

# Check how Source Control changed
- simple_math.py is listed under "Staged Changes" only
- still marked as modified (M)

# look at the staged changes
click on the file, changes are shown (last version only)

# and finally commit.
- enter a message
- press [ctrl]+[enter]


# -- Splitting Changes into several Commits --
# start to work on the division function
#
# add `def div(a,b):` to simple_math.py

# `subtract` has a wrong name as well.
# It should be `sub` not `subtract`. Fix it!

# Check how Source Control changed
- simple_math.py is listed under "Changes"
- marked as modified (M)

# We are not done with `div` yet but we want to commit the bugfix now.
# In addition, we would like to keep the bugfix in a separate commit:
#
# Add only the bugfix to the staging area.
- click on simple_math.py
- on the bar between the two versions there are buttons next to each block
- click on the block for the upper (subtract) block

# Check how Source Control changed
- simple_math.py is listed under "Changes" and "Staged Changes" again
- marked as modified (M)

# Commit *only* the bugfix
- enter a message
- click commit
# and see how Source Control accordingly.

# Finish implementing `div`

# stage changes
click on + symbol

# and commit the function.
- enter a message
- click commit


# -- Changes Affecting Several Files --
# Now we want to add output to each of our functions.
#
# Create a file `logger.py` with content
# ```
# def logger(a,b):
#     print(a,b)
# ```
Explorer > New File

# Check how Source Control changed
- logger.py is listed under "Changes"
- marked as untracked (U)
- simple_math.py is not listed as it is unchanged

# Now add a call to `logger()` to each function in simple_math.py.
# (Don't forget to import the function)

# Check how Source Control changed
- logger.py and simple_math.py are listed under "Changes"
- logger.py is marked as untracked (U)
- simple_math.py is marked as modified (M)

# Prepare the staging area for this commit.
click on + symbol for both files

# Check how Source Control changed
- logger.py and simple_math.py are listed under "Staged Changes"
- logger.py is marked as added (A)
- simple_math.py is marked as modified (M)

# Commit
- enter a message
- click commit

# -- Looking around --
# find the commit history
- list under Source Control "Incomming/Outgoing"

# look at changes from commits
- click on single commit
or
- select sevarl commits > right click > view changes

# copy commit ID
- right-click on commit > Copy commit ID

# -- Unwanted Changes --
# Go crazy.
#
# Remove logger.py
Explorer > right click on file > delete
# and commit the change.
Source Control
- logger.py is listed under "Changes"
- logger.py is marked as deleted (D)

# stage deletion
click on + symbol

# Commit
- enter a message
- click commit

# Replace the content of simple_math.py


# Oh no! What have you done! Shame!
#
# Check Source Control
- simple_math.py is listed under "Changes"
- marked as (M)
- bad commit is visible in list

# Get rid of the untracked changes
for simple_math.py click on "Discard Changes"

# check that simple_math.py is good now

# We are still missing a file.
#
# Check last commit
- click on last commit
- look at changes

# undo bad commit.
- ...-Menu > commit > undo last commit
- ...-Menu > changes > unstage all changes
- ...-Menu > changes > discard all changes

# Verify that everything is back to normal.

#Relax: your work is safe.
