Build the pieces of a Git repository. Initialise the directory, read and write objects, construct a tree and follow the steps that make a clone possible.
Set up your project
Choose a starter for your language. C · C++ · C# · Elixir · Gleam · Go · Haskell · Java · JavaScript · Kotlin · OCaml · PHP · Python · Ruby · Rust · TypeScript · Zig.
Use the examples in each stage to check your implementation locally. CodeCrafters operates its hosted runner separately.
- Starter templates
- Reference solutions
- Language environments
- Download the source snapshot (ZIP)
- Upstream test runner
Contents
- Initialize the .git directory
- Read a blob object
- Create a blob object
- Read a tree object
- Write a tree object
- Create a commit
- Clone a repository
Core project
1. Initialize the .git directory
All the data for a Git repository — from its commit history to its configuration — lives inside a hidden folder called .git. It’s created when you initialize a new repository with git init. In this stage, you’ll implement that command yourself.
2. Read a blob object
In this stage, you’ll add support for reading a blob using the git cat-file command.
Git objects
Click to expand/collapse
In this challenge, we’ll deal with three Git objects:
- Blobs (This stage)
- These are used to store file data.
- Blobs only store the contents of a file, not its name or permissions.
- Trees (Future stages)
- These are used to store directory structures.
- The information stored can include things like what files/directories are in a tree, their names and permissions.
- Commits (Future stages)
- These are used to store commit data.
- The information stored can include things like the commit message, author, committer, parent commit(s) and more.
All Git objects are identifiable by a 40-character SHA-1 hash, also known as the “object hash”.
Here’s an example of an object hash: e88f7a929cd70b0274c4ea33b209c97fa845fdbc.
Git Object Storage
Click to expand/collapse
Git objects are stored in the .git/objects directory. The path to an object is derived from its hash.
The path for the object with the hash e88f7a929cd70b0274c4ea33b209c97fa845fdbc would be:
.git/objects/e8/8f7a929cd70b0274c4ea33b209c97fa845fdbc
You’ll see that the file isn’t placed directly in the .git/objects directory. Instead, it’s placed in a directory named with the first two characters of the object’s hash. The remaining 38 characters are used as the file name.
Each Git object has its own format for storage. We’ll look at how Blobs are stored in this stage, and we’ll cover other objects in future stages.
Blob Object Storage
Click to expand/collapse
Each Git Blob is stored as a separate file in the .git/objects directory. The file contains a header and the contents of the blob object, compressed using Zlib.
The format of a blob object file looks like this (after Zlib decompression):
blob <size>\0<content>
<size>is the size of the content (in bytes)\0is a null byte<content>is the actual content of the file
For example, if the contents of a file are hello world, the blob object file would look like this (after Zlib decompression):
blob 11\0hello world
The cat-file command
Click to expand/collapse
In this stage, you’ll read a blob from a git repository by reading its contents from the .git/objects directory.
You’ll do this using the first of multiple “plumbing” commands we’ll encounter in this challenge: git cat-file.
git cat-file is used to view the type of an object, its size, and its content. Example usage:
$ git cat-file -p <blob_sha>
hello world # This is the contents of the blob
To implement this, you’ll need to:
- Read the contents of the blob object file from the
.git/objectsdirectory - Decompress the contents using Zlib
- Extract the actual “content” from the decompressed data
- Write the content to stdout
Tests
The tester will first initialize a new git repository using your program, and then insert a blob with random contents into the .git/objects directory:
$ mkdir /tmp/test_dir && cd /tmp/test_dir
$ /path/to/your_program.sh init
$ echo "hello world" > test.txt # The tester will use a random string, not "hello world"
$ git hash-object -w test.txt
3b18e512dba79e4c8300dd08aeb37f8e728b8dad
After that, it’ll run your program like this:
$ /path/to/your_program.sh cat-file -p 3b18e512dba79e4c8300dd08aeb37f8e728b8dad
hello world
The tester will verify that the output of your program matches the contents of the blob.
Notes
- In many programming languages the default print function (like
fmt.Println) will append a newline to the output. The output ofcat-filemust not contain a newline at the end, so you might need to use a different function to print the output.
For Python
- Keep in mind that Git uses Zlib to compress objects. You can use Python’s built-in zlib library to read these compressed files.
For Ruby
- Keep in mind that Git uses Zlib to compress objects. You can use Ruby’s built-in Zlib library to read these compressed files.
For Go
- Keep in mind that Git uses Zlib to compress objects. You can use Go’s built-in compress/zlib package to read these compressed files.
For Rust
- Keep in mind that Git uses Zlib to compress objects. You can use the flate2 crate to read these compressed files, we’ve included it in the
Cargo.tomlfile.
For languages other than Python
For languages other than Ruby
For languages other than Go
For languages other than Rust
- Keep in mind that Git uses Zlib to compress objects. Many languages have utils for dealing with zlib data in their standard library. If not, you might need to use a third-party library to read these compressed files.
3. Create a blob object
In this stage, you’ll implement support for creating a blob using the git hash-object command.
The git hash-object command
Click to expand/collapse
git hash-object is used to compute the SHA-1 hash of a Git object. When used with the -w flag, it also writes the object to the .git/objects directory.
Here’s an example of using git hash-object:
# Create a file with some content
$ echo -n "hello world" > test.txt
# Compute the SHA-1 hash of the file + write it to .git/objects
$ git hash-object -w test.txt
95d09f2b10159347eece71399a7e2e907ea3df4f
# Verify that the file was written to .git/objects
$ file .git/objects/95/d09f2b10159347eece71399a7e2e907ea3df4f
.git/objects/95/d09f2b10159347eece71399a7e2e907ea3df4f: zlib compressed data
Blob Object Storage (Recap)
Click to expand/collapse
As mentioned in the previous stage, each Git Blob is stored as a separate file in the .git/objects directory. The file contains a header and the contents of the blob object, compressed using Zlib.
The format of a blob object file looks like this (after Zlib decompression):
blob <size>\0<content>
<size>is the size of the content (in bytes)\0is a null byte<content>is the actual content of the file
For example, if the contents of a file are hello world, the blob object file would look like this (after Zlib decompression):
blob 11\0hello world
Tests
The tester will first initialize a new git repository using your program:
$ mkdir test_dir && cd test_dir
$ /path/to/your_program.sh init
It’ll write some random data to a file:
$ echo "hello world" > test.txt
It’ll then run your program like this:
$ ./your_program.sh hash-object -w test.txt
3b18e512dba79e4c8300dd08aeb37f8e728b8dad
The tester will verify that:
- Your program prints a 40-character SHA-1 hash to stdout
- The file written to
.git/objectsmatches what the officialgitimplementation would write
Notes
- Although the object file is stored with zlib compression, the SHA-1 hash needs to be computed over the “uncompressed” contents of the file, not the compressed version.
- The input for the SHA-1 hash is the header (
blob <size>\0) + the actual contents of the file, not just the contents of the file.
For C
- You can use
#include <openssl/sha.h>to access OpenSSL’s SHA1() hashing function.
For C++
- You can use
#include <openssl/sha.h>to access OpenSSL’s SHA1() hashing function.
4. Read a tree object
In this stage, you’ll implement the git ls-tree command, which is used to inspect a tree object.
Tree objects
Click to expand/collapse
In this stage, we’ll deal with our next Git object type: trees.
Trees are used to store directory structures.
A tree object has multiple “entries”. Each entry includes:
- A SHA-1 hash that points to a blob or tree object
- If the entry is a file, this points to a blob object
- If the entry is a directory, this points to a tree object
- The name of the file/directory
- The mode of the file/directory
- This is a simplified version of the permissions you’d see in a Unix file system.
- For files, the valid values are:
100644(regular file)100755(executable file)120000(symbolic link)
- For directories, the value is
40000 - There are other values for submodules, but we won’t be dealing with those in this challenge.
For example, if you had a directory structure like this:
your_repo/
- file1
- dir1/
- file_in_dir_1
- file_in_dir_2
- dir2/
- file_in_dir_3
The entries in the tree object would look like this:
40000 dir1 <tree_sha_1>
40000 dir2 <tree_sha_2>
100644 file1 <blob_sha_1>
- Line 1 (
40000 dir1 <tree_sha_1>) indicates thatdir1is a directory with the SHA hash<tree_sha_1> - Line 2 (
40000 dir2 <tree_sha_2>) indicates thatdir2is a directory with the SHA hash<tree_sha_2> - Line 3 (
100644 file1 <blob_sha_1>) indicates thatfile1is a regular file with the SHA hash<blob_sha_1>
dir1 and dir2 would be tree objects themselves, and their entries would contain the files/directories inside them.
The ls-tree command
Click to expand/collapse
The git ls-tree command is used to inspect a tree object.
For a directory structure like this:
your_repo/
- file1
- dir1/
- file_in_dir_1
- file_in_dir_2
- dir2/
- file_in_dir_3
The output of git ls-tree would look like this:
$ git ls-tree <tree_sha>
040000 tree <tree_sha_1> dir1
040000 tree <tree_sha_2> dir2
100644 blob <blob_sha_1> file1
Note that the output is alphabetically sorted, this is how Git stores entries in the tree object internally.
In this stage you’ll implement the git ls-tree command with the --name-only flag. Here’s how the output looks with the --name-only flag:
$ git ls-tree --name-only <tree_sha>
dir1
dir2
file1
The tester uses --name-only since this output format is easier to test against.
We recommend implementing the full ls-tree output too since that’ll require that you parse all data in the tree object, not just filenames.
Tree Object Storage
Click to expand/collapse
Just like blobs, tree objects are stored in the .git/objects directory. If the hash of a tree object is e88f7a929cd70b0274c4ea33b209c97fa845fdbc, the path to the object would be .git/objects/e8/8f7a929cd70b0274c4ea33b209c97fa845fdbc.
The format of a tree object file looks like this (after Zlib decompression):
tree <size>\0
<mode> <name>\0<20_byte_sha>
<mode> <name>\0<20_byte_sha>
(The above code block is formatted with newlines for readability, but the actual file doesn’t contain newlines)
- The file starts with
tree <size>\0. This is the “object header”, similar to what we saw with blob objects. - After the header, there are multiple entries. Each entry is of the form
<mode> <name>\0<sha>.<mode>is the mode of the file/directory (check the previous section for valid values)<name>is the name of the file/directory\0is a null byte<20_byte_sha>is the 20-byte SHA-1 hash of the blob/tree (this is not in hexadecimal format)
You can read more about the internal format of a tree object here.
Tests
The tester will use your program to initialize a new repository:
$ mkdir test_dir && cd test_dir
$ /path/to/your_program.sh init
It’ll then write a tree object to the .git/objects directory.
It’ll then run your program like this:
$ /path/to/your_program.sh ls-tree --name-only <tree_sha>
It’ll verify that the output of your program matches the contents of the tree object.
For a directory structure like this:
your_repo/
- file1
- dir1/
- file_in_dir_1
- file_in_dir_2
- dir2/
- file_in_dir_3
The output expected is:
dir1
dir2
file1
Notes
- In a tree object file, the SHA-1 hashes are not in hexadecimal format. They’re just raw bytes (20 bytes long).
- In a tree object file, entries are sorted by their name. The output of
ls-treematches this order.
5. Write a tree object
In this stage, you’ll implement writing a tree to the .git/objects directory.
Tree Object Storage (recap)
As a recap, tree objects store directory structures and are saved in the .git/objects directory.
For example, if a tree object has the hash e88f7a929cd70b0274c4ea33b209c97fa845fdbc, its path will be .git/objects/e8/8f7a929cd70b0274c4ea33b209c97fa845fdbc.
Here’s what a tree object file looks like (before Zlib compression):
tree <size>\0
<mode> <name>\0<20_byte_sha>
<mode> <name>\0<20_byte_sha>
(The code block above uses newlines for readability, but the actual file does not contain newlines.)
- The file begins with
tree <size>\0. This is called the object header, and it works like the header in blob objects. - After the header, the file contains several entries. Each entry follows the format
<mode> <name>\0<sha>:<mode>shows the type and permissions of the file or directory.<name>is the name of the file or directory.\0represents a null byte.<20_byte_sha>is the 20-byte SHA-1 hash of the file or directory.
The <mode> Field
The <mode> field shows the type and permissions for each entry. Some valid values include:
100644– Regular file100755– Executable file40000– Directory (Tree object)
Note that directory mode is 40000, not 040000. Although Git commands like git ls-tree show directory modes as 040000 for readability, the actual mode stored in the tree object is 40000.
You can read more about the internal format of a tree object here.
The git write-tree Command
The git write-tree command creates a tree object from the current state of the “staging area”. The staging area is a place where changes go when you run git add.
For this challenge, you will not implement a staging area. Instead, assume that all files in the working directory are already staged.
Here’s an example of using git write-tree:
# Create a file with some content
$ echo "hello world" > test.txt
# Add the file to the staging area (we won't implement a staging area in this challenge)
$ git add test.txt
# Write the tree to .git/objects
$ git write-tree
4b825dc642cb6eb9a060e54bf8d69288fbee4904
The output of git write-tree is the 40-character SHA-1 hash of the tree object that was written to .git/objects.
To implement the git write-tree command, you’ll need to:
- Iterate over the files or directories in the working directory.
- If the entry is a file, create a blob object and record its SHA-1 hash.
- If the entry is a directory, recursively create a tree object and record its SHA-1 hash.
- Sort all entries alphabetically by name.
- Once you have all the entries and their SHA-1 hashes, write the tree object to the
.git/objectsdirectory.
If you’re testing this against git locally, make sure to run git add . before git write-tree, so that all files in the working directory are staged.
Tests
The tester will initialize a new Git repository using your program:
$ mkdir test_dir && cd test_dir
$ /path/to/your_program.sh init
It will then create some random files and directories:
$ echo "hello world" > test_file_1.txt
$ mkdir test_dir_1
$ echo "hello world" > test_dir_1/test_file_2.txt
$ mkdir test_dir_2
$ echo "hello world" > test_dir_2/test_file_3.txt
And then run your program like this:
$ /path/to/your_program.sh write-tree
4b825dc642cb6eb9a060e54bf8d69288fbee4904
You’re expected to write the entire working directory as a tree object and print the 40-character SHA-1 hash to stdout.
The tester will verify that the output of your program matches the SHA-1 hash of the tree object that the official git implementation would write.
Notes
- Remember to ignore the
.gitdirectory when creating entries in the tree object and sort all entries alphabetically by name. - Your implementation of
git write-treewill need to handle nested directories. A recursive implementation will help here, since you’ll need to create tree objects for each subdirectory to be able to create the parent directory’s tree object. - The
git write-treeimplementation here differs slightly from the official one. The officialgitimplementation uses the staging area to determine what to write to the tree object. We’ll just assume that all files in the working directory are staged.
6. Create a commit
In this stage, you’ll implement the git commit-tree command, which creates a commit object.
Commits
The last git object we’ll be dealing with in this challenge is the commit object.
A commit object file looks like this (before Zlib compression):
commit <size>\0tree <tree_sha>
parent <parent_sha>
author <name> <<email>> <timestamp> <timezone>
committer <name> <<email>> <timestamp> <timezone>
<commit message>
- The file starts with
commit <size>\0(same header pattern as blob and tree objects). - After the header, the content is plain text (not binary).
- Each line ends with a newline character (
\n). - The tree and parent SHAs are in hexadecimal format (40 characters), unlike with tree objects, where they’re
20bytes. - There’s a blank line between the metadata and the commit message.
- The timestamp format is:
<seconds_since_epoch> <timezone_offset>(e.g.,1234567890 +0000).
Here’s an example of a commit object:
commit 177\0tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904
parent 3b18e512dba79e4c8300dd08aeb37f8e728b8dad
author John Doe <john@example.com> 1234567890 +0000
committer John Doe <john@example.com> 1234567890 +0000
Initial commit
You can read more about the commit object format here.
The git commit-tree Command
The git commit-tree command creates a commit object.
For example:
$ mkdir test_dir && cd test_dir
$ git init
Initialized empty Git repository in /path/to/test_dir/.git/
# Create a tree, get its SHA
$ echo "hello world" > test.txt
$ git add test.txt
$ git write-tree
4b825dc642cb6eb9a060e54bf8d69288fbee4904
# Create the initial commit
$ git commit-tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904 -m "Initial commit"
3b18e512dba79e4c8300dd08aeb37f8e728b8dad
# Write some changes, get another tree SHA
$ echo "hello world 2" > test.txt
$ git add test.txt
$ git write-tree
5b825dc642cb6eb9a060e54bf8d69288fbee4904
# Create a new commit with the new tree SHA and parent
$ git commit-tree 5b825dc642cb6eb9a060e54bf8d69288fbee4904 -p 3b18e512dba79e4c8300dd08aeb37f8e728b8dad -m "Second commit"
6c18e512dba79e4c8300dd08aeb37f8e728b8dad
The output of git commit-tree is the 40-character SHA-1 hash of the commit object that was written to .git/objects.
Tests
Your program will be invoked like this:
$ ./your_program.sh commit-tree <tree_sha> -p <commit_sha> -m <message>
Your program must create a commit object and print its 40-character SHA-1 hash to stdout.
To keep things simple:
- You’ll receive exactly one parent commit.
- You’ll receive exactly one line in the message.
- You’re free to hardcode any valid name and email for the author/committer fields.
- You can use any valid timestamp (e.g., current time or a hardcoded value).
The tester will verify your changes by reading the commit object from the .git directory using the git show command.
Notes
- The author and committer lines are usually the same for simple commits. You can hardcode values like
John Doe <john@example.com> 1234567890 +0000.
7. Clone a repository
In this stage, you’ll implement cloning a public repository from GitHub.
This is the last stage of the challenge, and probably the hardest across all of CodeCrafters!
We might split this into an extension with multiple stages in the future, but for now it’s just one big stage.
We don’t have detailed instructions for this stage, so you’re all on your own here. A few pointers to get you started:
- This forum post has some suggestions on how to incrementally implement this.
- You’ll need to use Git’s Smart HTTP transfer protocol for this.
- To know more about the protocol format, we recommend reading:
For Rust
You can use the reqwest crate to make HTTP requests, we’ve included it in the Cargo.toml file.
Tests
The tester will run your program like this:
$ /path/to/your_program.sh clone https://github.com/blah/blah <some_dir>
Your program must create <some_dir> and clone the given repository into it.
To verify your changes, the tester will:
- Check the contents of a random file
- Read commit object attributes from the
.gitdirectory
Source and licence
Stage material by CodeCrafters. Source snapshot · Original licence.
MIT License Copyright (c) 2022 CodeCrafters Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.