Git   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
eloc 118
c 2
b 0
f 0
dl 0
loc 199
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A createNewBranch() 0 19 1
A deleteBranch() 0 8 1
A checkoutBranch() 0 18 1
A fetchAll() 0 12 1
A CommitAndPush() 0 32 1
A Merge() 0 19 1
A Clone() 0 30 1
A createNewBranchIfItDoesNotExist() 0 19 1
A deleteBranchLocally() 0 10 1
A deleteBranchRemotely() 0 10 1
1
<?php
2
3
namespace Sunnysideup\UpgradeToSilverstripe4\Tasks\Helpers;
4
5
use Sunnysideup\UpgradeToSilverstripe4\Traits\HelperInst;
6
7
class Git
8
{
9
    use HelperInst;
10
11
    public function Clone(string $dir, string $gitLink, string $gitRootDir, ?string $branchName = 'master'): Git
12
    {
13
        $this->mu()->execMe(
14
            $dir,
15
            'git clone ' . $gitLink . ' ' . $gitRootDir,
16
            'clone ' . $gitLink . ' into ' . $gitRootDir,
17
            false
18
        );
19
        $this->mu()->execMe(
20
            $gitRootDir,
21
            'git branch -a',
22
            'check branch exists',
23
            false
24
        );
25
        $this->mu()->execMe(
26
            $gitRootDir,
27
            'git checkout ' . $branchName,
28
            'checkout ' . $branchName,
29
            false
30
        );
31
        $this->mu()->execMe(
32
            $gitRootDir,
33
            'git branch',
34
            'confirm branch',
35
            false
36
        );
37
38
        $this->fetchAll($gitRootDir);
39
40
        return $this;
41
    }
42
43
    public function CommitAndPush(string $dir, string $message, string $branchName): Git
44
    {
45
        $this->fetchAll($dir);
46
47
        $this->mu()->execMe(
48
            $dir,
49
            'git add . -A',
50
            'git add all',
51
            false
52
        );
53
54
        $this->mu()->execMe(
55
            $dir,
56
            // 'if ! git diff --quiet; then git commit . -m "'.addslashes($message).'"; fi;',
57
            '
58
            if [ -z "$(git status --porcelain)" ]; then
59
                echo \'OKI DOKI - Nothing to commit\';
60
            else
61
                git commit . -m "' . addslashes($message) . '"
62
            fi',
63
            'commit changes: ' . $message,
64
            false
65
        );
66
67
        $this->mu()->execMe(
68
            $dir,
69
            'git push origin ' . $branchName,
70
            'pushing changes to origin on the ' . $branchName . ' branch',
71
            false
72
        );
73
74
        return $this;
75
    }
76
77
    public function deleteBranch(string $dir, string $branchName): Git
78
    {
79
        $this->fetchAll($dir);
80
81
        $this->deleteBranchLocally($dir, $branchName);
82
        $this->deleteBranchRemotely($dir, $branchName);
83
84
        return $this;
85
    }
86
87
    public function createNewBranchIfItDoesNotExist(string $dir, string $newBranchName, string $fromBranchName): Git
88
    {
89
        $this->fetchAll($dir);
90
91
        $this->checkoutBranch($dir, $fromBranchName);
92
        $this->mu()->execMe(
93
            $dir,
94
            '
95
            if $(git ls-remote --heads ${REPO} ${BRANCH} | grep -q ' . "'refs/heads/" . $newBranchName . "'" . '); then
96
                    echo "branch exists"
97
                else
98
                    git checkout -b ' . $newBranchName . ' ' . $fromBranchName . '
99
                    git push origin ' . $newBranchName . ';
100
            fi',
101
            'create branch ' . $newBranchName . ' from the ' . $fromBranchName . ' branch in ' . $dir,
102
            false
103
        );
104
105
        return $this;
106
    }
107
108
    public function createNewBranch(string $dir, string $newBranchName, string $fromBranch): Git
109
    {
110
        $this->fetchAll($dir);
111
112
        $this->checkoutBranch($dir, $fromBranch);
113
        $this->mu()->execMe(
114
            $dir,
115
            'git checkout -b ' . $newBranchName . ' ' . $fromBranch,
116
            'create and checkout new branch: ' . $newBranchName . ' from ' . $fromBranch,
117
            false
118
        );
119
        $this->mu()->execMe(
120
            $dir,
121
            'git push -u origin ' . $newBranchName,
122
            'push it ' . $fromBranch,
123
            false
124
        );
125
126
        return $this;
127
    }
128
129
    public function checkoutBranch(string $dir, string $branch): Git
130
    {
131
        $this->fetchAll($dir);
132
133
        $this->mu()->execMe(
134
            $dir,
135
            'git checkout -b ' . $branch,
136
            'check out : ' . $branch . ' as a starting point',
137
            false
138
        );
139
        $this->mu()->execMe(
140
            $this->mu()->getGitRootDir(),
141
            'git pull origin ' . $branch,
142
            'get the latest details for : ' . $branch . '',
143
            false
144
        );
145
146
        return $this;
147
    }
148
149
    public function Merge(string $dir, string $fromBranch, string $intoBranch): Git
150
    {
151
        $this->fetchAll($dir);
152
153
        $this->mu()->execMe(
154
            $dir,
155
            '
156
                git checkout ' . $fromBranch . '
157
                git pull origin ' . $fromBranch . '
158
                git checkout ' . $intoBranch . '
159
                git merge --squash ' . $fromBranch . '
160
                git commit . -m "API:  upgrade merge"
161
                git push origin ' . $intoBranch . '
162
            ',
163
            'merging ' . $fromBranch . ' into ' . $intoBranch . ' in ' . $dir,
164
            false
165
        );
166
167
        return $this;
168
    }
169
170
    public function deleteBranchRemotely(string $dir, string $branchName): Git
171
    {
172
        $this->mu()->execMe(
173
            $dir,
174
            'git push origin --delete ' . $branchName,
175
            'delete branch (' . $branchName . ') remotely',
176
            false
177
        );
178
179
        return $this;
180
    }
181
182
    public function fetchAll(string $dir): Git
183
    {
184
        $this->mu()->execMe(
185
            $dir,
186
            'git fetch --all && git branch -a && git status',
187
            'get the latest',
188
            false,
189
            '',
190
            false //verbose = false!
191
        );
192
193
        return $this;
194
    }
195
196
    protected function deleteBranchLocally(string $dir, string $branchName): Git
197
    {
198
        $this->mu()->execMe(
199
            $dir,
200
            'if git show-ref --quiet refs/heads/' . $branchName . '; then git branch -d ' . $branchName . '; git push origin --delete ' . $this->mu()->getNameOfTempBranch() . '; fi',
0 ignored issues
show
Bug introduced by
The method getNameOfTempBranch() does not exist on Sunnysideup\UpgradeToSilverstripe4\ModuleUpgrader. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

200
            'if git show-ref --quiet refs/heads/' . $branchName . '; then git branch -d ' . $branchName . '; git push origin --delete ' . $this->mu()->/** @scrutinizer ignore-call */ getNameOfTempBranch() . '; fi',
Loading history...
Bug introduced by
Are you sure $this->mu()->getNameOfTempBranch() of type Sunnysideup\UpgradeToSil...duleUpgrader|mixed|null can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

200
            'if git show-ref --quiet refs/heads/' . $branchName . '; then git branch -d ' . $branchName . '; git push origin --delete ' . /** @scrutinizer ignore-type */ $this->mu()->getNameOfTempBranch() . '; fi',
Loading history...
201
            'delete branch (' . $branchName . ') locally',
202
            false
203
        );
204
205
        return $this;
206
    }
207
}
208