|
1 /* Copyright 2025 Mike Becker. All rights reserved. |
|
2 * |
|
3 * Redistribution and use in source and binary forms, with or without |
|
4 * modification, are permitted provided that the following conditions are met: |
|
5 * |
|
6 * 1. Redistributions of source code must retain the above copyright |
|
7 * notice, this list of conditions and the following disclaimer. |
|
8 * |
|
9 * 2. Redistributions in binary form must reproduce the above copyright |
|
10 * notice, this list of conditions and the following disclaimer in the |
|
11 * documentation and/or other materials provided with the distribution. |
|
12 * |
|
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
16 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
|
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|
20 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
|
21 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
22 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
23 */ |
|
24 |
|
25 #include "repositories.h" |
|
26 |
|
27 #include <utility> |
|
28 #include <filesystem> |
|
29 |
|
30 using namespace fm; |
|
31 namespace fs = std::filesystem; |
|
32 |
|
33 void repositories::scan(std::string path, unsigned depth) { |
|
34 // check the base path |
|
35 { |
|
36 auto p = fs::path{path}; |
|
37 if (is_directory(p / ".hg")) { |
|
38 m_repositories.emplace_back(repository{canonical(p), HG}); |
|
39 return; |
|
40 } else if (is_directory(p / ".git")) { |
|
41 m_repositories.emplace_back(repository{canonical(p), GIT}); |
|
42 return; |
|
43 } else if (depth == 0) { |
|
44 return; |
|
45 } |
|
46 } |
|
47 |
|
48 // if depth is non-zero, scan subdirs recursively |
|
49 --depth; |
|
50 for (auto i = fs::recursive_directory_iterator( |
|
51 std::move(path), |
|
52 fs::directory_options::skip_permission_denied); |
|
53 i != fs::recursive_directory_iterator(); ++i) { |
|
54 if (!i->is_directory()) continue; |
|
55 auto p = i->path(); |
|
56 if (is_directory(p / ".hg")) { |
|
57 m_repositories.emplace_back(repository{canonical(p), HG}); |
|
58 i.disable_recursion_pending(); |
|
59 } else if (is_directory(p / ".git")) { |
|
60 m_repositories.emplace_back(repository{canonical(p), GIT}); |
|
61 i.disable_recursion_pending(); |
|
62 } else if (i.depth() == depth) { |
|
63 i.disable_recursion_pending(); |
|
64 } |
|
65 } |
|
66 } |