BreakingChanges: document removal of grafting
[git/gitster.git] / diagnose.c
blob4d096c857f1e669a44bc2e43375889ce2952083c
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-ll.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;
74 static int count_files(struct strbuf *path)
76 DIR *dir = opendir(path->buf);
77 struct dirent *e;
78 int count = 0;
80 if (!dir)
81 return 0;
83 while ((e = readdir_skip_dot_and_dotdot(dir)) != NULL)
84 if (get_dtype(e, path, 0) == DT_REG)
85 count++;
87 closedir(dir);
88 return count;
91 static void loose_objs_stats(struct strbuf *buf, const char *path)
93 DIR *dir = opendir(path);
94 struct dirent *e;
95 int count;
96 int total = 0;
97 unsigned char c;
98 struct strbuf count_path = STRBUF_INIT;
99 size_t base_path_len;
101 if (!dir)
102 return;
104 strbuf_addstr(buf, "Object directory stats for ");
105 strbuf_add_absolute_path(buf, path);
106 strbuf_addstr(buf, ":\n");
108 strbuf_add_absolute_path(&count_path, path);
109 strbuf_addch(&count_path, '/');
110 base_path_len = count_path.len;
112 while ((e = readdir_skip_dot_and_dotdot(dir)) != NULL)
113 if (get_dtype(e, &count_path, 0) == DT_DIR &&
114 strlen(e->d_name) == 2 &&
115 !hex_to_bytes(&c, e->d_name, 1)) {
116 strbuf_setlen(&count_path, base_path_len);
117 strbuf_addf(&count_path, "%s/", e->d_name);
118 total += (count = count_files(&count_path));
119 strbuf_addf(buf, "%s : %7d files\n", e->d_name, count);
122 strbuf_addf(buf, "Total: %d loose objects", total);
124 strbuf_release(&count_path);
125 closedir(dir);
128 static int add_directory_to_archiver(struct strvec *archiver_args,
129 const char *path, int recurse)
131 int at_root = !*path;
132 DIR *dir;
133 struct dirent *e;
134 struct strbuf buf = STRBUF_INIT;
135 size_t len;
136 int res = 0;
138 dir = opendir(at_root ? "." : path);
139 if (!dir) {
140 if (errno == ENOENT) {
141 warning(_("could not archive missing directory '%s'"), path);
142 return 0;
144 return error_errno(_("could not open directory '%s'"), path);
147 if (!at_root)
148 strbuf_addf(&buf, "%s/", path);
149 len = buf.len;
150 strvec_pushf(archiver_args, "--prefix=%s", buf.buf);
152 while (!res && (e = readdir_skip_dot_and_dotdot(dir))) {
153 struct strbuf abspath = STRBUF_INIT;
154 unsigned char dtype;
156 strbuf_add_absolute_path(&abspath, at_root ? "." : path);
157 strbuf_addch(&abspath, '/');
158 dtype = get_dtype(e, &abspath, 0);
160 strbuf_setlen(&buf, len);
161 strbuf_addstr(&buf, e->d_name);
163 if (dtype == DT_REG)
164 strvec_pushf(archiver_args, "--add-file=%s", buf.buf);
165 else if (dtype != DT_DIR)
166 warning(_("skipping '%s', which is neither file nor "
167 "directory"), buf.buf);
168 else if (recurse &&
169 add_directory_to_archiver(archiver_args,
170 buf.buf, recurse) < 0)
171 res = -1;
173 strbuf_release(&abspath);
176 closedir(dir);
177 strbuf_release(&buf);
178 return res;
181 int create_diagnostics_archive(struct strbuf *zip_path, enum diagnose_mode mode)
183 struct strvec archiver_args = STRVEC_INIT;
184 char **argv_copy = NULL;
185 int stdout_fd = -1, archiver_fd = -1;
186 struct strbuf buf = STRBUF_INIT;
187 int res, i;
188 struct archive_dir archive_dirs[] = {
189 { ".git", 0 },
190 { ".git/hooks", 0 },
191 { ".git/info", 0 },
192 { ".git/logs", 1 },
193 { ".git/objects/info", 0 }
196 if (mode == DIAGNOSE_NONE) {
197 res = 0;
198 goto diagnose_cleanup;
201 stdout_fd = dup(STDOUT_FILENO);
202 if (stdout_fd < 0) {
203 res = error_errno(_("could not duplicate stdout"));
204 goto diagnose_cleanup;
207 archiver_fd = xopen(zip_path->buf, O_CREAT | O_WRONLY | O_TRUNC, 0666);
208 if (dup2(archiver_fd, STDOUT_FILENO) < 0) {
209 res = error_errno(_("could not redirect output"));
210 goto diagnose_cleanup;
213 init_zip_archiver();
214 strvec_pushl(&archiver_args, "git-diagnose", "--format=zip", NULL);
216 strbuf_reset(&buf);
217 strbuf_addstr(&buf, "Collecting diagnostic info\n\n");
218 get_version_info(&buf, 1);
220 strbuf_addf(&buf, "Repository root: %s\n", the_repository->worktree);
221 get_disk_info(&buf);
222 write_or_die(stdout_fd, buf.buf, buf.len);
223 strvec_pushf(&archiver_args,
224 "--add-virtual-file=diagnostics.log:%.*s",
225 (int)buf.len, buf.buf);
227 strbuf_reset(&buf);
228 strbuf_addstr(&buf, "--add-virtual-file=packs-local.txt:");
229 dir_file_stats(the_repository->objects->odb, &buf);
230 foreach_alt_odb(dir_file_stats, &buf);
231 strvec_push(&archiver_args, buf.buf);
233 strbuf_reset(&buf);
234 strbuf_addstr(&buf, "--add-virtual-file=objects-local.txt:");
235 loose_objs_stats(&buf, ".git/objects");
236 strvec_push(&archiver_args, buf.buf);
238 /* Only include this if explicitly requested */
239 if (mode == DIAGNOSE_ALL) {
240 for (i = 0; i < ARRAY_SIZE(archive_dirs); i++) {
241 if (add_directory_to_archiver(&archiver_args,
242 archive_dirs[i].path,
243 archive_dirs[i].recursive)) {
244 res = error_errno(_("could not add directory '%s' to archiver"),
245 archive_dirs[i].path);
246 goto diagnose_cleanup;
251 strvec_pushl(&archiver_args, "--prefix=",
252 oid_to_hex(the_hash_algo->empty_tree), "--", NULL);
254 /* `write_archive()` modifies the `argv` passed to it. Let it. */
255 argv_copy = xmemdupz(archiver_args.v,
256 sizeof(char *) * archiver_args.nr);
257 res = write_archive(archiver_args.nr, (const char **)argv_copy, NULL,
258 the_repository, NULL, 0);
259 if (res) {
260 error(_("failed to write archive"));
261 goto diagnose_cleanup;
264 fprintf(stderr, "\n"
265 "Diagnostics complete.\n"
266 "All of the gathered info is captured in '%s'\n",
267 zip_path->buf);
269 diagnose_cleanup:
270 if (archiver_fd >= 0) {
271 dup2(stdout_fd, STDOUT_FILENO);
272 close(stdout_fd);
273 close(archiver_fd);
275 free(argv_copy);
276 strvec_clear(&archiver_args);
277 strbuf_release(&buf);
279 return res;