Skip to content

Commit

Permalink
survey: add --json option and setup for pretty output
Browse files Browse the repository at this point in the history
Signed-off-by: Jeff Hostetler <jeffhostetler@github.com>
  • Loading branch information
jeffhostetler committed May 21, 2024
1 parent 9f2a71e commit 04a95ac
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions builtin/survey.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ static struct survey_refs_wanted refs_if_unspecified = {
struct survey_opts {
int verbose;
int show_progress;
int show_json;

int show_largest_commits_by_nr_parents;
int show_largest_commits_by_size_bytes;
Expand All @@ -76,6 +77,7 @@ struct survey_opts {
static struct survey_opts survey_opts = {
.verbose = 0,
.show_progress = -1, /* defaults to isatty(2) */
.show_json = 0, /* defaults to pretty */

/*
* Show the largest `n` objects for some scaling dimension.
Expand Down Expand Up @@ -172,6 +174,7 @@ static void fixup_refs_wanted(void)
static struct option survey_options[] = {
OPT__VERBOSE(&survey_opts.verbose, N_("verbose output")),
OPT_BOOL(0, "progress", &survey_opts.show_progress, N_("show progress")),
OPT_BOOL(0, "json", &survey_opts.show_json, N_("report stats in JSON")),

OPT_BOOL_F(0, "all-refs", &survey_opts.refs.want_all_refs, N_("include all refs"), PARSE_OPT_NONEG),

Expand Down Expand Up @@ -205,6 +208,10 @@ static int survey_load_config_cb(const char *var, const char *value,
survey_opts.show_progress = git_config_bool(var, value);
return 0;
}
if (!strcmp(var, "survey.json")) {
survey_opts.show_json = git_config_bool(var, value);
return 0;
}

if (!strcmp(var, "survey.showcommitparents")) {
survey_opts.show_largest_commits_by_nr_parents = git_config_ulong(var, value, ctx->kvi);
Expand Down Expand Up @@ -1319,7 +1326,10 @@ static void survey_json(struct json_writer *jw, int pretty)
jw_end(jw);
}

static void survey_print_results(void)
/*
* Print all of the stats that we have collected in JSON.
*/
static void survey_print_results_json(void)
{
struct json_writer jw = JSON_WRITER_INIT;

Expand All @@ -1328,6 +1338,14 @@ static void survey_print_results(void)
jw_release(&jw);
}

/*
* Print all of the stats that we have collected in a more pretty format.
*/
static void survey_print_results_pretty(void)
{
printf("TODO....\n");
}

int cmd_survey(int argc, const char **argv, const char *prefix)
{
prepare_repo_settings(the_repository);
Expand Down Expand Up @@ -1382,7 +1400,10 @@ int cmd_survey(int argc, const char **argv, const char *prefix)
jw_release(&jw);
}

survey_print_results();
if (survey_opts.show_json)
survey_print_results_json();
else
survey_print_results_pretty();

strvec_clear(&survey_vec_refs_wanted);
free_large_item_vec(survey_stats.commits.vec_largest_by_nr_parents);
Expand Down

0 comments on commit 04a95ac

Please sign in to comment.