@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<?php
2
3final class DiffusionExternalController extends DiffusionController {
4
5 public function shouldAllowPublic() {
6 return true;
7 }
8
9 public function handleRequest(AphrontRequest $request) {
10 $uri = $request->getStr('uri');
11 $id = $request->getStr('id');
12
13 $repositories = id(new PhabricatorRepositoryQuery())
14 ->setViewer($request->getUser())
15 ->execute();
16
17 if ($uri) {
18 $uri_path = id(new PhutilURI($uri))->getPath();
19 $matches = array();
20
21 // Try to figure out which tracked repository this external lives in by
22 // comparing repository metadata. We look for an exact match, but accept
23 // a partial match.
24
25 foreach ($repositories as $key => $repository) {
26 $remote_uri = new PhutilURI($repository->getRemoteURI());
27 if ($remote_uri->getPath() == $uri_path) {
28 $matches[$key] = 1;
29 }
30 if ($repository->getPublicCloneURI() == $uri) {
31 $matches[$key] = 2;
32 }
33 if ($repository->getRemoteURI() == $uri) {
34 $matches[$key] = 3;
35 }
36 }
37
38 arsort($matches);
39 $best_match = head_key($matches);
40
41 if ($best_match) {
42 $repository = $repositories[$best_match];
43 $redirect = $repository->generateURI(
44 array(
45 'action' => 'browse',
46 'branch' => $repository->getDefaultBranch(),
47 'commit' => $id,
48 ));
49
50 return id(new AphrontRedirectResponse())->setURI($redirect);
51 }
52 }
53
54 // TODO: This is a rare query but does a table scan, add a key?
55
56 $commits = id(new PhabricatorRepositoryCommit())->loadAllWhere(
57 'commitIdentifier = %s',
58 $id);
59
60 if (empty($commits)) {
61 $desc = null;
62 if (phutil_nonempty_string($uri)) {
63 $desc = pht('"%s", at "%s"', $uri, $id);
64 } else {
65 $desc = pht('"%s"', $id);
66 }
67
68 $content = id(new PHUIInfoView())
69 ->setTitle(pht('Unknown External'))
70 ->setSeverity(PHUIInfoView::SEVERITY_WARNING)
71 ->appendChild(phutil_tag(
72 'p',
73 array(),
74 pht(
75 'This external (%s) does not appear in any tracked '.
76 'repository. It may exist in an untracked repository that '.
77 'Diffusion does not know about.',
78 $desc)));
79 } else if (count($commits) == 1) {
80 $commit = head($commits);
81 $repo = $repositories[$commit->getRepositoryID()];
82 $redirect = $repo->generateURI(
83 array(
84 'action' => 'browse',
85 'branch' => $repo->getDefaultBranch(),
86 'commit' => $commit->getCommitIdentifier(),
87 ));
88 return id(new AphrontRedirectResponse())->setURI($redirect);
89 } else {
90
91 $rows = array();
92 foreach ($commits as $commit) {
93 $repo = $repositories[$commit->getRepositoryID()];
94 $href = $repo->generateURI(
95 array(
96 'action' => 'browse',
97 'branch' => $repo->getDefaultBranch(),
98 'commit' => $commit->getCommitIdentifier(),
99 ));
100 $rows[] = array(
101 phutil_tag(
102 'a',
103 array(
104 'href' => $href,
105 ),
106 $commit->getURI()),
107 $commit->loadCommitData()->getSummary(),
108 );
109 }
110
111 $table = new AphrontTableView($rows);
112 $table->setHeaders(
113 array(
114 pht('Commit'),
115 pht('Description'),
116 ));
117 $table->setColumnClasses(
118 array(
119 'pri',
120 'wide',
121 ));
122
123 $caption = id(new PHUIInfoView())
124 ->setSeverity(PHUIInfoView::SEVERITY_NOTICE)
125 ->appendChild(
126 pht('This external reference matches multiple known commits.'));
127
128 $content = new PHUIObjectBoxView();
129 $content->setHeaderText(pht('Multiple Matching Commits'));
130 $content->setInfoView($caption);
131 $content->setTable($table);
132 }
133
134 $crumbs = $this->buildApplicationCrumbs();
135 $crumbs->addTextCrumb(pht('External'));
136
137 return $this->newPage()
138 ->setTitle(pht('Unresolvable External'))
139 ->setCrumbs($crumbs)
140 ->appendChild($content);
141 }
142
143}