Introduce --pretty=oneline format.

This introduces --pretty=oneline to git-rev-tree and
git-rev-list commands to show only the first line of the commit
message, without frills. 

Signed-off-by: Junio C Hamano <junkio@cox.net>
This commit is contained in:
Junio C Hamano
2005-08-08 22:15:40 -07:00
parent 62033318ab
commit d87449c553
3 changed files with 36 additions and 9 deletions

View File

@ -33,6 +33,8 @@ enum cmit_fmt get_commit_format(const char *arg)
return CMIT_FMT_SHORT; return CMIT_FMT_SHORT;
if (!strcmp(arg, "=full")) if (!strcmp(arg, "=full"))
return CMIT_FMT_FULL; return CMIT_FMT_FULL;
if (!strcmp(arg, "=oneline"))
return CMIT_FMT_ONELINE;
die("invalid --pretty format"); die("invalid --pretty format");
} }
@ -350,6 +352,8 @@ static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const c
unsigned long time; unsigned long time;
int tz, ret; int tz, ret;
if (fmt == CMIT_FMT_ONELINE)
return 0;
date = strchr(line, '>'); date = strchr(line, '>');
if (!date) if (!date)
return 0; return 0;
@ -373,6 +377,9 @@ static int is_empty_line(const char *line, int len)
static int add_parent_info(enum cmit_fmt fmt, char *buf, const char *line, int parents) static int add_parent_info(enum cmit_fmt fmt, char *buf, const char *line, int parents)
{ {
int offset = 0; int offset = 0;
if (fmt == CMIT_FMT_ONELINE)
return offset;
switch (parents) { switch (parents) {
case 1: case 1:
break; break;
@ -393,6 +400,7 @@ unsigned long pretty_print_commit(enum cmit_fmt fmt, const char *msg, unsigned l
int hdr = 1, body = 0; int hdr = 1, body = 0;
unsigned long offset = 0; unsigned long offset = 0;
int parents = 0; int parents = 0;
int indent = (fmt == CMIT_FMT_ONELINE) ? 0 : 4;
for (;;) { for (;;) {
const char *line = msg; const char *line = msg;
@ -416,6 +424,7 @@ unsigned long pretty_print_commit(enum cmit_fmt fmt, const char *msg, unsigned l
if (hdr) { if (hdr) {
if (linelen == 1) { if (linelen == 1) {
hdr = 0; hdr = 0;
if (fmt != CMIT_FMT_ONELINE)
buf[offset++] = '\n'; buf[offset++] = '\n';
continue; continue;
} }
@ -446,13 +455,23 @@ unsigned long pretty_print_commit(enum cmit_fmt fmt, const char *msg, unsigned l
} else { } else {
body = 1; body = 1;
} }
memset(buf + offset, ' ', 4);
memcpy(buf + offset + 4, line, linelen); memset(buf + offset, ' ', indent);
offset += linelen + 4; memcpy(buf + offset + indent, line, linelen);
offset += linelen + indent;
if (fmt == CMIT_FMT_ONELINE)
break;
} }
if (fmt == CMIT_FMT_ONELINE) {
/* We do not want the terminating newline */
if (buf[offset - 1] == '\n')
offset--;
}
else {
/* Make sure there is an EOLN */ /* Make sure there is an EOLN */
if (buf[offset - 1] != '\n') if (buf[offset - 1] != '\n')
buf[offset++] = '\n'; buf[offset++] = '\n';
}
buf[offset] = '\0'; buf[offset] = '\0';
return offset; return offset;
} }

View File

@ -40,6 +40,7 @@ enum cmit_fmt {
CMIT_FMT_DEFAULT = CMIT_FMT_MEDIUM, CMIT_FMT_DEFAULT = CMIT_FMT_MEDIUM,
CMIT_FMT_SHORT, CMIT_FMT_SHORT,
CMIT_FMT_FULL, CMIT_FMT_FULL,
CMIT_FMT_ONELINE,
}; };
extern enum cmit_fmt get_commit_format(const char *arg); extern enum cmit_fmt get_commit_format(const char *arg);

View File

@ -63,7 +63,11 @@ static void show_commit(struct commit *commit)
parents = parents->next; parents = parents->next;
} }
} }
if (commit_format == CMIT_FMT_ONELINE)
putchar(' ');
else
putchar('\n'); putchar('\n');
if (verbose_header) { if (verbose_header) {
static char pretty_header[16384]; static char pretty_header[16384];
pretty_print_commit(commit_format, commit->buffer, ~0, pretty_header, sizeof(pretty_header)); pretty_print_commit(commit_format, commit->buffer, ~0, pretty_header, sizeof(pretty_header));
@ -503,6 +507,9 @@ int main(int argc, char **argv)
commit_format = get_commit_format(arg+8); commit_format = get_commit_format(arg+8);
verbose_header = 1; verbose_header = 1;
hdr_termination = '\n'; hdr_termination = '\n';
if (commit_format == CMIT_FMT_ONELINE)
prefix = "";
else
prefix = "commit "; prefix = "commit ";
continue; continue;
} }