SlHelpers
Branches.h
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #pragma once
4 
5 #include <chrono>
6 #include <optional>
7 #include <string>
8 #include <string_view>
9 #include <unordered_map>
10 #include <unordered_set>
11 #include <vector>
12 
13 #include "../helpers/String.h"
14 
15 namespace SlKernCVS {
16 
20 struct BranchProps {
22  using BranchesList = std::vector<std::string>;
23 
25  bool isBuild;
27  bool isPublish;
29  bool isExcluded;
30 
32  std::chrono::year_month_day eol;
33 
36 };
37 
41 class Branches {
42 public:
44  enum Filter : unsigned {
45  BUILD = 1U << 0,
46  PUBLISH = 1U << 1,
47  EXCLUDED = 1U << 2,
48  ANY = ~0U,
49  };
50 
52  using BranchesSet = std::unordered_set<std::string>;
56  using BranchesMap = std::unordered_map<std::string, BranchProps, SlHelpers::String::Hash,
58 
59  Branches() = delete;
65  static Branches create(std::string_view branchesConf) noexcept;
70  static std::optional<Branches> create();
71 
76  const BranchesMap &map() const noexcept { return m_map; }
77 
82  auto begin() const noexcept { return m_map.begin(); }
87  auto end() const noexcept { return m_map.end(); }
88 
95  BranchesList filter(unsigned include = ANY, unsigned exclude = EXCLUDED) const;
96 
98  const BranchProps &props(std::string_view branch) const {
99  return m_map.find(branch)->second;
100  }
101 
107  const BranchesList &merges(std::string_view branch) const {
108  return props(branch).merges;
109  }
115  BranchesSet mergesClosure(std::string_view branch) const;
116 
122  static BranchesList getBuildBranches(std::string_view branchesConf) noexcept;
123 
128  static std::optional<BranchesList> getBuildBranches();
129 private:
130  Branches(BranchesMap &map) : m_map(std::move(map)) {}
131 
132  void dfs(std::string_view u, BranchesSet &visited) const;
133 
134  BranchesMap m_map;
135 
136  static std::chrono::year_month_day parseDate(std::string_view date);
137  static bool isExcluded(std::string_view branch);
138 };
139 
140 }
Equality test for string and string_view to be used in containers.
Definition: String.h:250
Hash for string and string_view to be used in hashing containers.
Definition: String.h:223
static std::optional< BranchesList > getBuildBranches()
Download branches.conf and convert it to a list of branches which are built.
BranchesList filter(unsigned include=ANY, unsigned exclude=EXCLUDED) const
Obtain BranchesList according to a filter specified by include and exclude.
bool isExcluded
One of master, stable, vanilla, or similar branches.
Definition: Branches.h:29
const BranchProps & props(std::string_view branch) const
Return BranchProps for branch.
Definition: Branches.h:98
BranchesSet mergesClosure(std::string_view branch) const
Closure of branches that the specified branch merges.
Parse branches.conf into a map of branch -> properties (BranchProps)
Definition: Branches.h:41
std::unordered_set< std::string > BranchesSet
A set of branches.
Definition: Branches.h:52
auto begin() const noexcept
Obtain begin iterator of branches.
Definition: Branches.h:82
Filter
Constants used for filter()
Definition: Branches.h:44
Properties of a branch stored in Branches.
Definition: Branches.h:20
std::chrono::year_month_day eol
End of life for the branch.
Definition: Branches.h:32
bool isPublish
Marked as publish in branches.conf.
Definition: Branches.h:27
Definition: Branches.h:15
static std::optional< Branches > create()
Download branches.conf and parse it into Branches.
BranchProps::BranchesList BranchesList
A list of branches.
Definition: Branches.h:54
auto end() const noexcept
Obtain end iterator of branches.
Definition: Branches.h:87
bool isBuild
Marked as build in branches.conf.
Definition: Branches.h:25
std::vector< std::string > BranchesList
List of branches.
Definition: Branches.h:22
std::unordered_map< std::string, BranchProps, SlHelpers::String::Hash, SlHelpers::String::Eq > BranchesMap
Branch -> BranchProps mapping.
Definition: Branches.h:57
BranchesList merges
What immediate branches this branch merges.
Definition: Branches.h:35
const BranchesList & merges(std::string_view branch) const
Immediate branches that the specified branch merges.
Definition: Branches.h:107
const BranchesMap & map() const noexcept
Obtain whole branch map.
Definition: Branches.h:76