git
/
diagnose.c
282 строки · 6.8 Кб
1#define USE_THE_REPOSITORY_VARIABLE
2
3#include "git-compat-util.h"
4#include "diagnose.h"
5#include "compat/disk.h"
6#include "archive.h"
7#include "dir.h"
8#include "help.h"
9#include "gettext.h"
10#include "hex.h"
11#include "strvec.h"
12#include "object-store-ll.h"
13#include "packfile.h"
14#include "parse-options.h"
15#include "write-or-die.h"
16
17struct archive_dir {
18const char *path;
19int recursive;
20};
21
22struct diagnose_option {
23enum diagnose_mode mode;
24const char *option_name;
25};
26
27static struct diagnose_option diagnose_options[] = {
28{ DIAGNOSE_STATS, "stats" },
29{ DIAGNOSE_ALL, "all" },
30};
31
32int option_parse_diagnose(const struct option *opt, const char *arg, int unset)
33{
34int i;
35enum diagnose_mode *diagnose = opt->value;
36
37if (!arg) {
38*diagnose = unset ? DIAGNOSE_NONE : DIAGNOSE_STATS;
39return 0;
40}
41
42for (i = 0; i < ARRAY_SIZE(diagnose_options); i++) {
43if (!strcmp(arg, diagnose_options[i].option_name)) {
44*diagnose = diagnose_options[i].mode;
45return 0;
46}
47}
48
49return error(_("invalid --%s value '%s'"), opt->long_name, arg);
50}
51
52static void dir_file_stats_objects(const char *full_path,
53size_t full_path_len UNUSED,
54const char *file_name, void *data)
55{
56struct strbuf *buf = data;
57struct stat st;
58
59if (!stat(full_path, &st))
60strbuf_addf(buf, "%-70s %16" PRIuMAX "\n", file_name,
61(uintmax_t)st.st_size);
62}
63
64static int dir_file_stats(struct object_directory *object_dir, void *data)
65{
66struct strbuf *buf = data;
67
68strbuf_addf(buf, "Contents of %s:\n", object_dir->path);
69
70for_each_file_in_pack_dir(object_dir->path, dir_file_stats_objects,
71data);
72
73return 0;
74}
75
76static int count_files(struct strbuf *path)
77{
78DIR *dir = opendir(path->buf);
79struct dirent *e;
80int count = 0;
81
82if (!dir)
83return 0;
84
85while ((e = readdir_skip_dot_and_dotdot(dir)) != NULL)
86if (get_dtype(e, path, 0) == DT_REG)
87count++;
88
89closedir(dir);
90return count;
91}
92
93static void loose_objs_stats(struct strbuf *buf, const char *path)
94{
95DIR *dir = opendir(path);
96struct dirent *e;
97int count;
98int total = 0;
99unsigned char c;
100struct strbuf count_path = STRBUF_INIT;
101size_t base_path_len;
102
103if (!dir)
104return;
105
106strbuf_addstr(buf, "Object directory stats for ");
107strbuf_add_absolute_path(buf, path);
108strbuf_addstr(buf, ":\n");
109
110strbuf_add_absolute_path(&count_path, path);
111strbuf_addch(&count_path, '/');
112base_path_len = count_path.len;
113
114while ((e = readdir_skip_dot_and_dotdot(dir)) != NULL)
115if (get_dtype(e, &count_path, 0) == DT_DIR &&
116strlen(e->d_name) == 2 &&
117!hex_to_bytes(&c, e->d_name, 1)) {
118strbuf_setlen(&count_path, base_path_len);
119strbuf_addf(&count_path, "%s/", e->d_name);
120total += (count = count_files(&count_path));
121strbuf_addf(buf, "%s : %7d files\n", e->d_name, count);
122}
123
124strbuf_addf(buf, "Total: %d loose objects", total);
125
126strbuf_release(&count_path);
127closedir(dir);
128}
129
130static int add_directory_to_archiver(struct strvec *archiver_args,
131const char *path, int recurse)
132{
133int at_root = !*path;
134DIR *dir;
135struct dirent *e;
136struct strbuf buf = STRBUF_INIT;
137size_t len;
138int res = 0;
139
140dir = opendir(at_root ? "." : path);
141if (!dir) {
142if (errno == ENOENT) {
143warning(_("could not archive missing directory '%s'"), path);
144return 0;
145}
146return error_errno(_("could not open directory '%s'"), path);
147}
148
149if (!at_root)
150strbuf_addf(&buf, "%s/", path);
151len = buf.len;
152strvec_pushf(archiver_args, "--prefix=%s", buf.buf);
153
154while (!res && (e = readdir_skip_dot_and_dotdot(dir))) {
155struct strbuf abspath = STRBUF_INIT;
156unsigned char dtype;
157
158strbuf_add_absolute_path(&abspath, at_root ? "." : path);
159strbuf_addch(&abspath, '/');
160dtype = get_dtype(e, &abspath, 0);
161
162strbuf_setlen(&buf, len);
163strbuf_addstr(&buf, e->d_name);
164
165if (dtype == DT_REG)
166strvec_pushf(archiver_args, "--add-file=%s", buf.buf);
167else if (dtype != DT_DIR)
168warning(_("skipping '%s', which is neither file nor "
169"directory"), buf.buf);
170else if (recurse &&
171add_directory_to_archiver(archiver_args,
172buf.buf, recurse) < 0)
173res = -1;
174
175strbuf_release(&abspath);
176}
177
178closedir(dir);
179strbuf_release(&buf);
180return res;
181}
182
183int create_diagnostics_archive(struct strbuf *zip_path, enum diagnose_mode mode)
184{
185struct strvec archiver_args = STRVEC_INIT;
186char **argv_copy = NULL;
187int stdout_fd = -1, archiver_fd = -1;
188struct strbuf buf = STRBUF_INIT;
189int res, i;
190struct archive_dir archive_dirs[] = {
191{ ".git", 0 },
192{ ".git/hooks", 0 },
193{ ".git/info", 0 },
194{ ".git/logs", 1 },
195{ ".git/objects/info", 0 }
196};
197
198if (mode == DIAGNOSE_NONE) {
199res = 0;
200goto diagnose_cleanup;
201}
202
203stdout_fd = dup(STDOUT_FILENO);
204if (stdout_fd < 0) {
205res = error_errno(_("could not duplicate stdout"));
206goto diagnose_cleanup;
207}
208
209archiver_fd = xopen(zip_path->buf, O_CREAT | O_WRONLY | O_TRUNC, 0666);
210if (dup2(archiver_fd, STDOUT_FILENO) < 0) {
211res = error_errno(_("could not redirect output"));
212goto diagnose_cleanup;
213}
214
215init_zip_archiver();
216strvec_pushl(&archiver_args, "git-diagnose", "--format=zip", NULL);
217
218strbuf_reset(&buf);
219strbuf_addstr(&buf, "Collecting diagnostic info\n\n");
220get_version_info(&buf, 1);
221
222strbuf_addf(&buf, "Repository root: %s\n", the_repository->worktree);
223get_disk_info(&buf);
224write_or_die(stdout_fd, buf.buf, buf.len);
225strvec_pushf(&archiver_args,
226"--add-virtual-file=diagnostics.log:%.*s",
227(int)buf.len, buf.buf);
228
229strbuf_reset(&buf);
230strbuf_addstr(&buf, "--add-virtual-file=packs-local.txt:");
231dir_file_stats(the_repository->objects->odb, &buf);
232foreach_alt_odb(dir_file_stats, &buf);
233strvec_push(&archiver_args, buf.buf);
234
235strbuf_reset(&buf);
236strbuf_addstr(&buf, "--add-virtual-file=objects-local.txt:");
237loose_objs_stats(&buf, ".git/objects");
238strvec_push(&archiver_args, buf.buf);
239
240/* Only include this if explicitly requested */
241if (mode == DIAGNOSE_ALL) {
242for (i = 0; i < ARRAY_SIZE(archive_dirs); i++) {
243if (add_directory_to_archiver(&archiver_args,
244archive_dirs[i].path,
245archive_dirs[i].recursive)) {
246res = error_errno(_("could not add directory '%s' to archiver"),
247archive_dirs[i].path);
248goto diagnose_cleanup;
249}
250}
251}
252
253strvec_pushl(&archiver_args, "--prefix=",
254oid_to_hex(the_hash_algo->empty_tree), "--", NULL);
255
256/* `write_archive()` modifies the `argv` passed to it. Let it. */
257argv_copy = xmemdupz(archiver_args.v,
258sizeof(char *) * archiver_args.nr);
259res = write_archive(archiver_args.nr, (const char **)argv_copy, NULL,
260the_repository, NULL, 0);
261if (res) {
262error(_("failed to write archive"));
263goto diagnose_cleanup;
264}
265
266fprintf(stderr, "\n"
267"Diagnostics complete.\n"
268"All of the gathered info is captured in '%s'\n",
269zip_path->buf);
270
271diagnose_cleanup:
272if (archiver_fd >= 0) {
273dup2(stdout_fd, STDOUT_FILENO);
274close(stdout_fd);
275close(archiver_fd);
276}
277free(argv_copy);
278strvec_clear(&archiver_args);
279strbuf_release(&buf);
280
281return res;
282}
283