Mini Git
A reduced version of Git
|
Represents a tree object. More...
#include <tree.hpp>
Classes | |
struct | EntryData |
Public Member Functions | |
void | add_or_update_entry (const std::string &mode, const std::string &path, const std::string &sha) |
Adds a new entry to the tree object or updates an existing entry. | |
std::vector< char > | get_data () const |
Returns the tree as a vector of characters. This cannot be a string because the data contains null bytes. | |
std::map< std::string, EntryData > | get_entries () const |
std::string | write () const |
Writes the tree object to the disk. | |
Static Public Member Functions | |
static TreeObject | create_tree (const std::string &path) |
Creates a tree object from the given path. | |
static TreeObject | read (const std::string &file_path) |
Reads a tree object from the disk. | |
Represents a tree object.
Here's what a tree object file looks like (before Zlib compression). Note that this is slightly different from the actual format used by Git, as it is modified for our use case.
tree <size>
. This is the "object header", similar to what we saw with blob objects.<mode> <path>\t<sha>
.<mode>
is the mode of the file/directory<path>
is the path of the file/directory\t
is a tab character<sha>
is the 40-char SHA-1 hash of the blob/tree (in hexadecimal format) void TreeObject::add_or_update_entry | ( | const std::string & | mode, |
const std::string & | path, | ||
const std::string & | sha ) |
Adds a new entry to the tree object or updates an existing entry.
mode | The mode of the file/directory |
path | The path of the file/directory. Will be used as a key to look up the entry |
sha | The SHA-1 hash of the file/directory (in hexadecimal format) |
|
static |
Creates a tree object from the given path.
path | The path to the directory whose tree object is to be created |
std::vector< char > TreeObject::get_data | ( | ) | const |
Returns the tree as a vector of characters. This cannot be a string because the data contains null bytes.
std::map< std::string, TreeObject::EntryData > TreeObject::get_entries | ( | ) | const |
|
static |
Reads a tree object from the disk.
std::string TreeObject::write | ( | ) | const |
Writes the tree object to the disk.