Execute a git command
static public exec ( $cmd, $input = null, $gitdir = null, $worktree = null, $allow_guess = false, $ignore_git_errors = false ) |
/** * 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 sync_site_state() { ignore_user_abort(true); # verify repo setup, which also makes sure the repo setup (hooks, config, # etc) is up to date: self::repair_repo_setup(); # assure gitblog submodule is set up $dotgitmodules = gb::$site_dir . '/.gitmodules'; if (!is_file($dotgitmodules) || !preg_match('/\\[submodule[\\s\\t ]+"gitblog"\\]/', file_get_contents($dotgitmodules))) { self::add_gitblog_submodule(); } # read id of HEAD and current branch $gb_branch = 'master'; $gb_head = '0000000000000000000000000000000000000000'; try { $branches = trim(git::exec('branch --no-abbrev --verbose --no-color', null, gb::$dir . '/.git', gb::$dir)); foreach (explode("\n", $branches) as $line) { if (!$line) { continue; } if ($line[0] === '*') { if (strpos($line, '(no branch)') !== false) { $line = preg_split('/[ \\t]+/', $line, 5); $gb_branch = null; $gb_head_id = $line[3]; } else { $line = preg_split('/[ \\t]+/', $line, 4); $gb_branch = $line[1]; $gb_head_id = $line[2]; } break; } } } catch (GitError $e) { gb::log(LOG_WARNING, 'failed to read branch info for gitblog -- git: %s', $e->getMessage()); } # no previous state? if (!gb::$site_state) { gb::$site_state = array(); } # Set current values gb::$site_state['url'] = gb::$site_url; gb::$site_state['version'] = gb::$version; gb::$site_state['posts_pagesize'] = gb::$posts_pagesize; # appeard in 0.1.3: gb::$site_state['gitblog'] = array('branch' => $gb_branch, 'head' => $gb_head_id); # Write site url for hooks $bytes_written = file_put_contents(gb::$site_dir . '/.git/info/gitblog-site-url', gb::$site_url, LOCK_EX); # Encode site.json $json = json::pretty(gb::$site_state) . "\n"; $path = gb::$site_dir . '/data/site.json'; # create data/ ? if (!is_dir(gb::$site_dir . '/data')) { mkdir(gb::$site_dir . '/data', 0775); chmod(gb::$site_dir . '/data', 0775); } # Write site.json $bytes_written += file_put_contents($path, $json, LOCK_EX); chmod($path, 0664); gb::log(LOG_NOTICE, 'wrote site state to %s (%d bytes)', $path, $bytes_written); return $bytes_written; }
/** Retrieve ids for $pathspec (string or array of strings) at the current branch head */ static function id_for_pathspec($pathspec) { if (is_string($pathspec)) { return trim(git::exec('rev-parse ' . escapeshellarg(':' . $pathspec))); } if (!is_array($pathspec)) { throw new InvalidArgumentException('$pathspec is ' . gettype($pathspec)); } # array $pathspec_to_id = array(); foreach ($pathspec as $k => $s) { $pathspec[$k] = escapeshellarg(':' . $s); } $lines = explode("\n", trim(git::exec('rev-parse ' . implode(' ', $pathspec)))); $i = 0; foreach ($pathspec as $s) { $pathspec_to_id[$s] = $lines[$i++]; } return $pathspec_to_id; }
/** Batch-reload objects */ function reloadObjects($objects) { $names = array(); $ids = array(); # Demux foreach ($objects as $id => $obj) { $names[] = $obj->name; $ids[] = $id; } # Load commits $commits = GitCommit::find(array('names' => $names, 'mapnamestoc' => true)); $commitsbyname = $commits[2]; # Load blobs $out = git::exec("cat-file --batch", implode("\n", $ids)); $p = 0; $numobjects = count($objects); # Parse object blobs for ($i = 0; $i < $numobjects; $i++) { # <id> SP <type> SP <size> LF # <contents> LF $hend = strpos($out, "\n", $p); $h = explode(' ', substr($out, $p, $hend - $p)); $size = 0; $data = null; $dstart = $hend + 1; if ($h[1] === 'missing') { throw new UnexpectedValueException('missing blob ' . $obj->id . ' ' . var_export($obj->name, 1) . ' in repo stage'); } $obj = $objects[$h[0]]; $size = intval($h[2]); $data = substr($out, $dstart, $size); $obj->reload($data, isset($commitsbyname[$obj->name]) ? $commitsbyname[$obj->name] : array()); $p = $dstart + $size + 1; } }
# secret $secret = ''; while (strlen($secret) < 62) { mt_srand(); $secret .= base_convert(mt_rand(), 10, 36); } $s = preg_replace('/^(gb::\\$secret[\\t ]*=[\\t ]*)\'\';/m', '${1}' . var_export($secret, 1) . ";", $s, 1); file_put_contents($config_path, $s); chmod($config_path, 0660); # reload config require $config_path; } # ------------------------------------------------------------------------- # Can git be found and if so, what version? try { $version = array_pop(explode(' ', trim(git::exec("--version")))); $version = array_map('intval', explode('.', $version)); if ($version[0] < 1 || $version[1] < 6) { gb::$errors[] = '<b>To old git version.</b> Gitblog requires git version 1.6 or newer. Please upgrade your git. (' . h(`which git`) . ')'; } } catch (GitError $e) { gb::$errors[] = '<b>git not found in $PATH</b><br/><br/> If git is not installed, please install it. Otherwise you need to update <tt>PATH</tt>. Putting something like this in <tt>gb-config.php</tt> would do it:<br/><br/> <code>$_ENV[\'PATH\'] .= \':/opt/local/bin\';</code><br/><br/> <tt>/opt/local/bin</tt> being the directory in which git is installed. Alternatively edit PATH in your php.ini file.<br/><br/>
/** 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); }