foreach ($state_fields as $k => $discard) { if ($k === 'body') { $modified_state[$k] = $post->rawBody(); } else { $v = $post->{$k}; if ($v instanceof GBDateTime) { $v = strval($v); } $modified_state[$k] = $v; } } } # commit? if ($input['commit']) { git::add($post->name); git::commit(($created ? 'Created' : 'Updated') . ' post ' . r($post->title), gb::$authorized, $post->name); } # build response entity $rsp = array('name' => $post->name, 'version' => $post->id, 'exists' => $post->exists(), 'isTracked' => $post->isTracked(), 'isDirty' => $post->isDirty(), 'state' => $modified_state); # status $status = '200 OK'; if ($created) { $status = '201 Created'; } # send JSON response gb_admin::json_rsp($rsp, $status); gb::log('saved post %s', $post->name); } catch (Exception $e) { gb::log('failed to save post: %s', GBException::format($e, true, false, null, 0)); gb_admin::json_rsp($e->getMessage(), '400 Bad Request'); }
static function add_gitblog_submodule() { # Add gitblog submodule $roundtrip_temp = false; $dotgitmodules = gb::$dir . '/.gitmodules'; $did_have_dotgitmodules = is_file($dotgitmodules); $added = array(); $origin_url = 'git://github.com/rsms/gitblog.git'; # first, find the origin url if any $broken_gitblog_repo = false; if (is_dir(gb::$dir . '/.git')) { try { gb::log('deducing remote.origin.url for existing gitblog'); $s = trim(git::exec('config remote.origin.url', null, gb::$dir . '/.git', gb::$dir)); if ($s) { $origin_url = $s; } } catch (GitError $e) { gb::log(LOG_WARNING, 'failed to read config remote.origin.url: %s', $e->getMessage()); $broken_gitblog_repo = true; } } # if gitblog is not a repo or broken, rename existing and clone a fresh copy from upstream if ($broken_gitblog_repo) { $stash_dir = gb::$dir . '.old'; $i = 1; while (file_exists($stash_dir)) { $stash_dir = gb::$dir . '.old' . $i++; } gb::log('moving broken gitblog %s to %s', gb::$dir, $stash_dir); if (!rename(gb::$dir, $stash_dir)) { # Note: This is tricky. If we get here, it probably means we are unable to # write in dirname(gb::$dir) and gb::$site_dir which we will try to do # further down, where we clone a new copy, which will most likely fail # because we can not create a new directory called "gitblog" due to lack of # priveleges. # # Now, one solution would be to: # # git clone origin /tmp/xy # mkdir ./gitblog/old # mv ./gitblog/(?!old)+ ./gitblog/old/ # mv /tmp/xy/* ./gitblog/ # rm -rf ./gitblog/old # # But as this is a very thin use case, almost vanishingly small since # the gitblog itself can not function w/o write rights, we die hard: gb::log(LOG_CRIT, 'unable to replace gitblog with gitblog submodule (mv %s %s) -- directory not writable? Aborting.', gb::$dir, $stash_dir); exit; } } try { # remove "/gitblog" ignore from .gitignore if (self::gitignore_sub('/(?:\\r?\\n)\\/gitblog([\\t\\s \\r\\n]+|^)/m', '$1')) { $added[] = git::add('.gitignore'); } # register (and clone if needed) the gitblog submodule. This might take some time. try { git::exec('submodule --quiet add -b ' . escapeshellarg(self::$branch) . ' -- ' . escapeshellarg($origin_url) . ' gitblog'); # add gitblog $added[] = git::add('gitblog'); } catch (GitError $e) { if (strpos($e->getMessage(), 'already exists in the index') === false) { throw $e; } } # move back old shallow gitblog dir if ($roundtrip_temp) { $old_dst = gb::$dir . '/gitblog.old'; gb::log('moving %s to %s', $roundtrip_temp, $old_dst); if (!@rename($roundtrip_temp, $old_dst)) { gb::log(LOG_ERR, 'failed to move back %s to %s', $roundtrip_temp, $old_dst); } # we want to explicitly checkout the branch we requested git::exec('checkout ' . escapeshellarg(self::$branch), null, gb::$dir . '/.git', gb::$dir); } } catch (Exception $e) { # move back gitblog dir if ($roundtrip_temp) { gb::log('moving %s to %s', $roundtrip_temp, gb::$dir); if (!@rename($roundtrip_temp, gb::$dir)) { gb::log(LOG_ERR, 'failed to move back %s to %s', $roundtrip_temp, gb::$dir); } } # forward exception throw $e; } # if .submodules did not exist when we started, track it if (!$did_have_dotgitmodules && is_file(gb::$site_dir . '/.gitmodules')) { $added[] = git::add('.gitmodules'); } # commit any modifications if ($added) { try { git::commit('added ' . implode(', ', $added), GBUser::findAdmin()->gitAuthor(), $added); } catch (GitError $e) { if (strpos($e->getMessage(), 'no changes added to commit') === false) { throw $e; } } } }
<?php require_once "../../classes/git.class.php"; $git = new git(); $git->commit(); ?>
function importChannel(DOMNode $channel, $commit) { $channel_name = $channel->getElementsByTagName('title')->item(0)->nodeValue; $this->report('Importing channel "' . h($channel_name) . '"'); $fallbackTZOffset = $this->deduceChannelTimezoneOffset($channel); $count_posts = 0; $count_pages = 0; $count_attachments = 0; $timer = microtime(1); git::reset(); # rollback any previously prepared commit try { foreach ($channel->getElementsByTagName('item') as $item) { if ($item->nodeType !== XML_ELEMENT_NODE) { continue; } $obj = $this->importItem($item, $fallbackTZOffset); if (!$obj) { continue; } if ($obj instanceof GBExposedContent) { $this->postProcessExposedContent($obj); $this->report('Importing ' . ($obj instanceof WPPost ? 'post' : 'page') . ' ' . h($obj->name) . ' "' . h($obj->title) . '" by ' . h($obj->author->name) . ' published ' . $obj->published); if ($this->writeExposedContent($obj)) { if ($obj instanceof WPPost) { $count_posts++; } else { $count_pages++; } } } elseif ($obj instanceof WPAttachment) { $this->postProcessAttachment($obj); $this->report('Importing attachment ' . h($obj->name) . ' (' . h($obj->wpurl) . ')'); if ($this->writeAttachment($obj)) { $count_attachments++; } } if ($this->debug) { $this->dump($obj); } } $timer = microtime(1) - $timer; $count = $count_posts + $count_pages + $count_attachments; $this->importedObjectsCount += $count; $message = 'Imported ' . counted($count, 'object', 'objects', 'zero', 'one') . ' (' . counted($count_posts, 'post', 'posts', 'zero', 'one') . ', ' . counted($count_pages, 'page', 'pages', 'zero', 'one') . ' and ' . counted($count_attachments, 'attachment', 'attachments', 'zero', 'one') . ')'; $this->report($message . ' from channel "' . h($channel_name) . '"' . ' in ' . gb_format_duration($timer)); if ($commit) { $this->report('Creating commit...'); try { git::commit($message . ' from Wordpress blog ' . $channel_name, GBUser::findAdmin()->gitAuthor()); $this->report('Committed to git repository'); } catch (GitError $e) { if (strpos($e->getMessage(), 'nothing added to commit') !== false) { $this->report('Nothing committed because no changes where done'); } else { throw $e; } } } } catch (Exception $e) { git::reset(); # rollback prepared commit throw $e; } }
<small>(Original error from shell: ' . h($e->getMessage()) . ')</small>'; } # ------------------------------------------------------------------------- # create repository if (!gb::$errors) { $add_sample_content = isset($_POST['add-sample-content']) && $_POST['add-sample-content'] === 'true'; if (!gb::init($add_sample_content)) { gb::$errors[] = 'Failed to create and initialize repository at ' . var_export(gb::$site_dir, 1); } } # ------------------------------------------------------------------------- # commit changes (done by gb::init()) if (!gb::$errors) { try { if (!git::commit('gitblog created', trim($_POST['name']) . ' <' . trim($_POST['email']) . '>')) { gb::$errors[] = 'failed to commit creation'; } } catch (Exception $e) { gb::$errors[] = 'failed to commit creation: ' . nl2br(h(strval($e))); } } # ------------------------------------------------------------------------- # create admin account if (!gb::$errors) { $_POST['email'] = trim($_POST['email']); $passhash = GBUser::passhash($_POST['email'], $_POST['passphrase']); $u = new GBUser(trim($_POST['name']), $_POST['email'], $passhash, true); $u->save(); # issues git add, that's why we do this after init }
function commit() { if ($this->txFp === false) { throw new LogicException('transaction is not active'); } $ex = null; try { $did_write = $this->txWriteData(); } catch (Exception $e) { $ex = $e; } $this->txEnd($ex); if ($ex) { throw $ex; } # commit to repo if ($did_write && $this->autocommitToRepo) { gb::log('committing changes to ' . $this->file); if (!($author = $this->autocommitToRepoAuthor)) { $author = GBUser::findAdmin()->gitAuthor(); } $m = $this->autocommitToRepoMessage ? $this->autocommitToRepoMessage : 'autocommit:' . $this->file; git::add($this->file); git::commit($m, $author); # rollback() will handle gb::reset if needed } return $did_write; }
static function _000104($from, $to) { $datadir = gb::$site_dir . '/data/'; $added = array(); # create data/ if (!file_exists($datadir)) { gb::log('creating directory %s', $datadir); mkdir($datadir, 0775); chmod($datadir, 0775); } # load old site.json gb::log('loading %s', gb::$site_dir . '/site.json'); gb::$site_state = is_readable(gb::$site_dir . '/site.json') ? json_decode(file_get_contents(gb::$site_dir . '/site.json'), true) : array(); # move site.json:plugins to data/plugins.json $plugins = isset(gb::$site_state['plugins']) ? gb::$site_state['plugins'] : array(); gb::log('creating data:plugins'); gb::data('plugins')->storage()->set($plugins); unset(gb::$site_state['plugins']); # write data/site.json gb::log('moving %s -> data:site', gb::$site_dir . '/site.json'); # gb_maint::sync_site_state() will be called after this method returns @unlink(gb::$site_dir . '/site.json'); # remove /site.json from .gitignore if (gb_maint::gitignore_sub('/(?:\\r?\\n)\\/site\\.json([\\t\\s \\r\\n]+|^)/m', '$1')) { gb::log('removed "/site.json" from .gitignore'); $added[] = git::add('.gitignore'); } # load settings.json gb::log('loading %s', gb::$site_dir . '/settings.json'); $settings = is_readable(gb::$site_dir . '/settings.json') ? json_decode(file_get_contents(gb::$site_dir . '/settings.json'), true) : array(); # move settings.json:* to data/plugins/* foreach ($settings as $pluginn => $d) { if (!is_array($d)) { $d = $d !== null ? array($d) : array(); } if ($d) { gb::log('copying %s:%s -> data:plugins/%s', gb::$site_dir . '/settings.json', $pluginn, $pluginn); gb::data('plugins/' . $pluginn)->storage()->set($d); } } gb::log('removing old %s', gb::$site_dir . '/settings.json'); @unlink(gb::$site_dir . '/settings.json'); # load gb-users.php $users = array(); if (is_readable(gb::$site_dir . '/gb-users.php')) { gb::log('loading %s', gb::$site_dir . '/gb-users.php'); eval('class GBUserAccount { static function __set_state($state) { return GBUser::__set_state($state); } }'); require gb::$site_dir . '/gb-users.php'; if (isset($db)) { $admin = isset($db['_admin']) ? $db['_admin'] : ''; foreach ($db as $email => $user) { if (is_object($user)) { $user->admin = $email === $admin; $users[$email] = $user; gb::log('transponded user %s', $email); } } } } # move gb-users.php to data/users.json gb::log('moving %s -> data:users', gb::$site_dir . '/gb-users.php'); GBUser::storage()->set($users); @unlink(gb::$site_dir . '/gb-users.php'); # commit any modifications if ($added) { try { git::commit('upgrade 0.1.4 modified ' . implode(', ', $added), GBUser::findAdmin()->gitAuthor(), $added); } catch (GitError $e) { if (strpos($e->getMessage(), 'no changes added to commit') === false) { throw $e; } } } }