Build your own Paint
Follow with gitGoing further
EN/FR

Following along with git

How the repository is organized, and how to use it to follow the tutorial, compare your code or start again from a chapter.

All the tutorial's code is in the chapter pages: you can follow it from start to finish without ever opening git. But the project's GitHub repository is organized so you can use it too, if you'd rather read the code directly, compare your code to mine, or pick up the tutorial at a specific chapter.

This page assumes you know the basics of git: cloning a repository, switching branches. If not, no worries, the website is all you need.

How the repository is organized#

The repository is on GitHub: theZackD/build-your-own-paint. It has two branches.

master holds the final code of the app, the one you get at the end of the tutorial.

chapters holds one commit per chapter, starting from an empty project. Its history is separate from master's: it tells the story of building the app in the order of the tutorial, step by step.

Each commit on the chapters branch has a tag, a label with the chapter's name. The ch05-mouse tag, for example, points to the exact code at the end of chapter 5. Every tag compiles and runs with cargo run.

The last tag, ch10-export, has exactly the same code as master. Chapter 11, the wrap-up, adds no code, so it has no tag.

TagChapter
ch00-setup0. Setup
ch01-vec21. 2D vectors
ch02-ppm2. An image in memory
ch03-shapes3. Drawing shapes
ch04-window4. A window
ch05-mouse5. The mouse
ch06-shapes-enum6. Several shapes
ch07-select-move7. Selecting and moving
ch08-toolbar8. The toolbar
ch09-colors9. Colors
ch10-export10. Saving

Getting the code#

Terminal
git clone https://github.com/theZackD/build-your-own-paint.git
Terminal
cd build-your-own-paint

The clone gets everything: both branches and all the tags. You can see the list of tags with:

Terminal
git tag

Viewing the code of a chapter#

To get the code as it is at the end of a chapter, for example chapter 5:

Terminal
git checkout ch05-mouse
Terminal
cargo run

git then prints a long message that starts with:

Output
Note: switching to 'ch05-mouse'.

You are in 'detached HEAD' state.

No need to worry, that's normal. It means you're looking at a snapshot of the code at a precise moment, not a branch to work on. You can read the code, run it, switch to another tag. To go back to the final code:

Terminal
git checkout master

If you'd rather not install anything, you can also look at a chapter's code directly on GitHub, by putting the tag name in the address: github.com/theZackD/build-your-own-paint/tree/ch05-mouse.

Seeing what a chapter adds#

This is probably the most useful command. It shows every line added, changed or removed between two chapters:

Terminal
git diff ch04-window ch05-mouse

For just the list of files touched, with the number of lines:

Terminal
git diff --stat ch04-window ch05-mouse
Output
 src/action.rs      | 11 ++++++++
 src/app.rs         | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/canvas.rs      |  2 ++
 src/color.rs       |  1 +
 src/main.rs        | 20 ++++++--------
 src/shapes/mod.rs  |  3 ++
 src/shapes/rect.rs | 41 +++++++++++++++++++++++++++
 src/vec2.rs        |  8 ++++++
 8 files changed, 156 insertions(+), 11 deletions(-)

On GitHub, the same comparison is shown at github.com/theZackD/build-your-own-paint/compare/ch04-window...ch05-mouse.

Coding the tutorial inside the repository#

If you want to write the code yourself, chapter by chapter, while still being able to compare with mine, the most convenient way is to work directly in the cloned repository.

Create a branch of your own starting from a chapter, for example from the end of chapter 4 if you're about to do chapter 5:

Terminal
git checkout -b my-attempt ch04-window

You're now on your branch, my-attempt, and you can code chapter 5 by following the page on the website. When you're done, or if something doesn't work, compare your code with the code at the end of the chapter:

Terminal
git diff ch05-mouse

This command shows everything that differs between your files and the tag. Everything doesn't have to be identical: different variable names or an extra comment are perfectly fine. What matters is that it works and that you understand why.

Remember to commit on your branch as you go, to keep track of your progress.

If you get lost#

It happens: an error you don't understand, a chapter that no longer compiles, the urge to start over. You can always start again from a clean version.

If you want to keep your changes aside for later:

Terminal
git stash

Then start again from the end of the previous chapter, on a new branch:

Terminal
git checkout -b new-attempt ch04-window

git stash puts your changes aside without losing them, and git stash pop will bring them back if you need them.

Reading on the website, coding elsewhere#

You don't have to code inside the repository. You can perfectly well create your own project with cargo new, as in chapter 0, and only use the repository to look at the reference code when in doubt. To print a file as it is at a given chapter, without switching branches:

Terminal
git show ch05-mouse:src/app.rs

Or more simply, open the file on GitHub by choosing the tag in the branch menu.

Reporting a problem#

If a tag doesn't compile for you, if the code on a page doesn't match the tag's, or if an explanation isn't clear, open an issue on GitHub. Mention the chapter, your system (Windows, macOS or Linux) and your Rust version (rustc --version).

On this page

  1. How the repository is organized
  2. Getting the code
  3. Viewing the code of a chapter
  4. Seeing what a chapter adds
  5. Coding the tutorial inside the repository
  6. If you get lost
  7. Reading on the website, coding elsewhere
  8. Reporting a problem

Build your own Paint

A tutorial by theZackD

The code on GitHubFound a mistake? Open an issueCode under the MIT license