Sat, 01 Feb 2025 17:15:14 +0100
fix accidentally breaking the start of year
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" | |
14 | 43 | " -a, --author <name> Only report this author\n" |
44 | " (repeat option to report multiple authors)\n" | |
45 | " -A, --authormap <file> Apply an author mapping file\n" | |
46 | " -h, --help Print this help message\n" | |
47 | " -d, --depth <num> The search depth (default: 1, max: 255)\n" | |
48 | " -n, --no-pull Do not pull the repositories\n" | |
49 | " -s, --separate Output a separate heat map for each repository\n" | |
50 | " -y, --year <year> The year for which to create the heat map\n" | |
51 | " --hg <path> Path to hg binary (default: /usr/bin/hg)\n" | |
52 | " --git <path> Path to git binary (default: /usr/bin/git)\n\n" | |
1 | 53 | "Scans all specified paths recursively for Mercurial and Git repositories and\n" |
54 | "creates a commit heat map for the specified \033[1myear\033[22m or the current year.\n" | |
55 | "By default, the recursion \033[1mdepth\033[22m is one, meaning that this tool assumes that\n" | |
56 | "each \033[1mpath\033[22m is either a repository or contains repositories as subdirectories.\n" | |
57 | "You can change the \033[1mdepth\033[22m to support other directory structures.\n\n" | |
58 | "When you do not specify \033[1m--no-pull\033[22m, this tool will execute the pull command\n" | |
59 | "(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
|
60 | "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
|
61 | "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
|
62 | "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
|
63 | "requires authorization.\n\n" |
14 | 64 | "By default, this tool reports commits from all authors. If you want to include\n" |
65 | "only specific authors in the report, you can use the \033[1m--author\033[22m option with as\n" | |
66 | "many authors as you like. You can specify either the full author strings with\n" | |
67 | "name and mail address, just the mail address, or even just the local-part of\n" | |
68 | "the mail address. In case your repository contains commits from an author who\n" | |
69 | "used different names or mail addresses, you can use the \033[1m--authormap\033[22m option\n" | |
70 | "to specify a file that contains pairs of author strings, like in the following\n" | |
71 | "example:\n\n" | |
72 | " Full Name <full.name@example.org> = New Name <new.name@example.org>\n" | |
16 | 73 | " just.mail@example.org = Jus Mail <just.mail@example.org>\n" |
74 | " jane = Jane Doe <jane.doe@example.org>\n\n" | |
14 | 75 | "The different variants of full string, only mail address, and only local-part\n" |
16 | 76 | "should \033[4monly\033[24m be used on the left-hand side. When you use the \033[1m--author\033[22m option at\n" |
77 | "the same time, you only need to specify the new author names.\n\n" | |
14 | 78 | "Finally, this tool prints an HTML page to stdout. A separate heap map is\n" |
3 | 79 | "generated for each author showing commits across all repositories, unless the\n" |
80 | "\033[1m--separate\033[22m option is specified in which case each repository is displayed with\n" | |
81 | "its own heat map.\n" | |
1 | 82 | , stderr); |
83 | } | |
84 | ||
3 | 85 | static bool chk_arg(const char *arg, const char *opt1, const char *opt2) { |
1 | 86 | return strcmp(arg, opt1) == 0 || (opt2 != nullptr && strcmp(arg, opt2) == 0); |
87 | } | |
88 | ||
89 | template<typename T> | |
90 | static bool parse_unsigned(const char *str, T *result, unsigned long max) { | |
91 | char *endptr; | |
92 | errno = 0; | |
93 | unsigned long d = strtoul(str, &endptr, 10); | |
94 | if (*endptr != '\0' || errno == ERANGE) return true; | |
95 | if (d < max) { | |
96 | *result = d; | |
97 | return false; | |
98 | } else { | |
99 | return true; | |
100 | } | |
101 | } | |
102 | ||
103 | static int parse_args(fm::settings &settings, int argc, char *argv[]) { | |
104 | for (int i = 1; i < argc; i++) { | |
105 | if (chk_arg(argv[i], "-h", "--help")) { | |
106 | print_help(); | |
107 | return -1; | |
108 | } else if (chk_arg(argv[i], "-d", "--depth")) { | |
109 | if (i + 1 >= argc || parse_unsigned(argv[++i], &settings.depth, 256)) { | |
110 | fputs("missing or invalid depth\n", stderr); | |
111 | return -1; | |
112 | } | |
113 | } else if (chk_arg(argv[i], "-y", "--year")) { | |
114 | if (i + 1 >= argc || parse_unsigned(argv[++i], &settings.year, 9999)) { | |
115 | fputs("missing or invalid year\n", stderr); | |
116 | return -1; | |
117 | } | |
14 | 118 | } else if (chk_arg(argv[i], "-a", "--author")) { |
119 | if (i + 1 < argc) { | |
120 | settings.authors.emplace_back(argv[++i]); | |
121 | } else { | |
122 | fputs("missing author name\n", stderr); | |
123 | return -1; | |
124 | } | |
1 | 125 | } else if (chk_arg(argv[i], "-n", "--no-pull")) { |
3 | 126 | settings.update_repos = false; |
1 | 127 | } else if (chk_arg(argv[i], "-s", "--separate")) { |
128 | settings.separate = true; | |
16 | 129 | } else if (chk_arg(argv[i], "-A", "--authormap")) { |
130 | if (i + 1 < argc) { | |
131 | if (settings.parse_authormap(argv[++i])) { | |
132 | fputs("parsing authormap failed\n", stderr); | |
133 | return -1; | |
134 | } | |
135 | } else { | |
136 | fputs("missing filename for authormap\n", stderr); | |
137 | return -1; | |
138 | } | |
1 | 139 | } else if (chk_arg(argv[i], "--hg", nullptr)) { |
140 | if (i + 1 < argc) { | |
141 | settings.hg.assign(argv[++i]); | |
142 | } else { | |
143 | fputs("--hg is expecting a path\n", stderr); | |
144 | return -1; | |
145 | } | |
146 | } else if (chk_arg(argv[i], "--git", nullptr)) { | |
147 | if (i + 1 < argc) { | |
148 | settings.git.assign(argv[++i]); | |
149 | } else { | |
150 | fputs("--git is expecting a path\n", stderr); | |
151 | return -1; | |
152 | } | |
153 | } else if (argv[i][0] == '-') { | |
154 | fprintf(stderr, "Unknown option: %s\n", argv[i]); | |
155 | return -1; | |
156 | } else { | |
157 | settings.paths.emplace_back(argv[i]); | |
158 | } | |
159 | } | |
160 | ||
161 | if (settings.paths.empty()) { | |
162 | settings.paths.emplace_back("./"); | |
163 | } | |
164 | ||
0 | 165 | return 0; |
166 | } | |
167 | ||
1 | 168 | int main(int argc, char *argv[]) { |
4
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
169 | // parse settings |
1 | 170 | fm::settings settings; |
171 | if (parse_args(settings, argc, argv)) { | |
172 | return EXIT_FAILURE; | |
173 | } | |
174 | ||
4
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
175 | // check hg and git |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
176 | fm::process proc; |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
177 | proc.setbin(settings.hg); |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
178 | if (proc.exec({"--version"})) { |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
179 | 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
|
180 | return EXIT_FAILURE; |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
181 | } |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
182 | proc.setbin(settings.git); |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
183 | if (proc.exec({"--version"})) { |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
184 | 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
|
185 | return EXIT_FAILURE; |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
186 | } |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
187 | |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
188 | // scan for repos |
3 | 189 | fm::repositories repos; |
190 | for (auto &&path: settings.paths) { | |
191 | repos.scan(path, settings.depth); | |
192 | } | |
4
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
193 | |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
194 | // update repos, if not disabled |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
195 | if (settings.update_repos) { |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
196 | for (auto &&repo : repos.list()) { |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
197 | proc.chdir(repo.path); |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
198 | if (repo.type == fm::HG) { |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
199 | proc.setbin(settings.hg); |
9
98312f94dbdd
skip authorization requests when pulling
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
200 | if (proc.exec({"pull", "-y"})) { |
4
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
201 | 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
|
202 | } else if (proc.exec({"update"})) { |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
203 | 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
|
204 | } |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
205 | } else { |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
206 | proc.setbin(settings.git); |
9
98312f94dbdd
skip authorization requests when pulling
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
207 | if (proc.exec({"pull", "-q"})) { |
4
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
208 | 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
|
209 | } |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
210 | } |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
211 | } |
3 | 212 | } |
5 | 213 | |
214 | // determine our reporting range | |
13 | 215 | year report_year{ |
216 | settings.year == fm::settings_current_year | |
217 | ? year_month_day{floor<days>(system_clock::now())}.year() | |
218 | : year{settings.year} | |
219 | }; | |
220 | year_month_day report_begin{report_year, January, 1d}; | |
221 | year_month_day report_end{report_year, December, 31d}; | |
5 | 222 | |
223 | // read the commit logs | |
224 | fm::heatmap heatmap; | |
225 | for (auto &&repo : repos.list()) { | |
226 | if (settings.separate) { | |
227 | heatmap.set_repo(repo.path); | |
228 | } | |
229 | proc.chdir(repo.path); | |
230 | if (repo.type == fm::HG) { | |
231 | proc.setbin(settings.hg); | |
232 | if (proc.exec_log({"log", | |
13 | 233 | "--date", std::format("{0}-01-01 to {0}-12-31", report_year), |
5 | 234 | "--template", "{author}#{date|shortdate}\n"})) { |
235 | fprintf(stderr, "Reading commit log for repo '%s' failed!\n", repo.path.c_str()); | |
236 | return EXIT_FAILURE; | |
237 | } | |
16 | 238 | heatmap.add(settings, proc.output()); |
5 | 239 | } else { |
240 | proc.setbin(settings.git); | |
8
6a2e20a4a8ff
fix log not being captured from git
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
241 | if (proc.exec_log({"log", |
13 | 242 | "--since", std::format("{0}-01-01", report_year), |
243 | "--until", std::format("{0}-12-31", report_year), | |
5 | 244 | "--format=tformat:%an <%ae>#%cs"})) { |
245 | fprintf(stderr, "Reading commit log for repo '%s' failed!\n", repo.path.c_str()); | |
246 | return EXIT_FAILURE; | |
247 | } | |
16 | 248 | heatmap.add(settings, proc.output()); |
5 | 249 | } |
250 | } | |
251 | ||
252 | html::open(); | |
253 | for (const auto &[repo, authors] : heatmap.data()) { | |
254 | html::h1(repo); | |
255 | for (const auto &[author, entries] : authors) { | |
14 | 256 | if (settings.exclude_author(author)) continue; |
257 | ||
5 | 258 | html::h2(author); |
13 | 259 | html::table_begin(report_year); |
5 | 260 | |
261 | // initialize counters | |
262 | unsigned column = 0, row = 0; | |
3 | 263 | |
5 | 264 | // initialize first day (which must be a Monday, possibly the year before) |
13 | 265 | sys_days day_to_check = January / Monday[1] / report_year; |
19
2c128952f198
fix accidentally breaking the start of year
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
266 | if (year_month_day{day_to_check}.day() != 1d) { |
2c128952f198
fix accidentally breaking the start of year
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
267 | day_to_check -= days{7}; |
2c128952f198
fix accidentally breaking the start of year
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
268 | } |
5 | 269 | |
270 | // remember the starting point | |
271 | auto start = day_to_check; | |
272 | ||
273 | // now add all entries for Monday, Tuesdays, etc. always starting back in january | |
274 | while (true) { | |
275 | html::row_begin(row); | |
276 | ||
277 | // check if we need to add blank cells | |
278 | while (day_to_check < report_begin) { | |
279 | html::cell_out_of_range(); | |
13 | 280 | day_to_check += days{7}; |
5 | 281 | column++; |
282 | } | |
283 | ||
284 | while (day_to_check <= report_end) { | |
285 | // get the entry from the heatmap | |
286 | auto find_result = entries.find(day_to_check); | |
287 | if (find_result == entries.end()) { | |
7 | 288 | html::cell(day_to_check, 0); |
5 | 289 | } else { |
7 | 290 | html::cell(day_to_check, find_result->second); |
5 | 291 | } |
292 | // advance seven days and one column | |
13 | 293 | day_to_check += days{7}; |
5 | 294 | column++; |
295 | } | |
296 | // fill remaining columns with blank cells | |
297 | for (unsigned i = column ; i < html::columns ; i++) { | |
298 | html::cell_out_of_range(); | |
299 | } | |
300 | ||
301 | // terminate the row | |
302 | html::row_end(); | |
303 | ||
304 | // if we have seen all seven weekdays, that's it | |
305 | if (++row == 7) break; | |
306 | ||
307 | // otherwise, advance the starting point by one day, reset, and begin a new row | |
13 | 308 | start += days{1}; |
5 | 309 | day_to_check = start; |
310 | column =0; | |
311 | } | |
312 | ||
313 | html::table_end(); | |
314 | } | |
315 | } | |
316 | html::close(); | |
3 | 317 | |
1 | 318 | return EXIT_SUCCESS; |
319 | } |