Merge branch 'ns/pretty-format'

* ns/pretty-format:
  bash completion: add --format= and --oneline options for "git log"
  Add tests for git log --pretty, --format and --oneline.
  Add --oneline that is a synonym to "--pretty=oneline --abbrev-commit"
  Give short-hands to --pretty=tformat:%formatstring
  Add --format that is a synonym to --pretty
This commit is contained in:
Junio C Hamano 2009-03-05 15:41:43 -08:00
commit 458eaf5bf8
6 changed files with 85 additions and 9 deletions

View File

@ -152,3 +152,12 @@ $ git log -2 --pretty=tformat:%h 4da45bef \
4da45be
7134973
---------------------
+
In addition, any unrecognized string that has a `%` in it is interpreted
as if it has `tformat:` in front of it. For example, these two are
equivalent:
+
---------------------
$ git log -2 --pretty=tformat:%h 4da45bef
$ git log -2 --pretty=%h 4da45bef
---------------------

View File

@ -1,4 +1,5 @@
--pretty[='<format>']::
--format[='<format>']::
Pretty-print the contents of the commit logs in a given format,
where '<format>' can be one of 'oneline', 'short', 'medium',
@ -17,6 +18,10 @@ configuration (see linkgit:git-config[1]).
This should make "--pretty=oneline" a whole lot more readable for
people using 80-column terminals.
--oneline::
This is a shorthand for "--pretty=oneline --abbrev-commit"
used together.
--encoding[=<encoding>]::
The commit objects record the encoding used for the log message
in their encoding header; this option can be used to tell the

View File

@ -1018,6 +1018,11 @@ _git_log ()
" "" "${cur##--pretty=}"
return
;;
--format=*)
__gitcomp "$__git_log_pretty_formats
" "" "${cur##--format=}"
return
;;
--date=*)
__gitcomp "
relative iso8601 rfc2822 short local default
@ -1033,7 +1038,7 @@ _git_log ()
--follow
--abbrev-commit --abbrev=
--relative-date --date=
--pretty=
--pretty= --format= --oneline
--cherry-pick
--graph
--decorate
@ -1545,8 +1550,13 @@ _git_show ()
" "" "${cur##--pretty=}"
return
;;
--format=*)
__gitcomp "$__git_log_pretty_formats
" "" "${cur##--format=}"
return
;;
--*)
__gitcomp "--pretty=
__gitcomp "--pretty= --format=
$__git_diff_common_options
"
return

View File

@ -10,6 +10,15 @@
static char *user_format;
static void save_user_format(struct rev_info *rev, const char *cp, int is_tformat)
{
free(user_format);
user_format = xstrdup(cp);
if (is_tformat)
rev->use_terminator = 1;
rev->commit_format = CMIT_FMT_USERFORMAT;
}
void get_commit_format(const char *arg, struct rev_info *rev)
{
int i;
@ -33,12 +42,7 @@ void get_commit_format(const char *arg, struct rev_info *rev)
return;
}
if (!prefixcmp(arg, "format:") || !prefixcmp(arg, "tformat:")) {
const char *cp = strchr(arg, ':') + 1;
free(user_format);
user_format = xstrdup(cp);
if (arg[0] == 't')
rev->use_terminator = 1;
rev->commit_format = CMIT_FMT_USERFORMAT;
save_user_format(rev, strchr(arg, ':') + 1, arg[0] == 't');
return;
}
for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) {
@ -50,6 +54,10 @@ void get_commit_format(const char *arg, struct rev_info *rev)
return;
}
}
if (strchr(arg, '%')) {
save_user_format(rev, arg, 1);
return;
}
die("invalid --pretty format: %s", arg);
}

View File

@ -1144,9 +1144,13 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
} else if (!strcmp(arg, "--pretty")) {
revs->verbose_header = 1;
get_commit_format(arg+8, revs);
} else if (!prefixcmp(arg, "--pretty=")) {
} else if (!prefixcmp(arg, "--pretty=") || !prefixcmp(arg, "--format=")) {
revs->verbose_header = 1;
get_commit_format(arg+9, revs);
} else if (!strcmp(arg, "--oneline")) {
revs->verbose_header = 1;
get_commit_format("oneline", revs);
revs->abbrev_commit = 1;
} else if (!strcmp(arg, "--graph")) {
revs->topo_order = 1;
revs->rewrite_parents = 1;

View File

@ -37,6 +37,46 @@ test_expect_success setup '
'
printf "sixth\nfifth\nfourth\nthird\nsecond\ninitial" > expect
test_expect_success 'pretty' '
git log --pretty="format:%s" > actual &&
test_cmp expect actual
'
printf "sixth\nfifth\nfourth\nthird\nsecond\ninitial\n" > expect
test_expect_success 'pretty (tformat)' '
git log --pretty="tformat:%s" > actual &&
test_cmp expect actual
'
test_expect_success 'pretty (shortcut)' '
git log --pretty="%s" > actual &&
test_cmp expect actual
'
test_expect_success 'format' '
git log --format="%s" > actual &&
test_cmp expect actual
'
cat > expect << EOF
804a787 sixth
394ef78 fifth
5d31159 fourth
2fbe8c0 third
f7dab8e second
3a2fdcb initial
EOF
test_expect_success 'oneline' '
git log --oneline > actual &&
test_cmp expect actual
'
test_expect_success 'diff-filter=A' '
actual=$(git log --pretty="format:%s" --diff-filter=A HEAD) &&