Explorar o código

Add simple exercices

Barriat %!s(int64=7) %!d(string=hai) anos
pai
achega
6c804640df

+ 3 - 0
.gitignore

@@ -0,0 +1,3 @@
+*~
+repo/
+work/

+ 33 - 0
exercices/_generic_create.sh

@@ -0,0 +1,33 @@
+#!/bin/bash
+
+function _create() {
+    echo "Removing exercice"
+    rm -rf repo work
+    
+    echo "creating directories"
+    mkdir -p repo work
+    
+    echo "initializing repository"
+    cd repo
+    git init --bare > /dev/null
+    cd ..
+    
+    echo "cloning repository"
+    cd work
+    git clone ../repo . > /dev/null 2>&1
+    
+    echo "Initializing exercice"
+    if [ "$2" == "debug" ]; then
+	$1
+    else
+	$1 > /dev/null 2>&1
+    fi
+    
+    cd ..
+    echo ""
+    echo "Exercice in $(pwd)/work/"
+    echo "=========================================="
+    cat readme.txt
+    echo "=========================================="
+    echo ""
+}

+ 13 - 0
exercices/ex.1/create.sh

@@ -0,0 +1,13 @@
+#!/bin/bash
+
+source ../_generic_create.sh
+
+function _ex() {
+    echo "un" > first.txt
+    echo "deux" > second.txt
+
+    git add first.txt
+}
+
+_create _ex
+

+ 8 - 0
exercices/ex.1/readme.txt

@@ -0,0 +1,8 @@
+You have accidentaly put first.txt in the staging area
+While you actually want to only commit second.txt.
+
+You need to, from the current state, only commit second.txt.
+
+- Remove the file second.txt (Use `git status` to get the appropriate command`)
+- Add first.txt
+- Commit

+ 16 - 0
exercices/ex.2/create.sh

@@ -0,0 +1,16 @@
+#!/bin/bash
+
+source ../_generic_create.sh
+
+function _ex() {
+    touch main.cpp
+    touch main.cpp~
+
+    mkdir -p out
+    touch out/main.o
+    touch out/a.out
+    touch out/conf.ini
+}
+
+_create _ex
+

+ 11 - 0
exercices/ex.2/readme.txt

@@ -0,0 +1,11 @@
+You just wrote a cpp program that reads it config from a file named conf.ini.
+As you don't want to bother adding all files manualy, you have decided to use .gitignore.
+
+You need to create a .gitignore file that will:
+ - Ignore all files that end with a ~ (bakup files)
+ - Ignore every files in the out directory but conf.ini
+
+In other words, once the .gitignore file written, the command `git add .` should only stage:
+ - .gitignore
+ - main.cpp
+ - out/conf.ini

+ 13 - 0
exercices/ex.3/create.sh

@@ -0,0 +1,13 @@
+#!/bin/bash
+
+source ../_generic_create.sh
+
+function _ex() {
+    touch main.cpp
+    touch main.cpp~
+    git add .
+    git commit -m "first commit"
+}
+
+_create _ex
+

+ 9 - 0
exercices/ex.3/readme.txt

@@ -0,0 +1,9 @@
+You just realized that you have commited a backup file: main.cpp~.
+
+Backup files should not be commited, let's correct that mistake.
+
+- Delete the file with `rm`
+- Add all changes, including deleted files with `git add -A`
+- Write a gitignore that will correct your mistake
+- Add it
+- Commit

+ 19 - 0
exercices/ex.4/create.sh

@@ -0,0 +1,19 @@
+#!/bin/bash
+
+source ../_generic_create.sh
+
+function _ex() {
+    touch first.txt
+    git add .
+    git commit -m "First commit"
+    echo "one" >> first.txt
+    git commit -am "Added one"
+    echo "two" >> first.txt
+    git commit -am "Added two"
+    git push -u origin master
+    git reset --hard HEAD~1
+    echo "A B C D F G" >> second.txt
+}
+
+_create _ex
+

+ 56 - 0
exercices/ex.4/readme.txt

@@ -0,0 +1,56 @@
+PART 1:
+You have worked hard and the result is beautiful.
+But somehow there's a bug.
+
+It's late, your partner wants you to go home.
+
+Hell, maybe a good night sleep (or a good night something else)
+will help you find that stupid bug.
+
+So you want to go home, but you don't want to leave the work unsaved,
+that would be unprofessional. And you're a pro.
+
+So, as the code cannot be merged into master yet, you decide to create a branch.
+
+- Create and checkout a new branch named "file-second"
+- Commit the current state
+- Push it (remember, the branch does not exists yet on origin)
+
+
+PART 2:
+Your mind is clear and ready to tackle that nasty bug.
+As you march to your desk like the conquerant you are, your boss stops you.
+
+There's an urgent fix that needs to go out in prod just. right. now.
+
+- Checkout the branch master
+- Pull it to get the last version
+- Add a "three" to the file first.txt
+- Commit and push the changes to that new file
+
+
+PART 3:
+Now is the time to tackle the problem, let's get back to your work.
+
+- Checkout the branch file-second
+
+
+PART 4:
+Of couuuuurse !
+You found it, the 'E' is missing between 'D' and 'F'
+
+After fixing the bug, the awesomeness can be mnerged into master.
+
+- Modify the file so that it's beauty is complete
+- Commit the difference
+- Push it
+- Merge it into master
+- Push master
+- Brag
+
+
+PART 5:
+You are a good citizen (or at least in this story we assume you are).
+You are not working anymore on the branch file-second
+
+- Delete the branch file-second both localy and in origin

+ 19 - 0
exercices/ex.5/create.sh

@@ -0,0 +1,19 @@
+#!/bin/bash
+
+source ../_generic_create.sh
+
+function _ex() {
+    touch first.txt
+    git add .
+    git commit -m "First commit"
+    echo "one" >> first.txt
+    git commit -am "Added one"
+    echo "two" >> first.txt
+    git commit -am "Added two"
+    git push -u origin master
+    git reset --hard HEAD~1
+    echo "three" >> first.txt
+}
+
+_create _ex
+

+ 11 - 0
exercices/ex.5/readme.txt

@@ -0,0 +1,11 @@
+You just ran a `git fetch` and realize someone has modified
+the exact file at the exact line that you just worked on.
+
+You want the current state of your working directory to be saved.
+
+Conflict seems inevitable.
+
+- Create a commit with the new version of first.txt
+- Use `git pull` to merge origin/master into your master
+- Resolve the conflict in first.txt
+- Commit

+ 19 - 0
exercices/ex.6/create.sh

@@ -0,0 +1,19 @@
+#!/bin/bash
+
+source ../_generic_create.sh
+
+function _ex() {
+    touch first.txt
+    git add .
+    git commit -m "First commit"
+    echo "one" >> first.txt
+    git commit -am "Added one"
+    echo "two" >> first.txt
+    git commit -am "Added two"
+    git push -u origin master
+    git reset --hard HEAD~1
+    echo "three" >> first.txt
+}
+
+_create _ex
+

+ 12 - 0
exercices/ex.6/readme.txt

@@ -0,0 +1,12 @@
+You just ran a `git fetch` and realize someone has modified
+the exact file at the exact line that you just worked on.
+
+Well, it seems harmless, there's no need to polute the history.
+You don't want the world to remember the exact state of your work.
+
+- Use `git stash` to stash your changes
+- Use `git pull` to get the last version of origin/master
+- Use `git stash apply` to apply your changes
+- Resolve the conflict in first.txt
+- Use `git stash drop` to empty the stash
+- Commit

+ 19 - 0
exercices/ex.7/create.sh

@@ -0,0 +1,19 @@
+#!/bin/bash
+
+source ../_generic_create.sh
+
+function _ex() {
+    touch first.txt
+    git add .
+    git commit -m "First commit"
+    echo "one" >> first.txt
+    git commit -am "Added one"
+    echo "two" >> first.txt
+    git commit -am "Added two"
+    git push -u origin master
+    git reset --hard HEAD~1
+    echo "three" >> first.txt
+}
+
+_create _ex
+

+ 15 - 0
exercices/ex.7/readme.txt

@@ -0,0 +1,15 @@
+You just ran a `git fetch` and realize someone has modified
+the exact file at the exact line that you just worked on.
+
+Well, it seems harmless, there's no need to polute the history.
+You don't want the world to remember the exact state of your work.
+
+This time, instead of using stash like in ex.5, you are going to rebase.
+
+- Commit your changes
+- Use `git pull --rebase` to get the last version of origin/master and apply your changes.
+- Resolve the conflict in first.txt
+- Use `git rebase --continue` to finish the rebase
+- Push
+
+Can you elaborate on the differences between using stash and rebase ?

+ 30 - 0
exercices/ex.8/create.sh

@@ -0,0 +1,30 @@
+#!/bin/bash
+
+source ../_generic_create.sh
+
+function _ex() {
+    touch first.txt
+    git add .
+    git commit -m "First commit"
+    echo "one" >> first.txt
+    git commit -am "Added one"
+    echo "two" >> first.txt
+    git commit -am "Added two"
+    git push -u origin master
+    git reset --hard HEAD~1
+    touch second.txt
+    git add .
+    git commit -m "Created second.txt"
+    echo "A" >> second.txt
+    git commit -am "WIP"
+    echo "B" >> second.txt
+    echo "C" >> second.txt
+    git commit -am "WIP"
+    echo "D" >> second.txt
+    echo "E" >> second.txt
+    echo "F" >> second.txt
+    git commit -am "WIP"
+}
+
+_create _ex
+

+ 13 - 0
exercices/ex.8/readme.txt

@@ -0,0 +1,13 @@
+You have worked for many hours on a feature and have regularly
+commited localy your work so you can always go back to previous states.
+
+Now it is time to merge this great feature of yours into master.
+
+As you are a good citizen, you don't want every commit named "WIP"
+(Work In Progress) to be pushed to origin, so you're going to squash
+all your commits into one.
+
+- Use `git rebase -i` to squash all "WIP" commits into one
+  but leave the previous commits as they are semantic.
+  Don't forget to put a comprehensible commit message.
+- Use `git pull` to merge into master.

+ 20 - 0
exercices/ex.9/create.sh

@@ -0,0 +1,20 @@
+#!/bin/bash
+
+source ../_generic_create.sh
+
+function _ex() {
+    touch first.txt
+    git add .
+    git commit -m "First commit"
+    echo "one" >> first.txt
+    git commit -am "Added one"
+    echo "two" >> first.txt
+    git commit -am "Added two"
+    git push -u origin master
+    git reset --hard HEAD~1
+    echo "A B C D E F" >> second.txt
+    git add .
+    git commit --amend -m "Added one"
+}
+
+_create _ex

+ 7 - 0
exercices/ex.9/readme.txt

@@ -0,0 +1,7 @@
+Oh no!!!
+You have just made a commit amend on a commit that were already pushed!
+Worst yet: someone has already pushed a new commit on top.
+
+Your branch master and origin/master have diverged.
+
+Fix it!