Fri, 31 Jan 2025 22:27:28 +0100
improve alignment of month headers
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 | ||
5 | 37 | namespace chrono = std::chrono; |
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" | |
57 | "to be on the default branch with \033[4mno uncommitted changes\033[24m.\n\n" | |
3 | 58 | "Afterwards, this tool prints an HTML page to stdout. A separate heap map is\n" |
59 | "generated for each author showing commits across all repositories, unless the\n" | |
60 | "\033[1m--separate\033[22m option is specified in which case each repository is displayed with\n" | |
61 | "its own heat map.\n" | |
1 | 62 | , stderr); |
63 | } | |
64 | ||
3 | 65 | static bool chk_arg(const char *arg, const char *opt1, const char *opt2) { |
1 | 66 | return strcmp(arg, opt1) == 0 || (opt2 != nullptr && strcmp(arg, opt2) == 0); |
67 | } | |
68 | ||
69 | template<typename T> | |
70 | static bool parse_unsigned(const char *str, T *result, unsigned long max) { | |
71 | char *endptr; | |
72 | errno = 0; | |
73 | unsigned long d = strtoul(str, &endptr, 10); | |
74 | if (*endptr != '\0' || errno == ERANGE) return true; | |
75 | if (d < max) { | |
76 | *result = d; | |
77 | return false; | |
78 | } else { | |
79 | return true; | |
80 | } | |
81 | } | |
82 | ||
83 | static int parse_args(fm::settings &settings, int argc, char *argv[]) { | |
84 | for (int i = 1; i < argc; i++) { | |
85 | if (chk_arg(argv[i], "-h", "--help")) { | |
86 | print_help(); | |
87 | return -1; | |
88 | } else if (chk_arg(argv[i], "-d", "--depth")) { | |
89 | if (i + 1 >= argc || parse_unsigned(argv[++i], &settings.depth, 256)) { | |
90 | fputs("missing or invalid depth\n", stderr); | |
91 | return -1; | |
92 | } | |
93 | } else if (chk_arg(argv[i], "-y", "--year")) { | |
94 | if (i + 1 >= argc || parse_unsigned(argv[++i], &settings.year, 9999)) { | |
95 | fputs("missing or invalid year\n", stderr); | |
96 | return -1; | |
97 | } | |
98 | } else if (chk_arg(argv[i], "-n", "--no-pull")) { | |
3 | 99 | settings.update_repos = false; |
1 | 100 | } else if (chk_arg(argv[i], "-s", "--separate")) { |
101 | settings.separate = true; | |
102 | } else if (chk_arg(argv[i], "--hg", nullptr)) { | |
103 | if (i + 1 < argc) { | |
104 | settings.hg.assign(argv[++i]); | |
105 | } else { | |
106 | fputs("--hg is expecting a path\n", stderr); | |
107 | return -1; | |
108 | } | |
109 | } else if (chk_arg(argv[i], "--git", nullptr)) { | |
110 | if (i + 1 < argc) { | |
111 | settings.git.assign(argv[++i]); | |
112 | } else { | |
113 | fputs("--git is expecting a path\n", stderr); | |
114 | return -1; | |
115 | } | |
116 | } else if (argv[i][0] == '-') { | |
117 | fprintf(stderr, "Unknown option: %s\n", argv[i]); | |
118 | return -1; | |
119 | } else { | |
120 | settings.paths.emplace_back(argv[i]); | |
121 | } | |
122 | } | |
123 | ||
124 | if (settings.paths.empty()) { | |
125 | settings.paths.emplace_back("./"); | |
126 | } | |
127 | ||
0 | 128 | return 0; |
129 | } | |
130 | ||
1 | 131 | int main(int argc, char *argv[]) { |
4
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
132 | // parse settings |
1 | 133 | fm::settings settings; |
134 | if (parse_args(settings, argc, argv)) { | |
135 | return EXIT_FAILURE; | |
136 | } | |
137 | ||
4
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
138 | // check hg and git |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
139 | fm::process proc; |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
140 | proc.setbin(settings.hg); |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
141 | if (proc.exec({"--version"})) { |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
142 | 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
|
143 | return EXIT_FAILURE; |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
144 | } |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
145 | proc.setbin(settings.git); |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
146 | if (proc.exec({"--version"})) { |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
147 | 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
|
148 | return EXIT_FAILURE; |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
149 | } |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
150 | |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
151 | // scan for repos |
3 | 152 | fm::repositories repos; |
153 | for (auto &&path: settings.paths) { | |
154 | repos.scan(path, settings.depth); | |
155 | } | |
4
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
156 | |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
157 | // update repos, if not disabled |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
158 | if (settings.update_repos) { |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
159 | for (auto &&repo : repos.list()) { |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
160 | proc.chdir(repo.path); |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
161 | if (repo.type == fm::HG) { |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
162 | proc.setbin(settings.hg); |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
163 | if (proc.exec({"pull"})) { |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
164 | 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
|
165 | } else if (proc.exec({"update"})) { |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
166 | 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
|
167 | } |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
168 | } else { |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
169 | proc.setbin(settings.git); |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
170 | if (proc.exec({"pull"})) { |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
171 | 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
|
172 | } |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
173 | } |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
174 | } |
3 | 175 | } |
5 | 176 | |
177 | // determine our reporting range | |
178 | int year; | |
179 | if (settings.year == fm::settings_current_year) { | |
180 | year = static_cast<int>(chrono::year_month_day{chrono::floor<chrono::days>(chrono::system_clock::now())}.year()); | |
181 | } else { | |
182 | year = settings.year; | |
183 | } | |
184 | chrono::year_month_day report_begin{chrono::year{year}, chrono::month{1}, chrono::day{1}}; | |
185 | chrono::year_month_day report_end{chrono::year{year}, chrono::month{12}, chrono::day{31}}; | |
186 | ||
187 | // read the commit logs | |
188 | fm::heatmap heatmap; | |
189 | for (auto &&repo : repos.list()) { | |
190 | if (settings.separate) { | |
191 | heatmap.set_repo(repo.path); | |
192 | } | |
193 | proc.chdir(repo.path); | |
194 | if (repo.type == fm::HG) { | |
195 | proc.setbin(settings.hg); | |
196 | if (proc.exec_log({"log", | |
197 | "--date", std::format("{0}-01-01 to {0}-12-31", year), | |
198 | "--template", "{author}#{date|shortdate}\n"})) { | |
199 | fprintf(stderr, "Reading commit log for repo '%s' failed!\n", repo.path.c_str()); | |
200 | return EXIT_FAILURE; | |
201 | } | |
202 | heatmap.add(proc.output()); | |
203 | } else { | |
204 | proc.setbin(settings.git); | |
205 | if (proc.exec({"log", | |
206 | "--since", std::format("{0}-01-01", year), | |
207 | "--until", std::format("{0}-12-31", year), | |
208 | "--format=tformat:%an <%ae>#%cs"})) { | |
209 | fprintf(stderr, "Reading commit log for repo '%s' failed!\n", repo.path.c_str()); | |
210 | return EXIT_FAILURE; | |
211 | } | |
212 | heatmap.add(proc.output()); | |
213 | } | |
214 | } | |
215 | ||
216 | html::open(); | |
217 | for (const auto &[repo, authors] : heatmap.data()) { | |
218 | html::h1(repo); | |
219 | for (const auto &[author, entries] : authors) { | |
220 | html::h2(author); | |
6
1040ba37d4c9
improve alignment of month headers
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
221 | html::table_begin(year); |
5 | 222 | |
223 | // initialize counters | |
224 | unsigned column = 0, row = 0; | |
3 | 225 | |
5 | 226 | // initialize first day (which must be a Monday, possibly the year before) |
227 | chrono::sys_days day_to_check{report_begin}; | |
228 | day_to_check -= chrono::days{chrono::weekday{day_to_check}.iso_encoding() - 1}; | |
229 | ||
230 | // remember the starting point | |
231 | auto start = day_to_check; | |
232 | ||
233 | // now add all entries for Monday, Tuesdays, etc. always starting back in january | |
234 | while (true) { | |
235 | html::row_begin(row); | |
236 | ||
237 | // check if we need to add blank cells | |
238 | while (day_to_check < report_begin) { | |
239 | html::cell_out_of_range(); | |
240 | day_to_check += chrono::days{7}; | |
241 | column++; | |
242 | } | |
243 | ||
244 | while (day_to_check <= report_end) { | |
245 | // get the entry from the heatmap | |
246 | auto find_result = entries.find(day_to_check); | |
247 | if (find_result == entries.end()) { | |
248 | html::cell(0); | |
249 | } else { | |
250 | html::cell(find_result->second); | |
251 | } | |
252 | // advance seven days and one column | |
253 | day_to_check += chrono::days{7}; | |
254 | column++; | |
255 | } | |
256 | // fill remaining columns with blank cells | |
257 | for (unsigned i = column ; i < html::columns ; i++) { | |
258 | html::cell_out_of_range(); | |
259 | } | |
260 | ||
261 | // terminate the row | |
262 | html::row_end(); | |
263 | ||
264 | // if we have seen all seven weekdays, that's it | |
265 | if (++row == 7) break; | |
266 | ||
267 | // otherwise, advance the starting point by one day, reset, and begin a new row | |
268 | start += chrono::days{1}; | |
269 | day_to_check = start; | |
270 | column =0; | |
271 | } | |
272 | ||
273 | html::table_end(); | |
274 | } | |
275 | } | |
276 | html::close(); | |
3 | 277 | |
1 | 278 | return EXIT_SUCCESS; |
279 | } |