Git fork
at reftables-rust 70 lines 2.1 kB view raw
1#define USE_THE_REPOSITORY_VARIABLE 2 3#include "builtin.h" 4#include "abspath.h" 5#include "gettext.h" 6#include "parse-options.h" 7#include "path.h" 8#include "diagnose.h" 9 10static const char * const diagnose_usage[] = { 11 N_("git diagnose [(-o | --output-directory) <path>] [(-s | --suffix) <format>]\n" 12 " [--mode=<mode>]"), 13 NULL 14}; 15 16int cmd_diagnose(int argc, 17 const char **argv, 18 const char *prefix, 19 struct repository *repo UNUSED) 20{ 21 struct strbuf zip_path = STRBUF_INIT; 22 time_t now = time(NULL); 23 struct tm tm; 24 enum diagnose_mode mode = DIAGNOSE_STATS; 25 char *option_output = NULL; 26 const char *option_suffix = "%Y-%m-%d-%H%M"; 27 char *prefixed_filename; 28 29 const struct option diagnose_options[] = { 30 OPT_STRING('o', "output-directory", &option_output, N_("path"), 31 N_("specify a destination for the diagnostics archive")), 32 OPT_STRING('s', "suffix", &option_suffix, N_("format"), 33 N_("specify a strftime format suffix for the filename")), 34 OPT_CALLBACK_F(0, "mode", &mode, "(stats|all)", 35 N_("specify the content of the diagnostic archive"), 36 PARSE_OPT_NONEG, option_parse_diagnose), 37 OPT_END() 38 }; 39 40 argc = parse_options(argc, argv, prefix, diagnose_options, 41 diagnose_usage, 0); 42 43 /* Prepare the path to put the result */ 44 prefixed_filename = prefix_filename(prefix, 45 option_output ? option_output : ""); 46 strbuf_addstr(&zip_path, prefixed_filename); 47 strbuf_complete(&zip_path, '/'); 48 49 strbuf_addstr(&zip_path, "git-diagnostics-"); 50 strbuf_addftime(&zip_path, option_suffix, localtime_r(&now, &tm), 0, 0); 51 strbuf_addstr(&zip_path, ".zip"); 52 53 switch (safe_create_leading_directories(the_repository, zip_path.buf)) { 54 case SCLD_OK: 55 case SCLD_EXISTS: 56 break; 57 default: 58 die_errno(_("could not create leading directories for '%s'"), 59 zip_path.buf); 60 } 61 62 /* Prepare diagnostics */ 63 if (create_diagnostics_archive(the_repository, &zip_path, mode)) 64 die_errno(_("unable to create diagnostics archive %s"), 65 zip_path.buf); 66 67 free(prefixed_filename); 68 strbuf_release(&zip_path); 69 return 0; 70}