From 56e07b6224fce7e213bfb9b4d00b93628a50aca7 Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Thu, 31 May 2018 02:08:08 +0200 Subject: git-receiver: allow users to define the htdocs symlink path documentation pending --- src/blogc-git-receiver/pre-receive.c | 52 +++++++++------ tests/blogc-git-receiver/check_pre_receive.sh.in | 85 +++++++++++++++++++++++- 2 files changed, 115 insertions(+), 22 deletions(-) diff --git a/src/blogc-git-receiver/pre-receive.c b/src/blogc-git-receiver/pre-receive.c index 3668f8b..897e8f0 100644 --- a/src/blogc-git-receiver/pre-receive.c +++ b/src/blogc-git-receiver/pre-receive.c @@ -96,6 +96,7 @@ bgr_pre_receive_hook(int argc, char *argv[]) char *master = NULL; char *output_dir = NULL; char *tmpdir = NULL; + char *sym = NULL; char *hooks_dir = dirname(argv[0]); // this was validated by main() char *real_hooks_dir = realpath(hooks_dir, NULL); @@ -112,17 +113,37 @@ bgr_pre_receive_hook(int argc, char *argv[]) goto cleanup; } + bc_config_t *config = bgr_settings_parse(); + if (config == NULL) { + goto default_sym; + } + + char *section = bgr_settings_get_section(config, repo_dir); + if (section == NULL) { + bc_config_free(config); + goto default_sym; + } + + const char *sym_tmp = bc_config_get(config, section, "symlink"); + sym = bc_str_starts_with(sym_tmp, "/") ? bc_strdup(sym_tmp) : + bc_strdup_printf("%s/%s", repo_dir, sym_tmp); + free(section); + bc_config_free(config); + +default_sym: + + if (sym == NULL) { + sym = bc_strdup_printf("%s/htdocs", repo_dir); + } + if (NULL == getenv("GIT_DIR")) { - char *htdocs_sym = bc_strdup_printf("%s/htdocs", repo_dir); - if (0 != access(htdocs_sym, R_OK)) { + if (0 != access(sym, R_OK)) { fprintf(stderr, "error: no previous build found. nothing to " "rebuild.\n"); - free(htdocs_sym); rv = 3; goto cleanup; } - char *build_dir = realpath(htdocs_sym, NULL); - free(htdocs_sym); + char *build_dir = realpath(sym, NULL); if (build_dir == NULL) { fprintf(stderr, "error: failed to get the hash of last built " "commit.\n"); @@ -246,26 +267,18 @@ bgr_pre_receive_hook(int argc, char *argv[]) } free(build_cmd); - if (0 != chdir(repo_dir)) { - fprintf(stderr, "error: failed to chdir (%s): %s\n", repo_dir, + char *htdocs_sym = realpath(sym, NULL); + if ((htdocs_sym != NULL) && (0 != unlink(sym))) { + fprintf(stderr, "error: failed to remove symlink (%s): %s\n", sym, strerror(errno)); rmdir_recursive(output_dir); rv = 3; - goto cleanup; - } - - char *htdocs_sym = realpath("htdocs", NULL); - if ((htdocs_sym != NULL) && (0 != unlink("htdocs"))) { - fprintf(stderr, "error: failed to remove symlink (%s/htdocs): %s\n", - repo_dir, strerror(errno)); - rmdir_recursive(output_dir); - rv = 3; goto cleanup2; } - if (0 != symlink(output_dir, "htdocs")) { - fprintf(stderr, "error: failed to create symlink (%s/htdocs): %s\n", - repo_dir, strerror(errno)); + if (0 != symlink(output_dir, sym)) { + fprintf(stderr, "error: failed to create symlink (%s): %s\n", sym, + strerror(errno)); rmdir_recursive(output_dir); rv = 3; goto cleanup2; @@ -278,6 +291,7 @@ cleanup2: free(htdocs_sym); cleanup: + free(sym); free(master); free(output_dir); rmdir_recursive(tmpdir); diff --git a/tests/blogc-git-receiver/check_pre_receive.sh.in b/tests/blogc-git-receiver/check_pre_receive.sh.in index 6021cfb..4a670e1 100755 --- a/tests/blogc-git-receiver/check_pre_receive.sh.in +++ b/tests/blogc-git-receiver/check_pre_receive.sh.in @@ -93,13 +93,92 @@ cat > "${TEMP}/payload.txt" < "${TEMP}/blogc-git-receiver.ini" <&1 | tee "${TEMP}/output.txt" grep "echo lol" "${TEMP}/output.txt" &> /dev/null -[[ -h htdocs ]] -[[ "$(cat htdocs/foo.txt)" == "lol" ]] +[[ -h "${TEMP}/chunda" ]] +[[ "$(cat "${TEMP}/chunda/foo.txt")" == "lol" ]] -DEST="$(readlink htdocs)" +DEST="$(readlink "${TEMP}/chunda")" +[[ -e "${DEST}" ]] + +cd "${TEMP}" +git init --bare "${TEMP}/repos/foo2a.git" &> /dev/null +ln -s "${SELF}" "${TEMP}/repos/foo2a.git/hooks/pre-receive" + +cat > "${TEMP}/tmp.txt" < \$(OUTPUT_DIR)/foo.txt + + +commit refs/heads/master +mark :2 +author Rafael G. Martins 1476142917 +0200 +committer Rafael G. Martins 1476142917 +0200 +data 12 +testing2... +M 100644 :1 Makefile + +EOF + +cd "${TEMP}/repos/foo2a.git" +ln -s "${DEST}" "${TEMP}/guda" +git fast-import < "${TEMP}/tmp.txt" &> /dev/null + +cat > "${TEMP}/payload.txt" <&1 | tee "${TEMP}/output.txt" +grep "echo lol" "${TEMP}/output.txt" &> /dev/null + +[[ -h "${TEMP}/guda" ]] +[[ "$(cat "${TEMP}/guda/foo.txt")" == "lol" ]] +[[ "${DEST}" != "$(readlink "${TEMP}/guda")" ]] +[[ ! -e "${DEST}" ]] + +DEST="$(readlink "${TEMP}/guda")" +HOME="${TEMP}" ${TESTS_ENVIRONMENT} ./hooks/pre-receive 2>&1 | tee "${TEMP}/output.txt" + +[[ -h "${TEMP}/guda" ]] +[[ "$(cat "${TEMP}/guda/foo.txt")" == "lol" ]] +[[ "${DEST}" != "$(readlink "${TEMP}/guda")" ]] +[[ ! -e "${DEST}" ]] + +cd .. + +DEST="$(readlink "${TEMP}/guda")" +HOME="${TEMP}" ${TESTS_ENVIRONMENT} ./foo2a.git/hooks/pre-receive 2>&1 | tee "${TEMP}/output.txt" + +[[ -h "${TEMP}/guda" ]] +[[ "$(cat "${TEMP}/guda/foo.txt")" == "lol" ]] +[[ "${DEST}" != "$(readlink "${TEMP}/guda")" ]] +[[ ! -e "${DEST}" ]] + +cd .. + +DEST="$(readlink "${TEMP}/guda")" +HOME="${TEMP}" ${TESTS_ENVIRONMENT} ./repos/foo2a.git/hooks/pre-receive 2>&1 | tee "${TEMP}/output.txt" + +[[ -h "${TEMP}/guda" ]] +[[ "$(cat "${TEMP}/guda/foo.txt")" == "lol" ]] +[[ "${DEST}" != "$(readlink "${TEMP}/guda")" ]] +[[ ! -e "${DEST}" ]] + +DEST="$(readlink "${TEMP}/guda")" [[ -e "${DEST}" ]] cd "${TEMP}" -- cgit v1.2.3-18-g5258