diff options
Diffstat (limited to 'src/blogc-git-receiver')
-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 |
3 files changed, 32 insertions, 17 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); |