8000 Add example program to emulate 'git log' and create pathspec matching API by arrbee · Pull Request #1711 · libgit2/libgit2 · GitHub
[go: up one dir, main page]

Skip to content

Add example program to emulate 'git log' and create pathspec matching API #1711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add raw header access to commit API
  • Loading branch information
arrbee committed Jul 3, 2013
commit 270cb34f13ecb22acda6ca40e5dbbde78cb0112b
8 changes: 8 additions & 0 deletions include/git2/commit.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ GIT_EXTERN(const git_signature *) git_commit_committer(const git_commit *commit)
*/
GIT_EXTERN(const git_signature *) git_commit_author(const git_commit *commit);

/**
* Get the full raw text of the commit header.
*
* @param commit a previously loaded commit
* @return the header text of the commit
*/
GIT_EXTERN(const char *) git_commit_raw_header(const git_commit *commit);

/**
* Get the tree pointed to by a commit.
*
Expand Down
47 changes: 37 additions & 10 deletions src/commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ void git_commit__free(void *_commit)
git_signature_free(commit->author);
git_signature_free(commit->committer);

git__free(commit->raw_header);
git__free(commit->message);
git__free(commit->message_encoding);
git__free(commit);
Expand Down Expand Up @@ -171,11 +172,33 @@ int git_commit_create(
int git_commit__parse(void *_commit, git_odb_object *odb_obj)
{
git_commit *commit = _commit;
const char *buffer = git_odb_object_data(odb_obj);
const char *buffer_end = buffer + git_odb_object_size(odb_obj);
const char *buffer_start = git_odb_object_data(odb_obj), *buffer;
const char *buffer_end = buffer_start + git_odb_object_size(odb_obj);
git_oid parent_id;
size_t parent_count = 0, header_len;

if (git_vector_init(&commit->parent_ids, 4, NULL) < 0)
/* find end-of-header (counting parents as we go) */
for (buffer = buffer_start; buffer < buffer_end; ++buffer) {
if (!strncmp("\n\n", buffer, 2)) {
++buffer;
break;
}
if (!strncmp("\nparent ", buffer, strlen("\nparent ")))
++parent_count;
}

header_len = buffer - buffer_start;
commit->raw_header = git__strndup(buffer_start, header_len);
GITERR_CHECK_ALLOC(commit->raw_header);

/* point "buffer" to header data */
buffer = commit->raw_header;
buffer_end = commit->raw_header + header_len;

if (parent_count < 1)
parent_count = 1;

if (git_vector_init(&commit->parent_ids, parent_count, NULL) < 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be an ideal place to use the typedef array you created. The vector here has always felt overkill.

return -1;

if (git_oid__parse(&commit->tree_id, &buffer, buffer_end, "tree ") < 0)
Expand Down Expand Up @@ -208,8 +231,8 @@ int git_commit__parse(void *_commit, git_odb_object *odb_obj)
if (git_signature__parse(commit->committer, &buffer, buffer_end, "committer ", '\n') < 0)
return -1;

/* Parse add'l header entries until blank line found */
while (buffer < buffer_end && *buffer != '\n') {
/* Parse add'l header entries */
while (buffer < buffer_end) {
const char *eoln = buffer;
while (eoln < buffer_end && *eoln != '\n')
++eoln;
Expand All @@ -223,15 +246,18 @@ int git_commit__parse(void *_commit, git_odb_object *odb_obj)

if (eoln < buffer_end && *eoln == '\n')
++eoln;

buffer = eoln;
}

/* buffer is now at the end of the header, double-check and move forward into the message */
if (buffer < buffer_end && *buffer == '\n')
buffer++;
/* point "buffer" to data after header */
buffer = git_odb_object_data(odb_obj);
buffer_end = buffer + git_odb_object_size(odb_obj);

buffer += header_len;
if (*buffer == '\n')
++buffer;

/* parse commit message */
/* extract commit message */
if (buffer <= buffer_end) {
commit->message = git__strndup(buffer, buffer_end - buffer);
GITERR_CHECK_ALLOC(commit->message);
Expand All @@ -255,6 +281,7 @@ GIT_COMMIT_GETTER(const git_signature *, author, commit->author)
GIT_COMMIT_GETTER(const git_signature *, committer, commit->committer)
GIT_COMMIT_GETTER(const char *, message, commit->message)
GIT_COMMIT_GETTER(const char *, message_encoding, commit->message_encoding)
GIT_COMMIT_GETTER(const char *, raw_header, commit->raw_header)
GIT_COMMIT_GETTER(git_time_t, time, commit->committer->when.time)
GIT_COMMIT_GETTER(int, time_offset, commit->committer->when.offset)
GIT_COMMIT_GETTER(unsigned int, parentcount, (unsigned int)commit->parent_ids.length)
Expand Down
1 change: 1 addition & 0 deletions src/commit.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ struct git_commit {

char *message_encoding;
char *message;
char *raw_header;
};

void git_commit__free(void *commit);
Expand Down
0