|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | + |
| 4 | +require_once dirname(__FILE__).'/../__init_script__.php'; |
| 5 | + |
| 6 | +if (function_exists('posix_isatty') && posix_isatty(STDIN)) { |
| 7 | + $command = 'xargs -0 arc lint --output json | '.__FILE__; |
| 8 | + echo "Usage: git ls-files -z | {$command}\n"; |
| 9 | + echo "Usage: git diff --name-only -z | {$command}\n"; // TODO: Handle deletes. |
| 10 | + echo "Purpose: Save all lint errors to database.\n"; |
| 11 | + exit(1); |
| 12 | +} |
| 13 | + |
| 14 | +$working_copy = ArcanistWorkingCopyIdentity::newFromPath('.'); |
| 15 | +$api = ArcanistRepositoryAPI::newAPIFromWorkingCopyIdentity($working_copy); |
| 16 | +$svn_root = id(new PhutilURI($api->getSourceControlPath()))->getPath(); |
| 17 | + |
| 18 | +$project_id = $working_copy->getProjectID(); |
| 19 | +$project = id(new PhabricatorRepositoryArcanistProject()) |
| 20 | + ->loadOneWhere('name = %s', $project_id); |
| 21 | +if (!$project || !$project->getRepositoryID()) { |
| 22 | + throw new Exception("Couldn't find repository for {$project_id}."); |
| 23 | +} |
| 24 | + |
| 25 | +$branch_name = $api->getBranchName(); |
| 26 | +$branch = id(new PhabricatorRepositoryBranch())->loadOneWhere( |
| 27 | + 'repositoryID = %d AND name = %s', |
| 28 | + $project->getRepositoryID(), |
| 29 | + $branch_name); |
| 30 | +if (!$branch) { |
| 31 | + $branch = id(new PhabricatorRepositoryBranch()) |
| 32 | + ->setRepositoryID($project->getRepositoryID()) |
| 33 | + ->setName($branch_name); |
| 34 | +} |
| 35 | +$branch->setLintCommit($api->getWorkingCopyRevision()); |
| 36 | +$branch->save(); |
| 37 | +$conn = $branch->establishConnection('w'); |
| 38 | + |
| 39 | +$inserts = array(); |
| 40 | + |
| 41 | +while ($json = fgets(STDIN)) { |
| 42 | + $paths = json_decode(rtrim($json, "\n"), true); |
| 43 | + if (!is_array($paths)) { |
| 44 | + throw new Exception("Invalid JSON: {$json}"); |
| 45 | + } |
| 46 | + |
| 47 | + if (!$paths) { |
| 48 | + continue; |
| 49 | + } |
| 50 | + |
| 51 | + $conn->openTransaction(); |
| 52 | + |
| 53 | + foreach (array_chunk(array_keys($paths), 1024) as $some_paths) { |
| 54 | + $full_paths = array(); |
| 55 | + foreach ($some_paths as $path) { |
| 56 | + $full_paths[] = $svn_root.'/'.$path; |
| 57 | + } |
| 58 | + queryfx( |
| 59 | + $conn, |
| 60 | + 'DELETE FROM %T WHERE branchID = %d AND path IN (%Ls)', |
| 61 | + PhabricatorRepository::TABLE_LINTMESSAGE, |
| 62 | + $branch->getID(), |
| 63 | + $full_paths); |
| 64 | + } |
| 65 | + |
| 66 | + foreach ($paths as $path => $messages) { |
| 67 | + // TODO: Handle multiple $json for a single path. Don't save duplicates. |
| 68 | + |
| 69 | + foreach ($messages as $message) { |
| 70 | + $inserts[] = qsprintf( |
| 71 | + $conn, |
| 72 | + '(%d, %s, %d, %s, %s, %s, %s)', |
| 73 | + $branch->getID(), |
| 74 | + $svn_root.'/'.$path, |
| 75 | + idx($message, 'line', 0), |
| 76 | + idx($message, 'code', ''), |
| 77 | + idx($message, 'severity', ''), |
| 78 | + idx($message, 'name', ''), |
| 79 | + idx($message, 'description', '')); |
| 80 | + |
| 81 | + if (count($inserts) >= 256) { |
| 82 | + save_lint_messages($conn, $inserts); |
| 83 | + $inserts = array(); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + $conn->saveTransaction(); |
| 89 | +} |
| 90 | + |
| 91 | +save_lint_messages($conn, $inserts); |
| 92 | + |
| 93 | +function save_lint_messages($conn, array $inserts) { |
| 94 | + if ($inserts) { |
| 95 | + queryfx( |
| 96 | + $conn, |
| 97 | + 'INSERT INTO %T |
| 98 | + (branchID, path, line, code, severity, name, description) |
| 99 | + VALUES %Q', |
| 100 | + PhabricatorRepository::TABLE_LINTMESSAGE, |
| 101 | + implode(', ', $inserts)); |
| 102 | + } |
| 103 | +} |
0 commit comments