Skip to content

Commit f65fa33

Browse files
committed
Tighten the directory name mathing with -i.
Only match full directory names as parts of whole paths. So -isrc matches src/file.cpp and proj/src/file.cpp. But does not match mysrc/file.cpp or proj/srcfiles/file.cpp.
1 parent e9ec4bc commit f65fa33

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

cli/pathmatch.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,14 @@ bool PathMatch::Match(const std::string &path)
3535
if (findpath[findpath.length() - 1] != '/')
3636
findpath = RemoveFilename(findpath);
3737

38-
if (findpath.find(*iterMask) != std::string::npos)
38+
// Match relative paths starting with mask
39+
// -isrc matches src/foo.cpp
40+
if (findpath.compare(0, (*iterMask).size(), *iterMask) == 0)
41+
return true;
42+
// Match only full directory name in middle or end of the path
43+
// -isrc matches myproject/src/ but does not match
44+
// myproject/srcfiles/ or myproject/mysrc/
45+
if (findpath.find("/" + *iterMask) != std::string::npos)
3946
return true;
4047
}
4148
return false;

test/testpathmatch.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ class TestPathMatch : public TestFixture
3838
TEST_CASE(onemaskemptypath);
3939
TEST_CASE(onemasksamepath);
4040
TEST_CASE(onemasksamepathwithfile);
41+
TEST_CASE(onemaskdifferentdir1);
42+
TEST_CASE(onemaskdifferentdir2);
43+
TEST_CASE(onemaskdifferentdir3);
44+
TEST_CASE(onemaskdifferentdir4);
4145
TEST_CASE(onemasklongerpath1);
4246
TEST_CASE(onemasklongerpath2);
4347
TEST_CASE(onemasklongerpath3);
@@ -95,6 +99,38 @@ class TestPathMatch : public TestFixture
9599
ASSERT(match.Match("src/file.txt"));
96100
}
97101

102+
void onemaskdifferentdir1()
103+
{
104+
std::vector<std::string> masks;
105+
masks.push_back("src/");
106+
PathMatch match(masks);
107+
ASSERT(!match.Match("srcfiles/file.txt"));
108+
}
109+
110+
void onemaskdifferentdir2()
111+
{
112+
std::vector<std::string> masks;
113+
masks.push_back("src/");
114+
PathMatch match(masks);
115+
ASSERT(!match.Match("proj/srcfiles/file.txt"));
116+
}
117+
118+
void onemaskdifferentdir3()
119+
{
120+
std::vector<std::string> masks;
121+
masks.push_back("src/");
122+
PathMatch match(masks);
123+
ASSERT(!match.Match("proj/mysrc/file.txt"));
124+
}
125+
126+
void onemaskdifferentdir4()
127+
{
128+
std::vector<std::string> masks;
129+
masks.push_back("src/");
130+
PathMatch match(masks);
131+
ASSERT(!match.Match("proj/mysrcfiles/file.txt"));
132+
}
133+
98134
void onemasklongerpath1()
99135
{
100136
std::vector<std::string> masks;

0 commit comments

Comments
 (0)