feat: push command

This commit is contained in:
Jozef Steinhübl 2024-07-22 22:43:14 +02:00
parent 383ed08994
commit d72999fd9e
No known key found for this signature in database
GPG key ID: E6BC90C91973B08F
4 changed files with 89 additions and 2 deletions

View file

@ -2,6 +2,7 @@
#include "command/config.h"
#include "command/init.h"
#include "command/provider.h"
#include "command/push.h"
#include <stdio.h>
#include <string.h>
@ -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;
}

View file

@ -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) {

81
src/cli/command/push.c Normal file
View file

@ -0,0 +1,81 @@
#include "../../config.h"
#include <linux/limits.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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;
}

1
src/cli/command/push.h Normal file
View file

@ -0,0 +1 @@
int cli_command_push(int argc, char **argv);