]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/diagnose.c
Merge branch 'jk/clone-allow-bare-and-o-together'
[thirdparty/git.git] / builtin / diagnose.c
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>] [--mode=<mode>]"),
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 enum diagnose_mode mode = DIAGNOSE_STATS;
16 char *option_output = NULL;
17 char *option_suffix = "%Y-%m-%d-%H%M";
18 char *prefixed_filename;
19
20 const struct option diagnose_options[] = {
21 OPT_STRING('o', "output-directory", &option_output, N_("path"),
22 N_("specify a destination for the diagnostics archive")),
23 OPT_STRING('s', "suffix", &option_suffix, N_("format"),
24 N_("specify a strftime format suffix for the filename")),
25 OPT_CALLBACK_F(0, "mode", &mode, "(stats|all)",
26 N_("specify the content of the diagnostic archive"),
27 PARSE_OPT_NONEG, option_parse_diagnose),
28 OPT_END()
29 };
30
31 argc = parse_options(argc, argv, prefix, diagnose_options,
32 diagnose_usage, 0);
33
34 /* Prepare the path to put the result */
35 prefixed_filename = prefix_filename(prefix,
36 option_output ? option_output : "");
37 strbuf_addstr(&zip_path, prefixed_filename);
38 strbuf_complete(&zip_path, '/');
39
40 strbuf_addstr(&zip_path, "git-diagnostics-");
41 strbuf_addftime(&zip_path, option_suffix, localtime_r(&now, &tm), 0, 0);
42 strbuf_addstr(&zip_path, ".zip");
43
44 switch (safe_create_leading_directories(zip_path.buf)) {
45 case SCLD_OK:
46 case SCLD_EXISTS:
47 break;
48 default:
49 die_errno(_("could not create leading directories for '%s'"),
50 zip_path.buf);
51 }
52
53 /* Prepare diagnostics */
54 if (create_diagnostics_archive(&zip_path, mode))
55 die_errno(_("unable to create diagnostics archive %s"),
56 zip_path.buf);
57
58 free(prefixed_filename);
59 strbuf_release(&zip_path);
60 return 0;
61 }