Jeff King | d7a5649 | 2020-08-13 10:59:36 -0400 | [diff] [blame] | 1 | #include "builtin.h" |
Elijah Newren | 0b027f6 | 2023-03-21 06:25:58 +0000 | [diff] [blame] | 2 | #include "abspath.h" |
Elijah Newren | 4e12082 | 2023-04-11 00:41:57 -0700 | [diff] [blame] | 3 | #include "editor.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 4 | #include "gettext.h" |
Emily Shaffer | 238b439 | 2020-04-16 14:18:04 -0700 | [diff] [blame] | 5 | #include "parse-options.h" |
Emily Shaffer | 238b439 | 2020-04-16 14:18:04 -0700 | [diff] [blame] | 6 | #include "strbuf.h" |
Emily Shaffer | 617d571 | 2020-04-16 14:18:05 -0700 | [diff] [blame] | 7 | #include "help.h" |
Emily Shaffer | 69bcbbc | 2020-04-16 14:18:07 -0700 | [diff] [blame] | 8 | #include "compat/compiler.h" |
Ævar Arnfjörð Bjarmason | 5e3aba3 | 2021-09-26 21:03:26 +0200 | [diff] [blame] | 9 | #include "hook.h" |
Ævar Arnfjörð Bjarmason | cfe853e | 2021-09-26 21:03:29 +0200 | [diff] [blame] | 10 | #include "hook-list.h" |
Victoria Dye | aac0e8f | 2022-08-12 20:10:17 +0000 | [diff] [blame] | 11 | #include "diagnose.h" |
Elijah Newren | 87bed17 | 2023-04-11 00:41:53 -0700 | [diff] [blame] | 12 | #include "object-file.h" |
Elijah Newren | e38da48 | 2023-03-21 06:26:05 +0000 | [diff] [blame] | 13 | #include "setup.h" |
Emily Shaffer | 617d571 | 2020-04-16 14:18:05 -0700 | [diff] [blame] | 14 | |
| 15 | static void get_system_info(struct strbuf *sys_info) |
| 16 | { |
Emily Shaffer | 1411914 | 2020-04-16 14:18:06 -0700 | [diff] [blame] | 17 | struct utsname uname_info; |
Emily Shaffer | 4a4804e | 2020-05-12 16:42:13 -0700 | [diff] [blame] | 18 | char *shell = NULL; |
Emily Shaffer | 1411914 | 2020-04-16 14:18:06 -0700 | [diff] [blame] | 19 | |
Emily Shaffer | 617d571 | 2020-04-16 14:18:05 -0700 | [diff] [blame] | 20 | /* get git version from native cmd */ |
| 21 | strbuf_addstr(sys_info, _("git version:\n")); |
| 22 | get_version_info(sys_info, 1); |
Emily Shaffer | 1411914 | 2020-04-16 14:18:06 -0700 | [diff] [blame] | 23 | |
| 24 | /* system call for other version info */ |
| 25 | strbuf_addstr(sys_info, "uname: "); |
| 26 | if (uname(&uname_info)) |
| 27 | strbuf_addf(sys_info, _("uname() failed with error '%s' (%d)\n"), |
| 28 | strerror(errno), |
| 29 | errno); |
| 30 | else |
| 31 | strbuf_addf(sys_info, "%s %s %s %s\n", |
| 32 | uname_info.sysname, |
| 33 | uname_info.release, |
| 34 | uname_info.version, |
| 35 | uname_info.machine); |
Emily Shaffer | 69bcbbc | 2020-04-16 14:18:07 -0700 | [diff] [blame] | 36 | |
| 37 | strbuf_addstr(sys_info, _("compiler info: ")); |
| 38 | get_compiler_info(sys_info); |
Emily Shaffer | 4a4804e | 2020-05-12 16:42:13 -0700 | [diff] [blame] | 39 | |
Emily Shaffer | 69bcbbc | 2020-04-16 14:18:07 -0700 | [diff] [blame] | 40 | strbuf_addstr(sys_info, _("libc info: ")); |
| 41 | get_libc_info(sys_info); |
Emily Shaffer | 4a4804e | 2020-05-12 16:42:13 -0700 | [diff] [blame] | 42 | |
| 43 | shell = getenv("SHELL"); |
| 44 | strbuf_addf(sys_info, "$SHELL (typically, interactive shell): %s\n", |
| 45 | shell ? shell : "<unset>"); |
Emily Shaffer | 617d571 | 2020-04-16 14:18:05 -0700 | [diff] [blame] | 46 | } |
Emily Shaffer | 238b439 | 2020-04-16 14:18:04 -0700 | [diff] [blame] | 47 | |
Emily Shaffer | 788a776 | 2020-05-07 17:53:57 -0700 | [diff] [blame] | 48 | static void get_populated_hooks(struct strbuf *hook_info, int nongit) |
| 49 | { |
Ævar Arnfjörð Bjarmason | cfe853e | 2021-09-26 21:03:29 +0200 | [diff] [blame] | 50 | const char **p; |
Emily Shaffer | 788a776 | 2020-05-07 17:53:57 -0700 | [diff] [blame] | 51 | |
| 52 | if (nongit) { |
| 53 | strbuf_addstr(hook_info, |
| 54 | _("not run from a git repository - no hooks to show\n")); |
| 55 | return; |
| 56 | } |
| 57 | |
Ævar Arnfjörð Bjarmason | cfe853e | 2021-09-26 21:03:29 +0200 | [diff] [blame] | 58 | for (p = hook_name_list; *p; p++) { |
| 59 | const char *hook = *p; |
| 60 | |
| 61 | if (hook_exists(hook)) |
| 62 | strbuf_addf(hook_info, "%s\n", hook); |
| 63 | } |
Emily Shaffer | 788a776 | 2020-05-07 17:53:57 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Emily Shaffer | 238b439 | 2020-04-16 14:18:04 -0700 | [diff] [blame] | 66 | static const char * const bugreport_usage[] = { |
Jiamu Sun | b3b57c6 | 2024-03-14 04:00:16 +0000 | [diff] [blame] | 67 | N_("git bugreport [(-o | --output-directory) <path>]\n" |
| 68 | " [(-s | --suffix) <format> | --no-suffix]\n" |
Ævar Arnfjörð Bjarmason | 8bc6f92 | 2022-10-13 17:39:05 +0200 | [diff] [blame] | 69 | " [--diagnose[=<mode>]]"), |
Emily Shaffer | 238b439 | 2020-04-16 14:18:04 -0700 | [diff] [blame] | 70 | NULL |
| 71 | }; |
| 72 | |
| 73 | static int get_bug_template(struct strbuf *template) |
| 74 | { |
| 75 | const char template_text[] = N_( |
| 76 | "Thank you for filling out a Git bug report!\n" |
| 77 | "Please answer the following questions to help us understand your issue.\n" |
| 78 | "\n" |
| 79 | "What did you do before the bug happened? (Steps to reproduce your issue)\n" |
| 80 | "\n" |
| 81 | "What did you expect to happen? (Expected behavior)\n" |
| 82 | "\n" |
| 83 | "What happened instead? (Actual behavior)\n" |
| 84 | "\n" |
| 85 | "What's different between what you expected and what actually happened?\n" |
| 86 | "\n" |
| 87 | "Anything else you want to add:\n" |
| 88 | "\n" |
| 89 | "Please review the rest of the bug report below.\n" |
| 90 | "You can delete any lines you don't wish to share.\n"); |
| 91 | |
| 92 | strbuf_addstr(template, _(template_text)); |
| 93 | return 0; |
| 94 | } |
| 95 | |
Emily Shaffer | 617d571 | 2020-04-16 14:18:05 -0700 | [diff] [blame] | 96 | static void get_header(struct strbuf *buf, const char *title) |
| 97 | { |
| 98 | strbuf_addf(buf, "\n\n[%s]\n", title); |
| 99 | } |
| 100 | |
Jeff King | d7a5649 | 2020-08-13 10:59:36 -0400 | [diff] [blame] | 101 | int cmd_bugreport(int argc, const char **argv, const char *prefix) |
Emily Shaffer | 238b439 | 2020-04-16 14:18:04 -0700 | [diff] [blame] | 102 | { |
| 103 | struct strbuf buffer = STRBUF_INIT; |
| 104 | struct strbuf report_path = STRBUF_INIT; |
| 105 | int report = -1; |
| 106 | time_t now = time(NULL); |
Taylor Blau | 4f6460d | 2020-11-30 19:30:06 -0500 | [diff] [blame] | 107 | struct tm tm; |
Victoria Dye | aac0e8f | 2022-08-12 20:10:17 +0000 | [diff] [blame] | 108 | enum diagnose_mode diagnose = DIAGNOSE_NONE; |
Emily Shaffer | 238b439 | 2020-04-16 14:18:04 -0700 | [diff] [blame] | 109 | char *option_output = NULL; |
| 110 | char *option_suffix = "%Y-%m-%d-%H%M"; |
Emily Shaffer | 238b439 | 2020-04-16 14:18:04 -0700 | [diff] [blame] | 111 | const char *user_relative_path = NULL; |
Andrzej Hunt | 4fa2687 | 2021-04-25 14:16:13 +0000 | [diff] [blame] | 112 | char *prefixed_filename; |
Victoria Dye | aac0e8f | 2022-08-12 20:10:17 +0000 | [diff] [blame] | 113 | size_t output_path_len; |
Ævar Arnfjörð Bjarmason | ac95f5d | 2022-11-08 19:17:51 +0100 | [diff] [blame] | 114 | int ret; |
Emily Shaffer | 238b439 | 2020-04-16 14:18:04 -0700 | [diff] [blame] | 115 | |
| 116 | const struct option bugreport_options[] = { |
Victoria Dye | aac0e8f | 2022-08-12 20:10:17 +0000 | [diff] [blame] | 117 | OPT_CALLBACK_F(0, "diagnose", &diagnose, N_("mode"), |
| 118 | N_("create an additional zip archive of detailed diagnostics (default 'stats')"), |
| 119 | PARSE_OPT_OPTARG, option_parse_diagnose), |
Emily Shaffer | 238b439 | 2020-04-16 14:18:04 -0700 | [diff] [blame] | 120 | OPT_STRING('o', "output-directory", &option_output, N_("path"), |
Victoria Dye | aac0e8f | 2022-08-12 20:10:17 +0000 | [diff] [blame] | 121 | N_("specify a destination for the bugreport file(s)")), |
Emily Shaffer | 238b439 | 2020-04-16 14:18:04 -0700 | [diff] [blame] | 122 | OPT_STRING('s', "suffix", &option_suffix, N_("format"), |
Victoria Dye | aac0e8f | 2022-08-12 20:10:17 +0000 | [diff] [blame] | 123 | N_("specify a strftime format suffix for the filename(s)")), |
Emily Shaffer | 238b439 | 2020-04-16 14:18:04 -0700 | [diff] [blame] | 124 | OPT_END() |
| 125 | }; |
| 126 | |
Emily Shaffer | 238b439 | 2020-04-16 14:18:04 -0700 | [diff] [blame] | 127 | argc = parse_options(argc, argv, prefix, bugreport_options, |
| 128 | bugreport_usage, 0); |
| 129 | |
Emily Shaffer | 681c0a2 | 2023-10-26 11:22:31 -0700 | [diff] [blame] | 130 | if (argc) { |
| 131 | error(_("unknown argument `%s'"), argv[0]); |
| 132 | usage(bugreport_usage[0]); |
| 133 | } |
| 134 | |
Emily Shaffer | 238b439 | 2020-04-16 14:18:04 -0700 | [diff] [blame] | 135 | /* Prepare the path to put the result */ |
Andrzej Hunt | 4fa2687 | 2021-04-25 14:16:13 +0000 | [diff] [blame] | 136 | prefixed_filename = prefix_filename(prefix, |
| 137 | option_output ? option_output : ""); |
| 138 | strbuf_addstr(&report_path, prefixed_filename); |
Emily Shaffer | 238b439 | 2020-04-16 14:18:04 -0700 | [diff] [blame] | 139 | strbuf_complete(&report_path, '/'); |
Victoria Dye | aac0e8f | 2022-08-12 20:10:17 +0000 | [diff] [blame] | 140 | output_path_len = report_path.len; |
Emily Shaffer | 238b439 | 2020-04-16 14:18:04 -0700 | [diff] [blame] | 141 | |
Jiamu Sun | b3b57c6 | 2024-03-14 04:00:16 +0000 | [diff] [blame] | 142 | strbuf_addstr(&report_path, "git-bugreport"); |
| 143 | if (option_suffix) { |
| 144 | strbuf_addch(&report_path, '-'); |
| 145 | strbuf_addftime(&report_path, option_suffix, localtime_r(&now, &tm), 0, 0); |
| 146 | } |
Emily Shaffer | 238b439 | 2020-04-16 14:18:04 -0700 | [diff] [blame] | 147 | strbuf_addstr(&report_path, ".txt"); |
| 148 | |
| 149 | switch (safe_create_leading_directories(report_path.buf)) { |
| 150 | case SCLD_OK: |
| 151 | case SCLD_EXISTS: |
| 152 | break; |
| 153 | default: |
| 154 | die(_("could not create leading directories for '%s'"), |
| 155 | report_path.buf); |
| 156 | } |
| 157 | |
Victoria Dye | aac0e8f | 2022-08-12 20:10:17 +0000 | [diff] [blame] | 158 | /* Prepare diagnostics, if requested */ |
| 159 | if (diagnose != DIAGNOSE_NONE) { |
| 160 | struct strbuf zip_path = STRBUF_INIT; |
| 161 | strbuf_add(&zip_path, report_path.buf, output_path_len); |
| 162 | strbuf_addstr(&zip_path, "git-diagnostics-"); |
| 163 | strbuf_addftime(&zip_path, option_suffix, localtime_r(&now, &tm), 0, 0); |
| 164 | strbuf_addstr(&zip_path, ".zip"); |
| 165 | |
| 166 | if (create_diagnostics_archive(&zip_path, diagnose)) |
| 167 | die_errno(_("unable to create diagnostics archive %s"), zip_path.buf); |
| 168 | |
| 169 | strbuf_release(&zip_path); |
| 170 | } |
| 171 | |
Emily Shaffer | 238b439 | 2020-04-16 14:18:04 -0700 | [diff] [blame] | 172 | /* Prepare the report contents */ |
| 173 | get_bug_template(&buffer); |
| 174 | |
Emily Shaffer | 617d571 | 2020-04-16 14:18:05 -0700 | [diff] [blame] | 175 | get_header(&buffer, _("System Info")); |
| 176 | get_system_info(&buffer); |
| 177 | |
Emily Shaffer | 788a776 | 2020-05-07 17:53:57 -0700 | [diff] [blame] | 178 | get_header(&buffer, _("Enabled Hooks")); |
Jeff King | d7a5649 | 2020-08-13 10:59:36 -0400 | [diff] [blame] | 179 | get_populated_hooks(&buffer, !startup_info->have_repository); |
Emily Shaffer | 788a776 | 2020-05-07 17:53:57 -0700 | [diff] [blame] | 180 | |
Emily Shaffer | 238b439 | 2020-04-16 14:18:04 -0700 | [diff] [blame] | 181 | /* fopen doesn't offer us an O_EXCL alternative, except with glibc. */ |
René Scharfe | 66e905b | 2021-08-25 22:16:46 +0200 | [diff] [blame] | 182 | report = xopen(report_path.buf, O_CREAT | O_EXCL | O_WRONLY, 0666); |
Emily Shaffer | 238b439 | 2020-04-16 14:18:04 -0700 | [diff] [blame] | 183 | |
Randall S. Becker | f64b6a1 | 2020-06-19 16:23:19 -0400 | [diff] [blame] | 184 | if (write_in_full(report, buffer.buf, buffer.len) < 0) |
| 185 | die_errno(_("unable to write to %s"), report_path.buf); |
| 186 | |
Emily Shaffer | 238b439 | 2020-04-16 14:18:04 -0700 | [diff] [blame] | 187 | close(report); |
| 188 | |
| 189 | /* |
| 190 | * We want to print the path relative to the user, but we still need the |
| 191 | * path relative to us to give to the editor. |
| 192 | */ |
| 193 | if (!(prefix && skip_prefix(report_path.buf, prefix, &user_relative_path))) |
| 194 | user_relative_path = report_path.buf; |
| 195 | fprintf(stderr, _("Created new report at '%s'.\n"), |
| 196 | user_relative_path); |
| 197 | |
Andrzej Hunt | 4fa2687 | 2021-04-25 14:16:13 +0000 | [diff] [blame] | 198 | free(prefixed_filename); |
Ævar Arnfjörð Bjarmason | ac95f5d | 2022-11-08 19:17:51 +0100 | [diff] [blame] | 199 | strbuf_release(&buffer); |
| 200 | |
| 201 | ret = !!launch_editor(report_path.buf, NULL, NULL); |
| 202 | strbuf_release(&report_path); |
| 203 | return ret; |
Emily Shaffer | 238b439 | 2020-04-16 14:18:04 -0700 | [diff] [blame] | 204 | } |