From fd349d1d5f06a050ded97f6424446b5c902e0347 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20B=C3=B6sch-Plepelits?= Date: Sat, 23 Dec 2017 22:25:50 +0100 Subject: [PATCH] repo.php: new repository type 'git' --- modulekit.php | 1 + repo.php | 3 +++ src/RepositoryGit.php | 47 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 src/RepositoryGit.php diff --git a/modulekit.php b/modulekit.php index 3dfe3a26..28a9b88a 100644 --- a/modulekit.php +++ b/modulekit.php @@ -17,6 +17,7 @@ $include = array( 'src/wikipedia.php', 'src/ImageLoader.php', 'src/RepositoryDir.php', + 'src/RepositoryGit.php', ), 'css' => array( 'style.css', diff --git a/repo.php b/repo.php index 9eafb511..74231247 100644 --- a/repo.php +++ b/repo.php @@ -15,6 +15,9 @@ if (!isset($repositories)) { function getRepo ($repoId, $repoData) { switch (array_key_exists('type', $repoData) ? $repoData['type'] : 'dir') { + case 'git': + $repo = new RepositoryGit($repoId, $repoData); + break; default: $repo = new RepositoryDir($repoId, $repoData); } diff --git a/src/RepositoryGit.php b/src/RepositoryGit.php new file mode 100644 index 00000000..fa22857b --- /dev/null +++ b/src/RepositoryGit.php @@ -0,0 +1,47 @@ +def = $def; + $this->path = $def['path']; + } + + function info () { + $ret = array(); + + foreach (array('name') as $k) { + if (array_key_exists($k, $this->def)) { + $ret[$k] = $this->def[$k]; + } + } + + return $ret; + } + + function newestTimestamp () { + $ts = (int)shell_exec("cd " . escapeShellArg($this->path) . "; git log -1 --pretty=format:%ct"); + + return $ts; + } + + function data () { + $data = array(); + + $d = popen("cd " . escapeShellArg($this->path) . "; git ls-tree HEAD", "r"); + while ($r = fgets($d)) { + if (preg_match("/^[0-9]{6} blob [0-9a-f]{40}\t(([0-9a-zA-Z_\-]+)\.json)$/", $r, $m)) { + $f = $m[1]; + $id = $m[2]; + + if ($f === 'package.json') { + continue; + } + + $d1 = json_decode(shell_exec("cd " . escapeShellArg($this->path) . "; git show HEAD:" . escapeShellArg($f)), true); + $data[$id] = jsonMultilineStringsJoin($d1, array('exclude' => array(array('const')))); + } + } + pclose($d); + + return $data; + } +}