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