From 9fa645fe6964a58e58b4be3599c7becd531512f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl?= Date: Sat, 3 Aug 2024 21:24:20 +0200 Subject: [PATCH] fix: get current branch name correctly --- src/cli/command/push.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/cli/command/push.c b/src/cli/command/push.c index 8475ef1..9dff7e0 100644 --- a/src/cli/command/push.c +++ b/src/cli/command/push.c @@ -20,20 +20,23 @@ char *get_current_branch_name() { 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'; - } + if (fgets(output, sizeof(output), file_ptr) == NULL) { + pclose(file_ptr); + return NULL; + } + + // remove new line + size_t len = strlen(output); + if (len > 0 && output[len - 1] == '\n') { + output[len - 1] = '\0'; } pclose(file_ptr); - return branch; + return strdup(output); } + int git_push(char *provider_name, char *branch_name, int options) { FILE *file_ptr; char output[1024]; @@ -97,6 +100,7 @@ int cli_command_push(int argc, char **argv) { printf("info: successfully pushed into '%s'.\n", provider->name); } + free(branch_name); config_free(cfg); return 0;