git-notes: fix printing of multi-line notes
The line length was read from the same position every time, causing mangled output when printing notes with multiple lines. Also, adding new-line manually for each line ensures that we get a new-line between commits, matching git-log for commits without notes. Signed-off-by: Tor Arne Vestbø <tavestbo@trolltech.com> Acked-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Tor Arne Vestbø committed
Jan 13, 2009 at 20:57 UTC
22a3d060937072b0f197a8084af879c753c68fe7
2 files changed
+38
-7
notes.c
+7
-6
@@ -110,8 +110,8 @@ void get_commit_notes(const struct commit *commit, struct strbuf *sb,
110
{
111
static const char *utf8 = "utf-8";
112
unsigned char *sha1;
113
- char *msg;
114
- unsigned long msgoffset, msglen;
113
+ char *msg, *msg_p;
114
+ unsigned long linelen, msglen;
115
enum object_type type;
116
117
if (!initialized) {
@@ -148,12 +148,13 @@ void get_commit_notes(const struct commit *commit, struct strbuf *sb,
148
149
strbuf_addstr(sb, "\nNotes:\n");
150
151
- for (msgoffset = 0; msgoffset < msglen;) {
152
- int linelen = strchrnul(msg, '\n') - msg;
151
+ for (msg_p = msg; msg_p < msg + msglen; msg_p += linelen + 1) {
152
+ linelen = strchrnul(msg_p, '\n') - msg_p;
153
154
strbuf_addstr(sb, " ");
155
- strbuf_add(sb, msg + msgoffset, linelen);
156
- msgoffset += linelen;
155
+ strbuf_add(sb, msg_p, linelen);
156
+ strbuf_addch(sb, '\n');
157
}
158
+
159
free(msg);
160
}
t/t3301-notes.sh
+31
-1
@@ -59,7 +59,37 @@ EOF
59
test_expect_success 'show notes' '
60
! (git cat-file commit HEAD | grep b1) &&
61
git log -1 > output &&
62
- git diff expect output
62
+ test_cmp expect output
63
+'
64
+test_expect_success 'create multi-line notes (setup)' '
65
+ : > a3 &&
66
+ git add a3 &&
67
+ test_tick &&
68
+ git commit -m 3rd &&
69
+ MSG="b3
70
+c3c3c3c3
71
+d3d3d3" git notes edit
72
+'
73
+
74
+cat > expect-multiline << EOF
75
+commit 1584215f1d29c65e99c6c6848626553fdd07fd75
76
+Author: A U Thor <author@example.com>
77
+Date: Thu Apr 7 15:15:13 2005 -0700
78
+
79
+ 3rd
80
+
81
+Notes:
82
+ b3
83
+ c3c3c3c3
84
+ d3d3d3
85
+EOF
86
+
87
+printf "\n" >> expect-multiline
88
+cat expect >> expect-multiline
89
+
90
+test_expect_success 'show multi-line notes' '
91
+ git log -2 > output &&
92
+ test_cmp expect-multiline output
93
'
94
95
test_done