summaryrefslogtreecommitdiff
path: root/src/commit.c
diff options
context:
space:
mode:
authorVicent Marti <tanoku@gmail.com>2011-07-09 02:10:46 +0200
committerVicent Marti <tanoku@gmail.com>2011-07-09 02:40:16 +0200
commitafeecf4f262b74270368ef8a70c582ea9d5a18e8 (patch)
treec078ee522e3d9f7bf01fe7e85f5baa7f41dacde4 /src/commit.c
parent2fc78e700cc4684c1e5899d7a4a619da1e3e3679 (diff)
downloadlibgit2-afeecf4f262b74270368ef8a70c582ea9d5a18e8.tar.gz
odb: Direct writes are back
DIRECT WRITES ARE BACK AND FASTER THAN EVER. The streaming writer to the ODB was an overkill for the smaller objects like Commit and Tags; most of the streaming logic was taking too long. This commit makes Commits, Tags and Trees to be built-up in memory, and then written to disk in 2 pushes (header + data), instead of streaming everything. This is *always* faster, even for big files (since the git_filebuf class still does streaming writes when the memory cache overflows). This is also a gazillion lines of code smaller, because we don't have to precompute the final size of the object before starting the stream (this was kind of defeating the point of streaming, anyway). Blobs are still written with full streaming instead of loading them in memory, since this is still the fastest way. A new `git_buf` class has been added. It's missing some features, but it'll get there.
Diffstat (limited to 'src/commit.c')
-rw-r--r--src/commit.c55
1 files changed, 21 insertions, 34 deletions
diff --git a/src/commit.c b/src/commit.c
index f7c26a68..05f75731 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -116,53 +116,36 @@ int git_commit_create(
int parent_count,
const git_commit *parents[])
{
- size_t final_size = 0;
- int message_length, author_length, committer_length;
-
- char *author_str, *committer_str;
-
+ git_buf commit = GIT_BUF_INIT;
int error, i;
- git_odb_stream *stream;
-
- message_length = strlen(message);
- author_length = git_signature__write(&author_str, "author ", author);
- committer_length = git_signature__write(&committer_str, "committer ", committer);
-
- if (author_length < 0 || committer_length < 0)
- return git__throw(GIT_EINVALIDARGS, "Cannot create commit. Failed to parse signature");
-
- final_size += GIT_OID_LINE_LENGTH("tree");
- final_size += GIT_OID_LINE_LENGTH("parent") * parent_count;
- final_size += author_length;
- final_size += committer_length;
- final_size += 1 + message_length;
-
- if ((error = git_odb_open_wstream(&stream, repo->db, final_size, GIT_OBJ_COMMIT)) < GIT_SUCCESS)
- return git__rethrow(error, "Failed to create commit");
if (git_object_owner((const git_object *)tree) != repo)
return git__throw(GIT_EINVALIDARGS, "The given tree does not belong to this repository");
- git__write_oid(stream, "tree", git_object_id((const git_object *)tree));
+ git_oid__writebuf(&commit, "tree ", git_object_id((const git_object *)tree));
for (i = 0; i < parent_count; ++i) {
- if (git_object_owner((const git_object *)parents[i]) != repo)
- return git__throw(GIT_EINVALIDARGS, "The given parent does not belong to this repository");
+ if (git_object_owner((const git_object *)parents[i]) != repo) {
+ error = git__throw(GIT_EINVALIDARGS, "The given parent does not belong to this repository");
+ goto cleanup;
+ }
- git__write_oid(stream, "parent", git_object_id((const git_object *)parents[i]));
+ git_oid__writebuf(&commit, "parent ", git_object_id((const git_object *)parents[i]));
}
- stream->write(stream, author_str, author_length);
- free(author_str);
+ git_signature__writebuf(&commit, "author ", author);
+ git_signature__writebuf(&commit, "committer ", committer);
- stream->write(stream, committer_str, committer_length);
- free(committer_str);
+ git_buf_putc(&commit, '\n');
+ git_buf_puts(&commit, message);
- stream->write(stream, "\n", 1);
- stream->write(stream, message, message_length);
+ if (git_buf_oom(&commit)) {
+ error = git__throw(GIT_ENOMEM, "Not enough memory to build the commit data");
+ goto cleanup;
+ }
- error = stream->finalize_write(oid, stream);
- stream->free(stream);
+ error = git_odb_write(oid, git_repository_database(repo), commit.ptr, commit.size, GIT_OBJ_COMMIT);
+ git_buf_free(&commit);
if (error == GIT_SUCCESS && update_ref != NULL) {
git_reference *head;
@@ -192,6 +175,10 @@ int git_commit_create(
return git__rethrow(error, "Failed to create commit");
return GIT_SUCCESS;
+
+cleanup:
+ git_buf_free(&commit);
+ return error;
}
int commit_parse_buffer(git_commit *commit, const void *data, size_t len)