Mon, 19 May 2025 16:05:58 +0200
add custom fragment indentation
3 | 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 | ||
37
d7e9a1200e21
improve headings in separate repository view
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
33 | repository::repository(repository_type type, std::string path) noexcept |
d7e9a1200e21
improve headings in separate repository view
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
34 | : path(std::move(path)), |
d7e9a1200e21
improve headings in separate repository view
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
35 | name(fs::path(this->path).filename()), |
d7e9a1200e21
improve headings in separate repository view
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
36 | type(type) { |
d7e9a1200e21
improve headings in separate repository view
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
37 | } |
d7e9a1200e21
improve headings in separate repository view
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
38 | |
52
e9edc3bd0301
add custom fragment indentation
Mike Becker <universe@uap-core.de>
parents:
49
diff
changeset
|
39 | bool repositories::exists(const std::string &path) { |
e9edc3bd0301
add custom fragment indentation
Mike Becker <universe@uap-core.de>
parents:
49
diff
changeset
|
40 | return fs::is_directory(path); |
e9edc3bd0301
add custom fragment indentation
Mike Becker <universe@uap-core.de>
parents:
49
diff
changeset
|
41 | } |
e9edc3bd0301
add custom fragment indentation
Mike Becker <universe@uap-core.de>
parents:
49
diff
changeset
|
42 | |
3 | 43 | void repositories::scan(std::string path, unsigned depth) { |
44 | // check the base path | |
45 | { | |
46 | auto p = fs::path{path}; | |
47 | if (is_directory(p / ".hg")) { | |
37
d7e9a1200e21
improve headings in separate repository view
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
48 | m_repositories.emplace_back(HG, canonical(p)); |
3 | 49 | return; |
50 | } else if (is_directory(p / ".git")) { | |
37
d7e9a1200e21
improve headings in separate repository view
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
51 | m_repositories.emplace_back(GIT, canonical(p)); |
3 | 52 | return; |
53 | } else if (depth == 0) { | |
54 | return; | |
55 | } | |
56 | } | |
57 | ||
58 | // if depth is non-zero, scan subdirs recursively | |
59 | --depth; | |
60 | for (auto i = fs::recursive_directory_iterator( | |
61 | std::move(path), | |
62 | fs::directory_options::skip_permission_denied); | |
63 | i != fs::recursive_directory_iterator(); ++i) { | |
64 | if (!i->is_directory()) continue; | |
65 | auto p = i->path(); | |
66 | if (is_directory(p / ".hg")) { | |
37
d7e9a1200e21
improve headings in separate repository view
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
67 | m_repositories.emplace_back(HG, canonical(p)); |
3 | 68 | i.disable_recursion_pending(); |
69 | } else if (is_directory(p / ".git")) { | |
37
d7e9a1200e21
improve headings in separate repository view
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
70 | m_repositories.emplace_back(GIT, canonical(p)); |
3 | 71 | i.disable_recursion_pending(); |
49
e4f1aea7df99
fix comparison of signed and unsigned value
Mike Becker <universe@uap-core.de>
parents:
37
diff
changeset
|
72 | } else if (static_cast<unsigned>(i.depth()) == depth) { |
3 | 73 | i.disable_recursion_pending(); |
74 | } | |
75 | } | |
76 | } |