forked from sdboyer/drupalorg-git
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshared.php
114 lines (98 loc) · 2.62 KB
/
shared.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
<?php
if (!defined('LOGLEVEL')) {
// Let an environment variable set the log level
$level = getenv('LOGLEVEL');
if (is_string($level)) {
define('LOGLEVEL', (int) $level);
}
else {
// Or default to 'normal'
define('LOGLEVEL', 3);
}
}
function git_invoke($command, $fail_safe = FALSE, $repository_path = NULL, $env = NULL) {
if (!isset($env)) {
$env = $_ENV;
}
if ($repository_path) {
$env['GIT_DIR'] = $repository_path;
}
$descriptor_spec = array(
1 => array('pipe', 'w'),
2 => array('pipe', 'w'),
);
git_log('Invoking ' . $command, 'DEBUG');
$process = proc_open($command, $descriptor_spec, $pipes, NULL, $env);
if (is_resource($process)) {
$stdout = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[2]);
$return_code = proc_close($process);
if ($return_code != 0 && !$fail_safe) {
throw new Exception("Invocation of '" . $command . "' failed with return code " . $return_code .": \n" . $stdout . $stderr);
}
return $stdout;
}
}
function is_empty_dir($dir){
$files = @scandir($dir);
return (!$files || count($files) <= 2);
}
/**
* Check if directory has any CVS information.
*
* This is actually sort of a recursive problem. If any subdirectory has
* CVS information it can be imported.
*/
function is_cvs_dir($dir) {
$files = @scandir($dir);
// If there are no files, fail early.
if (!$files) {
return FALSE;
}
foreach ($files as $file) {
$absolute = $dir . '/' . $file;
// Skip POSIX aliases
if ($file == '.' || $file == '..') continue;
if (is_dir($absolute) && $file == 'Attic') {
return TRUE;
}
elseif (strpos($file, ',v') !== FALSE) {
return TRUE;
}
elseif (is_dir($absolute) && is_cvs_dir($absolute)) {
return TRUE;
}
}
return FALSE;
}
/**
* Recursively delete a directory on a local filesystem.
*
* @param string $path
* The path to the directory.
*/
function rmdirr($path) {
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::CHILD_FIRST) as $item) {
$item->isFile() ? unlink($item) : rmdir($item);
}
rmdir($path);
}
function git_log($message, $level = 'NORMAL', $project = NULL) {
$loglevels = array(
'WARN' => 1,
'QUIET' => 2,
'NORMAL' => 3,
'INFO' => 4,
'DEBUG' => 5,
);
if (LOGLEVEL !== 0 && LOGLEVEL >= $loglevels[$level]) {
if (isset($project)) {
echo "[" . date('Y-m-d H:i:s') . "] [$level] [$project] $message\n";
}
else {
echo "[" . date('Y-m-d H:i:s') . "] [$level] $message\n";
}
}
}