3 #include "compat/disk.h"
8 #include "object-store.h"
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
)
29 enum diagnose_mode
*diagnose
= opt
->value
;
32 *diagnose
= unset
? DIAGNOSE_NONE
: DIAGNOSE_STATS
;
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
;
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
;
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
,
69 static int count_files(char *path
)
71 DIR *dir
= opendir(path
);
78 while ((e
= readdir(dir
)) != NULL
)
79 if (!is_dot_or_dotdot(e
->d_name
) && e
->d_type
== DT_REG
)
86 static void loose_objs_stats(struct strbuf
*buf
, const char *path
)
88 DIR *dir
= opendir(path
);
93 struct strbuf count_path
= STRBUF_INIT
;
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
);
123 static int add_directory_to_archiver(struct strvec
*archiver_args
,
124 const char *path
, int recurse
)
126 int at_root
= !*path
;
129 struct strbuf buf
= STRBUF_INIT
;
133 dir
= opendir(at_root
? "." : path
);
135 if (errno
== ENOENT
) {
136 warning(_("could not archive missing directory '%s'"), path
);
139 return error_errno(_("could not open directory '%s'"), path
);
143 strbuf_addf(&buf
, "%s/", path
);
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
))
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
);
160 add_directory_to_archiver(archiver_args
,
161 buf
.buf
, recurse
) < 0)
166 strbuf_release(&buf
);
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
;
177 struct archive_dir archive_dirs
[] = {
182 { ".git/objects/info", 0 }
185 if (mode
== DIAGNOSE_NONE
) {
187 goto diagnose_cleanup
;
190 stdout_fd
= dup(STDOUT_FILENO
);
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
;
203 strvec_pushl(&archiver_args
, "git-diagnose", "--format=zip", NULL
);
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
);
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
);
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
);
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);
249 error(_("failed to write archive"));
250 goto diagnose_cleanup
;
254 "Diagnostics complete.\n"
255 "All of the gathered info is captured in '%s'\n",
259 if (archiver_fd
>= 0) {
260 dup2(stdout_fd
, STDOUT_FILENO
);
265 strvec_clear(&archiver_args
);
266 strbuf_release(&buf
);