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;
}