feat(cli): added proper display of modified files and the changes on the current branch for the status command
This commit is contained in:
parent
b1515d21c4
commit
cba5d43039
1 changed files with 51 additions and 4 deletions
55
src/main.c
55
src/main.c
|
|
@ -162,12 +162,61 @@ int main(int argc, char** argv) {
|
|||
StringBuffer* deleted_files = string_buffer_new();
|
||||
StringBuffer* untracked_files = string_buffer_new();
|
||||
|
||||
Changes* changes = calloc(1, sizeof(Changes));
|
||||
if (!changes) {
|
||||
printf("ERROR: unable to allocate memory for changes!\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
changes->insertions = 0;
|
||||
changes->deletions = 0;
|
||||
|
||||
for (size_t idx = 0; idx < files->len; idx++) {
|
||||
char* file_name = ((FileInfo*)files->items + idx)->name;
|
||||
if (base_file_buffer_search(tracked_list, file_name)) continue;
|
||||
string_buffer_push(untracked_files, file_name);
|
||||
}
|
||||
|
||||
for (size_t idx = 0; idx < files->len; idx++) {
|
||||
char* file_name = ((FileInfo*)files->items + idx)->name;
|
||||
if (base_file_buffer_search(tracked_list, file_name)) {
|
||||
BaseFileInfo* base_file = base_file_buffer_search(tracked_list, file_name);
|
||||
char base_file_hash[41];
|
||||
char id[2 + snprintf(NULL, 0, "%d", base_file->base_num) + strlen(file_name)];
|
||||
snprintf(id, sizeof(id), "%s %d", file_name, base_file->base_num);
|
||||
object_hash(BaseFileObject, id, base_file_hash);
|
||||
File* basefile = parse_object(base_file_hash, BaseFileObject, NULL, NULL);
|
||||
if (!basefile) {
|
||||
printf("ERROR: unable to parse base file %s!\n", file_name);
|
||||
return 1;
|
||||
}
|
||||
|
||||
File* modified_file = new_file(file_name);
|
||||
if (!modified_file) {
|
||||
printf("ERROR: unable to read modified file %s!\n", file_name);
|
||||
return 1;
|
||||
}
|
||||
|
||||
ActionList* diff = myers_diff(basefile, modified_file, 0, 0);
|
||||
if (!diff) {
|
||||
printf("ERROR: unable to compute diff for file %s!\n", file_name);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (diff->len != 0) {
|
||||
for (size_t idx = 0; idx < diff->len; idx++) {
|
||||
Action action = diff->actions[idx];
|
||||
if (action.type == DELETE) {
|
||||
changes->deletions++;
|
||||
} else if (action.type == INSERT) {
|
||||
changes->insertions++;
|
||||
}
|
||||
}
|
||||
string_buffer_push(modified_files, file_name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t idx = 0; idx < tracked_list->len; idx++) {
|
||||
char* file_name = ((BaseFileInfo*)tracked_list->items + idx)->name;
|
||||
if (file_info_buffer_search(files, file_name)) continue;
|
||||
|
|
@ -178,19 +227,17 @@ int main(int argc, char** argv) {
|
|||
printf("\x1b[36;1mM\x1b[0m \x1b[36m%s\x1b[0m\n", modified_files->items[idx]);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
for (size_t idx = 0; idx < deleted_files->len; idx++) {
|
||||
printf("\x1b[31;4mD\x1b[0m \x1b[31;9m%s\x1b[0m\n", deleted_files->items[idx]);
|
||||
}
|
||||
|
||||
if (deleted_files->len != 0) printf("\n");
|
||||
if (modified_files->len != 0 || deleted_files->len != 0) printf("\n");
|
||||
|
||||
for (size_t idx = 0; idx < untracked_files->len; idx++) {
|
||||
printf("\x1b[31;1mU\x1b[0m \x1b[31m%s\x1b[0m\n", untracked_files->items[idx]);
|
||||
}
|
||||
|
||||
printf("\nOn branch \x1b[39;1;4m%s\x1b[0m with %d commits\n", branch, log->len);
|
||||
printf("\n\x1b[32;1m%d+\x1b[0m \x1b[31;1m%d-\x1b[0m on branch \x1b[39;1;4m%s\x1b[0m with %d commits\n", changes->insertions, changes->deletions, branch, log->len);
|
||||
|
||||
file_info_buffer_free(files);
|
||||
free(root);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue