From 53ecd048919835a116fce1089b78a46a69ecb374 Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Sat, 19 Nov 2016 00:32:07 +0100 Subject: git-receiver: allow to push to mirror manually --- src/blogc-git-receiver/post-receive.c | 51 ++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 25 deletions(-) (limited to 'src/blogc-git-receiver') diff --git a/src/blogc-git-receiver/post-receive.c b/src/blogc-git-receiver/post-receive.c index efb41a5..98f034a 100644 --- a/src/blogc-git-receiver/post-receive.c +++ b/src/blogc-git-receiver/post-receive.c @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -46,8 +47,24 @@ bgr_post_receive_get_config_section(bc_config_t *config, const char *repo_path, int bgr_post_receive_hook(int argc, char *argv[]) { + int rv = 0; char *mirror = NULL; + char *hooks_dir = dirname(argv[0]); // this was validated by main() + char *real_hooks_dir = realpath(hooks_dir, NULL); + if (real_hooks_dir == NULL) { + fprintf(stderr, "error: failed to guess repository root.\n"); + return 1; + } + + char *repo_path = bc_strdup(dirname(real_hooks_dir)); + free(real_hooks_dir); + if (0 != chdir(repo_path)) { + fprintf(stderr, "error: failed to change to repository root\n"); + rv = 1; + goto cleanup; + } + // local repository settings should take precedence, so if the repo have // the 'mirror' remote, just push to it. // this will be removed at some point, but will be kept for compatibility @@ -59,34 +76,18 @@ bgr_post_receive_hook(int argc, char *argv[]) goto push; } - char buffer[4096]; - if (NULL == getcwd(buffer, sizeof(buffer))) { - fprintf(stderr, "warning: failed to get repository remote path, " - "mirroring disabled: %s\n", strerror(errno)); - return 0; - } - - char *repo_path = realpath(buffer, NULL); - if (repo_path == NULL) { - fprintf(stderr, "warning: failed to find remote repository directory, " - "mirroring disabled: %s\n", strerror(errno)); - return 0; - } - char *home = getenv("HOME"); if (home == NULL) { fprintf(stderr, "warning: failed to find user home path, " "mirroring disabled\n"); - free(repo_path); - return 0; + goto cleanup; } char *config_file = bc_strdup_printf("%s/blogc-git-receiver.ini", home); if ((0 != access(config_file, F_OK))) { fprintf(stderr, "warning: repository mirroring disabled\n"); - free(repo_path); free(config_file); - return 0; + goto cleanup; } size_t len; @@ -96,10 +97,9 @@ bgr_post_receive_hook(int argc, char *argv[]) fprintf(stderr, "warning: failed to read configuration file (%s), " "mirroring disabled: %s\n", config_file, err->msg); bc_error_free(err); - free(repo_path); free(config_file); free(config_content); - return 0; + goto cleanup; } bc_config_t *config = bc_config_parse(config_content, len, &err); @@ -108,19 +108,17 @@ bgr_post_receive_hook(int argc, char *argv[]) fprintf(stderr, "warning: failed to parse configuration file (%s), " "mirroring disabled: %s\n", config_file, err->msg); bc_error_free(err); - free(repo_path); free(config_file); - return 0; + goto cleanup; } free(config_file); char *config_section = bgr_post_receive_get_config_section(config, repo_path, home); - free(repo_path); if (config_section == NULL) { fprintf(stderr, "warning: repository mirroring disabled\n"); bc_config_free(config); - return 0; + goto cleanup; } mirror = bc_strdup(bc_config_get(config, config_section, "mirror")); @@ -129,7 +127,7 @@ bgr_post_receive_hook(int argc, char *argv[]) if (mirror == NULL) { fprintf(stderr, "warning: repository mirroring disabled\n"); - return 0; + goto cleanup; } push: @@ -143,5 +141,8 @@ push: free(mirror); +cleanup: + free(repo_path); + return 0; } -- cgit v1.2.3-18-g5258