Mini Git
A reduced version of Git
Loading...
Searching...
No Matches
hash_object.cpp File Reference
#include "blob.hpp"
#include <filesystem>
#include <format>
#include <fstream>
#include <iostream>
#include "sha.hpp"
#include <string>
#include <unistd.h>
#include <zstr.hpp>
Include dependency graph for hash_object.cpp:

Functions

int store_file_as_blob (const std::string &path, const std::string &sha1)
 Stores a file as a blob object in the repository.
 
int hash_object (int argc, char *argv[])
 

Variables

int opterr
 
int optind
 
int optopt
 
char * optarg
 

Function Documentation

◆ hash_object()

int hash_object ( int argc,
char * argv[] )

◆ store_file_as_blob()

int store_file_as_blob ( const std::string & path,
const std::string & sha1 )

Stores a file as a blob object in the repository.

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)
  • \0 is 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
Parameters
pathPath to the file
sha1Precomputed SHA-1 hash of the file contents (to avoid recomputing it)
Returns
EXIT_SUCCESS on success, EXIT_FAILURE on failure

Variable Documentation

◆ optarg

char* optarg
extern

◆ opterr

int opterr
extern

◆ optind

int optind
extern

◆ optopt

int optopt
extern