@matheusbsbs / matheus-bagliano / commits / 617d57195a
Fork 0

bugreport: gather git version and build info

Knowing which version of Git a user has and how it was built allows us to more precisely pin down the circumstances when a certain issue occurs, so teach bugreport how to tell us the same output as 'git version --build-options'. It's not ideal to directly call 'git version --build-options' because that output goes to stdout. Instead, wrap the version string in a helper within help.[ch] library, and call that helper from within the bugreport library. Signed-off-by: Emily Shaffer <emilyshaffer@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Emily Shaffer committed Apr 16, 2020 at 14:18 UTC 617d57195ae844a43486b2e0ad0b7ac36aaddfdd
4 files changed +51 -19
Documentation/git-bugreport.txt
+4
@@ -23,6 +23,10 @@ The following information is requested from the user:
23 - Expected behavior
24 - Actual behavior
25
26 +The following information is captured automatically:
27 +
28 + - 'git version --build-options'
29 +
30 This tool is invoked via the typical Git setup process, which means that in some
31 cases, it might not be able to launch - for example, if a relevant config file
32 is unreadable. In this kind of scenario, it may be helpful to manually gather
bugreport.c
+18 -1
@@ -1,8 +1,17 @@
1 -#include "builtin.h"
1 +#include "cache.h"
2 #include "parse-options.h"
3 #include "stdio.h"
4 #include "strbuf.h"
5 #include "time.h"
6 +#include "help.h"
7 +
8 +static void get_system_info(struct strbuf *sys_info)
9 +{
10 + /* get git version from native cmd */
11 + strbuf_addstr(sys_info, _("git version:\n"));
12 + get_version_info(sys_info, 1);
13 + strbuf_complete_line(sys_info);
14 +}
15
16 static const char * const bugreport_usage[] = {
17 N_("git bugreport [-o|--output-directory <file>] [-s|--suffix <format>]"),
@@ -32,6 +41,11 @@ static int get_bug_template(struct strbuf *template)
41 return 0;
42 }
43
44 +static void get_header(struct strbuf *buf, const char *title)
45 +{
46 + strbuf_addf(buf, "\n\n[%s]\n", title);
47 +}
48 +
49 int cmd_main(int argc, const char **argv)
50 {
51 struct strbuf buffer = STRBUF_INIT;
@@ -79,6 +93,9 @@ int cmd_main(int argc, const char **argv)
93 /* Prepare the report contents */
94 get_bug_template(&buffer);
95
96 + get_header(&buffer, _("System Info"));
97 + get_system_info(&buffer);
98 +
99 /* fopen doesn't offer us an O_EXCL alternative, except with glibc. */
100 report = open(report_path.buf, O_CREAT | O_EXCL | O_WRONLY, 0666);
101
help.c
+28 -18
@@ -622,8 +622,32 @@ const char *help_unknown_cmd(const char *cmd)
622 exit(1);
623 }
624
625 +void get_version_info(struct strbuf *buf, int show_build_options)
626 +{
627 + /*
628 + * The format of this string should be kept stable for compatibility
629 + * with external projects that rely on the output of "git version".
630 + *
631 + * Always show the version, even if other options are given.
632 + */
633 + strbuf_addf(buf, "git version %s\n", git_version_string);
634 +
635 + if (show_build_options) {
636 + strbuf_addf(buf, "cpu: %s\n", GIT_HOST_CPU);
637 + if (git_built_from_commit_string[0])
638 + strbuf_addf(buf, "built from commit: %s\n",
639 + git_built_from_commit_string);
640 + else
641 + strbuf_addstr(buf, "no commit associated with this build\n");
642 + strbuf_addf(buf, "sizeof-long: %d\n", (int)sizeof(long));
643 + strbuf_addf(buf, "sizeof-size_t: %d\n", (int)sizeof(size_t));
644 + /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
645 + }
646 +}
647 +
648 int cmd_version(int argc, const char **argv, const char *prefix)
649 {
650 + struct strbuf buf = STRBUF_INIT;
651 int build_options = 0;
652 const char * const usage[] = {
653 N_("git version [<options>]"),
@@ -637,25 +661,11 @@ int cmd_version(int argc, const char **argv, const char *prefix)
661
662 argc = parse_options(argc, argv, prefix, options, usage, 0);
663
640 - /*
641 - * The format of this string should be kept stable for compatibility
642 - * with external projects that rely on the output of "git version".
643 - *
644 - * Always show the version, even if other options are given.
645 - */
646 - printf("git version %s\n", git_version_string);
664 + get_version_info(&buf, build_options);
665 + printf("%s", buf.buf);
666 +
667 + strbuf_release(&buf);
668
648 - if (build_options) {
649 - printf("cpu: %s\n", GIT_HOST_CPU);
650 - if (git_built_from_commit_string[0])
651 - printf("built from commit: %s\n",
652 - git_built_from_commit_string);
653 - else
654 - printf("no commit associated with this build\n");
655 - printf("sizeof-long: %d\n", (int)sizeof(long));
656 - printf("sizeof-size_t: %d\n", (int)sizeof(size_t));
657 - /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
658 - }
669 return 0;
670 }
671
help.h
+1
@@ -37,6 +37,7 @@ void add_cmdname(struct cmdnames *cmds, const char *name, int len);
37 void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes);
38 int is_in_cmdlist(struct cmdnames *cmds, const char *name);
39 void list_commands(unsigned int colopts, struct cmdnames *main_cmds, struct cmdnames *other_cmds);
40 +void get_version_info(struct strbuf *buf, int show_build_options);
41
42 /*
43 * call this to die(), when it is suspected that the user mistyped a