]> git.ipfire.org Git - thirdparty/git.git/blob - bugreport.c
bugreport: add compiler info
[thirdparty/git.git] / bugreport.c
1 #include "cache.h"
2 #include "parse-options.h"
3 #include "stdio.h"
4 #include "strbuf.h"
5 #include "time.h"
6 #include "help.h"
7 #include "compat/compiler.h"
8
9 static void get_system_info(struct strbuf *sys_info)
10 {
11 struct utsname uname_info;
12
13 /* get git version from native cmd */
14 strbuf_addstr(sys_info, _("git version:\n"));
15 get_version_info(sys_info, 1);
16
17 /* system call for other version info */
18 strbuf_addstr(sys_info, "uname: ");
19 if (uname(&uname_info))
20 strbuf_addf(sys_info, _("uname() failed with error '%s' (%d)\n"),
21 strerror(errno),
22 errno);
23 else
24 strbuf_addf(sys_info, "%s %s %s %s\n",
25 uname_info.sysname,
26 uname_info.release,
27 uname_info.version,
28 uname_info.machine);
29
30 strbuf_addstr(sys_info, _("compiler info: "));
31 get_compiler_info(sys_info);
32 strbuf_addstr(sys_info, _("libc info: "));
33 get_libc_info(sys_info);
34 }
35
36 static const char * const bugreport_usage[] = {
37 N_("git bugreport [-o|--output-directory <file>] [-s|--suffix <format>]"),
38 NULL
39 };
40
41 static int get_bug_template(struct strbuf *template)
42 {
43 const char template_text[] = N_(
44 "Thank you for filling out a Git bug report!\n"
45 "Please answer the following questions to help us understand your issue.\n"
46 "\n"
47 "What did you do before the bug happened? (Steps to reproduce your issue)\n"
48 "\n"
49 "What did you expect to happen? (Expected behavior)\n"
50 "\n"
51 "What happened instead? (Actual behavior)\n"
52 "\n"
53 "What's different between what you expected and what actually happened?\n"
54 "\n"
55 "Anything else you want to add:\n"
56 "\n"
57 "Please review the rest of the bug report below.\n"
58 "You can delete any lines you don't wish to share.\n");
59
60 strbuf_addstr(template, _(template_text));
61 return 0;
62 }
63
64 static void get_header(struct strbuf *buf, const char *title)
65 {
66 strbuf_addf(buf, "\n\n[%s]\n", title);
67 }
68
69 int cmd_main(int argc, const char **argv)
70 {
71 struct strbuf buffer = STRBUF_INIT;
72 struct strbuf report_path = STRBUF_INIT;
73 int report = -1;
74 time_t now = time(NULL);
75 char *option_output = NULL;
76 char *option_suffix = "%Y-%m-%d-%H%M";
77 int nongit_ok = 0;
78 const char *prefix = NULL;
79 const char *user_relative_path = NULL;
80
81 const struct option bugreport_options[] = {
82 OPT_STRING('o', "output-directory", &option_output, N_("path"),
83 N_("specify a destination for the bugreport file")),
84 OPT_STRING('s', "suffix", &option_suffix, N_("format"),
85 N_("specify a strftime format suffix for the filename")),
86 OPT_END()
87 };
88
89 prefix = setup_git_directory_gently(&nongit_ok);
90
91 argc = parse_options(argc, argv, prefix, bugreport_options,
92 bugreport_usage, 0);
93
94 /* Prepare the path to put the result */
95 strbuf_addstr(&report_path,
96 prefix_filename(prefix,
97 option_output ? option_output : ""));
98 strbuf_complete(&report_path, '/');
99
100 strbuf_addstr(&report_path, "git-bugreport-");
101 strbuf_addftime(&report_path, option_suffix, localtime(&now), 0, 0);
102 strbuf_addstr(&report_path, ".txt");
103
104 switch (safe_create_leading_directories(report_path.buf)) {
105 case SCLD_OK:
106 case SCLD_EXISTS:
107 break;
108 default:
109 die(_("could not create leading directories for '%s'"),
110 report_path.buf);
111 }
112
113 /* Prepare the report contents */
114 get_bug_template(&buffer);
115
116 get_header(&buffer, _("System Info"));
117 get_system_info(&buffer);
118
119 /* fopen doesn't offer us an O_EXCL alternative, except with glibc. */
120 report = open(report_path.buf, O_CREAT | O_EXCL | O_WRONLY, 0666);
121
122 if (report < 0) {
123 UNLEAK(report_path);
124 die(_("couldn't create a new file at '%s'"), report_path.buf);
125 }
126
127 strbuf_write_fd(&buffer, report);
128 close(report);
129
130 /*
131 * We want to print the path relative to the user, but we still need the
132 * path relative to us to give to the editor.
133 */
134 if (!(prefix && skip_prefix(report_path.buf, prefix, &user_relative_path)))
135 user_relative_path = report_path.buf;
136 fprintf(stderr, _("Created new report at '%s'.\n"),
137 user_relative_path);
138
139 UNLEAK(buffer);
140 UNLEAK(report_path);
141 return !!launch_editor(report_path.buf, NULL, NULL);
142 }