diff options
author | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2022-02-10 02:52:01 +0100 |
---|---|---|
committer | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2022-02-10 02:52:19 +0100 |
commit | 8e74dace4b878027405c267670f559ff761f8ea6 (patch) | |
tree | 6475dd99fe888bcc9af32773b6e6ec3b60160d6f | |
parent | 06998bda19b6f9dd452a48ed12b61ca9df7dba23 (diff) | |
download | blogc-8e74dace4b878027405c267670f559ff761f8ea6.tar.gz blogc-8e74dace4b878027405c267670f559ff761f8ea6.tar.bz2 blogc-8e74dace4b878027405c267670f559ff761f8ea6.zip |
git-receiver: allow deploying from "main" branch
if a "main" branch is available and no "master" branch is available, use
it. "master" still takes precedence to avoid breaking old setups.
-rw-r--r-- | src/blogc-git-receiver/pre-receive-parser.c | 21 | ||||
-rw-r--r-- | src/blogc-git-receiver/pre-receive-parser.h | 3 | ||||
-rw-r--r-- | src/blogc-git-receiver/pre-receive.c | 25 | ||||
-rwxr-xr-x | tests/blogc-git-receiver/check_pre_receive.sh.in | 10 | ||||
-rw-r--r-- | tests/blogc-git-receiver/check_pre_receive_parser.c | 48 |
5 files changed, 74 insertions, 33 deletions
diff --git a/src/blogc-git-receiver/pre-receive-parser.c b/src/blogc-git-receiver/pre-receive-parser.c index 8b9b968..52b0b76 100644 --- a/src/blogc-git-receiver/pre-receive-parser.c +++ b/src/blogc-git-receiver/pre-receive-parser.c @@ -22,13 +22,15 @@ typedef enum { } input_state_t; -char* +bc_trie_t* bgr_pre_receive_parse(const char *input, size_t input_len) { input_state_t state = START_OLD; size_t start = 0; size_t start_new = 0; + bc_trie_t* rv = bc_trie_new(free); + for (size_t current = 0; current < input_len; current++) { char c = input[current]; @@ -62,16 +64,19 @@ bgr_pre_receive_parse(const char *input, size_t input_len) if (c != '\n') break; state = START_OLD; - // we just care about a ref (refs/heads/master), everything - // else is disposable :) - if ((current - start == 17) && - (0 == strncmp("refs/heads/master", input + start, 17))) - { - return bc_strndup(input + start_new, start - 1 - start_new); + if (current - start > 11) { + char *key = bc_strndup(input + start + 11, current - start - 11); + bc_trie_insert(rv, key, bc_strndup(input + start_new, start - 1 - start_new)); + free(key); } break; } } - return NULL; + if (bc_trie_size(rv) == 0) { + bc_trie_free(rv); + return NULL; + } + + return rv; } diff --git a/src/blogc-git-receiver/pre-receive-parser.h b/src/blogc-git-receiver/pre-receive-parser.h index f8e0686..45a9da8 100644 --- a/src/blogc-git-receiver/pre-receive-parser.h +++ b/src/blogc-git-receiver/pre-receive-parser.h @@ -10,7 +10,8 @@ #define _PRE_RECEIVE_PARSER_H #include <stddef.h> +#include "../common/utils.h" -char* bgr_pre_receive_parse(const char *input, size_t input_len); +bc_trie_t* bgr_pre_receive_parse(const char *input, size_t input_len); #endif /* _PRE_RECEIVE_PARSER_H */ diff --git a/src/blogc-git-receiver/pre-receive.c b/src/blogc-git-receiver/pre-receive.c index 9804824..12d29cf 100644 --- a/src/blogc-git-receiver/pre-receive.c +++ b/src/blogc-git-receiver/pre-receive.c @@ -93,7 +93,7 @@ int bgr_pre_receive_hook(int argc, char *argv[]) { int rv = 0; - char *master = NULL; + char *ref = NULL; char *output_dir = NULL; char *tmpdir = NULL; char *sym = NULL; @@ -166,18 +166,27 @@ default_sym: rv = 1; goto cleanup; } - master = bc_strdup(pieces[0]); + ref = bc_strdup(pieces[0]); bc_strv_free(pieces); } else { size_t input_len; char *input = bc_stdin_read(&input_len); - master = bgr_pre_receive_parse(input, input_len); + bc_trie_t *branches = bgr_pre_receive_parse(input, input_len); + + // try 'master' by default to avoid breaking existing setups + ref = bc_strdup(bc_trie_lookup(branches, "master")); + if (ref == NULL) { + // try 'main' + ref = bc_strdup(bc_trie_lookup(branches, "main")); + } + + bc_trie_free(branches); free(input); } - if (master == NULL) { - fprintf(stderr, "warning: no reference to master branch found. " + if (ref == NULL) { + fprintf(stderr, "warning: no suitable branch found. " "nothing to deploy.\n"); goto cleanup; } @@ -190,7 +199,7 @@ default_sym: tmpdir = dir; char *git_archive_cmd = bc_strdup_printf( - "git archive \"%s\" | tar -x -C \"%s\" -f -", master, tmpdir); + "git archive \"%s\" | tar -x -C \"%s\" -f -", ref, tmpdir); if (0 != system(git_archive_cmd)) { fprintf(stderr, "error: failed to extract git content to temporary " "directory: %s\n", tmpdir); @@ -215,7 +224,7 @@ default_sym: } unsigned long epoch = time(NULL); - output_dir = bc_strdup_printf("%s/%s-%lu", buildsd, master, epoch); + output_dir = bc_strdup_printf("%s/%s-%lu", buildsd, ref, epoch); free(buildsd); if (0 == access(output_dir, F_OK)) { @@ -301,7 +310,7 @@ cleanup2: cleanup: free(sym); - free(master); + free(ref); free(output_dir); rmdir_recursive(tmpdir); free(repo_dir); diff --git a/tests/blogc-git-receiver/check_pre_receive.sh.in b/tests/blogc-git-receiver/check_pre_receive.sh.in index 9ac12df..2cf3c89 100755 --- a/tests/blogc-git-receiver/check_pre_receive.sh.in +++ b/tests/blogc-git-receiver/check_pre_receive.sh.in @@ -29,7 +29,7 @@ EOF cd "${TEMP}/repos/foo.git" SHELL="${SELF}" HOME="${TEMP}" GIT_DIR=. ${TESTS_ENVIRONMENT} ./hooks/pre-receive < "${TEMP}/payload.txt" 2>&1 | tee "${TEMP}/output.txt" -grep "warning: no reference to master branch found. nothing to deploy." "${TEMP}/output.txt" &> /dev/null +grep "warning: no suitable branch found. nothing to deploy." "${TEMP}/output.txt" &> /dev/null cat > "${TEMP}/tmp.txt" <<EOF blob @@ -55,7 +55,7 @@ git fast-import < "${TEMP}/tmp.txt" &> /dev/null cat > "${TEMP}/payload.txt" <<EOF 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 refs/heads/foo 0000000000000000000000000000000000000000 $(git rev-parse master) refs/heads/master -0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 refs/heads/bar +0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 refs/heads/main EOF SHELL="${SELF}" HOME="${TEMP}" GIT_DIR=. ${TESTS_ENVIRONMENT} ./hooks/pre-receive < "${TEMP}/payload.txt" 2>&1 | tee "${TEMP}/output.txt" @@ -73,8 +73,8 @@ all: mkdir -p \$(OUTPUT_DIR) echo lol > \$(OUTPUT_DIR)/foo.txt -reset refs/heads/master -commit refs/heads/master +reset refs/heads/main +commit refs/heads/main mark :2 author Rafael G. Martins <rafael@rafaelmartins.eng.br> 1476139736 +0200 committer Rafael G. Martins <rafael@rafaelmartins.eng.br> 1476139736 +0200 @@ -89,7 +89,7 @@ git fast-import < "${TEMP}/tmp.txt" &> /dev/null cat > "${TEMP}/payload.txt" <<EOF 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 refs/heads/foo -0000000000000000000000000000000000000000 $(git rev-parse master) refs/heads/master +0000000000000000000000000000000000000000 $(git rev-parse main) refs/heads/main 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 refs/heads/bar EOF diff --git a/tests/blogc-git-receiver/check_pre_receive_parser.c b/tests/blogc-git-receiver/check_pre_receive_parser.c index 0415be1..c431821 100644 --- a/tests/blogc-git-receiver/check_pre_receive_parser.c +++ b/tests/blogc-git-receiver/check_pre_receive_parser.c @@ -43,18 +43,28 @@ test_pre_receive_parse(void **state) "4f1f932f6ef6d6c9770266775c2db072964d7a62 " "3fff4bb3172f77b292b0c913749e81bedd3545f3 " "refs/heads/master")); - assert_null(_bgr_pre_receive_parse( + + bc_trie_t *t; + t = _bgr_pre_receive_parse( "4f1f932f6ef6d6c9770266775c2db072964d7a62 " "3fff4bb3172f77b292b0c913749e81bedd3545f3 " - "refs/heads/master asd\n")); - char *t; + "refs/heads/master asd\n"); + assert_non_null(t); + assert_int_equal(bc_trie_size(t), 1); + assert_string_equal(bc_trie_lookup(t, "master asd"), + "3fff4bb3172f77b292b0c913749e81bedd3545f3"); + bc_trie_free(t); + t = _bgr_pre_receive_parse( "4f1f932f6ef6d6c9770266775c2db072964d7a62 " "3fff4bb3172f77b292b0c913749e81bedd3545f3 " "refs/heads/master\n"); assert_non_null(t); - assert_string_equal(t, "3fff4bb3172f77b292b0c913749e81bedd3545f3"); - free(t); + assert_int_equal(bc_trie_size(t), 1); + assert_string_equal(bc_trie_lookup(t, "master"), + "3fff4bb3172f77b292b0c913749e81bedd3545f3"); + bc_trie_free(t); + t = _bgr_pre_receive_parse( "4f1f932f6ef6d6c9770266775c2db072964d7a62 " "3fff4bb3172f77b292b0c913749e81bedd3545fa " @@ -64,8 +74,13 @@ test_pre_receive_parse(void **state) "refs/heads/bola\n" ); assert_non_null(t); - assert_string_equal(t, "3fff4bb3172f77b292b0c913749e81bedd3545fa"); - free(t); + assert_int_equal(bc_trie_size(t), 2); + assert_string_equal(bc_trie_lookup(t, "master"), + "3fff4bb3172f77b292b0c913749e81bedd3545fa"); + assert_string_equal(bc_trie_lookup(t, "bola"), + "3fff4bb3172f77b292b0c913749e81bedd3545f4"); + bc_trie_free(t); + t = _bgr_pre_receive_parse( "4f1f932f6ef6d6c9770266775c2db072964d7a63 " "3fff4bb3172f77b292b0c913749e81bedd3545f4 " @@ -75,8 +90,13 @@ test_pre_receive_parse(void **state) "refs/heads/master\n" ); assert_non_null(t); - assert_string_equal(t, "3fff4bb3172f77b292b0c913749e81bedd3545fb"); - free(t); + assert_int_equal(bc_trie_size(t), 2); + assert_string_equal(bc_trie_lookup(t, "bola"), + "3fff4bb3172f77b292b0c913749e81bedd3545f4"); + assert_string_equal(bc_trie_lookup(t, "master"), + "3fff4bb3172f77b292b0c913749e81bedd3545fb"); + bc_trie_free(t); + t = _bgr_pre_receive_parse( "4f1f932f6ef6d6c9770266775c2db072964d7a63 " "3fff4bb3172f77b292b0c913749e81bedd3545f4 " @@ -89,8 +109,14 @@ test_pre_receive_parse(void **state) "refs/heads/bolao\n" ); assert_non_null(t); - assert_string_equal(t, "3fff4bb3172f77b292b0c913749e81bedd3545fc"); - free(t); + assert_int_equal(bc_trie_size(t), 3); + assert_string_equal(bc_trie_lookup(t, "bola"), + "3fff4bb3172f77b292b0c913749e81bedd3545f4"); + assert_string_equal(bc_trie_lookup(t, "master"), + "3fff4bb3172f77b292b0c913749e81bedd3545fc"); + assert_string_equal(bc_trie_lookup(t, "bolao"), + "3fff4bb3172f77b292b0c913749e81bedd3545f5"); + bc_trie_free(t); } |