1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
5 #include "compat/disk.h"
12 #include "object-store-ll.h"
14 #include "parse-options.h"
15 #include "write-or-die.h"
22 struct diagnose_option
{
23 enum diagnose_mode mode
;
24 const char *option_name
;
27 static struct diagnose_option diagnose_options
[] = {
28 { DIAGNOSE_STATS
, "stats" },
29 { DIAGNOSE_ALL
, "all" },
32 int option_parse_diagnose(const struct option
*opt
, const char *arg
, int unset
)
35 enum diagnose_mode
*diagnose
= opt
->value
;
38 *diagnose
= unset
? DIAGNOSE_NONE
: DIAGNOSE_STATS
;
42 for (i
= 0; i
< ARRAY_SIZE(diagnose_options
); i
++) {
43 if (!strcmp(arg
, diagnose_options
[i
].option_name
)) {
44 *diagnose
= diagnose_options
[i
].mode
;
49 return error(_("invalid --%s value '%s'"), opt
->long_name
, arg
);
52 static void dir_file_stats_objects(const char *full_path
,
53 size_t full_path_len UNUSED
,
54 const char *file_name
, void *data
)
56 struct strbuf
*buf
= data
;
59 if (!stat(full_path
, &st
))
60 strbuf_addf(buf
, "%-70s %16" PRIuMAX
"\n", file_name
,
61 (uintmax_t)st
.st_size
);
64 static int dir_file_stats(struct object_directory
*object_dir
, void *data
)
66 struct strbuf
*buf
= data
;
68 strbuf_addf(buf
, "Contents of %s:\n", object_dir
->path
);
70 for_each_file_in_pack_dir(object_dir
->path
, dir_file_stats_objects
,
76 static int count_files(struct strbuf
*path
)
78 DIR *dir
= opendir(path
->buf
);
85 while ((e
= readdir_skip_dot_and_dotdot(dir
)) != NULL
)
86 if (get_dtype(e
, path
, 0) == DT_REG
)
93 static void loose_objs_stats(struct strbuf
*buf
, const char *path
)
95 DIR *dir
= opendir(path
);
100 struct strbuf count_path
= STRBUF_INIT
;
101 size_t base_path_len
;
106 strbuf_addstr(buf
, "Object directory stats for ");
107 strbuf_add_absolute_path(buf
, path
);
108 strbuf_addstr(buf
, ":\n");
110 strbuf_add_absolute_path(&count_path
, path
);
111 strbuf_addch(&count_path
, '/');
112 base_path_len
= count_path
.len
;
114 while ((e
= readdir_skip_dot_and_dotdot(dir
)) != NULL
)
115 if (get_dtype(e
, &count_path
, 0) == DT_DIR
&&
116 strlen(e
->d_name
) == 2 &&
117 !hex_to_bytes(&c
, e
->d_name
, 1)) {
118 strbuf_setlen(&count_path
, base_path_len
);
119 strbuf_addf(&count_path
, "%s/", e
->d_name
);
120 total
+= (count
= count_files(&count_path
));
121 strbuf_addf(buf
, "%s : %7d files\n", e
->d_name
, count
);
124 strbuf_addf(buf
, "Total: %d loose objects", total
);
126 strbuf_release(&count_path
);
130 static int add_directory_to_archiver(struct strvec
*archiver_args
,
131 const char *path
, int recurse
)
133 int at_root
= !*path
;
136 struct strbuf buf
= STRBUF_INIT
;
140 dir
= opendir(at_root
? "." : path
);
142 if (errno
== ENOENT
) {
143 warning(_("could not archive missing directory '%s'"), path
);
146 return error_errno(_("could not open directory '%s'"), path
);
150 strbuf_addf(&buf
, "%s/", path
);
152 strvec_pushf(archiver_args
, "--prefix=%s", buf
.buf
);
154 while (!res
&& (e
= readdir_skip_dot_and_dotdot(dir
))) {
155 struct strbuf abspath
= STRBUF_INIT
;
158 strbuf_add_absolute_path(&abspath
, at_root
? "." : path
);
159 strbuf_addch(&abspath
, '/');
160 dtype
= get_dtype(e
, &abspath
, 0);
162 strbuf_setlen(&buf
, len
);
163 strbuf_addstr(&buf
, e
->d_name
);
166 strvec_pushf(archiver_args
, "--add-file=%s", buf
.buf
);
167 else if (dtype
!= DT_DIR
)
168 warning(_("skipping '%s', which is neither file nor "
169 "directory"), buf
.buf
);
171 add_directory_to_archiver(archiver_args
,
172 buf
.buf
, recurse
) < 0)
175 strbuf_release(&abspath
);
179 strbuf_release(&buf
);
183 int create_diagnostics_archive(struct strbuf
*zip_path
, enum diagnose_mode mode
)
185 struct strvec archiver_args
= STRVEC_INIT
;
186 char **argv_copy
= NULL
;
187 int stdout_fd
= -1, archiver_fd
= -1;
188 struct strbuf buf
= STRBUF_INIT
;
190 struct archive_dir archive_dirs
[] = {
195 { ".git/objects/info", 0 }
198 if (mode
== DIAGNOSE_NONE
) {
200 goto diagnose_cleanup
;
203 stdout_fd
= dup(STDOUT_FILENO
);
205 res
= error_errno(_("could not duplicate stdout"));
206 goto diagnose_cleanup
;
209 archiver_fd
= xopen(zip_path
->buf
, O_CREAT
| O_WRONLY
| O_TRUNC
, 0666);
210 if (dup2(archiver_fd
, STDOUT_FILENO
) < 0) {
211 res
= error_errno(_("could not redirect output"));
212 goto diagnose_cleanup
;
216 strvec_pushl(&archiver_args
, "git-diagnose", "--format=zip", NULL
);
219 strbuf_addstr(&buf
, "Collecting diagnostic info\n\n");
220 get_version_info(&buf
, 1);
222 strbuf_addf(&buf
, "Repository root: %s\n", the_repository
->worktree
);
224 write_or_die(stdout_fd
, buf
.buf
, buf
.len
);
225 strvec_pushf(&archiver_args
,
226 "--add-virtual-file=diagnostics.log:%.*s",
227 (int)buf
.len
, buf
.buf
);
230 strbuf_addstr(&buf
, "--add-virtual-file=packs-local.txt:");
231 dir_file_stats(the_repository
->objects
->odb
, &buf
);
232 foreach_alt_odb(dir_file_stats
, &buf
);
233 strvec_push(&archiver_args
, buf
.buf
);
236 strbuf_addstr(&buf
, "--add-virtual-file=objects-local.txt:");
237 loose_objs_stats(&buf
, ".git/objects");
238 strvec_push(&archiver_args
, buf
.buf
);
240 /* Only include this if explicitly requested */
241 if (mode
== DIAGNOSE_ALL
) {
242 for (i
= 0; i
< ARRAY_SIZE(archive_dirs
); i
++) {
243 if (add_directory_to_archiver(&archiver_args
,
244 archive_dirs
[i
].path
,
245 archive_dirs
[i
].recursive
)) {
246 res
= error_errno(_("could not add directory '%s' to archiver"),
247 archive_dirs
[i
].path
);
248 goto diagnose_cleanup
;
253 strvec_pushl(&archiver_args
, "--prefix=",
254 oid_to_hex(the_hash_algo
->empty_tree
), "--", NULL
);
256 /* `write_archive()` modifies the `argv` passed to it. Let it. */
257 argv_copy
= xmemdupz(archiver_args
.v
,
258 sizeof(char *) * archiver_args
.nr
);
259 res
= write_archive(archiver_args
.nr
, (const char **)argv_copy
, NULL
,
260 the_repository
, NULL
, 0);
262 error(_("failed to write archive"));
263 goto diagnose_cleanup
;
267 "Diagnostics complete.\n"
268 "All of the gathered info is captured in '%s'\n",
272 if (archiver_fd
>= 0) {
273 dup2(stdout_fd
, STDOUT_FILENO
);
278 strvec_clear(&archiver_args
);
279 strbuf_release(&buf
);