aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael G. Martins <rafael@rafaelmartins.eng.br>2018-05-31 02:08:08 +0200
committerRafael G. Martins <rafael@rafaelmartins.eng.br>2018-05-31 02:08:11 +0200
commit56e07b6224fce7e213bfb9b4d00b93628a50aca7 (patch)
tree49c81bc0ce71242ec53d964211f3901ded650e36
parent316bd60579ea11daca4fa25e2720b7fa147cc7ba (diff)
downloadblogc-56e07b6224fce7e213bfb9b4d00b93628a50aca7.tar.gz
blogc-56e07b6224fce7e213bfb9b4d00b93628a50aca7.tar.bz2
blogc-56e07b6224fce7e213bfb9b4d00b93628a50aca7.zip
git-receiver: allow users to define the htdocs symlink path
documentation pending
-rw-r--r--src/blogc-git-receiver/pre-receive.c52
-rwxr-xr-xtests/blogc-git-receiver/check_pre_receive.sh.in85
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" <<EOF
0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 refs/heads/bar
EOF
+cat > "${TEMP}/blogc-git-receiver.ini" <<EOF
+[repo:foo2.git]
+symlink = ${TEMP}/chunda
+[repo:foo2a.git]
+symlink = ${TEMP}/guda
+EOF
+
SHELL="${SELF}" HOME="${TEMP}" GIT_DIR=. ${TESTS_ENVIRONMENT} ./hooks/pre-receive < "${TEMP}/payload.txt" 2>&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" <<EOF
+blob
+mark :1
+data 64
+all:
+ mkdir -p \$(OUTPUT_DIR)
+ echo lol > \$(OUTPUT_DIR)/foo.txt
+
+
+commit refs/heads/master
+mark :2
+author Rafael G. Martins <rafael@rafaelmartins.eng.br> 1476142917 +0200
+committer Rafael G. Martins <rafael@rafaelmartins.eng.br> 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" <<EOF
+0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 refs/heads/foo
+0000000000000000000000000000000000000000 $(git rev-parse HEAD) refs/heads/master
+0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 refs/heads/bar
+EOF
+
+SHELL="${SELF}" HOME="${TEMP}" GIT_DIR=. ${TESTS_ENVIRONMENT} ./hooks/pre-receive < "${TEMP}/payload.txt" 2>&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}"