1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-29 02:02:41 +01:00
phorge-phorge/src/applications/diffusion/conduit/DiffusionBranchQueryConduitAPIMethod.php

126 lines
3.5 KiB
PHP
Raw Normal View History

<?php
final class DiffusionBranchQueryConduitAPIMethod
extends DiffusionQueryConduitAPIMethod {
public function getAPIMethodName() {
return 'diffusion.branchquery';
}
public function getMethodDescription() {
return pht('Determine what branches exist for a repository.');
}
protected function defineReturnType() {
return 'list<dict>';
}
protected function defineCustomParamTypes() {
return array(
'closed' => 'optional bool',
'limit' => 'optional int',
'offset' => 'optional int',
'contains' => 'optional string',
);
}
protected function getGitResult(ConduitAPIRequest $request) {
$drequest = $this->getDiffusionRequest();
$repository = $drequest->getRepository();
$contains = $request->getValue('contains');
if (strlen($contains)) {
// NOTE: We can't use DiffusionLowLevelGitRefQuery here because
// `git for-each-ref` does not support `--contains`.
if ($repository->isWorkingCopyBare()) {
list($stdout) = $repository->execxLocalCommand(
'branch --verbose --no-abbrev --contains %s --',
$contains);
$ref_map = DiffusionGitBranch::parseLocalBranchOutput(
$stdout);
} else {
list($stdout) = $repository->execxLocalCommand(
'branch -r --verbose --no-abbrev --contains %s --',
$contains);
$ref_map = DiffusionGitBranch::parseRemoteBranchOutput(
$stdout,
DiffusionGitBranch::DEFAULT_GIT_REMOTE);
}
$refs = array();
foreach ($ref_map as $ref => $commit) {
$refs[] = id(new DiffusionRepositoryRef())
->setShortName($ref)
->setCommitIdentifier($commit);
}
} else {
$refs = id(new DiffusionLowLevelGitRefQuery())
->setRepository($repository)
->withIsOriginBranch(true)
->execute();
}
return $this->processBranchRefs($request, $refs);
}
protected function getMercurialResult(ConduitAPIRequest $request) {
$drequest = $this->getDiffusionRequest();
$repository = $drequest->getRepository();
Never use "{branches}" in Mercurial Summary: Fixes T5304. Mercurial features a "{branches}" template keyword, documented as: ``` branches List of strings. The name of the branch on which the changeset was committed. Will be empty if the branch name was default. ``` At some time long in the past, I misinterpreted this to mean "list of branches where the branch head is a descendant of the commit". It is more like "list of zero or one elements, possibly containing the name of the branch the commit was originally made to, if that branch was not 'default'". In fact, it seems like this is because a //very// long time in the past, Mercurial worked roughly like I expected: > Ages ago (2005), we had a very different and ultimately unworkable > approach to named branches that worked vaguely like .hgtags and allowed > multiple branch names per revision. http://marc.info/?l=mercurial-devel&m=129883069414855 This appears to be deprecated in modern Mercurial (it's not in the modern web documentation) although I can't find a commit about it so maybe that's just a documentation issue. In any case, `{branches}` seems to never be useful: `{branch}` provides the same information without the awkward "default-if-empty" case. Switch from `{branches}` to either `{branch}` (where that's good enough, notably in the hook engine) or `(descendants(%s) and head())`, which is equivalent to `--contains` in Git. This fixes pushing to branches with spaces in their names, and makes the "Branches" / "Contains" queries moderately more consistent. Test Plan: - Pushed to a Mercurial branch with a space in it. - Viewed list of branches in a Mercurial repository. - Viewed containing branches of a Mercurial commit in Diffusion. Reviewers: btrahan Reviewed By: btrahan Subscribers: epriestley Maniphest Tasks: T5304 Differential Revision: https://secure.phabricator.com/D9453
2014-06-20 20:48:31 +02:00
$query = id(new DiffusionLowLevelMercurialBranchesQuery())
->setRepository($repository);
$contains = $request->getValue('contains');
if (strlen($contains)) {
Never use "{branches}" in Mercurial Summary: Fixes T5304. Mercurial features a "{branches}" template keyword, documented as: ``` branches List of strings. The name of the branch on which the changeset was committed. Will be empty if the branch name was default. ``` At some time long in the past, I misinterpreted this to mean "list of branches where the branch head is a descendant of the commit". It is more like "list of zero or one elements, possibly containing the name of the branch the commit was originally made to, if that branch was not 'default'". In fact, it seems like this is because a //very// long time in the past, Mercurial worked roughly like I expected: > Ages ago (2005), we had a very different and ultimately unworkable > approach to named branches that worked vaguely like .hgtags and allowed > multiple branch names per revision. http://marc.info/?l=mercurial-devel&m=129883069414855 This appears to be deprecated in modern Mercurial (it's not in the modern web documentation) although I can't find a commit about it so maybe that's just a documentation issue. In any case, `{branches}` seems to never be useful: `{branch}` provides the same information without the awkward "default-if-empty" case. Switch from `{branches}` to either `{branch}` (where that's good enough, notably in the hook engine) or `(descendants(%s) and head())`, which is equivalent to `--contains` in Git. This fixes pushing to branches with spaces in their names, and makes the "Branches" / "Contains" queries moderately more consistent. Test Plan: - Pushed to a Mercurial branch with a space in it. - Viewed list of branches in a Mercurial repository. - Viewed containing branches of a Mercurial commit in Diffusion. Reviewers: btrahan Reviewed By: btrahan Subscribers: epriestley Maniphest Tasks: T5304 Differential Revision: https://secure.phabricator.com/D9453
2014-06-20 20:48:31 +02:00
$query->withContainsCommit($contains);
}
Never use "{branches}" in Mercurial Summary: Fixes T5304. Mercurial features a "{branches}" template keyword, documented as: ``` branches List of strings. The name of the branch on which the changeset was committed. Will be empty if the branch name was default. ``` At some time long in the past, I misinterpreted this to mean "list of branches where the branch head is a descendant of the commit". It is more like "list of zero or one elements, possibly containing the name of the branch the commit was originally made to, if that branch was not 'default'". In fact, it seems like this is because a //very// long time in the past, Mercurial worked roughly like I expected: > Ages ago (2005), we had a very different and ultimately unworkable > approach to named branches that worked vaguely like .hgtags and allowed > multiple branch names per revision. http://marc.info/?l=mercurial-devel&m=129883069414855 This appears to be deprecated in modern Mercurial (it's not in the modern web documentation) although I can't find a commit about it so maybe that's just a documentation issue. In any case, `{branches}` seems to never be useful: `{branch}` provides the same information without the awkward "default-if-empty" case. Switch from `{branches}` to either `{branch}` (where that's good enough, notably in the hook engine) or `(descendants(%s) and head())`, which is equivalent to `--contains` in Git. This fixes pushing to branches with spaces in their names, and makes the "Branches" / "Contains" queries moderately more consistent. Test Plan: - Pushed to a Mercurial branch with a space in it. - Viewed list of branches in a Mercurial repository. - Viewed containing branches of a Mercurial commit in Diffusion. Reviewers: btrahan Reviewed By: btrahan Subscribers: epriestley Maniphest Tasks: T5304 Differential Revision: https://secure.phabricator.com/D9453
2014-06-20 20:48:31 +02:00
$refs = $query->execute();
return $this->processBranchRefs($request, $refs);
}
2014-01-29 16:29:35 +01:00
protected function getSVNResult(ConduitAPIRequest $request) {
// Since SVN doesn't have meaningful branches, just return nothing for all
// queries.
return array();
}
private function processBranchRefs(ConduitAPIRequest $request, array $refs) {
$drequest = $this->getDiffusionRequest();
$repository = $drequest->getRepository();
$offset = $request->getValue('offset');
$limit = $request->getValue('limit');
foreach ($refs as $key => $ref) {
if (!$repository->shouldTrackBranch($ref->getShortName())) {
unset($refs[$key]);
}
}
$with_closed = $request->getValue('closed');
if ($with_closed !== null) {
foreach ($refs as $key => $ref) {
$fields = $ref->getRawFields();
if (idx($fields, 'closed') != $with_closed) {
unset($refs[$key]);
}
}
}
// NOTE: We can't apply the offset or limit until here, because we may have
// filtered untrackable branches out of the result set.
if ($offset) {
$refs = array_slice($refs, $offset);
}
if ($limit) {
$refs = array_slice($refs, 0, $limit);
}
return mpull($refs, 'toDictionary');
}
}