-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHgAddTask.php
200 lines (184 loc) · 5.12 KB
/
HgAddTask.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
/**
* Utilise Mercurial from within Phing.
*
* PHP Version 5.4
*
* @category Tasks
* @package phing.tasks.ext
* @author Ken Guest <[email protected]>
* @license LGPL (see http://www.gnu.org/licenses/lgpl.html)
* @link https://github.com/kenguest/Phing-HG
*/
namespace Phing\Task\Ext\Hg;
use Phing\Exception\BuildException;
use Phing\Project;
use Phing\Type\FileSet;
use Phing\Type\Element\FileSetAware;
/**
* Integration/Wrapper for hg add
*
* @category Tasks
* @package phing.tasks.ext.hg
* @author Ken Guest <[email protected]>
* @license LGPL (see http://www.gnu.org/licenses/lgpl.html)
* @link HgAddTask.php
*/
class HgAddTask extends HgBaseTask
{
use FileSetAware;
/**
* Array of files to ignore
*
* @var string[]
*/
protected $ignoreFile = [];
/**
* The main entry point method.
*
* @throws BuildException
* @return void
*/
public function main()
{
$filesAdded = false;
$clone = $this->getFactoryInstance('add');
$clone->setQuiet($this->getQuiet());
$cwd = getcwd();
$project = $this->getProject();
if ($this->repository === '') {
$dir = $project->getProperty('application.startdir');
} else {
$dir = $this->repository;
}
if (!file_exists($dir)) {
throw new BuildException("\"$dir\" does not exist.");
}
if (!is_dir($dir)) {
throw new BuildException("\"$dir\" is not a directory.");
}
chdir($dir);
if (file_exists('.hgignore')) {
$this->loadIgnoreFile();
}
if (count($this->filesets)) {
$this->log('filesets set', Project::MSG_DEBUG);
/**
* $fs is a FileSet
*
* @var $fs FileSet
*/
foreach ($this->filesets as $fs) {
$ds = $fs->getDirectoryScanner($project);
$fromDir = $fs->getDir($project);
if ($fromDir->getName() === '.') {
$statusClone = $this->getFactoryInstance('status');
$statusClone->setUnknown(true);
$statusClone->setNoStatus(true);
$statusClone->setRepository($this->getRepository());
$statusOut = $statusClone->execute();
if ($statusOut !== '') {
$files = explode(PHP_EOL, $statusOut);
foreach ($files as $file) {
if ($file != '') {
$clone->addFile($file);
$filesAdded = true;
}
}
}
}
}
}
if ($filesAdded) {
try {
$this->log("Executing: " . $clone->asString(), Project::MSG_INFO);
$output = $clone->execute();
if ($output !== '') {
$this->log($output);
}
} catch (\Exception $ex) {
$msg = $ex->getMessage();
$this->log("Exception: $msg", Project::MSG_INFO);
$p = strpos($msg, 'hg returned:');
if ($p !== false) {
$msg = substr($msg, $p + 13);
}
chdir($cwd);
throw new BuildException($msg);
}
}
chdir($cwd);
}
/**
* Load .hgignore file.
*
* @return void
*/
public function loadIgnoreFile()
{
$ignores = [];
$lines = file('.hgignore');
foreach ($lines as $line) {
$nline = trim($line);
$nline = preg_replace('/\/\*$/', '/', $nline);
$ignores[] = $nline;
}
$this->ignoreFile = $ignores;
}
/**
* Determine if a file is to be ignored.
*
* @param string $file filename
*
* @return bool
*/
public function fileIsIgnored($file)
{
$line = $this->ignoreFile[0];
$mode = 'regexp';
$ignored = false;
if (
preg_match('#^syntax\s*:\s*(glob|regexp)$#', $line, $matches)
|| $matches[1] === 'glob'
) {
$mode = 'glob';
}
if ($mode === 'glob') {
$ignored = $this->ignoredByGlob($file);
} elseif ($mode === 'regexp') {
$ignored = $this->ignoredByRegex($file);
}
return $ignored;
}
/**
* Determine if file is ignored by glob pattern.
*
* @param string $file filename
*
* @return bool
*/
public function ignoredByGlob($file)
{
$lfile = $file;
if (strpos($lfile, './') === 0) {
$lfile = substr($lfile, 2);
}
foreach ($this->ignoreFile as $line) {
if (strpos($lfile, $line) === 0) {
return true;
}
}
return false;
}
/**
* Is file ignored by regex?
*
* @param string $file Filename
*
* @return bool
*/
public function ignoredByRegex($file)
{
return true;
}
}