diff --git a/src/commit.c b/src/commit.c index cf73740..75df928 100644 --- a/src/commit.c +++ b/src/commit.c @@ -149,7 +149,7 @@ int commit() save_index(&index); } -void diff_commit_with_working_tree(struct commit *commit) +void diff_commit_with_working_tree(struct commit *commit, int for_print) { struct object obj = {0}; read_object(commit->tree, &obj); @@ -162,22 +162,25 @@ void diff_commit_with_working_tree(struct commit *commit) dump_tree(TMP"/a", &commit_tree); - FILE *p = popen("diff -ru --exclude-from=.gitignore "TMP"/a ./ > "LOCAL_REPO"/last.diff", "w"); +#define DIFF_WT_BASE_CMD "diff -ru --exclude-from=.gitignore --color=%s "TMP"/a ./ > "LOCAL_REPO"/last.diff" + + char cmd[strlen(DIFF_WT_BASE_CMD) + 7]; + if (for_print) + { + sprintf(cmd, DIFF_WT_BASE_CMD, "always"); + } else + { + sprintf(cmd, DIFF_WT_BASE_CMD, "never"); + } + + FILE *p = popen(cmd, "w"); pclose(p); free_tree(&commit_tree); free_object(&obj); } -void revert_to(struct commit *commit) -{ - diff_commit_with_working_tree(commit); - - FILE *p = popen("patch -p0 -R < "LOCAL_REPO"/last.diff", "r"); - pclose(p); -} - -void diff_commit(struct commit *commit_a, struct commit *commit_b) +void diff_commit(struct commit *commit_a, struct commit *commit_b, int for_print) { struct object tree_obj_a, tree_obj_b; read_object(commit_a->tree, &tree_obj_a); diff --git a/src/commit.h b/src/commit.h index 3bc1e69..910674d 100644 --- a/src/commit.h +++ b/src/commit.h @@ -5,9 +5,8 @@ int commit_from_object(struct commit *commit, struct object *object); void free_commit(struct commit *commit); -void diff_commit(struct commit *commit_a, struct commit *commit_b); -void diff_commit_with_working_tree(struct commit *commit); -void revert_to(struct commit *commit); +void diff_commit(struct commit *commit_a, struct commit *commit_b, int for_print); +void diff_commit_with_working_tree(struct commit *commit, int for_print); int commit(); #endif // COMMIT_H \ No newline at end of file diff --git a/src/fs.c b/src/fs.c index b8f2285..01bfcde 100644 --- a/src/fs.c +++ b/src/fs.c @@ -465,6 +465,27 @@ int new_branch(char* branch_name) return FS_OK; } +int revert_to(char* commit_checksum) +{ + struct object obj = {0}; + read_object(commit_checksum, &obj); + + if(obj.object_type != COMMIT) + return WRONG_OBJECT_TYPE; + + struct commit commit = {0}; + commit_from_object(&commit, &obj); + + diff_commit_with_working_tree(&commit, 0); + + FILE *p = popen("patch -p0 -R < "LOCAL_REPO"/last.diff > /dev/null", "w"); + pclose(p); + + free_commit(&commit); + free_object(&obj); + return FS_OK; +} + int checkout_branch(char *branch) { if(!branch_exist(branch)) @@ -480,16 +501,7 @@ int checkout_branch(char *branch) 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); + revert_to(commit_checksum); FILE *head_file = fopen(HEAD_FILE, "w"); fwrite(branch_path, DIGEST_LENGTH * 2 + 1, 1, head_file); @@ -498,24 +510,6 @@ int checkout_branch(char *branch) void print_diff() { - FILE *p = popen("cat "LOCAL_REPO"/last.diff | less", "w"); + FILE *p = popen("cat "LOCAL_REPO"/last.diff | less -R", "w"); pclose(p); -} - -int revert(char* commit_checksum) -{ - struct object obj = {0}; - read_object(commit_checksum, &obj); - - if(obj.object_type != COMMIT) - return WRONG_OBJECT_TYPE; - - struct commit commit = {0}; - commit_from_object(&commit, &obj); - - diff_commit_with_working_tree(&commit); - - free_commit(&commit); - free_object(&obj); - return FS_OK; } \ No newline at end of file diff --git a/src/fs.h b/src/fs.h index ceaf3eb..78c8cc3 100644 --- a/src/fs.h +++ b/src/fs.h @@ -42,6 +42,8 @@ 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); +void print_diff(); +int revert_to(char* commit_checksum); int create_dir(char *dir); int dump_tree(char *cwd, struct tree *tree);