3 #include "compat/disk.h"
10 #include "object-store.h"
12 #include "write-or-die.h"
19 struct diagnose_option
{
20 enum diagnose_mode mode
;
21 const char *option_name
;
24 static struct diagnose_option diagnose_options
[] = {
25 { DIAGNOSE_STATS
, "stats" },
26 { DIAGNOSE_ALL
, "all" },
29 int option_parse_diagnose(const struct option
*opt
, const char *arg
, int unset
)
32 enum diagnose_mode
*diagnose
= opt
->value
;
35 *diagnose
= unset
? DIAGNOSE_NONE
: DIAGNOSE_STATS
;
39 for (i
= 0; i
< ARRAY_SIZE(diagnose_options
); i
++) {
40 if (!strcmp(arg
, diagnose_options
[i
].option_name
)) {
41 *diagnose
= diagnose_options
[i
].mode
;
46 return error(_("invalid --%s value '%s'"), opt
->long_name
, arg
);
49 static void dir_file_stats_objects(const char *full_path
,
50 size_t full_path_len UNUSED
,
51 const char *file_name
, void *data
)
53 struct strbuf
*buf
= data
;
56 if (!stat(full_path
, &st
))
57 strbuf_addf(buf
, "%-70s %16" PRIuMAX
"\n", file_name
,
58 (uintmax_t)st
.st_size
);
61 static int dir_file_stats(struct object_directory
*object_dir
, void *data
)
63 struct strbuf
*buf
= data
;
65 strbuf_addf(buf
, "Contents of %s:\n", object_dir
->path
);
67 for_each_file_in_pack_dir(object_dir
->path
, dir_file_stats_objects
,
74 * Get the d_type of a dirent. If the d_type is unknown, derive it from
77 * Note that 'path' is assumed to have a trailing slash. It is also modified
78 * in-place during the execution of the function, but is then reverted to its
79 * original value before returning.
81 static unsigned char get_dtype(struct dirent
*e
, struct strbuf
*path
)
84 unsigned char dtype
= DTYPE(e
);
87 if (dtype
!= DT_UNKNOWN
)
90 /* d_type unknown in dirent, try to fall back on lstat results */
91 base_path_len
= path
->len
;
92 strbuf_addstr(path
, e
->d_name
);
93 if (lstat(path
->buf
, &st
))
96 /* determine d_type from st_mode */
97 if (S_ISREG(st
.st_mode
))
99 else if (S_ISDIR(st
.st_mode
))
101 else if (S_ISLNK(st
.st_mode
))
105 strbuf_setlen(path
, base_path_len
);
109 static int count_files(struct strbuf
*path
)
111 DIR *dir
= opendir(path
->buf
);
118 while ((e
= readdir_skip_dot_and_dotdot(dir
)) != NULL
)
119 if (get_dtype(e
, path
) == DT_REG
)
126 static void loose_objs_stats(struct strbuf
*buf
, const char *path
)
128 DIR *dir
= opendir(path
);
133 struct strbuf count_path
= STRBUF_INIT
;
134 size_t base_path_len
;
139 strbuf_addstr(buf
, "Object directory stats for ");
140 strbuf_add_absolute_path(buf
, path
);
141 strbuf_addstr(buf
, ":\n");
143 strbuf_add_absolute_path(&count_path
, path
);
144 strbuf_addch(&count_path
, '/');
145 base_path_len
= count_path
.len
;
147 while ((e
= readdir_skip_dot_and_dotdot(dir
)) != NULL
)
148 if (get_dtype(e
, &count_path
) == DT_DIR
&&
149 strlen(e
->d_name
) == 2 &&
150 !hex_to_bytes(&c
, e
->d_name
, 1)) {
151 strbuf_setlen(&count_path
, base_path_len
);
152 strbuf_addf(&count_path
, "%s/", e
->d_name
);
153 total
+= (count
= count_files(&count_path
));
154 strbuf_addf(buf
, "%s : %7d files\n", e
->d_name
, count
);
157 strbuf_addf(buf
, "Total: %d loose objects", total
);
159 strbuf_release(&count_path
);
163 static int add_directory_to_archiver(struct strvec
*archiver_args
,
164 const char *path
, int recurse
)
166 int at_root
= !*path
;
169 struct strbuf buf
= STRBUF_INIT
;
173 dir
= opendir(at_root
? "." : path
);
175 if (errno
== ENOENT
) {
176 warning(_("could not archive missing directory '%s'"), path
);
179 return error_errno(_("could not open directory '%s'"), path
);
183 strbuf_addf(&buf
, "%s/", path
);
185 strvec_pushf(archiver_args
, "--prefix=%s", buf
.buf
);
187 while (!res
&& (e
= readdir_skip_dot_and_dotdot(dir
))) {
188 struct strbuf abspath
= STRBUF_INIT
;
191 strbuf_add_absolute_path(&abspath
, at_root
? "." : path
);
192 strbuf_addch(&abspath
, '/');
193 dtype
= get_dtype(e
, &abspath
);
195 strbuf_setlen(&buf
, len
);
196 strbuf_addstr(&buf
, e
->d_name
);
199 strvec_pushf(archiver_args
, "--add-file=%s", buf
.buf
);
200 else if (dtype
!= DT_DIR
)
201 warning(_("skipping '%s', which is neither file nor "
202 "directory"), buf
.buf
);
204 add_directory_to_archiver(archiver_args
,
205 buf
.buf
, recurse
) < 0)
208 strbuf_release(&abspath
);
212 strbuf_release(&buf
);
216 int create_diagnostics_archive(struct strbuf
*zip_path
, enum diagnose_mode mode
)
218 struct strvec archiver_args
= STRVEC_INIT
;
219 char **argv_copy
= NULL
;
220 int stdout_fd
= -1, archiver_fd
= -1;
221 struct strbuf buf
= STRBUF_INIT
;
223 struct archive_dir archive_dirs
[] = {
228 { ".git/objects/info", 0 }
231 if (mode
== DIAGNOSE_NONE
) {
233 goto diagnose_cleanup
;
236 stdout_fd
= dup(STDOUT_FILENO
);
238 res
= error_errno(_("could not duplicate stdout"));
239 goto diagnose_cleanup
;
242 archiver_fd
= xopen(zip_path
->buf
, O_CREAT
| O_WRONLY
| O_TRUNC
, 0666);
243 if (dup2(archiver_fd
, STDOUT_FILENO
) < 0) {
244 res
= error_errno(_("could not redirect output"));
245 goto diagnose_cleanup
;
249 strvec_pushl(&archiver_args
, "git-diagnose", "--format=zip", NULL
);
252 strbuf_addstr(&buf
, "Collecting diagnostic info\n\n");
253 get_version_info(&buf
, 1);
255 strbuf_addf(&buf
, "Repository root: %s\n", the_repository
->worktree
);
257 write_or_die(stdout_fd
, buf
.buf
, buf
.len
);
258 strvec_pushf(&archiver_args
,
259 "--add-virtual-file=diagnostics.log:%.*s",
260 (int)buf
.len
, buf
.buf
);
263 strbuf_addstr(&buf
, "--add-virtual-file=packs-local.txt:");
264 dir_file_stats(the_repository
->objects
->odb
, &buf
);
265 foreach_alt_odb(dir_file_stats
, &buf
);
266 strvec_push(&archiver_args
, buf
.buf
);
269 strbuf_addstr(&buf
, "--add-virtual-file=objects-local.txt:");
270 loose_objs_stats(&buf
, ".git/objects");
271 strvec_push(&archiver_args
, buf
.buf
);
273 /* Only include this if explicitly requested */
274 if (mode
== DIAGNOSE_ALL
) {
275 for (i
= 0; i
< ARRAY_SIZE(archive_dirs
); i
++) {
276 if (add_directory_to_archiver(&archiver_args
,
277 archive_dirs
[i
].path
,
278 archive_dirs
[i
].recursive
)) {
279 res
= error_errno(_("could not add directory '%s' to archiver"),
280 archive_dirs
[i
].path
);
281 goto diagnose_cleanup
;
286 strvec_pushl(&archiver_args
, "--prefix=",
287 oid_to_hex(the_hash_algo
->empty_tree
), "--", NULL
);
289 /* `write_archive()` modifies the `argv` passed to it. Let it. */
290 argv_copy
= xmemdupz(archiver_args
.v
,
291 sizeof(char *) * archiver_args
.nr
);
292 res
= write_archive(archiver_args
.nr
, (const char **)argv_copy
, NULL
,
293 the_repository
, NULL
, 0);
295 error(_("failed to write archive"));
296 goto diagnose_cleanup
;
300 "Diagnostics complete.\n"
301 "All of the gathered info is captured in '%s'\n",
305 if (archiver_fd
>= 0) {
306 dup2(stdout_fd
, STDOUT_FILENO
);
311 strvec_clear(&archiver_args
);
312 strbuf_release(&buf
);