Skip to content

Commit 8e3bc14

Browse files
committed
Merge branch 'jn/shortlog'
* jn/shortlog: pretty: Respect --abbrev option shortlog: Document and test --format option t4201 (shortlog): Test output format with multiple authors t4201 (shortlog): guard setup with test_expect_success Documentation/shortlog: scripted users should not rely on implicit HEAD
2 parents f9a518e + c197702 commit 8e3bc14

File tree

6 files changed

+144
-29
lines changed

6 files changed

+144
-29
lines changed

Documentation/git-shortlog.txt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ SYNOPSIS
99
--------
1010
[verse]
1111
git log --pretty=short | 'git shortlog' [-h] [-n] [-s] [-e] [-w]
12-
'git shortlog' [-n|--numbered] [-s|--summary] [-e|--email] [-w[<width>[,<indent1>[,<indent2>]]]] [<committish>...]
12+
'git shortlog' [-n|--numbered] [-s|--summary] [-e|--email] [-w[<width>[,<indent1>[,<indent2>]]]] <commit>...
1313

1414
DESCRIPTION
1515
-----------
@@ -19,6 +19,11 @@ the first line of the commit message will be shown.
1919

2020
Additionally, "[PATCH]" will be stripped from the commit description.
2121

22+
If no revisions are passed on the command line and either standard input
23+
is not a terminal or there is no current branch, 'git shortlog' will
24+
output a summary of the log read from standard input, without
25+
reference to the current repository.
26+
2227
OPTIONS
2328
-------
2429

@@ -39,6 +44,14 @@ OPTIONS
3944
--email::
4045
Show the email address of each author.
4146

47+
--format[='<format>']::
48+
Instead of the commit subject, use some other information to
49+
describe each commit. '<format>' can be any string accepted
50+
by the `--format` option of 'git log', such as '{asterisk} [%h] %s'.
51+
(See the "PRETTY FORMATS" section of linkgit:git-log[1].)
52+
53+
Each pretty-printed commit will be rewrapped before it is shown.
54+
4255
-w[<width>[,<indent1>[,<indent2>]]]::
4356
Linewrap the output by wrapping each line at `width`. The first
4457
line of each entry is indented by `indent1` spaces, and the second

builtin/shortlog.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ void shortlog_add_commit(struct shortlog *log, struct commit *commit)
162162
sha1_to_hex(commit->object.sha1));
163163
if (log->user_format) {
164164
struct pretty_print_context ctx = {0};
165-
ctx.abbrev = DEFAULT_ABBREV;
165+
ctx.abbrev = log->abbrev;
166166
ctx.subject = "";
167167
ctx.after_subject = "";
168168
ctx.date_mode = DATE_NORMAL;
@@ -290,6 +290,7 @@ int cmd_shortlog(int argc, const char **argv, const char *prefix)
290290
}
291291

292292
log.user_format = rev.commit_format == CMIT_FMT_USERFORMAT;
293+
log.abbrev = rev.abbrev;
293294

294295
/* assume HEAD if from a tty */
295296
if (!nongit && !rev.pending.nr && isatty(0))

pretty.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
716716
if (add_again(sb, &c->abbrev_commit_hash))
717717
return 1;
718718
strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
719-
DEFAULT_ABBREV));
719+
c->pretty_ctx->abbrev));
720720
c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off;
721721
return 1;
722722
case 'T': /* tree hash */
@@ -726,7 +726,7 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
726726
if (add_again(sb, &c->abbrev_tree_hash))
727727
return 1;
728728
strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
729-
DEFAULT_ABBREV));
729+
c->pretty_ctx->abbrev));
730730
c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off;
731731
return 1;
732732
case 'P': /* parent hashes */
@@ -743,7 +743,8 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
743743
if (p != commit->parents)
744744
strbuf_addch(sb, ' ');
745745
strbuf_addstr(sb, find_unique_abbrev(
746-
p->item->object.sha1, DEFAULT_ABBREV));
746+
p->item->object.sha1,
747+
c->pretty_ctx->abbrev));
747748
}
748749
c->abbrev_parent_hashes.len = sb->len -
749750
c->abbrev_parent_hashes.off;

shortlog.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ struct shortlog {
1212
int in1;
1313
int in2;
1414
int user_format;
15+
int abbrev;
1516

1617
char *common_repo_prefix;
1718
int email;

t/t4201-shortlog.sh

Lines changed: 92 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,93 @@ test_description='git shortlog
88

99
. ./test-lib.sh
1010

11-
echo 1 > a1
12-
git add a1
13-
tree=$(git write-tree)
14-
commit=$( (echo "Test"; echo) | git commit-tree $tree )
15-
git update-ref HEAD $commit
11+
test_expect_success 'setup' '
12+
echo 1 >a1 &&
13+
git add a1 &&
14+
tree=$(git write-tree) &&
15+
commit=$(printf "%s\n" "Test" "" | git commit-tree "$tree") &&
16+
git update-ref HEAD "$commit" &&
17+
18+
echo 2 >a1 &&
19+
git commit --quiet -m "This is a very, very long first line for the commit message to see if it is wrapped correctly" a1 &&
20+
21+
# test if the wrapping is still valid
22+
# when replacing all is by treble clefs.
23+
echo 3 >a1 &&
24+
git commit --quiet -m "$(
25+
echo "This is a very, very long first line for the commit message to see if it is wrapped correctly" |
26+
sed "s/i/1234/g" |
27+
tr 1234 "\360\235\204\236")" a1 &&
28+
29+
# now fsck up the utf8
30+
git config i18n.commitencoding non-utf-8 &&
31+
echo 4 >a1 &&
32+
git commit --quiet -m "$(
33+
echo "This is a very, very long first line for the commit message to see if it is wrapped correctly" |
34+
sed "s/i/1234/g" |
35+
tr 1234 "\370\235\204\236")" a1 &&
36+
37+
echo 5 >a1 &&
38+
git commit --quiet -m "a 12 34 56 78" a1
39+
40+
echo 6 >a1 &&
41+
git commit --quiet -m "Commit by someone else" \
42+
--author="Someone else <not!me>" a1 &&
43+
44+
cat >expect.template <<-\EOF
45+
A U Thor (5):
46+
SUBJECT
47+
SUBJECT
48+
SUBJECT
49+
SUBJECT
50+
SUBJECT
51+
52+
Someone else (1):
53+
SUBJECT
54+
55+
EOF
56+
'
1657

17-
echo 2 > a1
18-
git commit --quiet -m "This is a very, very long first line for the commit message to see if it is wrapped correctly" a1
58+
fuzz() {
59+
file=$1 &&
60+
sed "
61+
s/$_x40/OBJECT_NAME/g
62+
s/$_x05/OBJID/g
63+
s/^ \{6\}[CTa].*/ SUBJECT/g
64+
s/^ \{8\}[^ ].*/ CONTINUATION/g
65+
" <"$file" >"$file.fuzzy" &&
66+
sed "/CONTINUATION/ d" <"$file.fuzzy"
67+
}
1968

20-
# test if the wrapping is still valid when replacing all i's by treble clefs.
21-
echo 3 > a1
22-
git commit --quiet -m "$(echo "This is a very, very long first line for the commit message to see if it is wrapped correctly" | sed "s/i/1234/g" | tr 1234 '\360\235\204\236')" a1
69+
test_expect_success 'default output format' '
70+
git shortlog HEAD >log &&
71+
fuzz log >log.predictable &&
72+
test_cmp expect.template log.predictable
73+
'
2374

24-
# now fsck up the utf8
25-
git config i18n.commitencoding non-utf-8
26-
echo 4 > a1
27-
git commit --quiet -m "$(echo "This is a very, very long first line for the commit message to see if it is wrapped correctly" | sed "s/i/1234/g" | tr 1234 '\370\235\204\236')" a1
75+
test_expect_success 'pretty format' '
76+
sed s/SUBJECT/OBJECT_NAME/ expect.template >expect &&
77+
git shortlog --format="%H" HEAD >log &&
78+
fuzz log >log.predictable &&
79+
test_cmp expect log.predictable
80+
'
2881

29-
echo 5 > a1
30-
git commit --quiet -m "a 12 34 56 78" a1
82+
test_expect_success '--abbrev' '
83+
sed s/SUBJECT/OBJID/ expect.template >expect &&
84+
git shortlog --format="%h" --abbrev=5 HEAD >log &&
85+
fuzz log >log.predictable &&
86+
test_cmp expect log.predictable
87+
'
3188

32-
git shortlog -w HEAD > out
89+
test_expect_success 'output from user-defined format is re-wrapped' '
90+
sed "s/SUBJECT/two lines/" expect.template >expect &&
91+
git shortlog --format="two%nlines" HEAD >log &&
92+
fuzz log >log.predictable &&
93+
test_cmp expect log.predictable
94+
'
3395

34-
cat > expect << EOF
96+
test_expect_success 'shortlog wrapping' '
97+
cat >expect <<\EOF &&
3598
A U Thor (5):
3699
Test
37100
This is a very, very long first line for the commit message to see if
@@ -43,14 +106,19 @@ A U Thor (5):
43106
a 12 34
44107
56 78
45108
46-
EOF
47-
48-
test_expect_success 'shortlog wrapping' 'test_cmp expect out'
109+
Someone else (1):
110+
Commit by someone else
49111
50-
git log HEAD > log
51-
GIT_DIR=non-existing git shortlog -w < log > out
112+
EOF
113+
git shortlog -w HEAD >out &&
114+
test_cmp expect out
115+
'
52116

53-
test_expect_success 'shortlog from non-git directory' 'test_cmp expect out'
117+
test_expect_success 'shortlog from non-git directory' '
118+
git log HEAD >log &&
119+
GIT_DIR=non-existing git shortlog -w <log >out &&
120+
test_cmp expect out
121+
'
54122

55123
iconvfromutf8toiso88591() {
56124
printf "%s" "$*" | iconv -f UTF-8 -t ISO8859-1

t/t6006-rev-list-format.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,31 @@ test_expect_success 'add LF before non-empty (2)' '
200200
grep "^$" actual
201201
'
202202

203+
test_expect_success '--abbrev' '
204+
echo SHORT SHORT SHORT >expect2 &&
205+
echo LONG LONG LONG >expect3 &&
206+
git log -1 --format="%h %h %h" HEAD >actual1 &&
207+
git log -1 --abbrev=5 --format="%h %h %h" HEAD >actual2 &&
208+
git log -1 --abbrev=5 --format="%H %H %H" HEAD >actual3 &&
209+
sed -e "s/$_x40/LONG/g" -e "s/$_x05/SHORT/g" <actual2 >fuzzy2 &&
210+
sed -e "s/$_x40/LONG/g" -e "s/$_x05/SHORT/g" <actual3 >fuzzy3 &&
211+
test_cmp expect2 fuzzy2 &&
212+
test_cmp expect3 fuzzy3 &&
213+
! test_cmp actual1 actual2
214+
'
215+
216+
test_expect_success '%H is not affected by --abbrev-commit' '
217+
git log -1 --format=%H --abbrev-commit --abbrev=20 HEAD >actual &&
218+
len=$(wc -c <actual) &&
219+
test $len = 41
220+
'
221+
222+
test_expect_success '%h is not affected by --abbrev-commit' '
223+
git log -1 --format=%h --abbrev-commit --abbrev=20 HEAD >actual &&
224+
len=$(wc -c <actual) &&
225+
test $len = 21
226+
'
227+
203228
test_expect_success '"%h %gD: %gs" is same as git-reflog' '
204229
git reflog >expect &&
205230
git log -g --format="%h %gD: %gs" >actual &&
@@ -212,6 +237,12 @@ test_expect_success '"%h %gD: %gs" is same as git-reflog (with date)' '
212237
test_cmp expect actual
213238
'
214239

240+
test_expect_success '"%h %gD: %gs" is same as git-reflog (with --abbrev)' '
241+
git reflog --abbrev=13 --date=raw >expect &&
242+
git log -g --abbrev=13 --format="%h %gD: %gs" --date=raw >actual &&
243+
test_cmp expect actual
244+
'
245+
215246
test_expect_success '%gd shortens ref name' '
216247
echo "master@{0}" >expect.gd-short &&
217248
git log -g -1 --format=%gd refs/heads/master >actual.gd-short &&

0 commit comments

Comments
 (0)