-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainClass.php
154 lines (130 loc) · 4.27 KB
/
MainClass.php
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env php
<?php
namespace App;
require_once(__DIR__."/../vendor/autoload.php");
use Exception;
use stdClass;
set_time_limit(1200); // 20 minutes.
class MainClass extends stdClass
{
protected string $version = "1.0.0";
protected string $appName = "Github to Bitbucket Backup Repo Updater";
protected Array|null $out = null;
public function __construct() {}
public function handle() {
try {
$bitbucketDir = implode(
"/",
[
getcwd(),
"bitbucket",
]
);
echo "Making bitbucket directory: " .
$bitbucketDir . PHP_EOL;
$bitbucketDirExists = true === file_exists($bitbucketDir);
echo "Directory already exists? " .
($bitbucketDirExists ? "true" : "false") .
PHP_EOL;
if ($bitbucketDirExists) {
throw new Exception(
"The bitbucket folder already exists in current directory." .
PHP_EOL
);
}
mkdir($bitbucketDir);
echo "Created directory: $bitbucketDir" .
PHP_EOL;
$config = json_decode(
file_get_contents(implode("/", [
getcwd(),
"config.json"
])),
true
);
$keys = array_keys($config["repos"]);
for($i = 0; $i < sizeof($keys); $i++) {
$repoName = $keys[$i];
echo "Updating repo: $repoName".PHP_EOL;
exec(
"git clone " .
$config["repos"][$repoName]
["github"]
["origin"] . " " .
implode("/", [
getcwd(),
"bitbucket",
$repoName
])
);
chdir(implode("/", [
getcwd(),
"bitbucket",
$repoName
]));
exec(
"git remote add bitbucket " .
$config["repos"][$repoName]
["bitbucket"]
["origin"],
$this->out
);
echo "Git replied: " .
implode("\n", $this->out) .
PHP_EOL;
$this->out = null;
echo "Pushing to bitbucket " .
$config["repos"][$repoName]
["bitbucket"]
["branch"] .
PHP_EOL;
exec(
"git push bitbucket " .
$config["repos"][$repoName]
["bitbucket"]
["branch"],
$this->out
);
echo "Git replied: " .
implode("\n", $this->out) .
PHP_EOL;
$this->out = null;
chdir(implode("/", [
getcwd(),
"..",
".."
]));
$this->repoCleanup($repoName);
echo "Successfully updated repo: $repoName" .
PHP_EOL;
}
$this->cleanup();
} catch (Exception $e) {
echo print_r($e->getMessage(), true);
}
}
protected function repoCleanup($repoName) {
$fullRepoPath = implode("/", [
getcwd(),
"bitbucket",
$repoName
]);
echo "Removing directory " .
$fullRepoPath . PHP_EOL;
exec("rm -rf $fullRepoPath");
echo "Removed directory " .
$fullRepoPath . PHP_EOL;
}
protected function cleanup() {
$fullBitbucketPath = implode("/", [
getcwd(),
"bitbucket"
]);
echo "Removing directory " .
$fullBitbucketPath . PHP_EOL;
exec("rm -rf $fullBitbucketPath");
echo "Removed directory " .
$fullBitbucketPath . PHP_EOL;
}
}
(new MainClass())->handle();