Skip to content

Commit 20d6421

Browse files
peffgitster
authored andcommitted
grep: re-order rev-parsing loop
We loop over the arguments, but every branch of the loop hits either a "continue" or a "break". Surely we can make this simpler. The final conditional is: if (arg is a rev) { ... handle rev ... continue; } break; We can rewrite this as: if (arg is not a rev) break; ... handle rev ... That makes the flow a little bit simpler, and will make things much easier to follow when we add more logic in future patches. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent dca3b5f commit 20d6421

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

builtin/grep.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,20 +1154,22 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
11541154
const char *arg = argv[i];
11551155
unsigned char sha1[20];
11561156
struct object_context oc;
1157+
struct object *object;
1158+
11571159
if (!strcmp(arg, "--")) {
11581160
i++;
11591161
seen_dashdash = 1;
11601162
break;
11611163
}
1162-
/* Is it a rev? */
1163-
if (!get_sha1_with_context(arg, 0, sha1, &oc)) {
1164-
struct object *object = parse_object_or_die(sha1, arg);
1165-
if (!seen_dashdash)
1166-
verify_non_filename(prefix, arg);
1167-
add_object_array_with_path(object, arg, &list, oc.mode, oc.path);
1168-
continue;
1169-
}
1170-
break;
1164+
1165+
/* Stop at the first non-rev */
1166+
if (get_sha1_with_context(arg, 0, sha1, &oc))
1167+
break;
1168+
1169+
object = parse_object_or_die(sha1, arg);
1170+
if (!seen_dashdash)
1171+
verify_non_filename(prefix, arg);
1172+
add_object_array_with_path(object, arg, &list, oc.mode, oc.path);
11711173
}
11721174

11731175
/* The rest are paths */

0 commit comments

Comments
 (0)