@matheusbsbs / matheus-bagliano / commits / 7d7b86f75f
Fork 0

log: parse separate options like git log --grep foo

Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Matthieu Moy committed Aug 5, 2010 at 10:22 UTC 7d7b86f75f01cd4a491dbf0170ba36806dacb1c5
2 files changed +53 -28
revision.c
+46 -28
@@ -1148,6 +1148,8 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
1148 int *unkc, const char **unkv)
1149 {
1150 const char *arg = argv[0];
1151 + const char *optarg;
1152 + int argcount;
1153
1154 /* pseudo revision arguments */
1155 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
@@ -1160,11 +1162,13 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
1162 return 1;
1163 }
1164
1163 - if (!prefixcmp(arg, "--max-count=")) {
1164 - revs->max_count = atoi(arg + 12);
1165 + if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
1166 + revs->max_count = atoi(optarg);
1167 revs->no_walk = 0;
1166 - } else if (!prefixcmp(arg, "--skip=")) {
1167 - revs->skip_count = atoi(arg + 7);
1168 + return argcount;
1169 + } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
1170 + revs->skip_count = atoi(optarg);
1171 + return argcount;
1172 } else if ((*arg == '-') && isdigit(arg[1])) {
1173 /* accept -<digit>, like traditional "head" */
1174 revs->max_count = atoi(arg + 1);
@@ -1178,18 +1182,24 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
1182 } else if (!prefixcmp(arg, "-n")) {
1183 revs->max_count = atoi(arg + 2);
1184 revs->no_walk = 0;
1181 - } else if (!prefixcmp(arg, "--max-age=")) {
1182 - revs->max_age = atoi(arg + 10);
1183 - } else if (!prefixcmp(arg, "--since=")) {
1184 - revs->max_age = approxidate(arg + 8);
1185 - } else if (!prefixcmp(arg, "--after=")) {
1186 - revs->max_age = approxidate(arg + 8);
1187 - } else if (!prefixcmp(arg, "--min-age=")) {
1188 - revs->min_age = atoi(arg + 10);
1189 - } else if (!prefixcmp(arg, "--before=")) {
1190 - revs->min_age = approxidate(arg + 9);
1191 - } else if (!prefixcmp(arg, "--until=")) {
1192 - revs->min_age = approxidate(arg + 8);
1185 + } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
1186 + revs->max_age = atoi(optarg);
1187 + return argcount;
1188 + } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
1189 + revs->max_age = approxidate(optarg);
1190 + return argcount;
1191 + } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
1192 + revs->max_age = approxidate(optarg);
1193 + return argcount;
1194 + } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
1195 + revs->min_age = atoi(optarg);
1196 + return argcount;
1197 + } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
1198 + revs->min_age = approxidate(optarg);
1199 + return argcount;
1200 + } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
1201 + revs->min_age = approxidate(optarg);
1202 + return argcount;
1203 } else if (!strcmp(arg, "--first-parent")) {
1204 revs->first_parent_only = 1;
1205 } else if (!strcmp(arg, "--ancestry-path")) {
@@ -1295,6 +1305,10 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
1305 revs->pretty_given = 1;
1306 get_commit_format(arg+8, revs);
1307 } else if (!prefixcmp(arg, "--pretty=") || !prefixcmp(arg, "--format=")) {
1308 + /*
1309 + * Detached form ("--pretty X" as opposed to "--pretty=X")
1310 + * not allowed, since the argument is optional.
1311 + */
1312 revs->verbose_header = 1;
1313 revs->pretty_given = 1;
1314 get_commit_format(arg+9, revs);
@@ -1359,21 +1373,25 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
1373 } else if (!strcmp(arg, "--relative-date")) {
1374 revs->date_mode = DATE_RELATIVE;
1375 revs->date_mode_explicit = 1;
1362 - } else if (!strncmp(arg, "--date=", 7)) {
1363 - revs->date_mode = parse_date_format(arg + 7);
1376 + } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
1377 + revs->date_mode = parse_date_format(optarg);
1378 revs->date_mode_explicit = 1;
1379 + return argcount;
1380 } else if (!strcmp(arg, "--log-size")) {
1381 revs->show_log_size = 1;
1382 }
1383 /*
1384 * Grepping the commit log
1385 */
1371 - else if (!prefixcmp(arg, "--author=")) {
1372 - add_header_grep(revs, GREP_HEADER_AUTHOR, arg+9);
1373 - } else if (!prefixcmp(arg, "--committer=")) {
1374 - add_header_grep(revs, GREP_HEADER_COMMITTER, arg+12);
1375 - } else if (!prefixcmp(arg, "--grep=")) {
1376 - add_message_grep(revs, arg+7);
1386 + else if ((argcount = parse_long_opt("author", argv, &optarg))) {
1387 + add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
1388 + return argcount;
1389 + } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
1390 + add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
1391 + return argcount;
1392 + } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
1393 + add_message_grep(revs, optarg);
1394 + return argcount;
1395 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
1396 revs->grep_filter.regflags |= REG_EXTENDED;
1397 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
@@ -1382,12 +1400,12 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
1400 revs->grep_filter.fixed = 1;
1401 } else if (!strcmp(arg, "--all-match")) {
1402 revs->grep_filter.all_match = 1;
1385 - } else if (!prefixcmp(arg, "--encoding=")) {
1386 - arg += 11;
1387 - if (strcmp(arg, "none"))
1388 - git_log_output_encoding = xstrdup(arg);
1403 + } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
1404 + if (strcmp(optarg, "none"))
1405 + git_log_output_encoding = xstrdup(optarg);
1406 else
1407 git_log_output_encoding = "";
1408 + return argcount;
1409 } else if (!strcmp(arg, "--reverse")) {
1410 revs->reverse ^= 1;
1411 } else if (!strcmp(arg, "--children")) {
t/t4202-log.sh
+7
@@ -201,6 +201,13 @@ test_expect_success 'log --grep' '
201 test_cmp expect actual
202 '
203
204 +test_expect_success 'log --grep option parsing' '
205 + echo second >expect &&
206 + git log -1 --pretty="tformat:%s" --grep sec >actual &&
207 + test_cmp expect actual &&
208 + test_must_fail git log -1 --pretty="tformat:%s" --grep
209 +'
210 +
211 test_expect_success 'log -i --grep' '
212 echo Second >expect &&
213 git log -1 --pretty="tformat:%s" -i --grep=sec >actual &&