|
| 1 | +#include "builtin.h" |
| 2 | +#include "parse-options.h" |
| 3 | +#include "diagnose.h" |
| 4 | + |
| 5 | +static const char * const diagnose_usage[] = { |
| 6 | + N_("git diagnose [-o|--output-directory <path>] [-s|--suffix <format>]"), |
| 7 | + NULL |
| 8 | +}; |
| 9 | + |
| 10 | +int cmd_diagnose(int argc, const char **argv, const char *prefix) |
| 11 | +{ |
| 12 | + struct strbuf zip_path = STRBUF_INIT; |
| 13 | + time_t now = time(NULL); |
| 14 | + struct tm tm; |
| 15 | + char *option_output = NULL; |
| 16 | + char *option_suffix = "%Y-%m-%d-%H%M"; |
| 17 | + char *prefixed_filename; |
| 18 | + |
| 19 | + const struct option diagnose_options[] = { |
| 20 | + OPT_STRING('o', "output-directory", &option_output, N_("path"), |
| 21 | + N_("specify a destination for the diagnostics archive")), |
| 22 | + OPT_STRING('s', "suffix", &option_suffix, N_("format"), |
| 23 | + N_("specify a strftime format suffix for the filename")), |
| 24 | + OPT_END() |
| 25 | + }; |
| 26 | + |
| 27 | + argc = parse_options(argc, argv, prefix, diagnose_options, |
| 28 | + diagnose_usage, 0); |
| 29 | + |
| 30 | + /* Prepare the path to put the result */ |
| 31 | + prefixed_filename = prefix_filename(prefix, |
| 32 | + option_output ? option_output : ""); |
| 33 | + strbuf_addstr(&zip_path, prefixed_filename); |
| 34 | + strbuf_complete(&zip_path, '/'); |
| 35 | + |
| 36 | + strbuf_addstr(&zip_path, "git-diagnostics-"); |
| 37 | + strbuf_addftime(&zip_path, option_suffix, localtime_r(&now, &tm), 0, 0); |
| 38 | + strbuf_addstr(&zip_path, ".zip"); |
| 39 | + |
| 40 | + switch (safe_create_leading_directories(zip_path.buf)) { |
| 41 | + case SCLD_OK: |
| 42 | + case SCLD_EXISTS: |
| 43 | + break; |
| 44 | + default: |
| 45 | + die_errno(_("could not create leading directories for '%s'"), |
| 46 | + zip_path.buf); |
| 47 | + } |
| 48 | + |
| 49 | + /* Prepare diagnostics */ |
| 50 | + if (create_diagnostics_archive(&zip_path, DIAGNOSE_STATS)) |
| 51 | + die_errno(_("unable to create diagnostics archive %s"), |
| 52 | + zip_path.buf); |
| 53 | + |
| 54 | + free(prefixed_filename); |
| 55 | + strbuf_release(&zip_path); |
| 56 | + return 0; |
| 57 | +} |
0 commit comments