forked from phacility/phabricator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiffusionCommitRevisionQuery.php
138 lines (109 loc) · 3.82 KB
/
DiffusionCommitRevisionQuery.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
<?php
final class DiffusionCommitRevisionQuery
extends Phobject {
public static function loadRevisionMapForCommits(
PhabricatorUser $viewer,
array $commits) {
assert_instances_of($commits, 'PhabricatorRepositoryCommit');
if (!$commits) {
return array();
}
$commit_phids = mpull($commits, 'getPHID');
$edge_query = id(new PhabricatorEdgeQuery())
->withSourcePHIDs($commit_phids)
->withEdgeTypes(
array(
DiffusionCommitHasRevisionEdgeType::EDGECONST,
));
$edge_query->execute();
$revision_phids = $edge_query->getDestinationPHIDs();
if (!$revision_phids) {
return array();
}
$revisions = id(new DifferentialRevisionQuery())
->setViewer($viewer)
->withPHIDs($revision_phids)
->execute();
$revisions = mpull($revisions, null, 'getPHID');
$map = array();
foreach ($commit_phids as $commit_phid) {
$revision_phids = $edge_query->getDestinationPHIDs(
array(
$commit_phid,
));
$map[$commit_phid] = array_select_keys($revisions, $revision_phids);
}
return $map;
}
public static function loadRevisionForCommit(
PhabricatorUser $viewer,
PhabricatorRepositoryCommit $commit) {
$data = $commit->getCommitData();
$revision_id = $data->getCommitDetail('differential.revisionID');
if (!$revision_id) {
return null;
}
return id(new DifferentialRevisionQuery())
->setViewer($viewer)
->withIDs(array($revision_id))
->needReviewers(true)
->executeOne();
}
public static function loadRevertedObjects(
PhabricatorUser $viewer,
$source_object,
array $object_names,
PhabricatorRepository $repository_scope = null) {
// Fetch commits first, since we need to load data on commits in order
// to identify associated revisions later on.
$commit_query = id(new DiffusionCommitQuery())
->setViewer($viewer)
->withIdentifiers($object_names)
->needCommitData(true);
// If we're acting in a specific repository, only allow commits in that
// repository to be affected: when commit X reverts commit Y by hash, we
// only want to revert commit Y in the same repository, even if other
// repositories have a commit with the same hash.
if ($repository_scope) {
$commit_query->withRepository($repository_scope);
}
$objects = $commit_query->execute();
$more_objects = id(new PhabricatorObjectQuery())
->setViewer($viewer)
->withNames($object_names)
->withTypes(
array(
DifferentialRevisionPHIDType::TYPECONST,
))
->execute();
foreach ($more_objects as $object) {
$objects[] = $object;
}
// See PHI1008 and T13276. If something reverts commit X, we also revert
// any associated revision.
// For now, we don't try to find associated commits if something reverts
// revision Y. This is less common, although we could make more of an
// effort in the future.
foreach ($objects as $object) {
if (!($object instanceof PhabricatorRepositoryCommit)) {
continue;
}
// NOTE: If our object "reverts X", where "X" is a commit hash, it is
// possible that "X" will not have parsed yet, so we'll fail to find
// a revision even though one exists.
// For now, do nothing. It's rare to push a commit which reverts some
// commit "X" before "X" has parsed, so we expect this to be unusual.
$revision = self::loadRevisionForCommit(
$viewer,
$object);
if ($revision) {
$objects[] = $revision;
}
}
$objects = mpull($objects, null, 'getPHID');
// Prevent an object from reverting itself, although this would be very
// clever in Git or Mercurial.
unset($objects[$source_object->getPHID()]);
return $objects;
}
}