public function commit($path = null)
{
if (!$path) {
foreach (array_keys($this->_staged['change']) as $path) {
$this->commit($path);
}
foreach ($this->_staged['move'] as $path => $to) {
if (isset($this->_staged['change'][$path])) {
continue;
}
$this->commit($path);
}
foreach ($this->_staged['delete'] as $path) {
$this->commit($path);
}
$Folder = new Folder(TMP . 'upgrade');
$Folder->delete();
return;
}
$dryRun = !empty($this->params['dry-run']);
$isMove = isset($this->_staged['move'][$path]);
$isChanged = isset($this->_staged['change'][$path]) && count($this->_staged['change'][$path]) > 1;
$isDelete = in_array($path, $this->_staged['delete']);
if (!$isMove && !$isChanged && !$isDelete) {
return;
}
$gitCd = sprintf('cd %s && ', escapeshellarg(dirname($path)));
if ($isDelete) {
$this->out(sprintf('<info>Delete %s</info>', Debugger::trimPath($path)));
if ($dryRun) {
return true;
}
if (!empty($this->params['git'])) {
exec($gitCd . sprintf('git rm -f %s', escapeshellarg($path)));
return;
}
if (is_dir($path)) {
$Folder = new Folder($path);
return $Folder->delete();
}
$File = new File($path, true);
return $File->delete();
}
if ($isMove && !$isChanged) {
$to = $this->_staged['move'][$path];
$this->out(sprintf('<info>Move %s to %s</info>', Debugger::trimPath($path), Debugger::trimPath($to)));
if ($dryRun || !file_exists($path)) {
return true;
}
if (!empty($this->params['git'])) {
return $this->_gitMove($gitCd, $path, $to);
}
if (is_dir($path)) {
$Folder = new Folder($path);
return $Folder->move($to);
}
$File = new File($to, true);
return $File->write(file_get_contents($path)) && unlink($path);
}
$start = reset($this->_staged['change'][$path]);
end($this->_staged['change'][$path]);
$final = end($this->_staged['change'][$path]);
$oPath = TMP . 'upgrade' . DS . $start;
$uPath = TMP . 'upgrade' . DS . $final;
exec('git diff --no-index ' . escapeshellarg($oPath) . ' ' . escapeshellarg($uPath), $output);
$output = implode($output, "\n");
$i = strrpos($output, $final);
$diff = substr($output, $i + 41);
if ($isMove) {
$to = $this->_staged['move'][$path];
$this->out(sprintf('<info>Move %s to %s and update</info>', Debugger::trimPath($path), Debugger::trimPath($to)));
} else {
$this->out(sprintf('<info>Update %s</info>', Debugger::trimPath($path)));
}
$this->out($diff, 1, $dryRun ? Shell::NORMAL : SHELL::VERBOSE);
if ($dryRun || !file_exists($path)) {
return true;
}
if ($isMove) {
if (!empty($this->params['git'])) {
$this->_gitMove($gitCd, $path, $to);
} else {
unlink($path);
}
$path = $to;
}
$File = new File($path, true);
return $File->write(file_get_contents($uPath));
}