|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | + |
| 4 | +/* |
| 5 | + * Copyright 2012 Facebook, Inc. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +$root = dirname(dirname(dirname(__FILE__))); |
| 21 | +require_once $root.'/scripts/__init_script__.php'; |
| 22 | + |
| 23 | +$args = new PhutilArgumentParser($argv); |
| 24 | +$args->setTagline('reconcile Phabricator state after repository changes'); |
| 25 | +$args->setSynopsis(<<<EOSYNOPSIS |
| 26 | +**reconcile.php** __repository_callsign__ |
| 27 | + Reconcile the state of Phabricator's caches with the actual state |
| 28 | + of the repository. |
| 29 | +
|
| 30 | + This is an administrative/maintenace operation and not generally |
| 31 | + necessary, but if repository history has changed or been rewritten |
| 32 | + (for example, if the repository was stored from a backup) |
| 33 | + Phabricator may think commits which are no longer present in the |
| 34 | + repository still exist. |
| 35 | +
|
| 36 | + This will delete all evidence of commits which Phabricator can't |
| 37 | + find in the actual repository. |
| 38 | +
|
| 39 | +EOSYNOPSIS |
| 40 | + ); |
| 41 | +$args->parseStandardArguments(); |
| 42 | +$args->parse( |
| 43 | + array( |
| 44 | + array( |
| 45 | + 'name' => 'more', |
| 46 | + 'wildcard' => true, |
| 47 | + ), |
| 48 | + )); |
| 49 | + |
| 50 | +$more = $args->getArg('more'); |
| 51 | +if (count($more) !== 1) { |
| 52 | + $args->printHelpAndExit(); |
| 53 | +} |
| 54 | +$callsign = reset($more); |
| 55 | + |
| 56 | + |
| 57 | +$repository = id(new PhabricatorRepository())->loadOneWhere( |
| 58 | + 'callsign = %s', |
| 59 | + $callsign); |
| 60 | +if (!$repository) { |
| 61 | + throw new Exception("No repository exists with callsign '{$callsign}'!"); |
| 62 | +} |
| 63 | + |
| 64 | +switch ($repository->getVersionControlSystem()) { |
| 65 | + case PhabricatorRepositoryType::REPOSITORY_TYPE_GIT: |
| 66 | + break; |
| 67 | + case PhabricatorRepositoryType::REPOSITORY_TYPE_SVN: |
| 68 | + case PhabricatorRepositoryType::REPOSITORY_TYPE_MERCURIAL: |
| 69 | + default: |
| 70 | + throw new Exception("For now, you can only reconcile git repositories."); |
| 71 | +} |
| 72 | + |
| 73 | +echo "Loading commits...\n"; |
| 74 | +$all_commits = id(new PhabricatorRepositoryCommit())->loadAllWhere( |
| 75 | + 'repositoryID = %d', |
| 76 | + $repository->getID()); |
| 77 | + |
| 78 | +echo "Updating repository..\n"; |
| 79 | +try { |
| 80 | + // Sanity-check the repository working copy and make sure we're up to date. |
| 81 | + $repository->execxLocalCommand('fetch --all'); |
| 82 | +} catch (Exception $ex) { |
| 83 | + echo "Unable to `git fetch` the working copy to update it. Reconciliation ". |
| 84 | + "requires an up-to-date working copy.\n"; |
| 85 | + throw $ex; |
| 86 | +} |
| 87 | + |
| 88 | +echo "Verifying commits (this may take some time if the repository is large)"; |
| 89 | +$futures = array(); |
| 90 | +foreach ($all_commits as $id => $commit) { |
| 91 | + $futures[$id] = $repository->getLocalCommandFuture( |
| 92 | + 'rev-parse --verify %s', |
| 93 | + $commit->getCommitIdentifier()); |
| 94 | +} |
| 95 | + |
| 96 | +$bad = array(); |
| 97 | +foreach (Futures($futures)->limit(8) as $id => $future) { |
| 98 | + list($err) = $future->resolve(); |
| 99 | + if ($err) { |
| 100 | + $bad[$id] = $all_commits[$id]; |
| 101 | + echo "#"; |
| 102 | + } else { |
| 103 | + echo "."; |
| 104 | + } |
| 105 | +} |
| 106 | +echo "\nDone.\n"; |
| 107 | + |
| 108 | +if (!count($bad)) { |
| 109 | + echo "No bad commits found!\n"; |
| 110 | +} else { |
| 111 | + echo "Found ".count($bad)." bad commits:\n\n"; |
| 112 | + echo ' '.implode("\n ", mpull($bad, 'getCommitIdentifier')); |
| 113 | + $ok = phutil_console_confirm("Do you want to delete these commits?"); |
| 114 | + if (!$ok) { |
| 115 | + echo "OK, aborting.\n"; |
| 116 | + exit(1); |
| 117 | + } |
| 118 | + |
| 119 | + echo "Deleting commits"; |
| 120 | + foreach ($bad as $commit) { |
| 121 | + echo "."; |
| 122 | + $commit->delete(); |
| 123 | + } |
| 124 | + echo "\nDone.\n"; |
| 125 | +} |
| 126 | + |
| 127 | +//// Clean Up Links //////////////////////////////////////////////////////// |
| 128 | + |
| 129 | +$table = new PhabricatorRepositoryCommit(); |
| 130 | + |
| 131 | +$valid_phids = queryfx_all( |
| 132 | + $table->establishConnection('r'), |
| 133 | + 'SELECT phid FROM %T', |
| 134 | + $table->getTableName()); |
| 135 | +$valid_phids = ipull($valid_phids, null, 'phid'); |
| 136 | + |
| 137 | +//////// Differential <-> Diffusion Links ////////////////////////////////// |
| 138 | + |
| 139 | +$dx_conn = id(new DifferentialRevision())->establishConnection('w'); |
| 140 | +$dx_table = DifferentialRevision::TABLE_COMMIT; |
| 141 | +$dx_phids = queryfx_all( |
| 142 | + $dx_conn, |
| 143 | + 'SELECT commitPHID FROM %T', |
| 144 | + $dx_table); |
| 145 | + |
| 146 | +$bad_phids = array(); |
| 147 | +foreach ($dx_phids as $dx_phid) { |
| 148 | + if (empty($valid_phids[$dx_phid['commitPHID']])) { |
| 149 | + $bad_phids[] = $dx_phid['commitPHID']; |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +if ($bad_phids) { |
| 154 | + echo "Deleting ".count($bad_phids)." bad Diffusion links...\n"; |
| 155 | + queryfx( |
| 156 | + $dx_conn, |
| 157 | + 'DELETE FROM %T WHERE commitPHID IN (%Ls)', |
| 158 | + $dx_table, |
| 159 | + $bad_phids); |
| 160 | + echo "Done.\n"; |
| 161 | +} else { |
| 162 | + echo "Diffusion links are clean.\n"; |
| 163 | +} |
| 164 | + |
| 165 | +// TODO: There are some links in owners that we should probably clean up too. |
0 commit comments