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 --- man/blogc-git-receiver.1.ronn | 12 +++++++++ src/blogc-git-receiver/post-receive.c | 51 ++++++++++++++++++----------------- 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/man/blogc-git-receiver.1.ronn b/man/blogc-git-receiver.1.ronn index 8aa8099..d5f695f 100644 --- a/man/blogc-git-receiver.1.ronn +++ b/man/blogc-git-receiver.1.ronn @@ -133,6 +133,18 @@ as `root`: # su -s /bin/sh - blogc +### Push to mirror manually + +If for some reason you want to push the repository of a given website to remote +mirror, you can run its `post-receive` hook manually in the server: + + # su -s /bin/sh - blogc + $ cd ~/repos/blogs/blogc-example.git + $ ./hooks/post-receive + +WARNING: If you push manually and your server's repository is empty, you'll clean +your mirror repository. + ## ENVIRONMENT VARIABLES `blogc-git-receiver` will export an environment variable called `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