summaryrefslogtreecommitdiffstats
path: root/rfc.sh
blob: 2a5e779c12c0c17f305c741dcb26711b20c40ea9 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/bash -e


branch="master";


function set_hooks_commit_msg()
{
    f=".git/hooks/commit-msg";
    u="http://review.gluster.com/tools/hooks/commit-msg";

    if [ -x "$f" ]; then
        return;
    fi

    curl -o $f $u || wget -O $f $u;

    chmod +x .git/hooks/commit-msg;
}


function is_num()
{
    local num;

    num="$1";

    [ -z "$(echo $num | sed -e 's/[0-9]//g')" ]
}


function rebase_changes()
{
    git fetch;

    GIT_EDITOR=$0 git rebase -i origin/$branch;
}


function editor_mode()
{
    if [ $(basename "$1") = "git-rebase-todo" ]; then
        sed -i 's/^pick /reword /g' "$1";
        return;
    fi

    if [ $(basename "$1") = "COMMIT_EDITMSG" ]; then
        if grep -qi '^BUG: ' $1; then
            return;
        fi
        while true; do
            echo Commit: "\"$(head -n 1 $1)\""
            echo -n "Enter Bug ID: "
            read bug
            if [ -z "$bug" ]; then
                return;
            fi
            if ! is_num "$bug"; then
                echo "Invalid Bug ID ($bug)!!!";
                continue;
            fi

            sed -i "s/^\(Change-Id: .*\)$/\1\nBUG: $bug/g" $1;
            return;
        done
    fi

    cat <<EOF
$0 - editor_mode called on unrecognized file $1 with content:
$(cat $1)
EOF
    return 1;
}


function assert_diverge()
{
    git diff origin/$branch..HEAD | grep -q .;
}


function main()
{
    if [ -e "$1" ]; then
        editor_mode "$@";
        return;
    fi

    set_hooks_commit_msg;

    rebase_changes;

    assert_diverge;

    bug=$(git show --format='%b' | grep -i '^BUG: ' | awk '{print $2}');

    if [ "$DRY_RUN" = 1 ]; then
        drier='echo -e Please use the following command to send your commits to review:\n\n'
    else
        drier=
    fi

    if [ -z "$bug" ]; then
        $drier git push origin HEAD:refs/for/$branch/rfc;
    else
        $drier git push origin HEAD:refs/for/$branch/bug-$bug;
    fi
}

main "$@"