pretty: split color parsing into a separate function

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Apr 19, 2013 at 09:08 UTC fcabc2d91cd5f2c9b92e461907d0155265ec0633
1 file changed +39 -32
pretty.c
+39 -32
@@ -954,6 +954,44 @@ static int format_reflog_person(struct strbuf *sb,
954 return format_person_part(sb, part, ident, strlen(ident), dmode);
955 }
956
957 +static size_t parse_color(struct strbuf *sb, /* in UTF-8 */
958 + const char *placeholder,
959 + struct format_commit_context *c)
960 +{
961 + if (placeholder[1] == '(') {
962 + const char *begin = placeholder + 2;
963 + const char *end = strchr(begin, ')');
964 + char color[COLOR_MAXLEN];
965 +
966 + if (!end)
967 + return 0;
968 + if (!prefixcmp(begin, "auto,")) {
969 + if (!want_color(c->pretty_ctx->color))
970 + return end - placeholder + 1;
971 + begin += 5;
972 + }
973 + color_parse_mem(begin,
974 + end - begin,
975 + "--pretty format", color);
976 + strbuf_addstr(sb, color);
977 + return end - placeholder + 1;
978 + }
979 + if (!prefixcmp(placeholder + 1, "red")) {
980 + strbuf_addstr(sb, GIT_COLOR_RED);
981 + return 4;
982 + } else if (!prefixcmp(placeholder + 1, "green")) {
983 + strbuf_addstr(sb, GIT_COLOR_GREEN);
984 + return 6;
985 + } else if (!prefixcmp(placeholder + 1, "blue")) {
986 + strbuf_addstr(sb, GIT_COLOR_BLUE);
987 + return 5;
988 + } else if (!prefixcmp(placeholder + 1, "reset")) {
989 + strbuf_addstr(sb, GIT_COLOR_RESET);
990 + return 6;
991 + } else
992 + return 0;
993 +}
994 +
995 static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
996 const char *placeholder,
997 void *context)
@@ -967,38 +1005,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
1005 /* these are independent of the commit */
1006 switch (placeholder[0]) {
1007 case 'C':
970 - if (placeholder[1] == '(') {
971 - const char *begin = placeholder + 2;
972 - const char *end = strchr(begin, ')');
973 - char color[COLOR_MAXLEN];
974 -
975 - if (!end)
976 - return 0;
977 - if (!prefixcmp(begin, "auto,")) {
978 - if (!want_color(c->pretty_ctx->color))
979 - return end - placeholder + 1;
980 - begin += 5;
981 - }
982 - color_parse_mem(begin,
983 - end - begin,
984 - "--pretty format", color);
985 - strbuf_addstr(sb, color);
986 - return end - placeholder + 1;
987 - }
988 - if (!prefixcmp(placeholder + 1, "red")) {
989 - strbuf_addstr(sb, GIT_COLOR_RED);
990 - return 4;
991 - } else if (!prefixcmp(placeholder + 1, "green")) {
992 - strbuf_addstr(sb, GIT_COLOR_GREEN);
993 - return 6;
994 - } else if (!prefixcmp(placeholder + 1, "blue")) {
995 - strbuf_addstr(sb, GIT_COLOR_BLUE);
996 - return 5;
997 - } else if (!prefixcmp(placeholder + 1, "reset")) {
998 - strbuf_addstr(sb, GIT_COLOR_RESET);
999 - return 6;
1000 - } else
1001 - return 0;
1008 + return parse_color(sb, placeholder, c);
1009 case 'n': /* newline */
1010 strbuf_addch(sb, '\n');
1011 return 1;