summary refs log tree commit diff stats
path: root/test/testlib.py
Path not found
'>plain) (blame)
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
#!/bin/bash

# final packages dir
packages_dir="/srv/gitolite/deploy/packages"
# hook work directory
hook_dir="/srv/gitolite/deploy/hook_dir"

function is_initial(){
    local prj_name=$1
    if [ ! -d ${hook_dir}/${prj_name} ]; then
        echo "true"
    else
        echo "false"
    fi
}

function get_remote_rev(){
    echo $(wget --no-check-certificate -qO- $1)
}

function valid_url(){
    if [[ `wget -S --spider $1 --no-check-certificate 2>&1 | grep 'HTTP/1.1 200 OK'` ]];
    then
        echo "true";
    fi
}

function create_package(){
    # project name
    local prj_name=$1
    # git repository directory
    local git_dir=$2
    # last/old commit revision
    local pkg_old=$3
    # new commit revision
    local pkg_new=$4
    # script deploy call when extracting this package
    local pkg_script=$5

    local pkg_new7=$(echo $pkg_new | cut -c1-7)

    # project directory
    local prj_dir="${hook_dir}/${prj_name}"
    # package directory
    local pkg_dir="${prj_dir}/${pkg_new7}"
    # final tar file
    local pkg_tar="${packages_dir}/${prj_name}_${pkg_new7}.tar.gz"

    # if temporary work directory exists maybe other process is creating packages
    if [ -d "$pkg_dir" ]; then
        echo "Deploy: temporary directory ${pkg_dir} exists, maybe other precess"
        exit 1
    fi
    # create temporary directory for this package
    mkdir -p ${pkg_dir}

    echo "Deploy: ${prj_name} ${pkg_new7} package call ${pkg_script} on deploy."

    # save metadata to be used by deploy script
    echo $prj_name > ${pkg_dir}/project
    echo $pkg_script >> ${pkg_dir}/project
    echo $pkg_new >> ${pkg_dir}/project

    # if is a valid old commit create a package with changes since then
    # else create a full package (all files)
    local is_commit=$(git --git-dir=${git_dir} cat-file -t ${pkg_old} 2>&1)
    if [[ $is_commit = "commit" ]]; then
        echo "Deploy: creating package from old commit."
        # list with files to extract (Added Copied Modified Renamed)
        file_list=$(git --git-dir=${git_dir} --no-pager diff \
            --diff-filter=ACMR \
            --name-only ${pkg_old} ${pkg_new})

        # create tar archive with same name as commit hash with files
        git --git-dir=${git_dir} archive -o ${pkg_dir}/files.tar.xz ${pkg_new} ${file_list}

        # first we create list of files to be removed
        git --git-dir=${git_dir} --no-pager diff \
            --diff-filter=DR \
            --name-status -t ${pkg_old} ${pkg_new} | cut -f 2 > ${pkg_dir}/deleted

        # save old commit on metadata
        echo $pkg_old >> ${pkg_dir}/project
    else
        echo "Deploy: creating initial package."
        git --git-dir=${git_dir} archive -o ${pkg_dir}/files.tar.xz ${pkg_new}

    fi

    tar -zcpf ${pkg_tar} --directory=${pkg_dir} .

    echo "Deploy: package ${pkg_tar} ready !"
    rm -r ${pkg_dir}
    return 0
}