Color when diff

This commit is contained in:
womax
2024-06-03 15:36:46 +02:00
parent 6bf58adf0d
commit a5bbd3f86d
4 changed files with 41 additions and 43 deletions

View File

@@ -149,7 +149,7 @@ int commit()
save_index(&index); 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}; struct object obj = {0};
read_object(commit->tree, &obj); read_object(commit->tree, &obj);
@@ -162,22 +162,25 @@ void diff_commit_with_working_tree(struct commit *commit)
dump_tree(TMP"/a", &commit_tree); 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); pclose(p);
free_tree(&commit_tree); free_tree(&commit_tree);
free_object(&obj); free_object(&obj);
} }
void revert_to(struct commit *commit) void diff_commit(struct commit *commit_a, struct commit *commit_b, int for_print)
{
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)
{ {
struct object tree_obj_a, tree_obj_b; struct object tree_obj_a, tree_obj_b;
read_object(commit_a->tree, &tree_obj_a); read_object(commit_a->tree, &tree_obj_a);

View File

@@ -5,9 +5,8 @@
int commit_from_object(struct commit *commit, struct object *object); int commit_from_object(struct commit *commit, struct object *object);
void free_commit(struct commit *commit); void free_commit(struct commit *commit);
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);
void diff_commit_with_working_tree(struct commit *commit); void diff_commit_with_working_tree(struct commit *commit, int for_print);
void revert_to(struct commit *commit);
int commit(); int commit();
#endif // COMMIT_H #endif // COMMIT_H

View File

@@ -465,6 +465,27 @@ int new_branch(char* branch_name)
return FS_OK; 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) int checkout_branch(char *branch)
{ {
if(!branch_exist(branch)) if(!branch_exist(branch))
@@ -480,16 +501,7 @@ int checkout_branch(char *branch)
fread(commit_checksum, DIGEST_LENGTH * 2 + 1, 1, branch_head); fread(commit_checksum, DIGEST_LENGTH * 2 + 1, 1, branch_head);
fclose(branch_head); fclose(branch_head);
struct object obj = {0}; revert_to(commit_checksum);
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"); FILE *head_file = fopen(HEAD_FILE, "w");
fwrite(branch_path, DIGEST_LENGTH * 2 + 1, 1, head_file); fwrite(branch_path, DIGEST_LENGTH * 2 + 1, 1, head_file);
@@ -498,24 +510,6 @@ int checkout_branch(char *branch)
void print_diff() 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); 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;
} }

View File

@@ -42,6 +42,8 @@ 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);
int new_branch(char* branch_name); int new_branch(char* branch_name);
void print_diff();
int revert_to(char* commit_checksum);
int create_dir(char *dir); int create_dir(char *dir);
int dump_tree(char *cwd, struct tree *tree); int dump_tree(char *cwd, struct tree *tree);