Skip to content

add git-bugreport tool #566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
/git-bisect--helper
/git-blame
/git-branch
/git-bugreport
/git-bundle
/git-cat-file
/git-check-attr
Expand Down Expand Up @@ -189,6 +190,7 @@
/gitweb/gitweb.cgi
/gitweb/static/gitweb.js
/gitweb/static/gitweb.min.*
/config-list.h
/command-list.h
*.tar.gz
*.dsc
Expand Down
52 changes: 52 additions & 0 deletions Documentation/git-bugreport.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
git-bugreport(1)
================

NAME
----
git-bugreport - Collect information for user to file a bug report

SYNOPSIS
--------
[verse]
'git bugreport' [(-o | --output-directory) <path>] [(-s | --suffix) <format>]

DESCRIPTION
-----------
Captures information about the user's machine, Git client, and repository state,
as well as a form requesting information about the behavior the user observed,
into a single text file which the user can then share, for example to the Git
mailing list, in order to report an observed bug.

The following information is requested from the user:

- Reproduction steps
- Expected behavior
- Actual behavior

The following information is captured automatically:

- 'git version --build-options'
- uname sysname, release, version, and machine strings
- Compiler-specific info string

This tool is invoked via the typical Git setup process, which means that in some
cases, it might not be able to launch - for example, if a relevant config file
is unreadable. In this kind of scenario, it may be helpful to manually gather
the kind of information listed above when manually asking for help.

OPTIONS
-------
-o <path>::
--output-directory <path>::
Place the resulting bug report file in `<path>` instead of the root of
the Git repository.

-s <format>::
--suffix <format>::
Specify an alternate suffix for the bugreport name, to create a file
named 'git-bugreport-<formatted suffix>'. This should take the form of a
link:strftime[3] format string; the current local time will be used.

GIT
---
Part of the linkgit:git[1] suite
18 changes: 15 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,7 @@ EXTRA_PROGRAMS =
# ... and all the rest that could be moved out of bindir to gitexecdir
PROGRAMS += $(EXTRA_PROGRAMS)

PROGRAM_OBJS += bugreport.o
PROGRAM_OBJS += credential-store.o
PROGRAM_OBJS += daemon.o
PROGRAM_OBJS += fast-import.o
Expand Down Expand Up @@ -815,6 +816,7 @@ LIB_FILE = libgit.a
XDIFF_LIB = xdiff/lib.a
VCSSVN_LIB = vcs-svn/lib.a

GENERATED_H += config-list.h
GENERATED_H += command-list.h

LIB_H := $(sort $(patsubst ./%,%,$(shell git ls-files '*.h' ':!t/' ':!Documentation/' 2>/dev/null || \
Expand Down Expand Up @@ -2132,7 +2134,7 @@ git$X: git.o GIT-LDFLAGS $(BUILTIN_OBJS) $(GITLIBS)

help.sp help.s help.o: command-list.h

builtin/help.sp builtin/help.s builtin/help.o: command-list.h GIT-PREFIX
builtin/help.sp builtin/help.s builtin/help.o: config-list.h GIT-PREFIX
builtin/help.sp builtin/help.s builtin/help.o: EXTRA_CPPFLAGS = \
'-DGIT_HTML_PATH="$(htmldir_relative_SQ)"' \
'-DGIT_MAN_PATH="$(mandir_relative_SQ)"' \
Expand All @@ -2152,6 +2154,12 @@ $(BUILT_INS): git$X
ln -s $< $@ 2>/dev/null || \
cp $< $@

config-list.h: generate-configlist.sh

config-list.h:
$(QUIET_GEN)$(SHELL_PATH) ./generate-configlist.sh \
>$@+ && mv $@+ $@

command-list.h: generate-cmdlist.sh command-list.txt

command-list.h: $(wildcard Documentation/git*.txt) Documentation/*config.txt Documentation/config/*.txt
Expand Down Expand Up @@ -2454,6 +2462,10 @@ endif
git-%$X: %.o GIT-LDFLAGS $(GITLIBS)
$(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(LIBS)

git-bugreport$X: bugreport.o GIT-LDFLAGS $(GITLIBS)
$(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \
$(LIBS)

git-imap-send$X: imap-send.o $(IMAP_SEND_BUILDDEPS) GIT-LDFLAGS $(GITLIBS)
$(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \
$(IMAP_SEND_LDFLAGS) $(LIBS)
Expand Down Expand Up @@ -2785,7 +2797,7 @@ $(SP_OBJ): %.sp: %.c GIT-CFLAGS FORCE
.PHONY: sparse $(SP_OBJ)
sparse: $(SP_OBJ)

EXCEPT_HDRS := command-list.h unicode-width.h compat/% xdiff/%
EXCEPT_HDRS := command-list.h config-list.h unicode-width.h compat/% xdiff/%
ifndef GCRYPT_SHA256
EXCEPT_HDRS += sha256/gcrypt.h
endif
Expand All @@ -2807,7 +2819,7 @@ hdr-check: $(HCO)
style:
git clang-format --style file --diff --extensions c,h

check: command-list.h
check: config-list.h command-list.h
@if sparse; \
then \
echo >&2 "Use 'make sparse' instead"; \
Expand Down
142 changes: 142 additions & 0 deletions bugreport.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#include "cache.h"
#include "parse-options.h"
#include "stdio.h"
#include "strbuf.h"
#include "time.h"
#include "help.h"
#include "compat/compiler.h"

static void get_system_info(struct strbuf *sys_info)
{
struct utsname uname_info;

/* get git version from native cmd */
strbuf_addstr(sys_info, _("git version:\n"));
get_version_info(sys_info, 1);

/* system call for other version info */
strbuf_addstr(sys_info, "uname: ");
if (uname(&uname_info))
strbuf_addf(sys_info, _("uname() failed with error '%s' (%d)\n"),
strerror(errno),
errno);
else
strbuf_addf(sys_info, "%s %s %s %s\n",
uname_info.sysname,
uname_info.release,
uname_info.version,
uname_info.machine);

strbuf_addstr(sys_info, _("compiler info: "));
get_compiler_info(sys_info);
strbuf_addstr(sys_info, _("libc info: "));
get_libc_info(sys_info);
}

static const char * const bugreport_usage[] = {
N_("git bugreport [-o|--output-directory <file>] [-s|--suffix <format>]"),
NULL
};

static int get_bug_template(struct strbuf *template)
{
const char template_text[] = N_(
"Thank you for filling out a Git bug report!\n"
"Please answer the following questions to help us understand your issue.\n"
"\n"
"What did you do before the bug happened? (Steps to reproduce your issue)\n"
"\n"
"What did you expect to happen? (Expected behavior)\n"
"\n"
"What happened instead? (Actual behavior)\n"
"\n"
"What's different between what you expected and what actually happened?\n"
"\n"
"Anything else you want to add:\n"
"\n"
"Please review the rest of the bug report below.\n"
"You can delete any lines you don't wish to share.\n");

strbuf_addstr(template, _(template_text));
return 0;
}

static void get_header(struct strbuf *buf, const char *title)
{
strbuf_addf(buf, "\n\n[%s]\n", title);
}

int cmd_main(int argc, const char **argv)
{
struct strbuf buffer = STRBUF_INIT;
struct strbuf report_path = STRBUF_INIT;
int report = -1;
time_t now = time(NULL);
char *option_output = NULL;
char *option_suffix = "%Y-%m-%d-%H%M";
int nongit_ok = 0;
const char *prefix = NULL;
const char *user_relative_path = NULL;

const struct option bugreport_options[] = {
OPT_STRING('o', "output-directory", &option_output, N_("path"),
N_("specify a destination for the bugreport file")),
OPT_STRING('s', "suffix", &option_suffix, N_("format"),
N_("specify a strftime format suffix for the filename")),
OPT_END()
};

prefix = setup_git_directory_gently(&nongit_ok);

argc = parse_options(argc, argv, prefix, bugreport_options,
bugreport_usage, 0);

/* Prepare the path to put the result */
strbuf_addstr(&report_path,
prefix_filename(prefix,
option_output ? option_output : ""));
strbuf_complete(&report_path, '/');

strbuf_addstr(&report_path, "git-bugreport-");
strbuf_addftime(&report_path, option_suffix, localtime(&now), 0, 0);
strbuf_addstr(&report_path, ".txt");

switch (safe_create_leading_directories(report_path.buf)) {
case SCLD_OK:
case SCLD_EXISTS:
break;
default:
die(_("could not create leading directories for '%s'"),
report_path.buf);
}

/* Prepare the report contents */
get_bug_template(&buffer);

get_header(&buffer, _("System Info"));
get_system_info(&buffer);

/* fopen doesn't offer us an O_EXCL alternative, except with glibc. */
report = open(report_path.buf, O_CREAT | O_EXCL | O_WRONLY, 0666);

if (report < 0) {
UNLEAK(report_path);
die(_("couldn't create a new file at '%s'"), report_path.buf);
}

strbuf_write_fd(&buffer, report);
close(report);

/*
* We want to print the path relative to the user, but we still need the
* path relative to us to give to the editor.
*/
if (!(prefix && skip_prefix(report_path.buf, prefix, &user_relative_path)))
user_relative_path = report_path.buf;
fprintf(stderr, _("Created new report at '%s'.\n"),
user_relative_path);

UNLEAK(buffer);
UNLEAK(report_path);
return !!launch_editor(report_path.buf, NULL, NULL);
}
86 changes: 86 additions & 0 deletions builtin/help.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "parse-options.h"
#include "run-command.h"
#include "column.h"
#include "config-list.h"
#include "help.h"
#include "alias.h"

Expand Down Expand Up @@ -62,6 +63,91 @@ static const char * const builtin_help_usage[] = {
NULL
};

struct slot_expansion {
const char *prefix;
const char *placeholder;
void (*fn)(struct string_list *list, const char *prefix);
int found;
};

static void list_config_help(int for_human)
{
struct slot_expansion slot_expansions[] = {
{ "advice", "*", list_config_advices },
{ "color.branch", "<slot>", list_config_color_branch_slots },
{ "color.decorate", "<slot>", list_config_color_decorate_slots },
{ "color.diff", "<slot>", list_config_color_diff_slots },
{ "color.grep", "<slot>", list_config_color_grep_slots },
{ "color.interactive", "<slot>", list_config_color_interactive_slots },
{ "color.remote", "<slot>", list_config_color_sideband_slots },
{ "color.status", "<slot>", list_config_color_status_slots },
{ "fsck", "<msg-id>", list_config_fsck_msg_ids },
{ "receive.fsck", "<msg-id>", list_config_fsck_msg_ids },
{ NULL, NULL, NULL }
};
const char **p;
struct slot_expansion *e;
struct string_list keys = STRING_LIST_INIT_DUP;
int i;

for (p = config_name_list; *p; p++) {
const char *var = *p;
struct strbuf sb = STRBUF_INIT;

for (e = slot_expansions; e->prefix; e++) {

strbuf_reset(&sb);
strbuf_addf(&sb, "%s.%s", e->prefix, e->placeholder);
if (!strcasecmp(var, sb.buf)) {
e->fn(&keys, e->prefix);
e->found++;
break;
}
}
strbuf_release(&sb);
if (!e->prefix)
string_list_append(&keys, var);
}

for (e = slot_expansions; e->prefix; e++)
if (!e->found)
BUG("slot_expansion %s.%s is not used",
e->prefix, e->placeholder);

string_list_sort(&keys);
for (i = 0; i < keys.nr; i++) {
const char *var = keys.items[i].string;
const char *wildcard, *tag, *cut;

if (for_human) {
puts(var);
continue;
}

wildcard = strchr(var, '*');
tag = strchr(var, '<');

if (!wildcard && !tag) {
puts(var);
continue;
}

if (wildcard && !tag)
cut = wildcard;
else if (!wildcard && tag)
cut = tag;
else
cut = wildcard < tag ? wildcard : tag;

/*
* We may produce duplicates, but that's up to
* git-completion.bash to handle
*/
printf("%.*s\n", (int)(cut - var), var);
}
string_list_clear(&keys, 0);
}

static enum help_format parse_help_format(const char *format)
{
if (!strcmp(format, "man"))
Expand Down
1 change: 1 addition & 0 deletions command-list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ git-archive mainporcelain
git-bisect mainporcelain info
git-blame ancillaryinterrogators complete
git-branch mainporcelain history
git-bugreport ancillaryinterrogators
git-bundle mainporcelain
git-cat-file plumbinginterrogators
git-check-attr purehelpers
Expand Down
Loading