Skip to content

Commit 598f069

Browse files
committed
commit: Introduce git_commit_message_raw()
1 parent 5bfead1 commit 598f069

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

include/git2/commit.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,22 @@ GIT_EXTERN(const char *) git_commit_message_encoding(const git_commit *commit);
100100
/**
101101
* Get the full message of a commit.
102102
*
103+
* The returned message will be slightly prettified by removing any
104+
* potential leading newlines.
105+
*
103106
* @param commit a previously loaded commit.
104107
* @return the message of a commit
105108
*/
106109
GIT_EXTERN(const char *) git_commit_message(const git_commit *commit);
107110

111+
/**
112+
* Get the full raw message of a commit.
113+
*
114+
* @param commit a previously loaded commit.
115+
* @return the raw message of a commit
116+
*/
117+
GIT_EXTERN(const char *) git_commit_message_raw(const git_commit *commit);
118+
108119
/**
109120
* Get the commit time (i.e. committer time) of a commit.
110121
*

src/commit.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void git_commit__free(void *_commit)
2929
git_signature_free(commit->committer);
3030

3131
git__free(commit->raw_header);
32-
git__free(commit->message);
32+
git__free(commit->raw_message);
3333
git__free(commit->message_encoding);
3434

3535
git__free(commit);
@@ -240,13 +240,13 @@ int git_commit__parse(void *_commit, git_odb_object *odb_obj)
240240
buffer_end = buffer + git_odb_object_size(odb_obj);
241241

242242
buffer += header_len;
243-
while (buffer < buffer_end && *buffer == '\n')
243+
if (*buffer == '\n')
244244
++buffer;
245245

246246
/* extract commit message */
247247
if (buffer <= buffer_end) {
248-
commit->message = git__strndup(buffer, buffer_end - buffer);
249-
GITERR_CHECK_ALLOC(commit->message);
248+
commit->raw_message = git__strndup(buffer, buffer_end - buffer);
249+
GITERR_CHECK_ALLOC(commit->raw_message);
250250
}
251251

252252
return 0;
@@ -265,14 +265,27 @@ int git_commit__parse(void *_commit, git_odb_object *odb_obj)
265265

266266
GIT_COMMIT_GETTER(const git_signature *, author, commit->author)
267267
GIT_COMMIT_GETTER(const git_signature *, committer, commit->committer)
268-
GIT_COMMIT_GETTER(const char *, message, commit->message)
268+
GIT_COMMIT_GETTER(const char *, message_raw, commit->raw_message)
269269
GIT_COMMIT_GETTER(const char *, message_encoding, commit->message_encoding)
270270
GIT_COMMIT_GETTER(const char *, raw_header, commit->raw_header)
271271
GIT_COMMIT_GETTER(git_time_t, time, commit->committer->when.time)
272272
GIT_COMMIT_GETTER(int, time_offset, commit->committer->when.offset)
273273
GIT_COMMIT_GETTER(unsigned int, parentcount, (unsigned int)git_array_size(commit->parent_ids))
274274
GIT_COMMIT_GETTER(const git_oid *, tree_id, &commit->tree_id);
275275

276+
const char *git_commit_message(const git_commit *commit)
277+
{
278+
const char *message = commit->raw_message;
279+
280+
assert(commit);
281+
282+
/* trim leading newlines from raw message */
283+
while (*message && *message == '\n')
284+
++message;
285+
286+
return message;
287+
}
288+
276289
int git_commit_tree(git_tree **tree_out, const git_commit *commit)
277290
{
278291
assert(commit);

src/commit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct git_commit {
2424
git_signature *committer;
2525

2626
char *message_encoding;
27-
char *message;
27+
char *raw_message;
2828
char *raw_header;
2929
};
3030

tests-clar/commit/parse.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,13 @@ committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
382382
This commit has a few LF at the start of the commit message";
383383
const char *message =
384384
"This commit has a few LF at the start of the commit message";
385-
385+
const char *raw_message =
386+
"\n\
387+
\n\
388+
This commit has a few LF at the start of the commit message";
386389
cl_git_pass(parse_commit(&commit, buffer));
387390
cl_assert_equal_s(message, git_commit_message(commit));
391+
cl_assert_equal_s(raw_message, git_commit_message_raw(commit));
388392
git_commit__free(commit);
389393
}
390394

@@ -400,8 +404,10 @@ committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
400404
\n\
401405
\n";
402406
const char *message = "";
407+
const char *raw_message = "\n\n";
403408

404409
cl_git_pass(parse_commit(&commit, buffer));
405410
cl_assert_equal_s(message, git_commit_message(commit));
411+
cl_assert_equal_s(raw_message, git_commit_message_raw(commit));
406412
git_commit__free(commit);
407413
}

0 commit comments

Comments
 (0)