builtin/bugreport.c: create '--diagnose' option

Create a '--diagnose' option for 'git bugreport' to collect additional information about the repository and write it to a zipped archive. The '--diagnose' option behaves effectively as an alias for simultaneously running 'git bugreport' and 'git diagnose'. In the documentation, users are explicitly recommended to attach the diagnostics alongside a bug report to provide additional context to readers, ideally reducing some back-and-forth between reporters and those debugging the issue. Note that '--diagnose' may take an optional string arg (either 'stats' or 'all'). If specified without the arg, the behavior corresponds to running 'git diagnose' without '--mode'. As with 'git diagnose', this default is intended to help reduce unintentional leaking of sensitive information). Users can also explicitly specify '--diagnose=(stats|all)' to generate the respective archive created by 'git diagnose --mode=(stats|all)'. Suggested-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Helped-by: Derrick Stolee <derrickstolee@github.com> Signed-off-by: Victoria Dye <vdye@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Victoria Dye committed Aug 12, 2022 at 20:10 UTC aac0e8ffeeed57cae2a047fd442f2125cbfc8e39
3 files changed +90 -3
Documentation/git-bugreport.txt
+18
@@ -9,6 +9,7 @@ SYNOPSIS
9 --------
10 [verse]
11 'git bugreport' [(-o | --output-directory) <path>] [(-s | --suffix) <format>]
12 + [--diagnose[=<mode>]]
13
14 DESCRIPTION
15 -----------
@@ -31,6 +32,10 @@ The following information is captured automatically:
32 - A list of enabled hooks
33 - $SHELL
34
35 +Additional information may be gathered into a separate zip archive using the
36 +`--diagnose` option, and can be attached alongside the bugreport document to
37 +provide additional context to readers.
38 +
39 This tool is invoked via the typical Git setup process, which means that in some
40 cases, it might not be able to launch - for example, if a relevant config file
41 is unreadable. In this kind of scenario, it may be helpful to manually gather
@@ -49,6 +54,19 @@ OPTIONS
54 named 'git-bugreport-<formatted suffix>'. This should take the form of a
55 strftime(3) format string; the current local time will be used.
56
57 +--no-diagnose::
58 +--diagnose[=<mode>]::
59 + Create a zip archive of supplemental information about the user's
60 + machine, Git client, and repository state. The archive is written to the
61 + same output directory as the bug report and is named
62 + 'git-diagnostics-<formatted suffix>'.
63 ++
64 +Without `mode` specified, the diagnostic archive will contain the default set of
65 +statistics reported by `git diagnose`. An optional `mode` value may be specified
66 +to change which information is included in the archive. See
67 +linkgit:git-diagnose[1] for the list of valid values for `mode` and details
68 +about their usage.
69 +
70 GIT
71 ---
72 Part of the linkgit:git[1] suite
builtin/bugreport.c
+24 -3
@@ -5,6 +5,7 @@
5 #include "compat/compiler.h"
6 #include "hook.h"
7 #include "hook-list.h"
8 +#include "diagnose.h"
9
10
11 static void get_system_info(struct strbuf *sys_info)
@@ -59,7 +60,7 @@ static void get_populated_hooks(struct strbuf *hook_info, int nongit)
60 }
61
62 static const char * const bugreport_usage[] = {
62 - N_("git bugreport [-o|--output-directory <file>] [-s|--suffix <format>]"),
63 + N_("git bugreport [-o|--output-directory <file>] [-s|--suffix <format>] [--diagnose[=<mode>]"),
64 NULL
65 };
66
@@ -98,16 +99,21 @@ int cmd_bugreport(int argc, const char **argv, const char *prefix)
99 int report = -1;
100 time_t now = time(NULL);
101 struct tm tm;
102 + enum diagnose_mode diagnose = DIAGNOSE_NONE;
103 char *option_output = NULL;
104 char *option_suffix = "%Y-%m-%d-%H%M";
105 const char *user_relative_path = NULL;
106 char *prefixed_filename;
107 + size_t output_path_len;
108
109 const struct option bugreport_options[] = {
110 + OPT_CALLBACK_F(0, "diagnose", &diagnose, N_("mode"),
111 + N_("create an additional zip archive of detailed diagnostics (default 'stats')"),
112 + PARSE_OPT_OPTARG, option_parse_diagnose),
113 OPT_STRING('o', "output-directory", &option_output, N_("path"),
108 - N_("specify a destination for the bugreport file")),
114 + N_("specify a destination for the bugreport file(s)")),
115 OPT_STRING('s', "suffix", &option_suffix, N_("format"),
110 - N_("specify a strftime format suffix for the filename")),
116 + N_("specify a strftime format suffix for the filename(s)")),
117 OPT_END()
118 };
119
@@ -119,6 +125,7 @@ int cmd_bugreport(int argc, const char **argv, const char *prefix)
125 option_output ? option_output : "");
126 strbuf_addstr(&report_path, prefixed_filename);
127 strbuf_complete(&report_path, '/');
128 + output_path_len = report_path.len;
129
130 strbuf_addstr(&report_path, "git-bugreport-");
131 strbuf_addftime(&report_path, option_suffix, localtime_r(&now, &tm), 0, 0);
@@ -133,6 +140,20 @@ int cmd_bugreport(int argc, const char **argv, const char *prefix)
140 report_path.buf);
141 }
142
143 + /* Prepare diagnostics, if requested */
144 + if (diagnose != DIAGNOSE_NONE) {
145 + struct strbuf zip_path = STRBUF_INIT;
146 + strbuf_add(&zip_path, report_path.buf, output_path_len);
147 + strbuf_addstr(&zip_path, "git-diagnostics-");
148 + strbuf_addftime(&zip_path, option_suffix, localtime_r(&now, &tm), 0, 0);
149 + strbuf_addstr(&zip_path, ".zip");
150 +
151 + if (create_diagnostics_archive(&zip_path, diagnose))
152 + die_errno(_("unable to create diagnostics archive %s"), zip_path.buf);
153 +
154 + strbuf_release(&zip_path);
155 + }
156 +
157 /* Prepare the report contents */
158 get_bug_template(&buffer);
159
t/t0091-bugreport.sh
+48
@@ -78,4 +78,52 @@ test_expect_success 'indicates populated hooks' '
78 test_cmp expect actual
79 '
80
81 +test_expect_success UNZIP '--diagnose creates diagnostics zip archive' '
82 + test_when_finished rm -rf report &&
83 +
84 + git bugreport --diagnose -o report -s test >out &&
85 +
86 + zip_path=report/git-diagnostics-test.zip &&
87 + grep "Available space" out &&
88 + test_path_is_file "$zip_path" &&
89 +
90 + # Check zipped archive content
91 + "$GIT_UNZIP" -p "$zip_path" diagnostics.log >out &&
92 + test_file_not_empty out &&
93 +
94 + "$GIT_UNZIP" -p "$zip_path" packs-local.txt >out &&
95 + grep ".git/objects" out &&
96 +
97 + "$GIT_UNZIP" -p "$zip_path" objects-local.txt >out &&
98 + grep "^Total: [0-9][0-9]*" out &&
99 +
100 + # Should not include .git directory contents by default
101 + ! "$GIT_UNZIP" -l "$zip_path" | grep ".git/"
102 +'
103 +
104 +test_expect_success UNZIP '--diagnose=stats excludes .git dir contents' '
105 + test_when_finished rm -rf report &&
106 +
107 + git bugreport --diagnose=stats -o report -s test >out &&
108 +
109 + # Includes pack quantity/size info
110 + "$GIT_UNZIP" -p "$zip_path" packs-local.txt >out &&
111 + grep ".git/objects" out &&
112 +
113 + # Does not include .git directory contents
114 + ! "$GIT_UNZIP" -l "$zip_path" | grep ".git/"
115 +'
116 +
117 +test_expect_success UNZIP '--diagnose=all includes .git dir contents' '
118 + test_when_finished rm -rf report &&
119 +
120 + git bugreport --diagnose=all -o report -s test >out &&
121 +
122 + # Includes .git directory contents
123 + "$GIT_UNZIP" -l "$zip_path" | grep ".git/" &&
124 +
125 + "$GIT_UNZIP" -p "$zip_path" .git/HEAD >out &&
126 + test_file_not_empty out
127 +'
128 +
129 test_done