diff options
author | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2016-10-10 02:13:47 +0200 |
---|---|---|
committer | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2016-10-10 02:13:47 +0200 |
commit | 3cd3dcb5bb3b0481812ddd3ff8dc182bdb20be23 (patch) | |
tree | d99f57f85d2472caf334a4b96fbebd95cee873f7 /src | |
parent | 62f999141701ba915a035a11cbac7fc7ddb08f31 (diff) | |
download | blogc-3cd3dcb5bb3b0481812ddd3ff8dc182bdb20be23.tar.gz blogc-3cd3dcb5bb3b0481812ddd3ff8dc182bdb20be23.tar.bz2 blogc-3cd3dcb5bb3b0481812ddd3ff8dc182bdb20be23.zip |
git-receiver: added support to get mirror url from config file
this commit also includes some "integration test" in shell script
Diffstat (limited to 'src')
-rw-r--r-- | src/blogc-git-receiver/post-receive.c | 136 | ||||
-rw-r--r-- | src/blogc-git-receiver/post-receive.h | 4 | ||||
-rw-r--r-- | src/common/error.h | 3 |
3 files changed, 134 insertions, 9 deletions
diff --git a/src/blogc-git-receiver/post-receive.c b/src/blogc-git-receiver/post-receive.c index 8310939..79c51ac 100644 --- a/src/blogc-git-receiver/post-receive.c +++ b/src/blogc-git-receiver/post-receive.c @@ -6,24 +6,142 @@ * See the file LICENSE. */ +#include <errno.h> #include <stdio.h> +#include <limits.h> +#include <unistd.h> #include <stdlib.h> +#include <string.h> +#include "../common/config-parser.h" +#include "../common/error.h" +#include "../common/file.h" + + +char* +bgr_post_receive_get_config_section(bc_config_t *config, const char *repo_path, + const char *home) +{ + char *rv = NULL; + char** sections = bc_config_list_sections(config); + for (size_t i = 0; sections[i] != NULL; i++) { + if (bc_str_starts_with(sections[i], "repo:")) { + char *tmp_repo = bc_strdup_printf("%s/repos/%s", home, sections[i] + 5); + char *real_tmp_repo = realpath(tmp_repo, NULL); // maybe not needed + free(tmp_repo); + if (real_tmp_repo == NULL) + continue; + if (0 == strcmp(real_tmp_repo, repo_path)) { + rv = bc_strdup(sections[i]); + free(real_tmp_repo); + break; + } + free(real_tmp_repo); + } + } + bc_strv_free(sections); + return rv; +} int bgr_post_receive_hook(int argc, char *argv[]) { - if (0 != system("git config --local remote.mirror.pushurl &> /dev/null")) { - if (0 != system("git config --local remote.mirror.url &> /dev/null")) { - fprintf(stderr, "warning: repository mirroring disabled\n"); - return 0; - } + char *mirror = NULL; + + // 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 + // with old setups. + if ((0 == system("git config --local remote.mirror.pushurl &> /dev/null")) || + (0 == system("git config --local remote.mirror.url &> /dev/null"))) + { + mirror = bc_strdup("mirror"); + 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; + } + + 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; + } + + size_t len; + bc_error_t *err = NULL; + char* config_content = bc_file_get_contents(config_file, true, &len, &err); + if (err != NULL) { + 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; + } + + bc_config_t *config = bc_config_parse(config_content, len, &err); + free(config_content); + if (err != NULL) { + 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; + } + 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; + } + + mirror = bc_strdup(bc_config_get(config, config_section, "mirror")); + free(config_section); + bc_config_free(config); + + if (mirror == NULL) { + fprintf(stderr, "warning: repository mirroring disabled\n"); + return 0; + } + +push: + + { + char *git_cmd = bc_strdup_printf("git push --mirror %s", mirror); + if (0 != system(git_cmd)) + fprintf(stderr, "warning: failed push to git mirror\n"); + free(git_cmd); } - // at this point we know that we have a remote called mirror, we can just - // push to it. - if (0 != system("git push --mirror mirror")) - fprintf(stderr, "warning: failed push to git mirror\n"); + free(mirror); return 0; } diff --git a/src/blogc-git-receiver/post-receive.h b/src/blogc-git-receiver/post-receive.h index a28dd5a..e17c3d4 100644 --- a/src/blogc-git-receiver/post-receive.h +++ b/src/blogc-git-receiver/post-receive.h @@ -9,6 +9,10 @@ #ifndef _POST_RECEIVE_H #define _POST_RECEIVE_H +#include "../common/config-parser.h" + +char* bgr_post_receive_get_config_section(bc_config_t *config, + const char *repo_path, const char *home); int bgr_post_receive_hook(int argc, char *argv[]); #endif /* _POST_RECEIVE_H */ diff --git a/src/common/error.h b/src/common/error.h index 5ac2b15..21b332a 100644 --- a/src/common/error.h +++ b/src/common/error.h @@ -23,6 +23,9 @@ typedef enum { BLOGC_ERROR_TEMPLATE_PARSER, BLOGC_ERROR_LOADER, BLOGC_WARNING_DATETIME_PARSER, + + // errors for src/blogc-git-receiver + BLOGC_GR_ERROR_POST_RECEIVE = 200, } bc_error_type_t; typedef struct { |