Color when diff
This commit is contained in:
25
src/commit.c
25
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);
|
||||
|
||||
@@ -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
|
||||
52
src/fs.c
52
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;
|
||||
}
|
||||
2
src/fs.h
2
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);
|
||||
|
||||
Reference in New Issue
Block a user