@matheusbsbs / matheus-bagliano / commits / 788a776069
Fork 0

bugreport: collect list of populated hooks

Occasionally a failure a user is seeing may be related to a specific hook which is being run, perhaps without the user realizing. While the contents of hooks can be sensitive - containing user data or process information specific to the user's organization - simply knowing that a hook is being run at a certain stage can help us to understand whether something is going wrong. Without a definitive list of hook names within the code, we compile our own list from the documentation. This is likely prone to bitrot, but designing a single source of truth for acceptable hooks is too much overhead for this small change to the bugreport tool. Signed-off-by: Emily Shaffer <emilyshaffer@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Emily Shaffer committed May 7, 2020 at 17:53 UTC 788a776069bffcbb7d0dd9b70d521826f97285b7
3 files changed +68
Documentation/git-bugreport.txt
+1
@@ -28,6 +28,7 @@ The following information is captured automatically:
28 - 'git version --build-options'
29 - uname sysname, release, version, and machine strings
30 - Compiler-specific info string
31 + - A list of enabled hooks
32
33 This tool is invoked via the typical Git setup process, which means that in some
34 cases, it might not be able to launch - for example, if a relevant config file
bugreport.c
+52
@@ -3,6 +3,8 @@
3 #include "strbuf.h"
4 #include "help.h"
5 #include "compat/compiler.h"
6 +#include "run-command.h"
7 +
8
9 static void get_system_info(struct strbuf *sys_info)
10 {
@@ -31,6 +33,53 @@ static void get_system_info(struct strbuf *sys_info)
33 get_libc_info(sys_info);
34 }
35
36 +static void get_populated_hooks(struct strbuf *hook_info, int nongit)
37 +{
38 + /*
39 + * NEEDSWORK: Doesn't look like there is a list of all possible hooks;
40 + * so below is a transcription of `git help hooks`. Later, this should
41 + * be replaced with some programmatically generated list (generated from
42 + * doc or else taken from some library which tells us about all the
43 + * hooks)
44 + */
45 + static const char *hook[] = {
46 + "applypatch-msg",
47 + "pre-applypatch",
48 + "post-applypatch",
49 + "pre-commit",
50 + "pre-merge-commit",
51 + "prepare-commit-msg",
52 + "commit-msg",
53 + "post-commit",
54 + "pre-rebase",
55 + "post-checkout",
56 + "post-merge",
57 + "pre-push",
58 + "pre-receive",
59 + "update",
60 + "post-receive",
61 + "post-update",
62 + "push-to-checkout",
63 + "pre-auto-gc",
64 + "post-rewrite",
65 + "sendemail-validate",
66 + "fsmonitor-watchman",
67 + "p4-pre-submit",
68 + "post-index-change",
69 + };
70 + int i;
71 +
72 + if (nongit) {
73 + strbuf_addstr(hook_info,
74 + _("not run from a git repository - no hooks to show\n"));
75 + return;
76 + }
77 +
78 + for (i = 0; i < ARRAY_SIZE(hook); i++)
79 + if (find_hook(hook[i]))
80 + strbuf_addf(hook_info, "%s\n", hook[i]);
81 +}
82 +
83 static const char * const bugreport_usage[] = {
84 N_("git bugreport [-o|--output-directory <file>] [-s|--suffix <format>]"),
85 NULL
@@ -114,6 +163,9 @@ int cmd_main(int argc, const char **argv)
163 get_header(&buffer, _("System Info"));
164 get_system_info(&buffer);
165
166 + get_header(&buffer, _("Enabled Hooks"));
167 + get_populated_hooks(&buffer, nongit_ok);
168 +
169 /* fopen doesn't offer us an O_EXCL alternative, except with glibc. */
170 report = open(report_path.buf, O_CREAT | O_EXCL | O_WRONLY, 0666);
171
t/t0091-bugreport.sh
+15
@@ -57,5 +57,20 @@ test_expect_success 'can create leading directories outside of a git dir' '
57 nongit git bugreport -o foo/bar/baz
58 '
59
60 +test_expect_success 'indicates populated hooks' '
61 + test_when_finished rm git-bugreport-hooks.txt &&
62 + test_when_finished rm -fr .git/hooks &&
63 + rm -fr .git/hooks &&
64 + mkdir .git/hooks &&
65 + for hook in applypatch-msg prepare-commit-msg.sample
66 + do
67 + write_script ".git/hooks/$hook" <<-EOF || return 1
68 + echo "hook $hook exists"
69 + EOF
70 + done &&
71 + git bugreport -s hooks &&
72 + grep applypatch-msg git-bugreport-hooks.txt &&
73 + ! grep prepare-commit-msg git-bugreport-hooks.txt
74 +'
75
76 test_done