From d72999fd9e2c3415698bd362dd580b4ca17bd5ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl?= Date: Mon, 22 Jul 2024 22:43:14 +0200 Subject: [PATCH] feat: push command --- src/cli/cli.c | 5 +++ src/cli/command/provider.c | 4 +- src/cli/command/push.c | 81 ++++++++++++++++++++++++++++++++++++++ src/cli/command/push.h | 1 + 4 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 src/cli/command/push.c create mode 100644 src/cli/command/push.h diff --git a/src/cli/cli.c b/src/cli/cli.c index 0802e3b..b46fee1 100644 --- a/src/cli/cli.c +++ b/src/cli/cli.c @@ -2,6 +2,7 @@ #include "command/config.h" #include "command/init.h" #include "command/provider.h" +#include "command/push.h" #include #include @@ -31,5 +32,9 @@ int cli_handle(int argc, char **argv) { return cli_command_provider(argc, argv); } + if (strcmp(sub_command, "push") == 0) { + return cli_command_push(argc, argv); + } + return 0; } diff --git a/src/cli/command/provider.c b/src/cli/command/provider.c index 844e5fe..79d9713 100644 --- a/src/cli/command/provider.c +++ b/src/cli/command/provider.c @@ -61,8 +61,8 @@ int provider_sync(int argc, char **argv) { config_free(cfg); char command[100]; - snprintf(command, sizeof(command), "git remote add %s %s", provider->name, - provider->ssh); + snprintf(command, sizeof(command), "git remote add gimi-%s %s", + provider->name, provider->ssh); int ret = system(command); if (ret != 0) { diff --git a/src/cli/command/push.c b/src/cli/command/push.c new file mode 100644 index 0000000..cf71d45 --- /dev/null +++ b/src/cli/command/push.c @@ -0,0 +1,81 @@ +#include "../../config.h" +#include +#include +#include +#include +#include +#include + +char *get_current_branch_name() { + FILE *file_ptr; + static char output[256]; + + file_ptr = popen("git rev-parse --abbrev-ref HEAD", "r"); + if (file_ptr == NULL) { + return NULL; + } + + char *branch = fgets(output, sizeof(output), file_ptr); + if (branch != NULL) { + // remove new line + size_t len = strlen(branch); + if (len > 0 && branch[len - 1] == '\n') { + branch[len - 1] = '\0'; + } + } + + pclose(file_ptr); + + return branch; +} + +int git_push(char *provider_name, char *branch_name, bool verbose) { + FILE *file_ptr; + char output[1024]; + char command[256]; + + snprintf(command, sizeof(command), "git push gimi-%s %s 2>&1", provider_name, + branch_name); + + file_ptr = popen(command, "r"); + if (file_ptr == NULL) { + return 0; + } + + while (fgets(output, sizeof(output), file_ptr) != + NULL) { // need to process for valid exit code + if (verbose) { + printf("%s", output); + } + } + + int ret = WEXITSTATUS(pclose(file_ptr)); + return ret; +} + +int cli_command_push(int argc, char **argv) { + struct gimi_config *cfg = config_read(); + if (!cfg) + return 1; + + bool verbose = argc == 2 && strcmp(argv[1], "--verbose") == 0; + + char *branch_name = get_current_branch_name(); + + for (int i = 0; i < 1; i++) { + struct gimi_config_provider *provider = cfg->providers[i]; + + int ret = git_push(provider->name, branch_name, verbose); + if (ret != 0) { + printf("error: failed to push into '%s' with git's exit code %d.\n", + provider->name, ret); + break; + } + + printf("info: successfully pushed into '%s'.\n", provider->name); + } + + config_free(cfg); + + return 0; +} diff --git a/src/cli/command/push.h b/src/cli/command/push.h new file mode 100644 index 0000000..b772188 --- /dev/null +++ b/src/cli/command/push.h @@ -0,0 +1 @@ +int cli_command_push(int argc, char **argv);