1 #include "git-compat-util.h"
3 #include "compat/disk.h"
10 #include "object-store.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
,
75 * Get the d_type of a dirent. If the d_type is unknown, derive it from
78 * Note that 'path' is assumed to have a trailing slash. It is also modified
79 * in-place during the execution of the function, but is then reverted to its
80 * original value before returning.
82 static unsigned char get_dtype(struct dirent
*e
, struct strbuf
*path
)
85 unsigned char dtype
= DTYPE(e
);
88 if (dtype
!= DT_UNKNOWN
)
91 /* d_type unknown in dirent, try to fall back on lstat results */
92 base_path_len
= path
->len
;
93 strbuf_addstr(path
, e
->d_name
);
94 if (lstat(path
->buf
, &st
))
97 /* determine d_type from st_mode */
98 if (S_ISREG(st
.st_mode
))
100 else if (S_ISDIR(st
.st_mode
))
102 else if (S_ISLNK(st
.st_mode
))
106 strbuf_setlen(path
, base_path_len
);
110 static int count_files(struct strbuf
*path
)
112 DIR *dir
= opendir(path
->buf
);
119 while ((e
= readdir_skip_dot_and_dotdot(dir
)) != NULL
)
120 if (get_dtype(e
, path
) == DT_REG
)
127 static void loose_objs_stats(struct strbuf
*buf
, const char *path
)
129 DIR *dir
= opendir(path
);
134 struct strbuf count_path
= STRBUF_INIT
;
135 size_t base_path_len
;
140 strbuf_addstr(buf
, "Object directory stats for ");
141 strbuf_add_absolute_path(buf
, path
);
142 strbuf_addstr(buf
, ":\n");
144 strbuf_add_absolute_path(&count_path
, path
);
145 strbuf_addch(&count_path
, '/');
146 base_path_len
= count_path
.len
;
148 while ((e
= readdir_skip_dot_and_dotdot(dir
)) != NULL
)
149 if (get_dtype(e
, &count_path
) == DT_DIR
&&
150 strlen(e
->d_name
) == 2 &&
151 !hex_to_bytes(&c
, e
->d_name
, 1)) {
152 strbuf_setlen(&count_path
, base_path_len
);
153 strbuf_addf(&count_path
, "%s/", e
->d_name
);
154 total
+= (count
= count_files(&count_path
));
155 strbuf_addf(buf
, "%s : %7d files\n", e
->d_name
, count
);
158 strbuf_addf(buf
, "Total: %d loose objects", total
);
160 strbuf_release(&count_path
);
164 static int add_directory_to_archiver(struct strvec
*archiver_args
,
165 const char *path
, int recurse
)
167 int at_root
= !*path
;
170 struct strbuf buf
= STRBUF_INIT
;
174 dir
= opendir(at_root
? "." : path
);
176 if (errno
== ENOENT
) {
177 warning(_("could not archive missing directory '%s'"), path
);
180 return error_errno(_("could not open directory '%s'"), path
);
184 strbuf_addf(&buf
, "%s/", path
);
186 strvec_pushf(archiver_args
, "--prefix=%s", buf
.buf
);
188 while (!res
&& (e
= readdir_skip_dot_and_dotdot(dir
))) {
189 struct strbuf abspath
= STRBUF_INIT
;
192 strbuf_add_absolute_path(&abspath
, at_root
? "." : path
);
193 strbuf_addch(&abspath
, '/');
194 dtype
= get_dtype(e
, &abspath
);
196 strbuf_setlen(&buf
, len
);
197 strbuf_addstr(&buf
, e
->d_name
);
200 strvec_pushf(archiver_args
, "--add-file=%s", buf
.buf
);
201 else if (dtype
!= DT_DIR
)
202 warning(_("skipping '%s', which is neither file nor "
203 "directory"), buf
.buf
);
205 add_directory_to_archiver(archiver_args
,
206 buf
.buf
, recurse
) < 0)
209 strbuf_release(&abspath
);
213 strbuf_release(&buf
);
217 int create_diagnostics_archive(struct strbuf
*zip_path
, enum diagnose_mode mode
)
219 struct strvec archiver_args
= STRVEC_INIT
;
220 char **argv_copy
= NULL
;
221 int stdout_fd
= -1, archiver_fd
= -1;
222 struct strbuf buf
= STRBUF_INIT
;
224 struct archive_dir archive_dirs
[] = {
229 { ".git/objects/info", 0 }
232 if (mode
== DIAGNOSE_NONE
) {
234 goto diagnose_cleanup
;
237 stdout_fd
= dup(STDOUT_FILENO
);
239 res
= error_errno(_("could not duplicate stdout"));
240 goto diagnose_cleanup
;
243 archiver_fd
= xopen(zip_path
->buf
, O_CREAT
| O_WRONLY
| O_TRUNC
, 0666);
244 if (dup2(archiver_fd
, STDOUT_FILENO
) < 0) {
245 res
= error_errno(_("could not redirect output"));
246 goto diagnose_cleanup
;
250 strvec_pushl(&archiver_args
, "git-diagnose", "--format=zip", NULL
);
253 strbuf_addstr(&buf
, "Collecting diagnostic info\n\n");
254 get_version_info(&buf
, 1);
256 strbuf_addf(&buf
, "Repository root: %s\n", the_repository
->worktree
);
258 write_or_die(stdout_fd
, buf
.buf
, buf
.len
);
259 strvec_pushf(&archiver_args
,
260 "--add-virtual-file=diagnostics.log:%.*s",
261 (int)buf
.len
, buf
.buf
);
264 strbuf_addstr(&buf
, "--add-virtual-file=packs-local.txt:");
265 dir_file_stats(the_repository
->objects
->odb
, &buf
);
266 foreach_alt_odb(dir_file_stats
, &buf
);
267 strvec_push(&archiver_args
, buf
.buf
);
270 strbuf_addstr(&buf
, "--add-virtual-file=objects-local.txt:");
271 loose_objs_stats(&buf
, ".git/objects");
272 strvec_push(&archiver_args
, buf
.buf
);
274 /* Only include this if explicitly requested */
275 if (mode
== DIAGNOSE_ALL
) {
276 for (i
= 0; i
< ARRAY_SIZE(archive_dirs
); i
++) {
277 if (add_directory_to_archiver(&archiver_args
,
278 archive_dirs
[i
].path
,
279 archive_dirs
[i
].recursive
)) {
280 res
= error_errno(_("could not add directory '%s' to archiver"),
281 archive_dirs
[i
].path
);
282 goto diagnose_cleanup
;
287 strvec_pushl(&archiver_args
, "--prefix=",
288 oid_to_hex(the_hash_algo
->empty_tree
), "--", NULL
);
290 /* `write_archive()` modifies the `argv` passed to it. Let it. */
291 argv_copy
= xmemdupz(archiver_args
.v
,
292 sizeof(char *) * archiver_args
.nr
);
293 res
= write_archive(archiver_args
.nr
, (const char **)argv_copy
, NULL
,
294 the_repository
, NULL
, 0);
296 error(_("failed to write archive"));
297 goto diagnose_cleanup
;
301 "Diagnostics complete.\n"
302 "All of the gathered info is captured in '%s'\n",
306 if (archiver_fd
>= 0) {
307 dup2(stdout_fd
, STDOUT_FILENO
);
312 strvec_clear(&archiver_args
);
313 strbuf_release(&buf
);