From 6bf58adf0de9d1e4550f0ee9b998ed5e7d9b239b Mon Sep 17 00:00:00 2001 From: womax Date: Mon, 3 Jun 2024 15:16:48 +0200 Subject: [PATCH] Branch checkout --- src/commit.c | 4 ++-- src/fs.c | 39 ++++++++++++++++++++++++++++++++++++++- src/fs.h | 3 ++- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/commit.c b/src/commit.c index 8f02a78..cf73740 100644 --- a/src/commit.c +++ b/src/commit.c @@ -137,7 +137,7 @@ int commit() char commit_checksum[DIGEST_LENGTH * 2 + 1]; hash_object(&commit_obj, commit_checksum); - update_head(commit_checksum); + update_current_branch_head(commit_checksum); free_commit(&commit); free_object(&commit_obj); @@ -196,7 +196,7 @@ void diff_commit(struct commit *commit_a, struct commit *commit_b) dump_tree(TMP"/a", &tree_a); dump_tree(TMP"/b", &tree_b); - FILE *f = popen("diff -ruN "TMP"/a "TMP"/b | less", "w"); + FILE *f = popen("diff -ruN "TMP"/a "TMP"/b > "LOCAL_REPO"/last.diff", "w"); pclose(f); free_tree(&tree_a); diff --git a/src/fs.c b/src/fs.c index 6309705..b8f2285 100644 --- a/src/fs.c +++ b/src/fs.c @@ -393,7 +393,7 @@ int get_last_commit(struct object *commit) return FS_OK; } -int update_head(char *new_head) +int update_current_branch_head(char *new_head) { size_t head_size = 0; if(!local_repo_exist() || !head_file_exist(&head_size) || !heads_dir_exist) @@ -465,6 +465,43 @@ int new_branch(char* branch_name) return FS_OK; } +int checkout_branch(char *branch) +{ + if(!branch_exist(branch)) + { + return BRANCH_DOES_NOT_EXIST; + } + + char branch_path[strlen(HEADS_DIR) + strlen(branch) + 2]; + sprintf(branch_path, "%s/%s", HEADS_DIR, branch); + + char commit_checksum[DIGEST_LENGTH * 2 + 1]; + FILE *branch_head = fopen(branch_path, "r"); + fread(commit_checksum, DIGEST_LENGTH * 2 + 1, 1, branch_head); + fclose(branch_head); + + struct object obj = {0}; + read_object(commit_checksum, &obj); + + struct commit commit = {0}; + commit_from_object(&commit, &obj); + + revert_to(&commit); + + free_commit(&commit); + free_object(&obj); + + FILE *head_file = fopen(HEAD_FILE, "w"); + fwrite(branch_path, DIGEST_LENGTH * 2 + 1, 1, head_file); + fclose(head_file); +} + +void print_diff() +{ + FILE *p = popen("cat "LOCAL_REPO"/last.diff | less", "w"); + pclose(p); +} + int revert(char* commit_checksum) { struct object obj = {0}; diff --git a/src/fs.h b/src/fs.h index efa65cb..ceaf3eb 100644 --- a/src/fs.h +++ b/src/fs.h @@ -23,6 +23,7 @@ #define OBJECT_DOES_NOT_EXIST (-21) #define WRONG_OBJECT_TYPE (-22) #define BRANCH_ALREADY_EXIST (-23) +#define BRANCH_DOES_NOT_EXIST (-24) #define FILE_NOT_FOUND (-30) #define ENTRY_NOT_FOUND (-31) @@ -36,7 +37,7 @@ int read_object(char *checksum, struct object *obj); int save_index(struct tree *tree); int load_index(struct tree *index); int load_tree(char* checksum, struct tree *tree); -int update_head(char *new_head); +int update_current_branch_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);