Tue, 21 Jan 2025 21:01:54 +0100
add automatic pull/udate of repositories
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" |
1 | 28 | |
29 | #include <cstdlib> | |
30 | #include <cstdio> | |
31 | #include <cstring> | |
32 | #include <cerrno> | |
33 | ||
34 | static void print_help() { | |
35 | fputs( | |
36 | "Usage: fallusmeter [OPTION]... [PATH]...\n\n" | |
37 | "Options:\n" | |
38 | " -h, --help Print this help message\n" | |
39 | " -d, --depth <num> The search depth (default: 1, max: 255)\n" | |
40 | " -n, --no-pull Do not pull the repositories\n" | |
41 | " -s, --separate Output a separate heat map for each repository\n" | |
42 | " -y, --year <year> The year for which to create the heat map\n" | |
43 | " --hg <path> Path to hg binary (default: /usr/bin/hg)\n" | |
44 | " --git <path> Path to git binary (default: /usr/bin/git)\n\n" | |
45 | "Scans all specified paths recursively for Mercurial and Git repositories and\n" | |
46 | "creates a commit heat map for the specified \033[1myear\033[22m or the current year.\n" | |
47 | "By default, the recursion \033[1mdepth\033[22m is one, meaning that this tool assumes that\n" | |
48 | "each \033[1mpath\033[22m is either a repository or contains repositories as subdirectories.\n" | |
49 | "You can change the \033[1mdepth\033[22m to support other directory structures.\n\n" | |
50 | "When you do not specify \033[1m--no-pull\033[22m, this tool will execute the pull command\n" | |
51 | "(and for hg the update command) before retrieving the commit log, assuming\n" | |
52 | "to be on the default branch with \033[4mno uncommitted changes\033[24m.\n\n" | |
3 | 53 | "Afterwards, this tool prints an HTML page to stdout. A separate heap map is\n" |
54 | "generated for each author showing commits across all repositories, unless the\n" | |
55 | "\033[1m--separate\033[22m option is specified in which case each repository is displayed with\n" | |
56 | "its own heat map.\n" | |
1 | 57 | , stderr); |
58 | } | |
59 | ||
3 | 60 | static bool chk_arg(const char *arg, const char *opt1, const char *opt2) { |
1 | 61 | return strcmp(arg, opt1) == 0 || (opt2 != nullptr && strcmp(arg, opt2) == 0); |
62 | } | |
63 | ||
64 | template<typename T> | |
65 | static bool parse_unsigned(const char *str, T *result, unsigned long max) { | |
66 | char *endptr; | |
67 | errno = 0; | |
68 | unsigned long d = strtoul(str, &endptr, 10); | |
69 | if (*endptr != '\0' || errno == ERANGE) return true; | |
70 | if (d < max) { | |
71 | *result = d; | |
72 | return false; | |
73 | } else { | |
74 | return true; | |
75 | } | |
76 | } | |
77 | ||
78 | static int parse_args(fm::settings &settings, int argc, char *argv[]) { | |
79 | for (int i = 1; i < argc; i++) { | |
80 | if (chk_arg(argv[i], "-h", "--help")) { | |
81 | print_help(); | |
82 | return -1; | |
83 | } else if (chk_arg(argv[i], "-d", "--depth")) { | |
84 | if (i + 1 >= argc || parse_unsigned(argv[++i], &settings.depth, 256)) { | |
85 | fputs("missing or invalid depth\n", stderr); | |
86 | return -1; | |
87 | } | |
88 | } else if (chk_arg(argv[i], "-y", "--year")) { | |
89 | if (i + 1 >= argc || parse_unsigned(argv[++i], &settings.year, 9999)) { | |
90 | fputs("missing or invalid year\n", stderr); | |
91 | return -1; | |
92 | } | |
93 | } else if (chk_arg(argv[i], "-n", "--no-pull")) { | |
3 | 94 | settings.update_repos = false; |
1 | 95 | } else if (chk_arg(argv[i], "-s", "--separate")) { |
96 | settings.separate = true; | |
97 | } else if (chk_arg(argv[i], "--hg", nullptr)) { | |
98 | if (i + 1 < argc) { | |
99 | settings.hg.assign(argv[++i]); | |
100 | } else { | |
101 | fputs("--hg is expecting a path\n", stderr); | |
102 | return -1; | |
103 | } | |
104 | } else if (chk_arg(argv[i], "--git", nullptr)) { | |
105 | if (i + 1 < argc) { | |
106 | settings.git.assign(argv[++i]); | |
107 | } else { | |
108 | fputs("--git is expecting a path\n", stderr); | |
109 | return -1; | |
110 | } | |
111 | } else if (argv[i][0] == '-') { | |
112 | fprintf(stderr, "Unknown option: %s\n", argv[i]); | |
113 | return -1; | |
114 | } else { | |
115 | settings.paths.emplace_back(argv[i]); | |
116 | } | |
117 | } | |
118 | ||
119 | if (settings.paths.empty()) { | |
120 | settings.paths.emplace_back("./"); | |
121 | } | |
122 | ||
0 | 123 | return 0; |
124 | } | |
125 | ||
3 | 126 | static void print_html_header() { |
127 | puts("<html>\n\t<body>"); | |
128 | } | |
129 | ||
130 | static void print_html_footer() { | |
131 | puts("\t</body>\n</html>"); | |
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); |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
166 | if (proc.exec({"pull"})) { |
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); |
82680ce258d6
add automatic pull/udate of repositories
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
173 | if (proc.exec({"pull"})) { |
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 | } |
179 | // TODO: calculate the heat maps | |
180 | ||
181 | print_html_header(); | |
182 | // TODO: output the heat maps here | |
183 | print_html_footer(); | |
184 | ||
1 | 185 | return EXIT_SUCCESS; |
186 | } |