diff --git a/src/fs.c b/src/fs.c index 325a1bf..ab5257d 100644 --- a/src/fs.c +++ b/src/fs.c @@ -37,10 +37,49 @@ int head_file_exist(size_t *head_size) { struct stat buffer; int result = stat(HEAD_FILE, &buffer) == 0; - *head_size = buffer.st_size; + if(head_size != NULL) + *head_size = buffer.st_size; return result; } +int init_repo() +{ + if(local_repo_exist()) + { + return 0; + } + + mkdir(LOCAL_REPO, DEFAULT_DIR_MODE); + + struct stat buffer = {0}; + if (stat(OBJECTS_DIR, &buffer) != 0) + mkdir(OBJECTS_DIR, DEFAULT_DIR_MODE); + + if (!index_exist()) + { + int fd = open(INDEX_FILE, O_WRONLY | O_CREAT | O_TRUNC, 0644); + FILE *index = fdopen(fd, "w"); + + fprintf(index, "0\n"); + fclose(index); + } + + if (!head_file_exist(NULL)) + { + open(HEAD_FILE, O_CREAT, 0644); + } + + if (stat(REFS_DIR, &buffer) != 0) + { + mkdir(REFS_DIR, DEFAULT_DIR_MODE); + } + + if (!heads_dir_exist()) + { + mkdir(HEADS_DIR, DEFAULT_DIR_MODE); + } +} + int blob_from_file(char *filename, struct object *object) { FILE* file = fopen(filename, "r"); diff --git a/src/fs.h b/src/fs.h index f170f11..2fb6508 100644 --- a/src/fs.h +++ b/src/fs.h @@ -25,6 +25,7 @@ int local_repo_exist(); int index_exist(); +int init_repo(); int blob_from_file(char *filename, struct object *object); int write_object(struct object *obj); int read_object(char *checksum, struct object *obj); diff --git a/src/main.c b/src/main.c index 8230ccb..8177a9d 100644 --- a/src/main.c +++ b/src/main.c @@ -6,32 +6,11 @@ #include -#include "commit.h" +#include "fs.h" int main(int argc, char** argv) { - // struct object obj = {0}; - // obj.content = "Hello, world!\n"; - // obj.size = strlen(obj.content); - // obj.object_type = "blob"; - // return write_object(&obj); - - // read_object("af5626b4a114abcb82d63db7c8082c3c4756e51b", &obj); - - // debug_print("Object type is \"%s\"", obj.object_type); - // debug_print("Content size is %li", obj.size); - // debug_print("Content is %.*s", (int)obj.size, obj.content); - - // free_object(&obj); - - // add_to_index(&index, "src/fs.c"); - // add_to_index(&index, "src/test/foo"); - // add_to_index(&index, ".gitignore"); - // add_to_index(&index, "src/fs.h"); - - // dump_index(&tree); - - commit(); + return 0; } \ No newline at end of file