Merge branch 'rj/cygwin-has-dev-tty'
[git/gitster.git] / diagnose.c
blobcc2d535b60d7c886640f458f062cf5720f9cf36b
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "diagnose.h"
5 #include "compat/disk.h"
6 #include "archive.h"
7 #include "dir.h"
8 #include "help.h"
9 #include "gettext.h"
10 #include "hex.h"
11 #include "strvec.h"
12 #include "object-store-ll.h"
13 #include "packfile.h"
14 #include "parse-options.h"
15 #include "write-or-die.h"
17 struct archive_dir {
18 const char *path;
19 int recursive;
22 struct diagnose_option {
23 enum diagnose_mode mode;
24 const char *option_name;
27 static struct diagnose_option diagnose_options[] = {
28 { DIAGNOSE_STATS, "stats" },
29 { DIAGNOSE_ALL, "all" },
32 int option_parse_diagnose(const struct option *opt, const char *arg, int unset)
34 int i;
35 enum diagnose_mode *diagnose = opt->value;
37 if (!arg) {
38 *diagnose = unset ? DIAGNOSE_NONE : DIAGNOSE_STATS;
39 return 0;
42 for (i = 0; i < ARRAY_SIZE(diagnose_options); i++) {
43 if (!strcmp(arg, diagnose_options[i].option_name)) {
44 *diagnose = diagnose_options[i].mode;
45 return 0;
49 return error(_("invalid --%s value '%s'"), opt->long_name, arg);
52 static void dir_file_stats_objects(const char *full_path,
53 size_t full_path_len UNUSED,
54 const char *file_name, void *data)
56 struct strbuf *buf = data;
57 struct stat st;
59 if (!stat(full_path, &st))
60 strbuf_addf(buf, "%-70s %16" PRIuMAX "\n", file_name,
61 (uintmax_t)st.st_size);
64 static int dir_file_stats(struct object_directory *object_dir, void *data)
66 struct strbuf *buf = data;
68 strbuf_addf(buf, "Contents of %s:\n", object_dir->path);
70 for_each_file_in_pack_dir(object_dir->path, dir_file_stats_objects,
71 data);
73 return 0;
76 static int count_files(struct strbuf *path)
78 DIR *dir = opendir(path->buf);
79 struct dirent *e;
80 int count = 0;
82 if (!dir)
83 return 0;
85 while ((e = readdir_skip_dot_and_dotdot(dir)) != NULL)
86 if (get_dtype(e, path, 0) == DT_REG)
87 count++;
89 closedir(dir);
90 return count;
93 static void loose_objs_stats(struct strbuf *buf, const char *path)
95 DIR *dir = opendir(path);
96 struct dirent *e;
97 int count;
98 int total = 0;
99 unsigned char c;
100 struct strbuf count_path = STRBUF_INIT;
101 size_t base_path_len;
103 if (!dir)
104 return;
106 strbuf_addstr(buf, "Object directory stats for ");
107 strbuf_add_absolute_path(buf, path);
108 strbuf_addstr(buf, ":\n");
110 strbuf_add_absolute_path(&count_path, path);
111 strbuf_addch(&count_path, '/');
112 base_path_len = count_path.len;
114 while ((e = readdir_skip_dot_and_dotdot(dir)) != NULL)
115 if (get_dtype(e, &count_path, 0) == DT_DIR &&
116 strlen(e->d_name) == 2 &&
117 !hex_to_bytes(&c, e->d_name, 1)) {
118 strbuf_setlen(&count_path, base_path_len);
119 strbuf_addf(&count_path, "%s/", e->d_name);
120 total += (count = count_files(&count_path));
121 strbuf_addf(buf, "%s : %7d files\n", e->d_name, count);
124 strbuf_addf(buf, "Total: %d loose objects", total);
126 strbuf_release(&count_path);
127 closedir(dir);
130 static int add_directory_to_archiver(struct strvec *archiver_args,
131 const char *path, int recurse)
133 int at_root = !*path;
134 DIR *dir;
135 struct dirent *e;
136 struct strbuf buf = STRBUF_INIT;
137 size_t len;
138 int res = 0;
140 dir = opendir(at_root ? "." : path);
141 if (!dir) {
142 if (errno == ENOENT) {
143 warning(_("could not archive missing directory '%s'"), path);
144 return 0;
146 return error_errno(_("could not open directory '%s'"), path);
149 if (!at_root)
150 strbuf_addf(&buf, "%s/", path);
151 len = buf.len;
152 strvec_pushf(archiver_args, "--prefix=%s", buf.buf);
154 while (!res && (e = readdir_skip_dot_and_dotdot(dir))) {
155 struct strbuf abspath = STRBUF_INIT;
156 unsigned char dtype;
158 strbuf_add_absolute_path(&abspath, at_root ? "." : path);
159 strbuf_addch(&abspath, '/');
160 dtype = get_dtype(e, &abspath, 0);
162 strbuf_setlen(&buf, len);
163 strbuf_addstr(&buf, e->d_name);
165 if (dtype == DT_REG)
166 strvec_pushf(archiver_args, "--add-file=%s", buf.buf);
167 else if (dtype != DT_DIR)
168 warning(_("skipping '%s', which is neither file nor "
169 "directory"), buf.buf);
170 else if (recurse &&
171 add_directory_to_archiver(archiver_args,
172 buf.buf, recurse) < 0)
173 res = -1;
175 strbuf_release(&abspath);
178 closedir(dir);
179 strbuf_release(&buf);
180 return res;
183 int create_diagnostics_archive(struct strbuf *zip_path, enum diagnose_mode mode)
185 struct strvec archiver_args = STRVEC_INIT;
186 char **argv_copy = NULL;
187 int stdout_fd = -1, archiver_fd = -1;
188 struct strbuf buf = STRBUF_INIT;
189 int res, i;
190 struct archive_dir archive_dirs[] = {
191 { ".git", 0 },
192 { ".git/hooks", 0 },
193 { ".git/info", 0 },
194 { ".git/logs", 1 },
195 { ".git/objects/info", 0 }
198 if (mode == DIAGNOSE_NONE) {
199 res = 0;
200 goto diagnose_cleanup;
203 stdout_fd = dup(STDOUT_FILENO);
204 if (stdout_fd < 0) {
205 res = error_errno(_("could not duplicate stdout"));
206 goto diagnose_cleanup;
209 archiver_fd = xopen(zip_path->buf, O_CREAT | O_WRONLY | O_TRUNC, 0666);
210 if (dup2(archiver_fd, STDOUT_FILENO) < 0) {
211 res = error_errno(_("could not redirect output"));
212 goto diagnose_cleanup;
215 init_zip_archiver();
216 strvec_pushl(&archiver_args, "git-diagnose", "--format=zip", NULL);
218 strbuf_reset(&buf);
219 strbuf_addstr(&buf, "Collecting diagnostic info\n\n");
220 get_version_info(&buf, 1);
222 strbuf_addf(&buf, "Repository root: %s\n", the_repository->worktree);
223 get_disk_info(&buf);
224 write_or_die(stdout_fd, buf.buf, buf.len);
225 strvec_pushf(&archiver_args,
226 "--add-virtual-file=diagnostics.log:%.*s",
227 (int)buf.len, buf.buf);
229 strbuf_reset(&buf);
230 strbuf_addstr(&buf, "--add-virtual-file=packs-local.txt:");
231 dir_file_stats(the_repository->objects->odb, &buf);
232 foreach_alt_odb(dir_file_stats, &buf);
233 strvec_push(&archiver_args, buf.buf);
235 strbuf_reset(&buf);
236 strbuf_addstr(&buf, "--add-virtual-file=objects-local.txt:");
237 loose_objs_stats(&buf, ".git/objects");
238 strvec_push(&archiver_args, buf.buf);
240 /* Only include this if explicitly requested */
241 if (mode == DIAGNOSE_ALL) {
242 for (i = 0; i < ARRAY_SIZE(archive_dirs); i++) {
243 if (add_directory_to_archiver(&archiver_args,
244 archive_dirs[i].path,
245 archive_dirs[i].recursive)) {
246 res = error_errno(_("could not add directory '%s' to archiver"),
247 archive_dirs[i].path);
248 goto diagnose_cleanup;
253 strvec_pushl(&archiver_args, "--prefix=",
254 oid_to_hex(the_hash_algo->empty_tree), "--", NULL);
256 /* `write_archive()` modifies the `argv` passed to it. Let it. */
257 argv_copy = xmemdupz(archiver_args.v,
258 sizeof(char *) * archiver_args.nr);
259 res = write_archive(archiver_args.nr, (const char **)argv_copy, NULL,
260 the_repository, NULL, 0);
261 if (res) {
262 error(_("failed to write archive"));
263 goto diagnose_cleanup;
266 fprintf(stderr, "\n"
267 "Diagnostics complete.\n"
268 "All of the gathered info is captured in '%s'\n",
269 zip_path->buf);
271 diagnose_cleanup:
272 if (archiver_fd >= 0) {
273 dup2(stdout_fd, STDOUT_FILENO);
274 close(stdout_fd);
275 close(archiver_fd);
277 free(argv_copy);
278 strvec_clear(&archiver_args);
279 strbuf_release(&buf);
281 return res;