aboutsummaryrefslogtreecommitdiffstats
path: root/src/blogc-git-receiver/pre-receive-parser.c
diff options
context:
space:
mode:
authorRafael G. Martins <rafael@rafaelmartins.eng.br>2016-10-04 23:58:10 +0200
committerRafael G. Martins <rafael@rafaelmartins.eng.br>2016-10-04 23:58:10 +0200
commit13be5bd50238daa2be6b42104ba20aeea427655b (patch)
tree3e66986cd1ebbd98f06f61ff45127745fd54bc01 /src/blogc-git-receiver/pre-receive-parser.c
parent3fff4bb3172f77b292b0c913749e81bedd3545f3 (diff)
downloadblogc-13be5bd50238daa2be6b42104ba20aeea427655b.tar.gz
blogc-13be5bd50238daa2be6b42104ba20aeea427655b.tar.bz2
blogc-13be5bd50238daa2be6b42104ba20aeea427655b.zip
git-receiver: splitted and tested pre-receive input parser
Diffstat (limited to 'src/blogc-git-receiver/pre-receive-parser.c')
-rw-r--r--src/blogc-git-receiver/pre-receive-parser.c77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/blogc-git-receiver/pre-receive-parser.c b/src/blogc-git-receiver/pre-receive-parser.c
new file mode 100644
index 0000000..a842a9d
--- /dev/null
+++ b/src/blogc-git-receiver/pre-receive-parser.c
@@ -0,0 +1,77 @@
+/*
+ * blogc: A blog compiler.
+ * Copyright (C) 2015-2016 Rafael G. Martins <rafael@rafaelmartins.eng.br>
+ *
+ * This program can be distributed under the terms of the BSD License.
+ * See the file LICENSE.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include "../common/utils.h"
+#include "pre-receive-parser.h"
+
+typedef enum {
+ START_OLD = 1,
+ OLD,
+ START_NEW,
+ NEW,
+ START_REF,
+ REF
+} input_state_t;
+
+
+char*
+bgr_pre_receive_parse(const char *input)
+{
+ input_state_t state = START_OLD;
+ size_t start = 0;
+ size_t start_new = 0;
+ size_t input_len = strlen(input);
+
+ for (size_t current = 0; current < input_len; current++) {
+
+ char c = input[current];
+
+ switch (state) {
+ case START_OLD:
+ start = current;
+ state = OLD;
+ break;
+ case OLD:
+ if (c != ' ')
+ break;
+ // no need to store old
+ state = START_NEW;
+ break;
+ case START_NEW:
+ start = current;
+ state = NEW;
+ break;
+ case NEW:
+ if (c != ' ')
+ break;
+ state = START_REF;
+ start_new = start;
+ break;
+ case START_REF:
+ start = current;
+ state = REF;
+ break;
+ case REF:
+ 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);
+ }
+ break;
+ }
+ }
+
+ return NULL;
+}