Skip to content

Commit 9155369

Browse files
author
epriestley
committed
Add a helper function to DiffusionPathIDQuery
Summary: Just breaking D960 into some smaller parts, this is a standalone method used in Mercurial parsing. (There's a bad version of this function in the SVN stuff but I'll get rid of it the next time I'm in there.) Test Plan: See D960. Reviewers: Makinde, jungejason, nh, tuomaspelkonen, aran Reviewed By: Makinde CC: aran, Makinde Differential Revision: 965
1 parent cd71098 commit 9155369

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

src/applications/diffusion/query/pathid/base/DiffusionPathIDQuery.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
* limitations under the License.
1717
*/
1818

19+
/**
20+
* @task pathutil Path Utilities
21+
*/
1922
final class DiffusionPathIDQuery {
2023

2124
public function __construct(array $paths) {
@@ -49,13 +52,55 @@ public function loadPathIDs() {
4952
return $result;
5053
}
5154

55+
56+
/**
57+
* Convert a path to the canonical, absolute representation used by Diffusion.
58+
*
59+
* @param string Some repository path.
60+
* @return string Canonicalized Diffusion path.
61+
* @task pathutil
62+
*/
5263
public static function normalizePath($path) {
64+
65+
// Normalize to single slashes, e.g. "///" => "/".
66+
$path = preg_replace('@[/]{2,}@', '/', $path);
67+
5368
return '/'.trim($path, '/');
5469
}
5570

71+
72+
/**
73+
* Return the canonical parent directory for a path. Note, returns "/" when
74+
* passed "/".
75+
*
76+
* @param string Some repository path.
77+
* @return string That path's canonical parent directory.
78+
* @task pathutil
79+
*/
5680
public static function getParentPath($path) {
5781
$path = self::normalizePath($path);
5882
return dirname($path);
5983
}
6084

85+
86+
/**
87+
* Generate a list of parents for a repository path. The path itself is
88+
* included.
89+
*
90+
* @param string Some repository path.
91+
* @return list List of canonical paths between the path and the root.
92+
* @task pathutil
93+
*/
94+
public static function expandPathToRoot($path) {
95+
$path = self::normalizePath($path);
96+
$parents = array($path);
97+
$parts = explode('/', trim($path, '/'));
98+
while (count($parts) >= 1) {
99+
if (array_pop($parts)) {
100+
$parents[] = '/'.implode('/', $parts);
101+
}
102+
}
103+
return $parents;
104+
}
105+
61106
}

src/applications/diffusion/query/pathid/base/__tests__/DiffusionPathQueryTestCase.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,28 @@ public function testParentEdgeCases() {
3131
'/a',
3232
DiffusionPathIDQuery::getParentPath('/a/b'),
3333
'Parent of /a/b');
34+
$this->assertEqual(
35+
'/a',
36+
DiffusionPathIDQuery::getParentPath('/a///b'),
37+
'Parent of /a///b');
38+
}
39+
40+
public function testExpandEdgeCases() {
41+
$this->assertEqual(
42+
array('/'),
43+
DiffusionPathIDQuery::expandPathToRoot('/'));
44+
$this->assertEqual(
45+
array('/'),
46+
DiffusionPathIDQuery::expandPathToRoot('//'));
47+
$this->assertEqual(
48+
array('/a/b', '/a', '/'),
49+
DiffusionPathIDQuery::expandPathToRoot('/a/b'));
50+
$this->assertEqual(
51+
array('/a/b', '/a', '/'),
52+
DiffusionPathIDQuery::expandPathToRoot('/a//b'));
53+
$this->assertEqual(
54+
array('/a/b', '/a', '/'),
55+
DiffusionPathIDQuery::expandPathToRoot('a/b'));
3456
}
3557

3658
}

0 commit comments

Comments
 (0)