Branch checkout

This commit is contained in:
womax
2024-06-03 15:16:48 +02:00
parent 167e369004
commit 6bf58adf0d
3 changed files with 42 additions and 4 deletions

View File

@@ -137,7 +137,7 @@ int commit()
char commit_checksum[DIGEST_LENGTH * 2 + 1]; char commit_checksum[DIGEST_LENGTH * 2 + 1];
hash_object(&commit_obj, commit_checksum); hash_object(&commit_obj, commit_checksum);
update_head(commit_checksum); update_current_branch_head(commit_checksum);
free_commit(&commit); free_commit(&commit);
free_object(&commit_obj); 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"/a", &tree_a);
dump_tree(TMP"/b", &tree_b); 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); pclose(f);
free_tree(&tree_a); free_tree(&tree_a);

View File

@@ -393,7 +393,7 @@ int get_last_commit(struct object *commit)
return FS_OK; return FS_OK;
} }
int update_head(char *new_head) int update_current_branch_head(char *new_head)
{ {
size_t head_size = 0; size_t head_size = 0;
if(!local_repo_exist() || !head_file_exist(&head_size) || !heads_dir_exist) 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; 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) int revert(char* commit_checksum)
{ {
struct object obj = {0}; struct object obj = {0};

View File

@@ -23,6 +23,7 @@
#define OBJECT_DOES_NOT_EXIST (-21) #define OBJECT_DOES_NOT_EXIST (-21)
#define WRONG_OBJECT_TYPE (-22) #define WRONG_OBJECT_TYPE (-22)
#define BRANCH_ALREADY_EXIST (-23) #define BRANCH_ALREADY_EXIST (-23)
#define BRANCH_DOES_NOT_EXIST (-24)
#define FILE_NOT_FOUND (-30) #define FILE_NOT_FOUND (-30)
#define ENTRY_NOT_FOUND (-31) #define ENTRY_NOT_FOUND (-31)
@@ -36,7 +37,7 @@ int read_object(char *checksum, struct object *obj);
int save_index(struct tree *tree); int save_index(struct tree *tree);
int load_index(struct tree *index); int load_index(struct tree *index);
int load_tree(char* checksum, struct tree *tree); 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 get_last_commit(struct object *commit);
int tmp_dump(struct object *obj, char* filename); int tmp_dump(struct object *obj, char* filename);
int init_tmp_diff_dir(char* dir); int init_tmp_diff_dir(char* dir);