Change tree encoding to be compatible with git; Add cat-file and show-index command
This commit is contained in:
107
src/objects.c
107
src/objects.c
@@ -5,29 +5,33 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <zlib.h>
|
||||
|
||||
#include "includes.h"
|
||||
#include "fs.h"
|
||||
#include "tree.h"
|
||||
#include "utils.h"
|
||||
#include "objects.h"
|
||||
|
||||
char *object_type_str[3] = {
|
||||
"blob",
|
||||
"blob",
|
||||
"tree",
|
||||
"commit",
|
||||
};
|
||||
|
||||
char* object_type_to_str(enum object_type type)
|
||||
char *object_type_to_str(enum object_type type)
|
||||
{
|
||||
return object_type_str[type];
|
||||
}
|
||||
|
||||
enum object_type str_to_object_type(char* str)
|
||||
enum object_type str_to_object_type(char *str)
|
||||
{
|
||||
if(strncmp("tree", str, 4) == 0)
|
||||
if (strncmp("tree", str, 4) == 0)
|
||||
{
|
||||
return TREE;
|
||||
} else if (strncmp("commit", str, 5) == 0)
|
||||
}
|
||||
else if (strncmp("commit", str, 5) == 0)
|
||||
{
|
||||
return COMMIT;
|
||||
}
|
||||
@@ -47,7 +51,7 @@ size_t object_size(struct object *obj)
|
||||
|
||||
int full_object(struct object *obj, char *buffer, size_t buffer_size)
|
||||
{
|
||||
char* hr_size = malloc(decimal_len(obj->size) + 1);
|
||||
char *hr_size = malloc(decimal_len(obj->size) + 1);
|
||||
sprintf(hr_size, "%li", obj->size);
|
||||
|
||||
size_t data_size = object_size(obj);
|
||||
@@ -58,11 +62,11 @@ int full_object(struct object *obj, char *buffer, size_t buffer_size)
|
||||
memcpy(writing, type, strlen(type));
|
||||
writing += strlen(type);
|
||||
memcpy(writing, " ", 1);
|
||||
writing ++;
|
||||
writing++;
|
||||
memcpy(writing, hr_size, strlen(hr_size));
|
||||
writing += strlen(hr_size);
|
||||
memcpy(writing, "\0", 1);
|
||||
writing ++;
|
||||
writing++;
|
||||
memcpy(writing, obj->content, obj->size);
|
||||
|
||||
free(hr_size);
|
||||
@@ -70,29 +74,45 @@ int full_object(struct object *obj, char *buffer, size_t buffer_size)
|
||||
return 0;
|
||||
}
|
||||
|
||||
void hash_object(struct object *obj, char *result)
|
||||
/// @brief Hash object and copy it in result
|
||||
/// @param obj
|
||||
/// @param result char array of size DIGEST_LENGTH, it is not a C-string.
|
||||
void hash_object(object_t *obj, unsigned char *result)
|
||||
{
|
||||
unsigned char md_buffer[DIGEST_LENGTH] = {0};
|
||||
size_t data_size = object_size(obj);
|
||||
char* data = malloc(data_size);
|
||||
char data[data_size];
|
||||
full_object(obj, data, data_size);
|
||||
|
||||
SHA1(data, data_size, md_buffer);
|
||||
|
||||
for (int i = 0; i < DIGEST_LENGTH; i++)
|
||||
{
|
||||
sprintf((result + (2 * i)), "%.2x", md_buffer[i]);
|
||||
}
|
||||
|
||||
free(data);
|
||||
SHA1(data, data_size, result);
|
||||
return;
|
||||
}
|
||||
|
||||
int uncompress_object(struct object *obj, char* compressed, uLongf comp_size)
|
||||
void hash_to_hexa(unsigned char *hash, char *result)
|
||||
{
|
||||
for (int i = 0; i < DIGEST_LENGTH; i ++)
|
||||
sprintf((result + (2 * i)), "%.2x", hash[i]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/// @brief Hash object and copy its hexa representation in result
|
||||
/// @param obj
|
||||
/// @param result A C-string of length equals DIGEST_LENGTH * 2
|
||||
void hash_object_str(object_t *obj, char *result)
|
||||
{
|
||||
unsigned char md_buffer[DIGEST_LENGTH] = {0};
|
||||
hash_object(obj, md_buffer);
|
||||
|
||||
hash_to_hexa(md_buffer, result);
|
||||
return;
|
||||
}
|
||||
|
||||
int uncompress_object(struct object *obj, char *compressed, uLongf comp_size)
|
||||
{
|
||||
uLongf def_size = HEADER_MAX_SIZE;
|
||||
uLongf content_size = 0;
|
||||
char* deflated = malloc(def_size);
|
||||
int res = uncompress((Bytef *) deflated, &def_size, (Bytef *)compressed, comp_size);
|
||||
char *deflated = malloc(def_size);
|
||||
int res = uncompress((Bytef *)deflated, &def_size, (Bytef *)compressed, comp_size);
|
||||
if (res != Z_OK && res != Z_BUF_ERROR)
|
||||
{
|
||||
return res;
|
||||
@@ -104,8 +124,9 @@ int uncompress_object(struct object *obj, char* compressed, uLongf comp_size)
|
||||
content_size = strtol(hr_size, NULL, 10);
|
||||
def_size += content_size;
|
||||
deflated = realloc(deflated, def_size);
|
||||
res = uncompress((Bytef *) deflated, &def_size, (Bytef *)compressed, comp_size);
|
||||
if(res != Z_OK) {
|
||||
res = uncompress((Bytef *)deflated, &def_size, (Bytef *)compressed, comp_size);
|
||||
if (res != Z_OK)
|
||||
{
|
||||
return res;
|
||||
}
|
||||
char *content_type_tmp = strtok(deflated, " ");
|
||||
@@ -119,10 +140,10 @@ int uncompress_object(struct object *obj, char* compressed, uLongf comp_size)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int compress_object(struct object *obj, char* compressed, uLongf *comp_size)
|
||||
int compress_object(struct object *obj, char *compressed, uLongf *comp_size)
|
||||
{
|
||||
size_t data_size = object_size(obj);
|
||||
char* data = malloc(data_size);
|
||||
char *data = malloc(data_size);
|
||||
full_object(obj, data, data_size);
|
||||
|
||||
int res = compress((Bytef *)compressed, comp_size, (Bytef *)data, data_size);
|
||||
@@ -131,6 +152,40 @@ int compress_object(struct object *obj, char* compressed, uLongf *comp_size)
|
||||
return res;
|
||||
}
|
||||
|
||||
int cat_object(int fd, object_t *obj)
|
||||
{
|
||||
|
||||
switch (obj->object_type)
|
||||
{
|
||||
case BLOB:
|
||||
case COMMIT:
|
||||
write(fd, obj->content, obj->size);
|
||||
break;
|
||||
|
||||
case TREE:
|
||||
tree_t tree = {0};
|
||||
tree_from_object(&tree, obj);
|
||||
|
||||
entry_t *current = tree.first_entry;
|
||||
while(current != NULL)
|
||||
{
|
||||
char buf[DIGEST_LENGTH * 2 + 1];
|
||||
hash_to_hexa(current->checksum, buf);
|
||||
dprintf(fd, "%.6o %s %s %s\n", current->mode, object_type_to_str(current->type), buf, current->filename);
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
free_tree(&tree);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
free_object;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void free_object(struct object *obj)
|
||||
{
|
||||
if (obj->content != NULL)
|
||||
|
||||
Reference in New Issue
Block a user