scalar: implement the `help` subcommand
[git.git] / diagnose.c
blobbeb0a8741ba3ce65a389a55810e0b8d5b03010fa
1 #include "cache.h"
2 #include "diagnose.h"
3 #include "compat/disk.h"
4 #include "archive.h"
5 #include "dir.h"
6 #include "help.h"
7 #include "strvec.h"
8 #include "object-store.h"
9 #include "packfile.h"
11 struct archive_dir {
12 const char *path;
13 int recursive;
16 struct diagnose_option {
17 enum diagnose_mode mode;
18 const char *option_name;
21 static struct diagnose_option diagnose_options[] = {
22 { DIAGNOSE_STATS, "stats" },
23 { DIAGNOSE_ALL, "all" },
26 int option_parse_diagnose(const struct option *opt, const char *arg, int unset)
28 int i;
29 enum diagnose_mode *diagnose = opt->value;
31 if (!arg) {
32 *diagnose = unset ? DIAGNOSE_NONE : DIAGNOSE_STATS;
33 return 0;
36 for (i = 0; i < ARRAY_SIZE(diagnose_options); i++) {
37 if (!strcmp(arg, diagnose_options[i].option_name)) {
38 *diagnose = diagnose_options[i].mode;
39 return 0;
43 return error(_("invalid --%s value '%s'"), opt->long_name, arg);
46 static void dir_file_stats_objects(const char *full_path, size_t full_path_len,
47 const char *file_name, void *data)
49 struct strbuf *buf = data;
50 struct stat st;
52 if (!stat(full_path, &st))
53 strbuf_addf(buf, "%-70s %16" PRIuMAX "\n", file_name,
54 (uintmax_t)st.st_size);
57 static int dir_file_stats(struct object_directory *object_dir, void *data)
59 struct strbuf *buf = data;
61 strbuf_addf(buf, "Contents of %s:\n", object_dir->path);
63 for_each_file_in_pack_dir(object_dir->path, dir_file_stats_objects,
64 data);
66 return 0;
69 static int count_files(char *path)
71 DIR *dir = opendir(path);
72 struct dirent *e;
73 int count = 0;
75 if (!dir)
76 return 0;
78 while ((e = readdir(dir)) != NULL)
79 if (!is_dot_or_dotdot(e->d_name) && e->d_type == DT_REG)
80 count++;
82 closedir(dir);
83 return count;
86 static void loose_objs_stats(struct strbuf *buf, const char *path)
88 DIR *dir = opendir(path);
89 struct dirent *e;
90 int count;
91 int total = 0;
92 unsigned char c;
93 struct strbuf count_path = STRBUF_INIT;
94 size_t base_path_len;
96 if (!dir)
97 return;
99 strbuf_addstr(buf, "Object directory stats for ");
100 strbuf_add_absolute_path(buf, path);
101 strbuf_addstr(buf, ":\n");
103 strbuf_add_absolute_path(&count_path, path);
104 strbuf_addch(&count_path, '/');
105 base_path_len = count_path.len;
107 while ((e = readdir(dir)) != NULL)
108 if (!is_dot_or_dotdot(e->d_name) &&
109 e->d_type == DT_DIR && strlen(e->d_name) == 2 &&
110 !hex_to_bytes(&c, e->d_name, 1)) {
111 strbuf_setlen(&count_path, base_path_len);
112 strbuf_addstr(&count_path, e->d_name);
113 total += (count = count_files(count_path.buf));
114 strbuf_addf(buf, "%s : %7d files\n", e->d_name, count);
117 strbuf_addf(buf, "Total: %d loose objects", total);
119 strbuf_release(&count_path);
120 closedir(dir);
123 static int add_directory_to_archiver(struct strvec *archiver_args,
124 const char *path, int recurse)
126 int at_root = !*path;
127 DIR *dir;
128 struct dirent *e;
129 struct strbuf buf = STRBUF_INIT;
130 size_t len;
131 int res = 0;
133 dir = opendir(at_root ? "." : path);
134 if (!dir) {
135 if (errno == ENOENT) {
136 warning(_("could not archive missing directory '%s'"), path);
137 return 0;
139 return error_errno(_("could not open directory '%s'"), path);
142 if (!at_root)
143 strbuf_addf(&buf, "%s/", path);
144 len = buf.len;
145 strvec_pushf(archiver_args, "--prefix=%s", buf.buf);
147 while (!res && (e = readdir(dir))) {
148 if (!strcmp(".", e->d_name) || !strcmp("..", e->d_name))
149 continue;
151 strbuf_setlen(&buf, len);
152 strbuf_addstr(&buf, e->d_name);
154 if (e->d_type == DT_REG)
155 strvec_pushf(archiver_args, "--add-file=%s", buf.buf);
156 else if (e->d_type != DT_DIR)
157 warning(_("skipping '%s', which is neither file nor "
158 "directory"), buf.buf);
159 else if (recurse &&
160 add_directory_to_archiver(archiver_args,
161 buf.buf, recurse) < 0)
162 res = -1;
165 closedir(dir);
166 strbuf_release(&buf);
167 return res;
170 int create_diagnostics_archive(struct strbuf *zip_path, enum diagnose_mode mode)
172 struct strvec archiver_args = STRVEC_INIT;
173 char **argv_copy = NULL;
174 int stdout_fd = -1, archiver_fd = -1;
175 struct strbuf buf = STRBUF_INIT;
176 int res, i;
177 struct archive_dir archive_dirs[] = {
178 { ".git", 0 },
179 { ".git/hooks", 0 },
180 { ".git/info", 0 },
181 { ".git/logs", 1 },
182 { ".git/objects/info", 0 }
185 if (mode == DIAGNOSE_NONE) {
186 res = 0;
187 goto diagnose_cleanup;
190 stdout_fd = dup(STDOUT_FILENO);
191 if (stdout_fd < 0) {
192 res = error_errno(_("could not duplicate stdout"));
193 goto diagnose_cleanup;
196 archiver_fd = xopen(zip_path->buf, O_CREAT | O_WRONLY | O_TRUNC, 0666);
197 if (dup2(archiver_fd, STDOUT_FILENO) < 0) {
198 res = error_errno(_("could not redirect output"));
199 goto diagnose_cleanup;
202 init_zip_archiver();
203 strvec_pushl(&archiver_args, "git-diagnose", "--format=zip", NULL);
205 strbuf_reset(&buf);
206 strbuf_addstr(&buf, "Collecting diagnostic info\n\n");
207 get_version_info(&buf, 1);
209 strbuf_addf(&buf, "Repository root: %s\n", the_repository->worktree);
210 get_disk_info(&buf);
211 write_or_die(stdout_fd, buf.buf, buf.len);
212 strvec_pushf(&archiver_args,
213 "--add-virtual-file=diagnostics.log:%.*s",
214 (int)buf.len, buf.buf);
216 strbuf_reset(&buf);
217 strbuf_addstr(&buf, "--add-virtual-file=packs-local.txt:");
218 dir_file_stats(the_repository->objects->odb, &buf);
219 foreach_alt_odb(dir_file_stats, &buf);
220 strvec_push(&archiver_args, buf.buf);
222 strbuf_reset(&buf);
223 strbuf_addstr(&buf, "--add-virtual-file=objects-local.txt:");
224 loose_objs_stats(&buf, ".git/objects");
225 strvec_push(&archiver_args, buf.buf);
227 /* Only include this if explicitly requested */
228 if (mode == DIAGNOSE_ALL) {
229 for (i = 0; i < ARRAY_SIZE(archive_dirs); i++) {
230 if (add_directory_to_archiver(&archiver_args,
231 archive_dirs[i].path,
232 archive_dirs[i].recursive)) {
233 res = error_errno(_("could not add directory '%s' to archiver"),
234 archive_dirs[i].path);
235 goto diagnose_cleanup;
240 strvec_pushl(&archiver_args, "--prefix=",
241 oid_to_hex(the_hash_algo->empty_tree), "--", NULL);
243 /* `write_archive()` modifies the `argv` passed to it. Let it. */
244 argv_copy = xmemdupz(archiver_args.v,
245 sizeof(char *) * archiver_args.nr);
246 res = write_archive(archiver_args.nr, (const char **)argv_copy, NULL,
247 the_repository, NULL, 0);
248 if (res) {
249 error(_("failed to write archive"));
250 goto diagnose_cleanup;
253 fprintf(stderr, "\n"
254 "Diagnostics complete.\n"
255 "All of the gathered info is captured in '%s'\n",
256 zip_path->buf);
258 diagnose_cleanup:
259 if (archiver_fd >= 0) {
260 dup2(stdout_fd, STDOUT_FILENO);
261 close(stdout_fd);
262 close(archiver_fd);
264 free(argv_copy);
265 strvec_clear(&archiver_args);
266 strbuf_release(&buf);
268 return res;