Completed
Push — master ( ddcec3...9710fb )
by Matthias
02:13
created

None::getPreviousCommit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace MatthiasMullie\CI\Providers;
4
5
use MatthiasMullie\CI\Environment;
6
7
/**
8
 * @author Matthias Mullie <[email protected]>
9
 * @copyright Copyright (c) 2016, Matthias Mullie. All rights reserved.
10
 * @license LICENSE MIT
11
 */
12
class None implements Environment
13
{
14
    /**
15
     * {@inheritdoc}
16
     */
17
    public static function isCurrent()
18
    {
19
        return false;
20
    }
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function getProvider()
26
    {
27
        return 'none';
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function getRepo()
34
    {
35
        if (!$this->isGitRepo()) {
36
            return '';
37
        }
38
39
        return exec('git config --get remote.origin.url');
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function getSlug()
46
    {
47
        // when using any of the popular cloud git providers (or anything that's
48
        // modeled after the same username\project model), the repo name will
49
        // likely be included in this fashion... at least we can try :)
50
        $url = $this->getRepo();
51
        $found = preg_match('/([^:\/]+\/[^:\/]+?)(\.git|$)/', $url, $matches);
52
53
        return $found ? $matches[1] : '';
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function getBranch()
60
    {
61
        if (!$this->isGitRepo()) {
62
            return '';
63
        }
64
65
        $branches = shell_exec('git branch');
66
        preg_match('/^\* (.+)$/m', $branches, $branch);
67
68
        return isset($branch) ? $branch[1] : '';
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function getPullRequest()
75
    {
76
        if (!$this->isGitRepo()) {
77
            return '';
78
        }
79
80
        $head = shell_exec('test -f .git/FETCH_HEAD && cat .git/FETCH_HEAD');
81
        $commit = preg_quote($this->getCommit(), '/');
82
        preg_match("/^$commit\\s+'refs\\/pull\\/(.+)\\/head'/m", $head, $match);
83
84
        return isset($match[1]) ? $match[1] : '';
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function getCommit()
91
    {
92
        if (!$this->isGitRepo()) {
93
            return '';
94
        }
95
96
        return exec('git log --pretty=format:"%H" -1');
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function getPreviousCommit()
103
    {
104
        if (!$this->isGitRepo()) {
105
            return '';
106
        }
107
108
        return exec('git log --pretty=format:"%H" -1 --skip=1');
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function getAuthor()
115
    {
116
        if (!$this->isGitRepo()) {
117
            return '';
118
        }
119
120
        return exec('git log --pretty=format:"%aN" -1');
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function getAuthorEmail()
127
    {
128
        if (!$this->isGitRepo()) {
129
            return '';
130
        }
131
132
        return exec('git log --pretty=format:"%aE" -1');
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138
    public function getTimestamp()
139
    {
140
        if (!$this->isGitRepo()) {
141
            return '';
142
        }
143
144
        return exec('git log --pretty=format:"%aI" -1');
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150
    public function getBuild()
151
    {
152
        return '';
153
    }
154
155
    /**
156
     * @return bool
157
     */
158
    protected function isGitRepo()
159
    {
160
        exec('git status 2>&1', $output, $status);
161
162
        return $status === 0;
163
    }
164
}
165