Creation of new branch, starting to work on revert
This commit is contained in:
81
src/fs.c
81
src/fs.c
@@ -14,6 +14,7 @@
|
||||
#include "tree.h"
|
||||
#include "objects.h"
|
||||
#include "utils.h"
|
||||
#include "commit.h"
|
||||
|
||||
int local_repo_exist()
|
||||
{
|
||||
@@ -308,7 +309,7 @@ int save_index(struct tree *tree)
|
||||
free_object(&object);
|
||||
}
|
||||
|
||||
int get_last_commit(struct object *commit)
|
||||
int get_head_commit_checksum(char* checksum)
|
||||
{
|
||||
size_t head_size = 0;
|
||||
if(!local_repo_exist() || !head_file_exist(&head_size) || !heads_dir_exist)
|
||||
@@ -327,18 +328,25 @@ int get_last_commit(struct object *commit)
|
||||
struct stat buffer = {0};
|
||||
if (stat(head_path, &buffer) != 0) return 0;
|
||||
|
||||
char commit_checksum[buffer.st_size + 1];
|
||||
memset(commit_checksum, 0, buffer.st_size + 1);
|
||||
memset(checksum, 0, buffer.st_size + 1);
|
||||
head_file = fopen(head_path, "r");
|
||||
fread(commit_checksum, buffer.st_size, 1, head_file);
|
||||
fread(checksum, buffer.st_size, 1, head_file);
|
||||
fclose(head_file);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int get_last_commit(struct object *commit)
|
||||
{
|
||||
char commit_checksum[DIGEST_LENGTH * 2 + 1];
|
||||
get_head_commit_checksum(commit_checksum);
|
||||
|
||||
int res = read_object(commit_checksum, commit);
|
||||
if (res != 0) return FS_ERROR;
|
||||
|
||||
if (commit->object_type != COMMIT) return WRONG_OBJECT_TYPE;
|
||||
|
||||
return 0;
|
||||
return FS_OK;
|
||||
}
|
||||
|
||||
int update_head(char *new_head)
|
||||
@@ -369,3 +377,66 @@ int update_head(char *new_head)
|
||||
fclose(file);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int branch_exist(char *branch)
|
||||
{
|
||||
if(!local_repo_exist() || !heads_dir_exist())
|
||||
return REPO_NOT_INITIALIZED;
|
||||
|
||||
DIR *dir;
|
||||
struct dirent *ent;
|
||||
if ((dir = opendir(HEADS_DIR)) != 0)
|
||||
{
|
||||
while((ent = readdir(dir)) != NULL)
|
||||
{
|
||||
if (strcmp(ent->d_name, branch) == 0)
|
||||
return 1;
|
||||
}
|
||||
closedir(dir);
|
||||
} else
|
||||
return FS_ERROR;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int new_branch(char* branch_name)
|
||||
{
|
||||
if(branch_exist(branch_name))
|
||||
return BRANCH_ALREADY_EXIST;
|
||||
|
||||
char path[strlen(HEADS_DIR) + strlen(branch_name) + 2];
|
||||
sprintf(path, "%s/%s", HEADS_DIR, branch_name);
|
||||
|
||||
char old_head[DIGEST_LENGTH * 2 + 1];
|
||||
get_head_commit_checksum(old_head);
|
||||
|
||||
FILE *branch_head = fopen(path, "w");
|
||||
fprintf(branch_head, "%s", old_head);
|
||||
fclose(branch_head);
|
||||
|
||||
FILE *head_file = fopen(HEAD_FILE, "w");
|
||||
fprintf(head_file, "%s", path);
|
||||
fclose(head_file);
|
||||
|
||||
return FS_OK;
|
||||
}
|
||||
|
||||
int restore_tree();
|
||||
|
||||
int revert(char* commit_checksum)
|
||||
{
|
||||
struct object commit_obj = {0};
|
||||
read_object(commit_checksum, &commit_obj);
|
||||
|
||||
if(commit_obj.object_type != COMMIT)
|
||||
return WRONG_OBJECT_TYPE;
|
||||
|
||||
struct commit commit = {0};
|
||||
commit_from_object(&commit, &commit_obj);
|
||||
|
||||
|
||||
|
||||
free_commit(&commit);
|
||||
free_object(&commit_obj);
|
||||
return FS_OK;
|
||||
}
|
||||
2
src/fs.h
2
src/fs.h
@@ -22,6 +22,7 @@
|
||||
#define OBJECT_ALREADY_EXIST (-20)
|
||||
#define OBJECT_DOES_NOT_EXIST (-21)
|
||||
#define WRONG_OBJECT_TYPE (-22)
|
||||
#define BRANCH_ALREADY_EXIST (-23)
|
||||
#define FILE_NOT_FOUND (-30)
|
||||
#define ENTRY_NOT_FOUND (-31)
|
||||
|
||||
@@ -39,5 +40,6 @@ int update_head(char *new_head);
|
||||
int get_last_commit(struct object *commit);
|
||||
int tmp_dump(struct object *obj, char* filename);
|
||||
int init_tmp_diff_dir(char* dir);
|
||||
int new_branch(char* branch_name);
|
||||
|
||||
#endif // FS_H
|
||||
|
||||
18
src/main.c
18
src/main.c
@@ -17,21 +17,9 @@
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
struct object a = {0}, b = {0};
|
||||
read_object("5568f9722af893d2e6214617eefb62f40c9f8c69", &a);
|
||||
read_object("4640f65f9340784086a5ddee4e1a46cdc3274424", &b);
|
||||
|
||||
struct commit c_a = {0}, c_b = {0};
|
||||
commit_from_object(&c_a, &a);
|
||||
commit_from_object(&c_b, &b);
|
||||
|
||||
diff_commit(&c_a, &c_b);
|
||||
|
||||
free_commit(&c_a);
|
||||
free_commit(&c_b);
|
||||
free_object(&a);
|
||||
free_object(&b);
|
||||
|
||||
// debug_print("master: %i", branch_exist("master"));
|
||||
// debug_print("main: %i", branch_exist("main"));
|
||||
// struct tree index = {0};
|
||||
// load_index(&index);
|
||||
|
||||
@@ -42,5 +30,7 @@ int main(int argc, char** argv)
|
||||
|
||||
// commit();
|
||||
|
||||
new_branch("new_feature");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user