diff --git a/site-tools/update-doc-list.php b/site-tools/update-doc-list.php index f189a64d..5afbd218 100644 --- a/site-tools/update-doc-list.php +++ b/site-tools/update-doc-list.php @@ -1,13 +1,114 @@ to_xml()); file_put_contents(dirname(__FILE__) . '/../generated/libraries.txt', serialize($libs)); } -main(); \ No newline at end of file +function update_from_git($libs, $location, $branch) { + echo "Updating from {$branch}\n"; + + $git_command = "cd '${location}' && git"; + + $is_bare = get_bool_from_array(run_process("{$git_command} rev-parse --is-bare-repository")); + + $modules = Array(); + + foreach(run_process("{$git_command} config -l --blob ". + "\$({$git_command} ls-tree {$branch} .gitmodules | cut -f 1 | cut -f 3 -d ' ')") + as $line_number => $line) + { + if (!$line) continue; + + if (preg_match('@^submodule\.(\w+)\.(\w+)=(.*)$@', trim($line), $matches)) { + $modules[$matches[1]][$matches[2]] = $matches[3]; + } + else { + throw new RuntimeException("Unsupported config line: {$line}"); + } + } + + $modules_by_path = Array(); + foreach($modules as $name => $details) { + $modules_by_path[$details['path']] = $name; + } + + foreach(run_process("{$git_command} ls-tree {$branch} ".implode(' ', array_keys($modules_by_path))) + as $line_number => $line) + { + if (!$line) continue; + + if (preg_match("@^160000 commit ([a-zA-Z0-9]+)\t(.*)$@", $line, $matches)) { + $modules[$modules_by_path[$matches[2]]]['hash'] = $matches[1]; + } + else { + throw new RuntimeException("Unmatched submodule line: {$line}"); + } + } + + foreach($modules as $name => $module) { + $module_location = $is_bare ? "{$location}/{$module['url']}" : + "{$location}/{$module['path']}"; + $module_command = "cd '{$module_location}' && git"; + + foreach(run_process("{$module_command} ls-tree {$module['hash']} metadata.xml") as $entry) { + $entry = trim($entry); + if (preg_match("@^100644 blob ([a-zA-Z0-9]+)\t(.*)$@", $entry, $matches)) { + $libs->update( + implode("\n", (run_process("{$module_command} show {$matches[1]}"))), + $branch); + } + } + } +} + +function get_bool_from_array($array) { + if (count($array) != 1) throw new RuntimeException("get_bool_from_array: invalid array"); + switch ($array[0]) { + case 'true': return true; + case 'false': return false; + default: throw new RuntimeException("invalid bool: ${array[0]}"); + } +} + +class ProcessError extends RuntimeException { + public $error_code; + + function __construct($error_code) { + $this->error_code = $error_code; + parent::__construct("Process failed with status: {$error_code}"); + } +} + +function run_process($command) { + exec($command, $output, $return_var); + + if ($return_var != 0) { + throw new ProcessError($return_var); + } + + return $output; +} + +main();