Mini Git
A reduced version of Git
Loading...
Searching...
No Matches
TreeObject Class Reference

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, EntryDataget_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.
 

Detailed Description

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>\0
<mode> <path>\t<sha>
<mode> <path>\t<sha>
  • The file starts with tree <size>. 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> <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)

Member Function Documentation

◆ add_or_update_entry()

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.

Parameters
modeThe mode of the file/directory
pathThe path of the file/directory. Will be used as a key to look up the entry
shaThe SHA-1 hash of the file/directory (in hexadecimal format)

◆ create_tree()

TreeObject TreeObject::create_tree ( const std::string & path)
static

Creates a tree object from the given path.

Parameters
pathThe path to the directory whose tree object is to be created
Returns
The tree object

◆ get_data()

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.

◆ get_entries()

std::map< std::string, TreeObject::EntryData > TreeObject::get_entries ( ) const

◆ read()

TreeObject TreeObject::read ( const std::string & file_path)
static

Reads a tree object from the disk.

◆ write()

std::string TreeObject::write ( ) const

Writes the tree object to the disk.


The documentation for this class was generated from the following files: