From 54c4614d5ee62034fec821ae6762f088de513b56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl?= Date: Sat, 14 Dec 2024 12:16:04 +0100 Subject: [PATCH] feat: ssh to http better logging --- src/cli/command/push.c | 7 ++++++- src/config.c | 39 +++++++++++++++++++++++++++++++++++++++ src/config.h | 1 + 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/cli/command/push.c b/src/cli/command/push.c index 8afdfec..fcebaf7 100644 --- a/src/cli/command/push.c +++ b/src/cli/command/push.c @@ -109,7 +109,12 @@ int cli_command_push(int argc, char **argv) { break; } - printf("info: successfully pushed into '%s'.\n", provider->name); + if (provider->http != NULL) { + printf("info: successfully pushed into '%s' @ %s\n", provider->name, + provider->http); + } else { + printf("info: successfully pushed into '%s'.\n", provider->name); + } } free(branch_name); diff --git a/src/config.c b/src/config.c index 73137dc..375fcd2 100644 --- a/src/config.c +++ b/src/config.c @@ -5,6 +5,42 @@ #include #include +char *ssh_to_http(char *ssh) { + if (ssh == NULL) + return NULL; + + if (strncmp(ssh, "git@", 4) != 0) + return NULL; + + size_t len = strlen(ssh); + char *https = (char *)malloc(len + 10); // 8 for https://, 1 for /, 1 for \0 + if (https == NULL) + return NULL; + + strcpy(https, "https://"); + + char *colon = strchr(ssh + 4, ':'); + if (colon == NULL) { + free(https); + return NULL; + } + + size_t domain_len = colon - (ssh + 4); + strncat(https, ssh + 4, domain_len); + + strcat(https, "/"); + + char *path = colon + 1; + char *git_ext = strstr(path, ".git"); + if (git_ext != NULL) { + *git_ext = '\0'; + } + + strcat(https, path); + + return https; +} + struct gimi_config *config_read() { FILE *file_ptr; char errbuf[200]; @@ -46,6 +82,8 @@ struct gimi_config *config_read() { toml_datum_t ssh = toml_string_in(toml_provider, "ssh"); provider->ssh = strdup(ssh.u.s); + provider->http = ssh_to_http(provider->ssh); + toml_datum_t toml_primary = toml_bool_in(toml_provider, "primary"); if (!toml_primary.ok) { provider->primary = 0; @@ -91,6 +129,7 @@ void config_free(struct gimi_config *cfg) { for (int i = 0; i < cfg->providers_size; i++) { free(cfg->providers[i]->name); free(cfg->providers[i]->ssh); + free(cfg->providers[i]->http); } free(cfg->providers); diff --git a/src/config.h b/src/config.h index aeb0a2c..179f787 100644 --- a/src/config.h +++ b/src/config.h @@ -12,6 +12,7 @@ struct gimi_config_provider { char *name; char *ssh; + char *http; bool primary; };