Sat, 01 Feb 2025 14:19:36 +0100
improve readability
0 | 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 | ||
1 | 25 | #include "settings.h" |
3 | 26 | #include "repositories.h" |
4
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
27 | #include "process.h" |
5 | 28 | #include "heatmap.h" |
29 | #include "html.h" | |
1 | 30 | |
5 | 31 | #include <chrono> |
1 | 32 | #include <cstdlib> |
33 | #include <cstdio> | |
34 | #include <cstring> | |
35 | #include <cerrno> | |
36 | ||
13 | 37 | using namespace std::chrono; |
5 | 38 | |
1 | 39 | static void print_help() { |
40 | fputs( | |
41 | "Usage: fallusmeter [OPTION]... [PATH]...\n\n" | |
42 | "Options:\n" | |
43 | " -h, --help Print this help message\n" | |
44 | " -d, --depth <num> The search depth (default: 1, max: 255)\n" | |
45 | " -n, --no-pull Do not pull the repositories\n" | |
46 | " -s, --separate Output a separate heat map for each repository\n" | |
47 | " -y, --year <year> The year for which to create the heat map\n" | |
48 | " --hg <path> Path to hg binary (default: /usr/bin/hg)\n" | |
49 | " --git <path> Path to git binary (default: /usr/bin/git)\n\n" | |
50 | "Scans all specified paths recursively for Mercurial and Git repositories and\n" | |
51 | "creates a commit heat map for the specified \033[1myear\033[22m or the current year.\n" | |
52 | "By default, the recursion \033[1mdepth\033[22m is one, meaning that this tool assumes that\n" | |
53 | "each \033[1mpath\033[22m is either a repository or contains repositories as subdirectories.\n" | |
54 | "You can change the \033[1mdepth\033[22m to support other directory structures.\n\n" | |
55 | "When you do not specify \033[1m--no-pull\033[22m, this tool will execute the pull command\n" | |
56 | "(and for hg the update command) before retrieving the commit log, assuming\n" | |
9
98312f94dbdd
skip authorization requests when pulling
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
57 | "to be on the default branch with \033[4mno uncommitted changes\033[24m. If pulling leads to\n" |
98312f94dbdd
skip authorization requests when pulling
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
58 | "an error, an error message is written to stderr and the process continues\n" |
98312f94dbdd
skip authorization requests when pulling
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
59 | "with the repository in its current state. This is also the case when pulling\n" |
98312f94dbdd
skip authorization requests when pulling
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
60 | "requires authorization.\n\n" |
3 | 61 | "Afterwards, this tool prints an HTML page to stdout. A separate heap map is\n" |
62 | "generated for each author showing commits across all repositories, unless the\n" | |
63 | "\033[1m--separate\033[22m option is specified in which case each repository is displayed with\n" | |
64 | "its own heat map.\n" | |
1 | 65 | , stderr); |
66 | } | |
67 | ||
3 | 68 | static bool chk_arg(const char *arg, const char *opt1, const char *opt2) { |
1 | 69 | return strcmp(arg, opt1) == 0 || (opt2 != nullptr && strcmp(arg, opt2) == 0); |
70 | } | |
71 | ||
72 | template<typename T> | |
73 | static bool parse_unsigned(const char *str, T *result, unsigned long max) { | |
74 | char *endptr; | |
75 | errno = 0; | |
76 | unsigned long d = strtoul(str, &endptr, 10); | |
77 | if (*endptr != '\0' || errno == ERANGE) return true; | |
78 | if (d < max) { | |
79 | *result = d; | |
80 | return false; | |
81 | } else { | |
82 | return true; | |
83 | } | |
84 | } | |
85 | ||
86 | static int parse_args(fm::settings &settings, int argc, char *argv[]) { | |
87 | for (int i = 1; i < argc; i++) { | |
88 | if (chk_arg(argv[i], "-h", "--help")) { | |
89 | print_help(); | |
90 | return -1; | |
91 | } else if (chk_arg(argv[i], "-d", "--depth")) { | |
92 | if (i + 1 >= argc || parse_unsigned(argv[++i], &settings.depth, 256)) { | |
93 | fputs("missing or invalid depth\n", stderr); | |
94 | return -1; | |
95 | } | |
96 | } else if (chk_arg(argv[i], "-y", "--year")) { | |
97 | if (i + 1 >= argc || parse_unsigned(argv[++i], &settings.year, 9999)) { | |
98 | fputs("missing or invalid year\n", stderr); | |
99 | return -1; | |
100 | } | |
101 | } else if (chk_arg(argv[i], "-n", "--no-pull")) { | |
3 | 102 | settings.update_repos = false; |
1 | 103 | } else if (chk_arg(argv[i], "-s", "--separate")) { |
104 | settings.separate = true; | |
105 | } else if (chk_arg(argv[i], "--hg", nullptr)) { | |
106 | if (i + 1 < argc) { | |
107 | settings.hg.assign(argv[++i]); | |
108 | } else { | |
109 | fputs("--hg is expecting a path\n", stderr); | |
110 | return -1; | |
111 | } | |
112 | } else if (chk_arg(argv[i], "--git", nullptr)) { | |
113 | if (i + 1 < argc) { | |
114 | settings.git.assign(argv[++i]); | |
115 | } else { | |
116 | fputs("--git is expecting a path\n", stderr); | |
117 | return -1; | |
118 | } | |
119 | } else if (argv[i][0] == '-') { | |
120 | fprintf(stderr, "Unknown option: %s\n", argv[i]); | |
121 | return -1; | |
122 | } else { | |
123 | settings.paths.emplace_back(argv[i]); | |
124 | } | |
125 | } | |
126 | ||
127 | if (settings.paths.empty()) { | |
128 | settings.paths.emplace_back("./"); | |
129 | } | |
130 | ||
0 | 131 | return 0; |
132 | } | |
133 | ||
1 | 134 | int main(int argc, char *argv[]) { |
4
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
135 | // parse settings |
1 | 136 | fm::settings settings; |
137 | if (parse_args(settings, argc, argv)) { | |
138 | return EXIT_FAILURE; | |
139 | } | |
140 | ||
4
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
141 | // check hg and git |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
142 | fm::process proc; |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
143 | proc.setbin(settings.hg); |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
144 | if (proc.exec({"--version"})) { |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
145 | fprintf(stderr, "Testing hg binary '%s' failed!\n", settings.hg.c_str()); |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
146 | return EXIT_FAILURE; |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
147 | } |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
148 | proc.setbin(settings.git); |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
149 | if (proc.exec({"--version"})) { |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
150 | fprintf(stderr, "Testing git binary '%s' failed!\n", settings.git.c_str()); |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
151 | return EXIT_FAILURE; |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
152 | } |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
153 | |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
154 | // scan for repos |
3 | 155 | fm::repositories repos; |
156 | for (auto &&path: settings.paths) { | |
157 | repos.scan(path, settings.depth); | |
158 | } | |
4
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
159 | |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
160 | // update repos, if not disabled |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
161 | if (settings.update_repos) { |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
162 | for (auto &&repo : repos.list()) { |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
163 | proc.chdir(repo.path); |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
164 | if (repo.type == fm::HG) { |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
165 | proc.setbin(settings.hg); |
9
98312f94dbdd
skip authorization requests when pulling
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
166 | if (proc.exec({"pull", "-y"})) { |
4
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
167 | fprintf(stderr, "Pulling repo '%s' failed!\nMaybe there is no remote?\n", repo.path.c_str()); |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
168 | } else if (proc.exec({"update"})) { |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
169 | fprintf(stderr, "Updating repo '%s' failed!\nMaybe there are local changes?\n", repo.path.c_str()); |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
170 | } |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
171 | } else { |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
172 | proc.setbin(settings.git); |
9
98312f94dbdd
skip authorization requests when pulling
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
173 | if (proc.exec({"pull", "-q"})) { |
4
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
174 | fprintf(stderr, "Pulling repo '%s' failed!\nMaybe there is no remote or there are local changes?\n", repo.path.c_str()); |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
175 | } |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
176 | } |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
177 | } |
3 | 178 | } |
5 | 179 | |
180 | // determine our reporting range | |
13 | 181 | year report_year{ |
182 | settings.year == fm::settings_current_year | |
183 | ? year_month_day{floor<days>(system_clock::now())}.year() | |
184 | : year{settings.year} | |
185 | }; | |
186 | year_month_day report_begin{report_year, January, 1d}; | |
187 | year_month_day report_end{report_year, December, 31d}; | |
5 | 188 | |
189 | // read the commit logs | |
190 | fm::heatmap heatmap; | |
191 | for (auto &&repo : repos.list()) { | |
192 | if (settings.separate) { | |
193 | heatmap.set_repo(repo.path); | |
194 | } | |
195 | proc.chdir(repo.path); | |
196 | if (repo.type == fm::HG) { | |
197 | proc.setbin(settings.hg); | |
198 | if (proc.exec_log({"log", | |
13 | 199 | "--date", std::format("{0}-01-01 to {0}-12-31", report_year), |
5 | 200 | "--template", "{author}#{date|shortdate}\n"})) { |
201 | fprintf(stderr, "Reading commit log for repo '%s' failed!\n", repo.path.c_str()); | |
202 | return EXIT_FAILURE; | |
203 | } | |
204 | heatmap.add(proc.output()); | |
205 | } else { | |
206 | proc.setbin(settings.git); | |
8
6a2e20a4a8ff
fix log not being captured from git
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
207 | if (proc.exec_log({"log", |
13 | 208 | "--since", std::format("{0}-01-01", report_year), |
209 | "--until", std::format("{0}-12-31", report_year), | |
5 | 210 | "--format=tformat:%an <%ae>#%cs"})) { |
211 | fprintf(stderr, "Reading commit log for repo '%s' failed!\n", repo.path.c_str()); | |
212 | return EXIT_FAILURE; | |
213 | } | |
214 | heatmap.add(proc.output()); | |
215 | } | |
216 | } | |
217 | ||
218 | html::open(); | |
219 | for (const auto &[repo, authors] : heatmap.data()) { | |
220 | html::h1(repo); | |
221 | for (const auto &[author, entries] : authors) { | |
222 | html::h2(author); | |
13 | 223 | html::table_begin(report_year); |
5 | 224 | |
225 | // initialize counters | |
226 | unsigned column = 0, row = 0; | |
3 | 227 | |
5 | 228 | // initialize first day (which must be a Monday, possibly the year before) |
13 | 229 | sys_days day_to_check = January / Monday[1] / report_year; |
5 | 230 | |
231 | // remember the starting point | |
232 | auto start = day_to_check; | |
233 | ||
234 | // now add all entries for Monday, Tuesdays, etc. always starting back in january | |
235 | while (true) { | |
236 | html::row_begin(row); | |
237 | ||
238 | // check if we need to add blank cells | |
239 | while (day_to_check < report_begin) { | |
240 | html::cell_out_of_range(); | |
13 | 241 | day_to_check += days{7}; |
5 | 242 | column++; |
243 | } | |
244 | ||
245 | while (day_to_check <= report_end) { | |
246 | // get the entry from the heatmap | |
247 | auto find_result = entries.find(day_to_check); | |
248 | if (find_result == entries.end()) { | |
7 | 249 | html::cell(day_to_check, 0); |
5 | 250 | } else { |
7 | 251 | html::cell(day_to_check, find_result->second); |
5 | 252 | } |
253 | // advance seven days and one column | |
13 | 254 | day_to_check += days{7}; |
5 | 255 | column++; |
256 | } | |
257 | // fill remaining columns with blank cells | |
258 | for (unsigned i = column ; i < html::columns ; i++) { | |
259 | html::cell_out_of_range(); | |
260 | } | |
261 | ||
262 | // terminate the row | |
263 | html::row_end(); | |
264 | ||
265 | // if we have seen all seven weekdays, that's it | |
266 | if (++row == 7) break; | |
267 | ||
268 | // otherwise, advance the starting point by one day, reset, and begin a new row | |
13 | 269 | start += days{1}; |
5 | 270 | day_to_check = start; |
271 | column =0; | |
272 | } | |
273 | ||
274 | html::table_end(); | |
275 | } | |
276 | } | |
277 | html::close(); | |
3 | 278 | |
1 | 279 | return EXIT_SUCCESS; |
280 | } |