/** * Rebuild caches, indexes, etc. */ static function rebuild($forceFullRebuild = false) { gb::log(LOG_NOTICE, 'rebuilding cache' . ($forceFullRebuild ? ' (forcing full rebuild)' : '')); $time_started = microtime(1); $failures = array(); # Load rebuild plugins gb::load_plugins('rebuild'); # Load rebuilders if needed if (empty(self::$rebuilders)) { self::loadRebuilders(); } # Create rebuilder instances $rebuilders = array(); foreach (self::$rebuilders as $cls) { $rebuilders[] = new $cls($forceFullRebuild); } # Load rebuild plugins (2nd offer) gb::load_plugins('rebuild'); # Query ls-tree $ls = rtrim(git::exec('ls-files --stage')); if ($ls) { # Iterate objects $ls = explode("\n", $ls); foreach ($ls as $line) { try { # <mode> SP <object> SP <stage no> TAB <name> if (!$line) { continue; } $line = explode(' ', $line, 3); $id = $line[1]; $name = gb_normalize_git_name(substr($line[2], strpos($line[2], "\t") + 1)); foreach ($rebuilders as $rebuilder) { $rebuilder->onObject($name, $id); } } catch (RuntimeException $e) { gb::log(LOG_ERR, 'failed to rebuild object %s %s: %s', var_export($name, 1), $e->getMessage(), $e->getTraceAsString()); $failures[] = array($rebuilder, $name); } } } # Let rebuilders finalize foreach ($rebuilders as $rebuilder) { try { $rebuilder->finalize(); } catch (RuntimeException $e) { gb::log(LOG_ERR, 'rebuilder %s (0x%x) failed to finalize: %s', get_class($rebuilder), spl_object_hash($rebuilder), GBException::format($e, true, false, null, 0)); $failures[] = array($rebuilder, null); } } gb::log(LOG_NOTICE, 'cache updated -- time spent: %s', gb_format_duration(microtime(1) - $time_started)); return $failures; }
static function ls_basic($args = null, $paths = null) { $args = self::escargs($args); $paths = self::escargs($paths); $ls = rtrim(self::exec('ls-files -z ' . $args . ' -- ' . $paths)); $files = array(); if ($ls) { # Iterate objects $ls = explode("", $ls); foreach ($ls as $line) { if (!$line) { continue; } $files[] = gb_normalize_git_name($line); } } return $files; }
/** Returns array($commits, $existing, $ntoc) */ static function find($kwargs = null) { static $defaultkwargs = array('names' => null, 'treeish' => 'HEAD', 'limit' => -1, 'sortrcron' => true, 'detectRC' => true, 'mapnamestoc' => false); $kwargs = $kwargs ? array_merge($defaultkwargs, $kwargs) : $defaultkwargs; $commits = array(); $existing = array(); # tracks existing files $ntoc = $kwargs['mapnamestoc'] ? array() : null; $cmd = "log --name-status --pretty='format:" . self::$logFormat . "' " . "--encoding=UTF-8 --date=iso --dense"; # do not sort reverse chronological $rcron = $kwargs['sortrcron']; if (!$rcron) { $cmd .= ' --reverse'; } # detect renames and copies if ($kwargs['detectRC']) { $cmd .= ' -C'; } else { $cmd .= ' --no-renames'; } # limit if ($kwargs['limit'] > 0) { $cmd .= " --max-count=" . $kwargs['limit']; } # treeish $cmd .= " " . $kwargs['treeish'] . " -- "; # filter object names if ($kwargs['names']) { $cmd .= implode(' ', array_map('escapeshellarg', $kwargs['names'])); } #var_dump($cmd); $out = git::exec($cmd); #var_dump($out); $a = 0; $len = strlen($out); while ($a !== false && $a <= $len) { $c = new self(); foreach (self::$fields as $field) { if ($field == 'message') { $b = strpos($out, "", $a); } else { $b = strpos($out, "\n", $a); } if ($b === false) { break; } if (substr($field, -4) == 'Date') { $c->{$field} = new GBDateTime(substr($out, $a, $b - $a)); } else { $c->{$field} = substr($out, $a, $b - $a); } $a = $b + 1; } if ($b === false) { break; } $b = strpos($out, "\n\n", $a); $files = $b === false ? substr($out, $a) : substr($out, $a, $b - $a); $files = explode("\n", trim($files)); $c->files = array(); foreach ($files as $line) { $line = explode("\t", $line); if (count($line) < 2) { continue; } $t = $line[0][0]; $name = gb_normalize_git_name($line[1]); $previousName = null; # R|C have two names wherether the last is the new name if ($t === GitPatch::RENAME || $t === GitPatch::COPY) { $previousName = $name; $name = $line[2]; if ($c->previousFiles === null) { $c->previousFiles = array($previousName); } else { $c->previousFiles[] = $previousName; } } # add to files[tag] => [name, ..] if (isset($c->files[$t])) { $c->files[$t][] = $name; } else { $c->files[$t] = array($name); } # if kwarg mapnamestoc == true if ($ntoc !== null) { if (!isset($ntoc[$name])) { $ntoc[$name] = array($c); } else { $ntoc[$name][] = $c; } } # update cached objects #if (isset($repo->objectCacheByName[$name])) { # $obj = $repo->objectCacheByName[$name]; # if ($obj->_commit === null) # $obj->_commit = $c; #} # update existing if (!$rcron) { if ($t === GitPatch::CREATE || $t === GitPatch::COPY) { $existing[$name] = $c; } elseif ($t === GitPatch::DELETE && isset($existing[$name])) { unset($existing[$name]); } elseif ($t === GitPatch::RENAME) { if (isset($existing[$previousName])) { # move original CREATE $existing[$name] = $existing[$previousName]; unset($existing[$previousName]); } else { $existing[$name] = $c; } # move commits from previous file if kwarg mapnamestoc == true if ($ntoc !== null && isset($ntoc[$previousName])) { $ntoc[$name] = array_merge($ntoc[$previousName], $ntoc[$name]); unset($ntoc[$previousName]); } } } } $commits[$c->id] = $c; if ($b === false) { break; } $a = $b + 2; } return array($commits, $existing, $ntoc); }