forked from phacility/phabricator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiffusionRepositoryController.php
545 lines (460 loc) · 15.4 KB
/
DiffusionRepositoryController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
<?php
final class DiffusionRepositoryController extends DiffusionController {
private $browseFuture;
private $branchButton = null;
private $branchFuture;
public function shouldAllowPublic() {
return true;
}
public function handleRequest(AphrontRequest $request) {
$response = $this->loadDiffusionContext();
if ($response) {
return $response;
}
require_celerity_resource('diffusion-css');
$viewer = $this->getViewer();
$drequest = $this->getDiffusionRequest();
$repository = $drequest->getRepository();
$crumbs = $this->buildCrumbs();
$crumbs->setBorder(true);
$header = $this->buildHeaderView($repository);
$actions = $this->buildActionList($repository);
$description = $this->buildDescriptionView($repository);
$locate_file = $this->buildLocateFile();
// Before we do any work, make sure we're looking at a some content: we're
// on a valid branch, and the repository is not empty.
$page_has_content = false;
$empty_title = null;
$empty_message = null;
// If this VCS supports branches, check that the selected branch actually
// exists.
if ($drequest->supportsBranches()) {
// NOTE: Mercurial may have multiple branch heads with the same name.
$ref_cursors = id(new PhabricatorRepositoryRefCursorQuery())
->setViewer($viewer)
->withRepositoryPHIDs(array($repository->getPHID()))
->withRefTypes(array(PhabricatorRepositoryRefCursor::TYPE_BRANCH))
->withRefNames(array($drequest->getBranch()))
->needPositions(true)
->execute();
// It's possible that this branch previously existed, but has been
// deleted. Make sure we have valid cursor positions, not just cursors.
$any_positions = false;
foreach ($ref_cursors as $ref_cursor) {
if ($ref_cursor->getPositions()) {
$any_positions = true;
break;
}
}
if ($any_positions) {
// This is a valid branch, so we necessarily have some content.
$page_has_content = true;
} else {
$default = $repository->getDefaultBranch();
if ($default != $drequest->getBranch()) {
$empty_title = pht('No Such Branch');
$empty_message = pht(
'There is no branch named "%s" in this repository.',
$drequest->getBranch());
} else {
$empty_title = pht('No Default Branch');
$empty_message = pht(
'This repository is configured with default branch "%s" but '.
'there is no branch with that name in this repository.',
$default);
}
}
}
// If we didn't find any branches, check if there are any commits at all.
// This can tailor the message for empty repositories.
$any_commit = null;
if (!$page_has_content) {
$any_commit = id(new DiffusionCommitQuery())
->setViewer($viewer)
->withRepository($repository)
->setLimit(1)
->execute();
if ($any_commit) {
if (!$drequest->supportsBranches()) {
$page_has_content = true;
}
} else {
$empty_title = pht('Empty Repository');
$empty_message = pht('This repository does not have any commits yet.');
}
}
if ($page_has_content) {
$content = $this->buildNormalContent($drequest);
} else {
// If we have a commit somewhere, find branches.
// TODO: Evan will replace
// $this->buildNormalContent($drequest);
$content = id(new PHUIInfoView())
->setTitle($empty_title)
->setSeverity(PHUIInfoView::SEVERITY_WARNING)
->setErrors(array($empty_message));
}
$tabs = $this->buildTabsView('code');
$clone_uri = $drequest->generateURI(
array(
'action' => 'clone',
));
if ($repository->isSVN()) {
$clone_text = pht('Checkout');
} else {
$clone_text = pht('Clone');
}
$actions_button = id(new PHUIButtonView())
->setTag('a')
->setText(pht('Actions'))
->setIcon('fa-bars')
->addClass('mmr')
->setColor(PHUIButtonView::GREY)
->setDropdown(true)
->setDropdownMenu($actions);
$clone_button = id(new PHUIButtonView())
->setTag('a')
->setText($clone_text)
->setColor(PHUIButtonView::GREEN)
->setIcon('fa-download')
->setWorkflow(true)
->setHref($clone_uri);
$bar = id(new PHUILeftRightView())
->setLeft($locate_file)
->setRight(array($this->branchButton, $actions_button, $clone_button))
->addClass('diffusion-action-bar');
$status_view = null;
if ($repository->isReadOnly()) {
$status_view = id(new PHUIInfoView())
->setSeverity(PHUIInfoView::SEVERITY_WARNING)
->setErrors(
array(
phutil_escape_html_newlines(
$repository->getReadOnlyMessageForDisplay()),
));
}
$view = id(new PHUITwoColumnView())
->setHeader($header)
->setFooter(
array(
$status_view,
$bar,
$description,
$content,
));
if ($page_has_content) {
$view->setTabs($tabs);
}
return $this->newPage()
->setTitle(
array(
$repository->getName(),
$repository->getDisplayName(),
))
->setCrumbs($crumbs)
->appendChild(array(
$view,
));
}
private function buildNormalContent(DiffusionRequest $drequest) {
$request = $this->getRequest();
$repository = $drequest->getRepository();
$commit = $drequest->getCommit();
$path = $drequest->getPath();
$futures = array();
$browse_pager = id(new PHUIPagerView())
->readFromRequest($request);
$this->browseFuture = $this->callConduitMethod(
'diffusion.browsequery',
array(
'commit' => $commit,
'path' => $path,
'limit' => $browse_pager->getPageSize() + 1,
));
$futures[] = $this->browseFuture;
if ($this->needBranchFuture()) {
$branch_limit = $this->getBranchLimit();
$this->branchFuture = $this->callConduitMethod(
'diffusion.branchquery',
array(
'closed' => false,
'limit' => $branch_limit + 1,
));
$futures[] = $this->branchFuture;
}
$futures = array_filter($futures);
$futures = new FutureIterator($futures);
foreach ($futures as $future) {
// Just resolve all the futures before continuing.
}
$content = array();
try {
$browse_results = $this->browseFuture->resolve();
$browse_results = DiffusionBrowseResultSet::newFromConduit(
$browse_results);
$browse_paths = $browse_results->getPaths();
$browse_paths = $browse_pager->sliceResults($browse_paths);
$browse_exception = null;
} catch (Exception $ex) {
$browse_results = null;
$browse_paths = null;
$browse_exception = $ex;
}
if ($browse_results) {
$readme = $this->renderDirectoryReadme($browse_results);
} else {
$readme = null;
}
$content[] = $this->buildBrowseTable(
$browse_results,
$browse_paths,
$browse_exception,
$browse_pager);
if ($readme) {
$content[] = $readme;
}
try {
$branch_button = $this->buildBranchList($drequest);
$this->branchButton = $branch_button;
} catch (Exception $ex) {
if (!$repository->isImporting()) {
$content[] = $this->renderStatusMessage(
pht('Unable to Load Branches'),
$ex->getMessage());
}
}
return $content;
}
private function buildHeaderView(PhabricatorRepository $repository) {
$viewer = $this->getViewer();
$drequest = $this->getDiffusionRequest();
$search = $this->renderSearchForm();
$header = id(new PHUIHeaderView())
->setHeader($repository->getName())
->setUser($viewer)
->setPolicyObject($repository)
->setProfileHeader(true)
->setImage($repository->getProfileImageURI())
->setImageEditURL('/diffusion/picture/'.$repository->getID().'/')
->addActionItem($search)
->addClass('diffusion-profile-header');
if (!$repository->isTracked()) {
$header->setStatus('fa-ban', 'dark', pht('Inactive'));
} else if ($repository->isReadOnly()) {
$header->setStatus('fa-wrench', 'indigo', pht('Under Maintenance'));
} else if ($repository->isImporting()) {
$ratio = $repository->loadImportProgress();
$percentage = sprintf('%.2f%%', 100 * $ratio);
$header->setStatus(
'fa-clock-o',
'indigo',
pht('Importing (%s)...', $percentage));
} else if ($repository->isPublishingDisabled()) {
$header->setStatus('fa-minus', 'bluegrey', pht('Publishing Disabled'));
} else {
$header->setStatus('fa-check', 'bluegrey', pht('Active'));
}
if (!$repository->isSVN()) {
$default = $repository->getDefaultBranch();
if ($default != $drequest->getBranch()) {
$branch_tag = $this->renderBranchTag($drequest);
$header->addTag($branch_tag);
}
}
return $header;
}
private function buildActionList(PhabricatorRepository $repository) {
$viewer = $this->getViewer();
$edit_uri = $repository->getPathURI('manage/');
$action_view = id(new PhabricatorActionListView())
->setUser($viewer)
->setObject($repository);
$action_view->addAction(
id(new PhabricatorActionView())
->setName(pht('Manage Repository'))
->setIcon('fa-cogs')
->setHref($edit_uri));
if ($repository->isHosted()) {
$push_uri = $this->getApplicationURI(
'pushlog/?repositories='.$repository->getPHID());
$action_view->addAction(
id(new PhabricatorActionView())
->setName(pht('View Push Logs'))
->setIcon('fa-upload')
->setHref($push_uri));
$pull_uri = $this->getApplicationURI(
'synclog/?repositories='.$repository->getPHID());
$action_view->addAction(
id(new PhabricatorActionView())
->setName(pht('View Sync Logs'))
->setIcon('fa-exchange')
->setHref($pull_uri));
}
$pull_uri = $this->getApplicationURI(
'pulllog/?repositories='.$repository->getPHID());
$action_view->addAction(
id(new PhabricatorActionView())
->setName(pht('View Pull Logs'))
->setIcon('fa-download')
->setHref($pull_uri));
return $action_view;
}
private function buildDescriptionView(PhabricatorRepository $repository) {
$viewer = $this->getViewer();
$view = id(new PHUIPropertyListView())
->setUser($viewer);
$description = $repository->getDetail('description');
if (strlen($description)) {
$description = new PHUIRemarkupView($viewer, $description);
$view->addTextContent($description);
return id(new PHUIObjectBoxView())
->appendChild($view)
->addClass('diffusion-profile-description');
}
return null;
}
private function buildBranchList(DiffusionRequest $drequest) {
$viewer = $this->getViewer();
if (!$this->needBranchFuture()) {
return null;
}
$branches = $this->branchFuture->resolve();
if (!$branches) {
return null;
}
$limit = $this->getBranchLimit();
$more_branches = (count($branches) > $limit);
$branches = array_slice($branches, 0, $limit);
$branches = DiffusionRepositoryRef::loadAllFromDictionaries($branches);
$actions = id(new PhabricatorActionListView())
->setViewer($viewer);
foreach ($branches as $branch) {
$branch_uri = $drequest->generateURI(
array(
'action' => 'browse',
'branch' => $branch->getShortname(),
));
$actions->addAction(
id(new PhabricatorActionView())
->setName($branch->getShortname())
->setIcon('fa-code-fork')
->setHref($branch_uri));
}
if ($more_branches) {
$more_uri = $drequest->generateURI(
array(
'action' => 'branches',
));
$actions->addAction(
id(new PhabricatorActionView())
->setType(PhabricatorActionView::TYPE_DIVIDER));
$actions->addAction(
id(new PhabricatorActionView())
->setName(pht('See More Branches'))
->setIcon('fa-external-link')
->setHref($more_uri));
}
$button = id(new PHUIButtonView())
->setText(pht('Branch: %s', $drequest->getBranch()))
->setTag('a')
->addClass('mmr')
->setIcon('fa-code-fork')
->setColor(PHUIButtonView::GREY)
->setDropdown(true)
->setDropdownMenu($actions);
return $button;
}
private function buildLocateFile() {
$request = $this->getRequest();
$viewer = $request->getUser();
$drequest = $this->getDiffusionRequest();
$repository = $drequest->getRepository();
$form_box = null;
if ($repository->canUsePathTree()) {
Javelin::initBehavior(
'diffusion-locate-file',
array(
'controlID' => 'locate-control',
'inputID' => 'locate-input',
'browseBaseURI' => (string)$drequest->generateURI(
array(
'action' => 'browse',
)),
'uri' => (string)$drequest->generateURI(
array(
'action' => 'pathtree',
)),
));
$form = id(new AphrontFormView())
->setUser($viewer)
->appendChild(
id(new AphrontFormTypeaheadControl())
->setHardpointID('locate-control')
->setID('locate-input')
->setPlaceholder(pht('Locate File')));
$form_box = id(new PHUIBoxView())
->appendChild($form->buildLayoutView())
->addClass('diffusion-profile-locate');
}
return $form_box;
}
private function buildBrowseTable(
$browse_results,
$browse_paths,
$browse_exception,
PHUIPagerView $pager) {
require_celerity_resource('diffusion-icons-css');
$request = $this->getRequest();
$viewer = $request->getUser();
$drequest = $this->getDiffusionRequest();
$repository = $drequest->getRepository();
if ($browse_exception) {
if ($repository->isImporting()) {
// The history table renders a useful message.
return null;
} else {
return $this->renderStatusMessage(
pht('Unable to Retrieve Paths'),
$browse_exception->getMessage());
}
}
$browse_table = id(new DiffusionBrowseTableView())
->setUser($viewer)
->setDiffusionRequest($drequest);
if ($browse_paths) {
$browse_table->setPaths($browse_paths);
} else {
$browse_table->setPaths(array());
}
$browse_uri = $drequest->generateURI(array('action' => 'browse'));
$pager->setURI($browse_uri, 'offset');
$repository_name = $repository->getName();
$branch_name = $drequest->getBranch();
if (strlen($branch_name)) {
$repository_name .= ' ('.$branch_name.')';
}
$header = phutil_tag(
'a',
array(
'href' => $browse_uri,
'class' => 'diffusion-view-browse-header',
),
$repository_name);
return id(new PHUIObjectBoxView())
->setHeaderText($header)
->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)
->setTable($browse_table)
->addClass('diffusion-mobile-view')
->setPager($pager);
}
private function needBranchFuture() {
$drequest = $this->getDiffusionRequest();
if ($drequest->getBranch() === null) {
return false;
}
return true;
}
private function getBranchLimit() {
return 15;
}
}