refactor: split up the utilities file into smaller modules
This commit is contained in:
parent
abaa6e12fc
commit
ff71a92249
13 changed files with 572 additions and 488 deletions
81
src/main.c
81
src/main.c
|
|
@ -6,14 +6,11 @@
|
|||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "action_list.h"
|
||||
#include "file.h"
|
||||
#include "myers.h"
|
||||
#include "tree.h"
|
||||
#include "base_file_buffer.h"
|
||||
#include "config.h"
|
||||
#include "commit.h"
|
||||
#include "hash.h"
|
||||
#include "object.h"
|
||||
|
||||
static void usage(int exitcode) {
|
||||
printf("usage: merk <command> [<args>]\n\
|
||||
|
|
@ -24,7 +21,7 @@ static void usage(int exitcode) {
|
|||
exit(exitcode);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int main(int argc, char** argv) {
|
||||
if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
|
||||
usage(0);
|
||||
}
|
||||
|
|
@ -247,32 +244,25 @@ int main(int argc, char **argv) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
char relative[PATH_MAX] = "";
|
||||
char* root = find_root(relative);
|
||||
if (!root) {
|
||||
free_config(&config);
|
||||
free(commit_message);
|
||||
return 1;
|
||||
}
|
||||
|
||||
char tmp_path[PATH_MAX] = ".merk/BRANCH";
|
||||
char branch_path[PATH_MAX];
|
||||
snprintf(branch_path, sizeof(branch_path), "%s/.merk/BRANCH", root);
|
||||
realpath(tmp_path, branch_path);
|
||||
|
||||
char* branch = get_file_content(branch_path);
|
||||
if (!branch) {
|
||||
free(root);
|
||||
free_config(&config);
|
||||
free(commit_message);
|
||||
return 1;
|
||||
}
|
||||
|
||||
char tmp_path2[PATH_MAX];
|
||||
char info_path[PATH_MAX];
|
||||
snprintf(info_path, sizeof(info_path), "%s/.merk/info/%s", root, branch);
|
||||
snprintf(tmp_path2, sizeof(tmp_path2), ".merk/info/%s", branch);
|
||||
realpath(tmp_path2, info_path);
|
||||
|
||||
BaseFileBuffer* base_files = base_file_buffer_new();
|
||||
if (!base_files) {
|
||||
free(branch);
|
||||
free(root);
|
||||
free_config(&config);
|
||||
free(commit_message);
|
||||
return 1;
|
||||
|
|
@ -284,7 +274,16 @@ int main(int argc, char **argv) {
|
|||
if (!files) {
|
||||
base_file_buffer_free(base_files);
|
||||
free(branch);
|
||||
free(root);
|
||||
free_config(&config);
|
||||
free(commit_message);
|
||||
return 1;
|
||||
}
|
||||
|
||||
char* root = find_root(NULL);
|
||||
if (!root) {
|
||||
list_free(files);
|
||||
base_file_buffer_free(base_files);
|
||||
free(branch);
|
||||
free_config(&config);
|
||||
free(commit_message);
|
||||
return 1;
|
||||
|
|
@ -297,7 +296,6 @@ int main(int argc, char **argv) {
|
|||
list_free(files);
|
||||
base_file_buffer_free(base_files);
|
||||
free(branch);
|
||||
free(root);
|
||||
free_config(&config);
|
||||
free(commit_message);
|
||||
return 1;
|
||||
|
|
@ -366,8 +364,8 @@ int main(int argc, char **argv) {
|
|||
}
|
||||
}
|
||||
|
||||
StringBuffer* file_hashes = string_buffer_new();
|
||||
if (!file_hashes) {
|
||||
Changes* changes = calloc(1, sizeof(Changes));
|
||||
if (!changes) {
|
||||
free(commit_message);
|
||||
list_free(files);
|
||||
base_file_buffer_free(base_files);
|
||||
|
|
@ -377,6 +375,9 @@ int main(int argc, char **argv) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
changes->insertions = 0;
|
||||
changes->deletions = 0;
|
||||
|
||||
for (size_t idx = 0; idx < files->len; idx++) {
|
||||
BaseFileInfo* base_file = base_file_buffer_search(base_files, files->items[idx]);
|
||||
if (!base_file) {
|
||||
|
|
@ -385,7 +386,6 @@ int main(int argc, char **argv) {
|
|||
// Compress the files content and put it into the object database
|
||||
char file_hash[41];
|
||||
snapshot_file(files->items[idx], root, 0, file_hash);
|
||||
string_buffer_push(file_hashes, file_hash);
|
||||
continue;
|
||||
}
|
||||
size_t base_num = base_file->base_num;
|
||||
|
|
@ -398,17 +398,13 @@ int main(int argc, char **argv) {
|
|||
char id[2 + snprintf(NULL, 0, "%d", base_num) + strlen(files->items[idx])];
|
||||
snprintf(id, sizeof(id), "%s %d", files->items[idx], base_num);
|
||||
object_hash(BaseFileObject, id, basefile_hash);
|
||||
|
||||
char diff_hash[41];
|
||||
save_file_diff(files->items[idx], root, base_num, basefile_hash, diff_hash);
|
||||
string_buffer_push(file_hashes, diff_hash);
|
||||
save_file_diff(files->items[idx], root, base_num, basefile_hash, changes);
|
||||
}
|
||||
|
||||
base_file_buffer_sort(base_files);
|
||||
write_base_file_list(base_files, info_path);
|
||||
FileInfoBuffer* tree = basefilebuffer_to_fileinfobuffer(base_files);
|
||||
if (!tree) {
|
||||
list_free(file_hashes);
|
||||
free(commit_message);
|
||||
list_free(files);
|
||||
base_file_buffer_free(base_files);
|
||||
|
|
@ -417,6 +413,23 @@ int main(int argc, char **argv) {
|
|||
free_config(&config);
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (size_t idx = 0; idx < tree->len; idx++) {
|
||||
FileInfo* info = (FileInfo*)tree->items + idx;
|
||||
BaseFileInfo* base_info = base_file_buffer_search(base_files, info->name);
|
||||
char hash[41];
|
||||
if (base_info->diff_num == 0) {
|
||||
char id[2 + snprintf(NULL, 0, "%d", base_info->base_num) + strlen(info->name)];
|
||||
snprintf(id, sizeof(id), "%s %d", info->name, base_info->base_num);
|
||||
object_hash(BaseFileObject, id, hash);
|
||||
} else {
|
||||
char id[2 + snprintf(NULL, 0, "%d", base_info->diff_num-1) + strlen(info->name)];
|
||||
snprintf(id, sizeof(id), "%s %d", info->name, base_info->diff_num-1);
|
||||
object_hash(FileDiffObject, id, hash);
|
||||
}
|
||||
info->hash = strdup(hash);
|
||||
}
|
||||
|
||||
char tree_hash[41];
|
||||
snapshot_tree(tree, tree_hash);
|
||||
|
||||
|
|
@ -427,7 +440,6 @@ int main(int argc, char **argv) {
|
|||
|
||||
if (!log) {
|
||||
file_info_buffer_free(tree);
|
||||
list_free(file_hashes);
|
||||
free(commit_message);
|
||||
list_free(files);
|
||||
base_file_buffer_free(base_files);
|
||||
|
|
@ -484,7 +496,6 @@ int main(int argc, char **argv) {
|
|||
if (!write_commit_log(log, log_path)) {
|
||||
commit_log_free(log);
|
||||
file_info_buffer_free(tree);
|
||||
list_free(file_hashes);
|
||||
free(commit_message);
|
||||
list_free(files);
|
||||
base_file_buffer_free(base_files);
|
||||
|
|
@ -497,14 +508,18 @@ int main(int argc, char **argv) {
|
|||
char ref_path[PATH_MAX];
|
||||
snprintf(ref_path, sizeof(ref_path), "%s/.merk/refs/branches/%s", root, branch);
|
||||
|
||||
printf("this is the ref path: %s\n", ref_path);
|
||||
|
||||
int ref = open(ref_path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
|
||||
if (ref < 0) return -1;
|
||||
write(ref, commit_hash, 40);
|
||||
close(ref);
|
||||
|
||||
// Cleanup
|
||||
if (changes->insertions == 0 && changes->deletions == 0) {
|
||||
printf("(\x1b[33;1m%.7s\x1b[0m on \x1b[39;1;4m%s\x1b[0m) %s\n", commit_hash, branch, commit_message);
|
||||
}
|
||||
else {
|
||||
printf("(\x1b[33;1m%.7s\x1b[0m on \x1b[39;1;4m%s\x1b[0m \x1b[32;1m%d+\x1b[0m \x1b[31;1m%d-\x1b[0m) %s\n", commit_hash, branch, changes->insertions, changes->deletions, commit_message);
|
||||
}
|
||||
|
||||
list_free(files);
|
||||
base_file_buffer_free(base_files);
|
||||
free(branch);
|
||||
|
|
@ -541,7 +556,7 @@ int main(int argc, char **argv) {
|
|||
time_t timestamp = atol(commit->committer.timestamp);
|
||||
strftime(human_readable_time, sizeof(human_readable_time), "%Y-%m-%d %H:%M:%S %z", localtime(×tamp));
|
||||
|
||||
printf("\x1b[33;1m%.7s\x1b[0m: %s\n%s <\x1b[38;5;240m\x1b]8;;mailto:%s\x1b\\%s\x1b]8;;\x1b\\\x1b[0m> \x1b[36m%s\x1b[0m\n\n",
|
||||
printf("\x1b[33;1m%.7s\x1b[0m: %s\n%s <\x1b[38;5;240m\x1b]8;;mailto:%s\x1b\\%s\x1b]8;;\x1b\\\x1b[0m> (\x1b[36m%s\x1b[0m)\n\n",
|
||||
commit->hash,
|
||||
commit->message,
|
||||
commit->committer.name,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue