1 #include "git-compat-util.h"
3 #include "compat/disk.h"
10 #include "object-store-ll.h"
12 #include "parse-options.h"
13 #include "write-or-die.h"
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
)
33 enum diagnose_mode
*diagnose
= opt
->value
;
36 *diagnose
= unset
? DIAGNOSE_NONE
: DIAGNOSE_STATS
;
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
;
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
;
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
,
74 static int count_files(struct strbuf
*path
)
76 DIR *dir
= opendir(path
->buf
);
83 while ((e
= readdir_skip_dot_and_dotdot(dir
)) != NULL
)
84 if (get_dtype(e
, path
, 0) == DT_REG
)
91 static void loose_objs_stats(struct strbuf
*buf
, const char *path
)
93 DIR *dir
= opendir(path
);
98 struct strbuf count_path
= STRBUF_INIT
;
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
);
128 static int add_directory_to_archiver(struct strvec
*archiver_args
,
129 const char *path
, int recurse
)
131 int at_root
= !*path
;
134 struct strbuf buf
= STRBUF_INIT
;
138 dir
= opendir(at_root
? "." : path
);
140 if (errno
== ENOENT
) {
141 warning(_("could not archive missing directory '%s'"), path
);
144 return error_errno(_("could not open directory '%s'"), path
);
148 strbuf_addf(&buf
, "%s/", path
);
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
;
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
);
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
);
169 add_directory_to_archiver(archiver_args
,
170 buf
.buf
, recurse
) < 0)
173 strbuf_release(&abspath
);
177 strbuf_release(&buf
);
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
;
188 struct archive_dir archive_dirs
[] = {
193 { ".git/objects/info", 0 }
196 if (mode
== DIAGNOSE_NONE
) {
198 goto diagnose_cleanup
;
201 stdout_fd
= dup(STDOUT_FILENO
);
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
;
214 strvec_pushl(&archiver_args
, "git-diagnose", "--format=zip", NULL
);
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
);
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
);
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
);
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);
260 error(_("failed to write archive"));
261 goto diagnose_cleanup
;
265 "Diagnostics complete.\n"
266 "All of the gathered info is captured in '%s'\n",
270 if (archiver_fd
>= 0) {
271 dup2(stdout_fd
, STDOUT_FILENO
);
276 strvec_clear(&archiver_args
);
277 strbuf_release(&buf
);