show-branch: reject --[no-](topo|date)-order
[alt-git.git] / diagnose.c
blobc8c7ebcfa392f674286a39a5d37a1bba292d10a3
1 #include "git-compat-util.h"
2 #include "diagnose.h"
3 #include "compat/disk.h"
4 #include "archive.h"
5 #include "dir.h"
6 #include "help.h"
7 #include "gettext.h"
8 #include "hex.h"
9 #include "strvec.h"
10 #include "object-store.h"
11 #include "packfile.h"
12 #include "parse-options.h"
13 #include "write-or-die.h"
15 struct archive_dir {
16 const char *path;
17 int recursive;
20 struct diagnose_option {
21 enum diagnose_mode mode;
22 const char *option_name;
25 static struct diagnose_option diagnose_options[] = {
26 { DIAGNOSE_STATS, "stats" },
27 { DIAGNOSE_ALL, "all" },
30 int option_parse_diagnose(const struct option *opt, const char *arg, int unset)
32 int i;
33 enum diagnose_mode *diagnose = opt->value;
35 if (!arg) {
36 *diagnose = unset ? DIAGNOSE_NONE : DIAGNOSE_STATS;
37 return 0;
40 for (i = 0; i < ARRAY_SIZE(diagnose_options); i++) {
41 if (!strcmp(arg, diagnose_options[i].option_name)) {
42 *diagnose = diagnose_options[i].mode;
43 return 0;
47 return error(_("invalid --%s value '%s'"), opt->long_name, arg);
50 static void dir_file_stats_objects(const char *full_path,
51 size_t full_path_len UNUSED,
52 const char *file_name, void *data)
54 struct strbuf *buf = data;
55 struct stat st;
57 if (!stat(full_path, &st))
58 strbuf_addf(buf, "%-70s %16" PRIuMAX "\n", file_name,
59 (uintmax_t)st.st_size);
62 static int dir_file_stats(struct object_directory *object_dir, void *data)
64 struct strbuf *buf = data;
66 strbuf_addf(buf, "Contents of %s:\n", object_dir->path);
68 for_each_file_in_pack_dir(object_dir->path, dir_file_stats_objects,
69 data);
71 return 0;
75 * Get the d_type of a dirent. If the d_type is unknown, derive it from
76 * stat.st_mode.
78 * Note that 'path' is assumed to have a trailing slash. It is also modified
79 * in-place during the execution of the function, but is then reverted to its
80 * original value before returning.
82 static unsigned char get_dtype(struct dirent *e, struct strbuf *path)
84 struct stat st;
85 unsigned char dtype = DTYPE(e);
86 size_t base_path_len;
88 if (dtype != DT_UNKNOWN)
89 return dtype;
91 /* d_type unknown in dirent, try to fall back on lstat results */
92 base_path_len = path->len;
93 strbuf_addstr(path, e->d_name);
94 if (lstat(path->buf, &st))
95 goto cleanup;
97 /* determine d_type from st_mode */
98 if (S_ISREG(st.st_mode))
99 dtype = DT_REG;
100 else if (S_ISDIR(st.st_mode))
101 dtype = DT_DIR;
102 else if (S_ISLNK(st.st_mode))
103 dtype = DT_LNK;
105 cleanup:
106 strbuf_setlen(path, base_path_len);
107 return dtype;
110 static int count_files(struct strbuf *path)
112 DIR *dir = opendir(path->buf);
113 struct dirent *e;
114 int count = 0;
116 if (!dir)
117 return 0;
119 while ((e = readdir_skip_dot_and_dotdot(dir)) != NULL)
120 if (get_dtype(e, path) == DT_REG)
121 count++;
123 closedir(dir);
124 return count;
127 static void loose_objs_stats(struct strbuf *buf, const char *path)
129 DIR *dir = opendir(path);
130 struct dirent *e;
131 int count;
132 int total = 0;
133 unsigned char c;
134 struct strbuf count_path = STRBUF_INIT;
135 size_t base_path_len;
137 if (!dir)
138 return;
140 strbuf_addstr(buf, "Object directory stats for ");
141 strbuf_add_absolute_path(buf, path);
142 strbuf_addstr(buf, ":\n");
144 strbuf_add_absolute_path(&count_path, path);
145 strbuf_addch(&count_path, '/');
146 base_path_len = count_path.len;
148 while ((e = readdir_skip_dot_and_dotdot(dir)) != NULL)
149 if (get_dtype(e, &count_path) == DT_DIR &&
150 strlen(e->d_name) == 2 &&
151 !hex_to_bytes(&c, e->d_name, 1)) {
152 strbuf_setlen(&count_path, base_path_len);
153 strbuf_addf(&count_path, "%s/", e->d_name);
154 total += (count = count_files(&count_path));
155 strbuf_addf(buf, "%s : %7d files\n", e->d_name, count);
158 strbuf_addf(buf, "Total: %d loose objects", total);
160 strbuf_release(&count_path);
161 closedir(dir);
164 static int add_directory_to_archiver(struct strvec *archiver_args,
165 const char *path, int recurse)
167 int at_root = !*path;
168 DIR *dir;
169 struct dirent *e;
170 struct strbuf buf = STRBUF_INIT;
171 size_t len;
172 int res = 0;
174 dir = opendir(at_root ? "." : path);
175 if (!dir) {
176 if (errno == ENOENT) {
177 warning(_("could not archive missing directory '%s'"), path);
178 return 0;
180 return error_errno(_("could not open directory '%s'"), path);
183 if (!at_root)
184 strbuf_addf(&buf, "%s/", path);
185 len = buf.len;
186 strvec_pushf(archiver_args, "--prefix=%s", buf.buf);
188 while (!res && (e = readdir_skip_dot_and_dotdot(dir))) {
189 struct strbuf abspath = STRBUF_INIT;
190 unsigned char dtype;
192 strbuf_add_absolute_path(&abspath, at_root ? "." : path);
193 strbuf_addch(&abspath, '/');
194 dtype = get_dtype(e, &abspath);
196 strbuf_setlen(&buf, len);
197 strbuf_addstr(&buf, e->d_name);
199 if (dtype == DT_REG)
200 strvec_pushf(archiver_args, "--add-file=%s", buf.buf);
201 else if (dtype != DT_DIR)
202 warning(_("skipping '%s', which is neither file nor "
203 "directory"), buf.buf);
204 else if (recurse &&
205 add_directory_to_archiver(archiver_args,
206 buf.buf, recurse) < 0)
207 res = -1;
209 strbuf_release(&abspath);
212 closedir(dir);
213 strbuf_release(&buf);
214 return res;
217 int create_diagnostics_archive(struct strbuf *zip_path, enum diagnose_mode mode)
219 struct strvec archiver_args = STRVEC_INIT;
220 char **argv_copy = NULL;
221 int stdout_fd = -1, archiver_fd = -1;
222 struct strbuf buf = STRBUF_INIT;
223 int res, i;
224 struct archive_dir archive_dirs[] = {
225 { ".git", 0 },
226 { ".git/hooks", 0 },
227 { ".git/info", 0 },
228 { ".git/logs", 1 },
229 { ".git/objects/info", 0 }
232 if (mode == DIAGNOSE_NONE) {
233 res = 0;
234 goto diagnose_cleanup;
237 stdout_fd = dup(STDOUT_FILENO);
238 if (stdout_fd < 0) {
239 res = error_errno(_("could not duplicate stdout"));
240 goto diagnose_cleanup;
243 archiver_fd = xopen(zip_path->buf, O_CREAT | O_WRONLY | O_TRUNC, 0666);
244 if (dup2(archiver_fd, STDOUT_FILENO) < 0) {
245 res = error_errno(_("could not redirect output"));
246 goto diagnose_cleanup;
249 init_zip_archiver();
250 strvec_pushl(&archiver_args, "git-diagnose", "--format=zip", NULL);
252 strbuf_reset(&buf);
253 strbuf_addstr(&buf, "Collecting diagnostic info\n\n");
254 get_version_info(&buf, 1);
256 strbuf_addf(&buf, "Repository root: %s\n", the_repository->worktree);
257 get_disk_info(&buf);
258 write_or_die(stdout_fd, buf.buf, buf.len);
259 strvec_pushf(&archiver_args,
260 "--add-virtual-file=diagnostics.log:%.*s",
261 (int)buf.len, buf.buf);
263 strbuf_reset(&buf);
264 strbuf_addstr(&buf, "--add-virtual-file=packs-local.txt:");
265 dir_file_stats(the_repository->objects->odb, &buf);
266 foreach_alt_odb(dir_file_stats, &buf);
267 strvec_push(&archiver_args, buf.buf);
269 strbuf_reset(&buf);
270 strbuf_addstr(&buf, "--add-virtual-file=objects-local.txt:");
271 loose_objs_stats(&buf, ".git/objects");
272 strvec_push(&archiver_args, buf.buf);
274 /* Only include this if explicitly requested */
275 if (mode == DIAGNOSE_ALL) {
276 for (i = 0; i < ARRAY_SIZE(archive_dirs); i++) {
277 if (add_directory_to_archiver(&archiver_args,
278 archive_dirs[i].path,
279 archive_dirs[i].recursive)) {
280 res = error_errno(_("could not add directory '%s' to archiver"),
281 archive_dirs[i].path);
282 goto diagnose_cleanup;
287 strvec_pushl(&archiver_args, "--prefix=",
288 oid_to_hex(the_hash_algo->empty_tree), "--", NULL);
290 /* `write_archive()` modifies the `argv` passed to it. Let it. */
291 argv_copy = xmemdupz(archiver_args.v,
292 sizeof(char *) * archiver_args.nr);
293 res = write_archive(archiver_args.nr, (const char **)argv_copy, NULL,
294 the_repository, NULL, 0);
295 if (res) {
296 error(_("failed to write archive"));
297 goto diagnose_cleanup;
300 fprintf(stderr, "\n"
301 "Diagnostics complete.\n"
302 "All of the gathered info is captured in '%s'\n",
303 zip_path->buf);
305 diagnose_cleanup:
306 if (archiver_fd >= 0) {
307 dup2(stdout_fd, STDOUT_FILENO);
308 close(stdout_fd);
309 close(archiver_fd);
311 free(argv_copy);
312 strvec_clear(&archiver_args);
313 strbuf_release(&buf);
315 return res;