aboutsummaryrefslogtreecommitdiffstats
path: root/src/blogc-git-receiver/pre-receive-parser.c
blob: 84ca93efd0e01c8f54a455ee0c2ef9980886c94e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
 * blogc: A blog compiler.
 * Copyright (C) 2014-2018 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;
}