SlHelpers
Pattern.h
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #pragma once
4 
5 #include <filesystem>
6 #include <optional>
7 #include <string>
8 
9 #include "../git/PathSpec.h"
10 
11 namespace SlKernCVS {
12 
16 struct Pattern {
17  Pattern() = delete;
18 
24  unsigned match(const std::filesystem::path &path) const {
25  if (m_pathspec.matchesPath(path))
26  return m_weight;
27  return 0;
28  }
29 
35  static std::optional<Pattern> create(std::string pattern);
36 private:
37  SlGit::PathSpec m_pathspec;
38  unsigned m_weight;
39 
40  Pattern(SlGit::PathSpec pathspec, unsigned weight) : m_pathspec(std::move(pathspec)),
41  m_weight(weight) {}
42 
43  static constexpr unsigned pattern_weight(std::string_view pattern);
44 };
45 
46 }
static std::optional< Pattern > create(std::string pattern)
Build a (git) Pattern.
PathSpec is a representation of git pathspecs.
Definition: PathSpec.h:68
Holds git patterns and reports weights if matched.
Definition: Pattern.h:16
unsigned match(const std::filesystem::path &path) const
Match a path against the stored patterns.
Definition: Pattern.h:24
bool matchesPath(const std::string &path, uint32_t flags=GIT_PATHSPEC_DEFAULT) const noexcept
Return true if this PathSpec matches path.
Definition: PathSpec.h:80
Definition: Branches.h:15