fix(utilities): made save diff accept a changes struct to count deletions and insertions
This commit is contained in:
parent
550b47d568
commit
8831b267e5
2 changed files with 21 additions and 9 deletions
|
|
@ -38,6 +38,11 @@ typedef struct {
|
|||
typedef List StringBuffer;
|
||||
typedef List FileInfoBuffer;
|
||||
|
||||
typedef struct {
|
||||
uint32_t insertions;
|
||||
uint32_t deletions;
|
||||
} Changes;
|
||||
|
||||
List* list_new(size_t);
|
||||
int list_push(List*, void*);
|
||||
int list_remove(List*, const void*, int (*compare)(const void*, const void*));
|
||||
|
|
@ -64,9 +69,9 @@ PathType get_path_type(const char*);
|
|||
char* get_file_content(char*);
|
||||
char* get_file_content_with_size(char*, size_t*);
|
||||
int create_default_config_file(char*);
|
||||
int save_diff(ActionList*, char*, char*, size_t, char*, File*, int, char*);
|
||||
int save_diff(ActionList*, char*, char*, size_t, char*, File*, int);
|
||||
int read_diff(char*, char*, size_t, char*, ActionList*);
|
||||
int save_file_diff(char*, char*, size_t, char*, char*);
|
||||
int save_file_diff(char*, char*, size_t, char*, Changes*);
|
||||
File* apply_diff(File*, ActionList*);
|
||||
void sort_action_list(ActionList*);
|
||||
|
||||
|
|
|
|||
|
|
@ -591,7 +591,7 @@ int create_default_config_file(char* config_path) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
int save_diff(ActionList* diff, char* path, char* root, size_t diff_id, char* basefile_hash, File* modified_file, int tree, char* hash) {
|
||||
int save_diff(ActionList* diff, char* path, char* root, size_t diff_id, char* basefile_hash, File* modified_file, int tree) {
|
||||
size_t buffer_size = 41;
|
||||
|
||||
buffer_size += 1 + snprintf(NULL, 0, "%d", diff->len);
|
||||
|
|
@ -645,6 +645,7 @@ int save_diff(ActionList* diff, char* path, char* root, size_t diff_id, char* ba
|
|||
|
||||
char id[2+snprintf(NULL, 0, "%d", diff_id)+strlen(path)];
|
||||
snprintf(id, sizeof(id), "%s %d", path, diff_id);
|
||||
char hash[41];
|
||||
if (tree) object_hash(TreeDiffObject, id, hash);
|
||||
else object_hash(FileDiffObject, id, hash);
|
||||
|
||||
|
|
@ -664,9 +665,6 @@ int save_diff(ActionList* diff, char* path, char* root, size_t diff_id, char* ba
|
|||
return 0;
|
||||
}
|
||||
|
||||
// Check if the file already exists
|
||||
|
||||
|
||||
// Ensure the directory structure exists
|
||||
if (mkdir_recursive(dir_path, 0755) < 0 && errno != EEXIST) {
|
||||
perror("ERROR: failed to create directory structure in save_diff!");
|
||||
|
|
@ -1012,7 +1010,7 @@ int read_diff(char* path, char* root, size_t diff_id, char* basefile_hash, Actio
|
|||
}
|
||||
|
||||
|
||||
int save_file_diff(char* path, char* root, size_t diff_id, char* basefile_hash, char* hash) {
|
||||
int save_file_diff(char* path, char* root, size_t diff_id, char* basefile_hash, Changes* changes) {
|
||||
char basefile_location[2+strlen(root)+strlen("/.merk/objects/")+strlen(basefile_hash)];
|
||||
snprintf(basefile_location, sizeof(basefile_location), "%s/.merk/objects/%.2s/%s", root, basefile_hash, basefile_hash+2);
|
||||
|
||||
|
|
@ -1078,12 +1076,21 @@ int save_file_diff(char* path, char* root, size_t diff_id, char* basefile_hash,
|
|||
|
||||
ActionList* diff = myers_diff(basefile, modified_file, 0, 0);
|
||||
if (diff->len == 0) {
|
||||
hash = NULL;
|
||||
free(basefile_content);
|
||||
return 1;
|
||||
}
|
||||
|
||||
save_diff(diff, path, root, diff_id, basefile_hash, modified_file, 0, hash);
|
||||
|
||||
for (size_t idx = 0; idx < diff->len; idx++) {
|
||||
Action action = diff->actions[idx];
|
||||
if (action.type == INSERT) {
|
||||
changes->insertions += 1;
|
||||
} else if (action.type == DELETE) {
|
||||
changes->deletions += 1;
|
||||
}
|
||||
}
|
||||
|
||||
save_diff(diff, path, root, diff_id, basefile_hash, modified_file, 0);
|
||||
|
||||
free(basefile_content);
|
||||
return 1;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue