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