@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Diffusion - add verifyGitOrigin check to git fetch operation

Summary: Fixes T4946. Theoretically.

Test Plan:
iiam

also unit tests.

also

```
cd /var/repo/X
git remote remove origin # simulates origin-missing clone under 1.7.1
cd /path/to/phabricator
./bin/repository pull X
```

and observed no errors

Reviewers: epriestley

Reviewed By: epriestley

Subscribers: Korvin, epriestley

Maniphest Tasks: T4946, T5938

Differential Revision: https://secure.phabricator.com/D10855

+95 -93
-93
src/applications/repository/engine/PhabricatorRepositoryDiscoveryEngine.php
··· 135 135 } 136 136 137 137 138 - /** 139 - * Verify that the "origin" remote exists, and points at the correct URI. 140 - * 141 - * This catches or corrects some types of misconfiguration, and also repairs 142 - * an issue where Git 1.7.1 does not create an "origin" for `--bare` clones. 143 - * See T4041. 144 - * 145 - * @param PhabricatorRepository Repository to verify. 146 - * @return void 147 - */ 148 - private function verifyGitOrigin(PhabricatorRepository $repository) { 149 - list($remotes) = $repository->execxLocalCommand( 150 - 'remote show -n origin'); 151 - 152 - $matches = null; 153 - if (!preg_match('/^\s*Fetch URL:\s*(.*?)\s*$/m', $remotes, $matches)) { 154 - throw new Exception( 155 - "Expected 'Fetch URL' in 'git remote show -n origin'."); 156 - } 157 - 158 - $remote_uri = $matches[1]; 159 - $expect_remote = $repository->getRemoteURI(); 160 - 161 - if ($remote_uri == 'origin') { 162 - // If a remote does not exist, git pretends it does and prints out a 163 - // made up remote where the URI is the same as the remote name. This is 164 - // definitely not correct. 165 - 166 - // Possibly, we should use `git remote --verbose` instead, which does not 167 - // suffer from this problem (but is a little more complicated to parse). 168 - $valid = false; 169 - $exists = false; 170 - } else { 171 - $normal_type_git = PhabricatorRepositoryURINormalizer::TYPE_GIT; 172 - 173 - $remote_normal = id(new PhabricatorRepositoryURINormalizer( 174 - $normal_type_git, 175 - $remote_uri))->getNormalizedPath(); 176 - 177 - $expect_normal = id(new PhabricatorRepositoryURINormalizer( 178 - $normal_type_git, 179 - $expect_remote))->getNormalizedPath(); 180 - 181 - $valid = ($remote_normal == $expect_normal); 182 - $exists = true; 183 - } 184 - 185 - if (!$valid) { 186 - if (!$exists) { 187 - // If there's no "origin" remote, just create it regardless of how 188 - // strongly we own the working copy. There is almost no conceivable 189 - // scenario in which this could do damage. 190 - $this->log( 191 - pht( 192 - 'Remote "origin" does not exist. Creating "origin", with '. 193 - 'URI "%s".', 194 - $expect_remote)); 195 - $repository->execxLocalCommand( 196 - 'remote add origin %P', 197 - $repository->getRemoteURIEnvelope()); 198 - 199 - // NOTE: This doesn't fetch the origin (it just creates it), so we won't 200 - // know about origin branches until the next "pull" happens. That's fine 201 - // for our purposes, but might impact things in the future. 202 - } else { 203 - if ($repository->canDestroyWorkingCopy()) { 204 - // Bad remote, but we can try to repair it. 205 - $this->log( 206 - pht( 207 - 'Remote "origin" exists, but is pointed at the wrong URI, "%s". '. 208 - 'Resetting origin URI to "%s.', 209 - $remote_uri, 210 - $expect_remote)); 211 - $repository->execxLocalCommand( 212 - 'remote set-url origin %P', 213 - $repository->getRemoteURIEnvelope()); 214 - } else { 215 - // Bad remote and we aren't comfortable repairing it. 216 - $message = pht( 217 - 'Working copy at "%s" has a mismatched origin URI, "%s". '. 218 - 'The expected origin URI is "%s". Fix your configuration, or '. 219 - 'set the remote URI correctly. To avoid breaking anything, '. 220 - 'Phabricator will not automatically fix this.', 221 - $repository->getLocalPath(), 222 - $remote_uri, 223 - $expect_remote); 224 - throw new Exception($message); 225 - } 226 - } 227 - } 228 - } 229 - 230 - 231 138 /* -( Discovering Subversion Repositories )-------------------------------- */ 232 139 233 140
+94
src/applications/repository/engine/PhabricatorRepositoryEngine.php
··· 51 51 return PhabricatorUser::getOmnipotentUser(); 52 52 } 53 53 54 + /** 55 + * Verify that the "origin" remote exists, and points at the correct URI. 56 + * 57 + * This catches or corrects some types of misconfiguration, and also repairs 58 + * an issue where Git 1.7.1 does not create an "origin" for `--bare` clones. 59 + * See T4041. 60 + * 61 + * @param PhabricatorRepository Repository to verify. 62 + * @return void 63 + */ 64 + protected function verifyGitOrigin(PhabricatorRepository $repository) { 65 + list($remotes) = $repository->execxLocalCommand( 66 + 'remote show -n origin'); 67 + 68 + $matches = null; 69 + if (!preg_match('/^\s*Fetch URL:\s*(.*?)\s*$/m', $remotes, $matches)) { 70 + throw new Exception( 71 + "Expected 'Fetch URL' in 'git remote show -n origin'."); 72 + } 73 + 74 + $remote_uri = $matches[1]; 75 + $expect_remote = $repository->getRemoteURI(); 76 + 77 + if ($remote_uri == 'origin') { 78 + // If a remote does not exist, git pretends it does and prints out a 79 + // made up remote where the URI is the same as the remote name. This is 80 + // definitely not correct. 81 + 82 + // Possibly, we should use `git remote --verbose` instead, which does not 83 + // suffer from this problem (but is a little more complicated to parse). 84 + $valid = false; 85 + $exists = false; 86 + } else { 87 + $normal_type_git = PhabricatorRepositoryURINormalizer::TYPE_GIT; 88 + 89 + $remote_normal = id(new PhabricatorRepositoryURINormalizer( 90 + $normal_type_git, 91 + $remote_uri))->getNormalizedPath(); 92 + 93 + $expect_normal = id(new PhabricatorRepositoryURINormalizer( 94 + $normal_type_git, 95 + $expect_remote))->getNormalizedPath(); 96 + 97 + $valid = ($remote_normal == $expect_normal); 98 + $exists = true; 99 + } 100 + 101 + if (!$valid) { 102 + if (!$exists) { 103 + // If there's no "origin" remote, just create it regardless of how 104 + // strongly we own the working copy. There is almost no conceivable 105 + // scenario in which this could do damage. 106 + $this->log( 107 + pht( 108 + 'Remote "origin" does not exist. Creating "origin", with '. 109 + 'URI "%s".', 110 + $expect_remote)); 111 + $repository->execxLocalCommand( 112 + 'remote add origin %P', 113 + $repository->getRemoteURIEnvelope()); 114 + 115 + // NOTE: This doesn't fetch the origin (it just creates it), so we won't 116 + // know about origin branches until the next "pull" happens. That's fine 117 + // for our purposes, but might impact things in the future. 118 + } else { 119 + if ($repository->canDestroyWorkingCopy()) { 120 + // Bad remote, but we can try to repair it. 121 + $this->log( 122 + pht( 123 + 'Remote "origin" exists, but is pointed at the wrong URI, "%s". '. 124 + 'Resetting origin URI to "%s.', 125 + $remote_uri, 126 + $expect_remote)); 127 + $repository->execxLocalCommand( 128 + 'remote set-url origin %P', 129 + $repository->getRemoteURIEnvelope()); 130 + } else { 131 + // Bad remote and we aren't comfortable repairing it. 132 + $message = pht( 133 + 'Working copy at "%s" has a mismatched origin URI, "%s". '. 134 + 'The expected origin URI is "%s". Fix your configuration, or '. 135 + 'set the remote URI correctly. To avoid breaking anything, '. 136 + 'Phabricator will not automatically fix this.', 137 + $repository->getLocalPath(), 138 + $remote_uri, 139 + $expect_remote); 140 + throw new Exception($message); 141 + } 142 + } 143 + } 144 + } 145 + 146 + 147 + 54 148 55 149 /** 56 150 * @task internal
+1
src/applications/repository/engine/PhabricatorRepositoryPullEngine.php
··· 88 88 "Updating the working copy for repository '%s'.", 89 89 $callsign)); 90 90 if ($is_git) { 91 + $this->verifyGitOrigin($repository); 91 92 $this->executeGitUpdate(); 92 93 } else if ($is_hg) { 93 94 $this->executeMercurialUpdate();