Completed
Push — master ( 7f4001...3bb096 )
by Matthias
02:06
created

None::getTimestamp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 13
rs 9.4285
cc 2
eloc 8
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
     * Git has an exact ISO 8601 format (only since August 2014): %aI.
137
     * However, since it's relatively young, it isn't yet available
138
     * everywhere. There's another very similar format, though: %ai.
139
     *
140
     * This is an example of what %ai gives, and what it should look like:
141
     * Current: 2016-02-18T16:02:46+01:00
142
     * Correct: 2016-02-18 16:02:46 +0100
143
     * Close enough. Let's just transform it to how we want it to look...
144
     *
145
     * @see https://github.com/git/git/commit/466fb6742d7fb7d3e6994b2d0d8db83a8786ebcf
146
     *
147
     * {@inheritdoc}
148
     */
149
    public function getTimestamp()
150
    {
151
        if (!$this->isGitRepo()) {
152
            return '';
153
        }
154
155
        $timestamp = exec('git log --pretty=format:"%ai" -1');
156
        $timestamp = preg_replace('/ /', 'T', $timestamp, 1);
157
        $timestamp = str_replace(' ', '', $timestamp);
158
        $timestamp = preg_replace('/([0-5]{2})([0-5]{2})$/', '$1:$2', $timestamp);
159
160
        return $timestamp;
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166
    public function getBuild()
167
    {
168
        return '';
169
    }
170
171
    /**
172
     * @return bool
173
     */
174
    protected function isGitRepo()
175
    {
176
        exec('git status 2>&1', $output, $status);
177
178
        return $status === 0;
179
    }
180
}
181