Skip to content

Commit aac0e8f

Browse files
vdyegitster
authored andcommitted
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 <[email protected]> Helped-by: Derrick Stolee <[email protected]> Signed-off-by: Victoria Dye <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7ecf193 commit aac0e8f

File tree

3 files changed

+90
-3
lines changed

3 files changed

+90
-3
lines changed

Documentation/git-bugreport.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ SYNOPSIS
99
--------
1010
[verse]
1111
'git bugreport' [(-o | --output-directory) <path>] [(-s | --suffix) <format>]
12+
[--diagnose[=<mode>]]
1213

1314
DESCRIPTION
1415
-----------
@@ -31,6 +32,10 @@ The following information is captured automatically:
3132
- A list of enabled hooks
3233
- $SHELL
3334

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+
3439
This tool is invoked via the typical Git setup process, which means that in some
3540
cases, it might not be able to launch - for example, if a relevant config file
3641
is unreadable. In this kind of scenario, it may be helpful to manually gather
@@ -49,6 +54,19 @@ OPTIONS
4954
named 'git-bugreport-<formatted suffix>'. This should take the form of a
5055
strftime(3) format string; the current local time will be used.
5156

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+
5270
GIT
5371
---
5472
Part of the linkgit:git[1] suite

builtin/bugreport.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "compat/compiler.h"
66
#include "hook.h"
77
#include "hook-list.h"
8+
#include "diagnose.h"
89

910

1011
static void get_system_info(struct strbuf *sys_info)
@@ -59,7 +60,7 @@ static void get_populated_hooks(struct strbuf *hook_info, int nongit)
5960
}
6061

6162
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>]"),
6364
NULL
6465
};
6566

@@ -98,16 +99,21 @@ int cmd_bugreport(int argc, const char **argv, const char *prefix)
9899
int report = -1;
99100
time_t now = time(NULL);
100101
struct tm tm;
102+
enum diagnose_mode diagnose = DIAGNOSE_NONE;
101103
char *option_output = NULL;
102104
char *option_suffix = "%Y-%m-%d-%H%M";
103105
const char *user_relative_path = NULL;
104106
char *prefixed_filename;
107+
size_t output_path_len;
105108

106109
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),
107113
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)")),
109115
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)")),
111117
OPT_END()
112118
};
113119

@@ -119,6 +125,7 @@ int cmd_bugreport(int argc, const char **argv, const char *prefix)
119125
option_output ? option_output : "");
120126
strbuf_addstr(&report_path, prefixed_filename);
121127
strbuf_complete(&report_path, '/');
128+
output_path_len = report_path.len;
122129

123130
strbuf_addstr(&report_path, "git-bugreport-");
124131
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)
133140
report_path.buf);
134141
}
135142

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+
136157
/* Prepare the report contents */
137158
get_bug_template(&buffer);
138159

t/t0091-bugreport.sh

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,52 @@ test_expect_success 'indicates populated hooks' '
7878
test_cmp expect actual
7979
'
8080

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+
81129
test_done

0 commit comments

Comments
 (0)