Coverage Report

Created: 2024-09-08 06:23

/src/git/builtin/diagnose.c
Line
Count
Source (jump to first uncovered line)
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"
7
8
static const char * const diagnose_usage[] = {
9
  N_("git diagnose [(-o | --output-directory) <path>] [(-s | --suffix) <format>]\n"
10
     "             [--mode=<mode>]"),
11
  NULL
12
};
13
14
int cmd_diagnose(int argc, const char **argv, const char *prefix)
15
0
{
16
0
  struct strbuf zip_path = STRBUF_INIT;
17
0
  time_t now = time(NULL);
18
0
  struct tm tm;
19
0
  enum diagnose_mode mode = DIAGNOSE_STATS;
20
0
  char *option_output = NULL;
21
0
  const char *option_suffix = "%Y-%m-%d-%H%M";
22
0
  char *prefixed_filename;
23
24
0
  const struct option diagnose_options[] = {
25
0
    OPT_STRING('o', "output-directory", &option_output, N_("path"),
26
0
         N_("specify a destination for the diagnostics archive")),
27
0
    OPT_STRING('s', "suffix", &option_suffix, N_("format"),
28
0
         N_("specify a strftime format suffix for the filename")),
29
0
    OPT_CALLBACK_F(0, "mode", &mode, "(stats|all)",
30
0
             N_("specify the content of the diagnostic archive"),
31
0
             PARSE_OPT_NONEG, option_parse_diagnose),
32
0
    OPT_END()
33
0
  };
34
35
0
  argc = parse_options(argc, argv, prefix, diagnose_options,
36
0
           diagnose_usage, 0);
37
38
  /* Prepare the path to put the result */
39
0
  prefixed_filename = prefix_filename(prefix,
40
0
              option_output ? option_output : "");
41
0
  strbuf_addstr(&zip_path, prefixed_filename);
42
0
  strbuf_complete(&zip_path, '/');
43
44
0
  strbuf_addstr(&zip_path, "git-diagnostics-");
45
0
  strbuf_addftime(&zip_path, option_suffix, localtime_r(&now, &tm), 0, 0);
46
0
  strbuf_addstr(&zip_path, ".zip");
47
48
0
  switch (safe_create_leading_directories(zip_path.buf)) {
49
0
  case SCLD_OK:
50
0
  case SCLD_EXISTS:
51
0
    break;
52
0
  default:
53
0
    die_errno(_("could not create leading directories for '%s'"),
54
0
        zip_path.buf);
55
0
  }
56
57
  /* Prepare diagnostics */
58
0
  if (create_diagnostics_archive(&zip_path, mode))
59
0
    die_errno(_("unable to create diagnostics archive %s"),
60
0
        zip_path.buf);
61
62
0
  free(prefixed_filename);
63
0
  strbuf_release(&zip_path);
64
0
  return 0;
65
0
}