|
| 1 | +#include "builtin.h" |
| 2 | +#include "parse-options.h" |
| 3 | +#include "stdio.h" |
| 4 | +#include "strbuf.h" |
| 5 | +#include "time.h" |
| 6 | + |
| 7 | +static const char * const bugreport_usage[] = { |
| 8 | + N_("git bugreport [-o|--output-directory <file>] [-s|--suffix <format>]"), |
| 9 | + NULL |
| 10 | +}; |
| 11 | + |
| 12 | +static int get_bug_template(struct strbuf *template) |
| 13 | +{ |
| 14 | + const char template_text[] = N_( |
| 15 | +"Thank you for filling out a Git bug report!\n" |
| 16 | +"Please answer the following questions to help us understand your issue.\n" |
| 17 | +"\n" |
| 18 | +"What did you do before the bug happened? (Steps to reproduce your issue)\n" |
| 19 | +"\n" |
| 20 | +"What did you expect to happen? (Expected behavior)\n" |
| 21 | +"\n" |
| 22 | +"What happened instead? (Actual behavior)\n" |
| 23 | +"\n" |
| 24 | +"What's different between what you expected and what actually happened?\n" |
| 25 | +"\n" |
| 26 | +"Anything else you want to add:\n" |
| 27 | +"\n" |
| 28 | +"Please review the rest of the bug report below.\n" |
| 29 | +"You can delete any lines you don't wish to share.\n"); |
| 30 | + |
| 31 | + strbuf_addstr(template, template_text); |
| 32 | + return 0; |
| 33 | +} |
| 34 | + |
| 35 | +int cmd_main(int argc, const char **argv) |
| 36 | +{ |
| 37 | + struct strbuf buffer = STRBUF_INIT; |
| 38 | + struct strbuf report_path = STRBUF_INIT; |
| 39 | + FILE *report; |
| 40 | + time_t now = time(NULL); |
| 41 | + char *option_output = NULL; |
| 42 | + char *option_suffix = "%F-%H%M"; |
| 43 | + struct stat statbuf; |
| 44 | + |
| 45 | + const struct option bugreport_options[] = { |
| 46 | + OPT_STRING('o', "output-directory", &option_output, N_("path"), |
| 47 | + N_("specify a destination for the bugreport file")), |
| 48 | + OPT_STRING('s', "suffix", &option_suffix, N_("format"), |
| 49 | + N_("specify a strftime format suffix for the filename")), |
| 50 | + OPT_END() |
| 51 | + }; |
| 52 | + argc = parse_options(argc, argv, "", bugreport_options, |
| 53 | + bugreport_usage, 0); |
| 54 | + |
| 55 | + if (option_output) { |
| 56 | + strbuf_addstr(&report_path, option_output); |
| 57 | + strbuf_complete(&report_path, '/'); |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + strbuf_addstr(&report_path, "git-bugreport-"); |
| 62 | + strbuf_addftime(&report_path, option_suffix, localtime(&now), 0, 0); |
| 63 | + strbuf_addstr(&report_path, ".txt"); |
| 64 | + |
| 65 | + if (!stat(report_path.buf, &statbuf)) |
| 66 | + die("'%s' already exists", report_path.buf); |
| 67 | + |
| 68 | + switch (safe_create_leading_directories(report_path.buf)) { |
| 69 | + case SCLD_OK: |
| 70 | + case SCLD_EXISTS: |
| 71 | + break; |
| 72 | + default: |
| 73 | + die(_("could not create leading directories for '%s'"), |
| 74 | + report_path.buf); |
| 75 | + } |
| 76 | + |
| 77 | + get_bug_template(&buffer); |
| 78 | + |
| 79 | + report = fopen_for_writing(report_path.buf); |
| 80 | + |
| 81 | + if (report == NULL) { |
| 82 | + strbuf_release(&report_path); |
| 83 | + die("couldn't open '%s' for writing", report_path.buf); |
| 84 | + } |
| 85 | + |
| 86 | + strbuf_write(&buffer, report); |
| 87 | + fclose(report); |
| 88 | + |
| 89 | + fprintf(stderr, _("Created new report at '%s'.\n"), report_path.buf); |
| 90 | + |
| 91 | + UNLEAK(buffer); |
| 92 | + UNLEAK(report_path); |
| 93 | + return !!launch_editor(report_path.buf, NULL, NULL); |
| 94 | +} |
0 commit comments