Merge branch 'mh/maint-parse-dirstat-fix'

Cleans up some code and avoids a potential bug.

* mh/maint-parse-dirstat-fix:
  parse_dirstat_params(): use string_list to split comma-separated string
This commit is contained in:
Jeff King 2012-11-09 12:42:21 -05:00
commit 8736c9010c

40
diff.c
View File

@ -15,6 +15,7 @@
#include "sigchain.h" #include "sigchain.h"
#include "submodule.h" #include "submodule.h"
#include "ll-merge.h" #include "ll-merge.h"
#include "string-list.h"
#ifdef NO_FAST_WORKING_DIRECTORY #ifdef NO_FAST_WORKING_DIRECTORY
#define FAST_WORKING_DIRECTORY 0 #define FAST_WORKING_DIRECTORY 0
@ -69,26 +70,30 @@ static int parse_diff_color_slot(const char *var, int ofs)
return -1; return -1;
} }
static int parse_dirstat_params(struct diff_options *options, const char *params, static int parse_dirstat_params(struct diff_options *options, const char *params_string,
struct strbuf *errmsg) struct strbuf *errmsg)
{ {
const char *p = params; char *params_copy = xstrdup(params_string);
int p_len, ret = 0; struct string_list params = STRING_LIST_INIT_NODUP;
int ret = 0;
int i;
while (*p) { if (*params_copy)
p_len = strchrnul(p, ',') - p; string_list_split_in_place(&params, params_copy, ',', -1);
if (!memcmp(p, "changes", p_len)) { for (i = 0; i < params.nr; i++) {
const char *p = params.items[i].string;
if (!strcmp(p, "changes")) {
DIFF_OPT_CLR(options, DIRSTAT_BY_LINE); DIFF_OPT_CLR(options, DIRSTAT_BY_LINE);
DIFF_OPT_CLR(options, DIRSTAT_BY_FILE); DIFF_OPT_CLR(options, DIRSTAT_BY_FILE);
} else if (!memcmp(p, "lines", p_len)) { } else if (!strcmp(p, "lines")) {
DIFF_OPT_SET(options, DIRSTAT_BY_LINE); DIFF_OPT_SET(options, DIRSTAT_BY_LINE);
DIFF_OPT_CLR(options, DIRSTAT_BY_FILE); DIFF_OPT_CLR(options, DIRSTAT_BY_FILE);
} else if (!memcmp(p, "files", p_len)) { } else if (!strcmp(p, "files")) {
DIFF_OPT_CLR(options, DIRSTAT_BY_LINE); DIFF_OPT_CLR(options, DIRSTAT_BY_LINE);
DIFF_OPT_SET(options, DIRSTAT_BY_FILE); DIFF_OPT_SET(options, DIRSTAT_BY_FILE);
} else if (!memcmp(p, "noncumulative", p_len)) { } else if (!strcmp(p, "noncumulative")) {
DIFF_OPT_CLR(options, DIRSTAT_CUMULATIVE); DIFF_OPT_CLR(options, DIRSTAT_CUMULATIVE);
} else if (!memcmp(p, "cumulative", p_len)) { } else if (!strcmp(p, "cumulative")) {
DIFF_OPT_SET(options, DIRSTAT_CUMULATIVE); DIFF_OPT_SET(options, DIRSTAT_CUMULATIVE);
} else if (isdigit(*p)) { } else if (isdigit(*p)) {
char *end; char *end;
@ -100,24 +105,21 @@ static int parse_dirstat_params(struct diff_options *options, const char *params
while (isdigit(*++end)) while (isdigit(*++end))
; /* nothing */ ; /* nothing */
} }
if (end - p == p_len) if (!*end)
options->dirstat_permille = permille; options->dirstat_permille = permille;
else { else {
strbuf_addf(errmsg, _(" Failed to parse dirstat cut-off percentage '%.*s'\n"), strbuf_addf(errmsg, _(" Failed to parse dirstat cut-off percentage '%s'\n"),
p_len, p); p);
ret++; ret++;
} }
} else { } else {
strbuf_addf(errmsg, _(" Unknown dirstat parameter '%.*s'\n"), strbuf_addf(errmsg, _(" Unknown dirstat parameter '%s'\n"), p);
p_len, p);
ret++; ret++;
} }
p += p_len;
if (*p)
p++; /* more parameters, swallow separator */
} }
string_list_clear(&params, 0);
free(params_copy);
return ret; return ret;
} }