Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/libgit2.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVicent Marti <tanoku@gmail.com>2012-05-19 03:46:11 +0400
committerVicent Marti <tanoku@gmail.com>2012-05-19 03:46:11 +0400
commit5b9fac39d8a76b9139667c26a63e6b3f204b3977 (patch)
treee6ba28025f92c16563c4ffa8bc60b95f17d69691 /src/commit.c
parent7ef9f1b5606c2672105ecbbf34c022a71ef212fe (diff)
parentad5df35a47d56c3d716d7a56eac4aeb611987c11 (diff)
Merge branch 'development'v0.17.0
Conflicts: .travis.yml
Diffstat (limited to 'src/commit.c')
-rw-r--r--src/commit.c192
1 files changed, 113 insertions, 79 deletions
diff --git a/src/commit.c b/src/commit.c
index 5d077d54e..2bf12f3a5 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2009-2011 the libgit2 contributors
+ * Copyright (C) 2009-2012 the libgit2 contributors
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
@@ -14,6 +14,7 @@
#include "odb.h"
#include "commit.h"
#include "signature.h"
+#include "message.h"
#include <stdarg.h>
@@ -69,24 +70,84 @@ int git_commit_create_v(
...)
{
va_list ap;
- int i, error;
+ int i, res;
const git_commit **parents;
parents = git__malloc(parent_count * sizeof(git_commit *));
+ GITERR_CHECK_ALLOC(parents);
va_start(ap, parent_count);
for (i = 0; i < parent_count; ++i)
parents[i] = va_arg(ap, const git_commit *);
va_end(ap);
- error = git_commit_create(
+ res = git_commit_create(
oid, repo, update_ref, author, committer,
message_encoding, message,
tree, parent_count, parents);
git__free((void *)parents);
+ return res;
+}
+
+/* Update the reference named `ref_name` so it points to `oid` */
+static int update_reference(git_repository *repo, git_oid *oid, const char *ref_name)
+{
+ git_reference *ref;
+ int res;
+
+ res = git_reference_lookup(&ref, repo, ref_name);
+
+ /* If we haven't found the reference at all, we assume we need to create
+ * a new reference and that's it */
+ if (res == GIT_ENOTFOUND) {
+ giterr_clear();
+ return git_reference_create_oid(NULL, repo, ref_name, oid, 1);
+ }
+
+ if (res < 0)
+ return -1;
+
+ /* If we have found a reference, but it's symbolic, we need to update
+ * the direct reference it points to */
+ if (git_reference_type(ref) == GIT_REF_SYMBOLIC) {
+ git_reference *aux;
+ const char *sym_target;
+
+ /* The target pointed at by this reference */
+ sym_target = git_reference_target(ref);
+
+ /* resolve the reference to the target it points to */
+ res = git_reference_resolve(&aux, ref);
+
+ /*
+ * if the symbolic reference pointed to an inexisting ref,
+ * this is means we're creating a new branch, for example.
+ * We need to create a new direct reference with that name
+ */
+ if (res == GIT_ENOTFOUND) {
+ giterr_clear();
+ res = git_reference_create_oid(NULL, repo, sym_target, oid, 1);
+ git_reference_free(ref);
+ return res;
+ }
+
+ /* free the original symbolic reference now; not before because
+ * we're using the `sym_target` pointer */
+ git_reference_free(ref);
+
+ if (res < 0)
+ return -1;
+
+ /* store the newly found direct reference in its place */
+ ref = aux;
+ }
- return error;
+ /* ref is made to point to `oid`: ref is either the original reference,
+ * or the target of the symbolic reference we've looked up */
+ res = git_reference_set_oid(ref, oid);
+ git_reference_free(ref);
+ return res;
}
int git_commit_create(
@@ -101,21 +162,16 @@ int git_commit_create(
int parent_count,
const git_commit *parents[])
{
- git_buf commit = GIT_BUF_INIT;
- int error, i;
+ git_buf commit = GIT_BUF_INIT, cleaned_message = GIT_BUF_INIT;
+ int i;
git_odb *odb;
- if (git_object_owner((const git_object *)tree) != repo)
- return git__throw(GIT_EINVALIDARGS, "The given tree does not belong to this repository");
+ assert(git_object_owner((const git_object *)tree) == repo);
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) {
- error = git__throw(GIT_EINVALIDARGS, "The given parent does not belong to this repository");
- goto cleanup;
- }
-
+ assert(git_object_owner((const git_object *)parents[i]) == repo);
git_oid__writebuf(&commit, "parent ", git_object_id((const git_object *)parents[i]));
}
@@ -126,65 +182,34 @@ int git_commit_create(
git_buf_printf(&commit, "encoding %s\n", message_encoding);
git_buf_putc(&commit, '\n');
- git_buf_puts(&commit, message);
- if (git_buf_oom(&commit)) {
- error = git__throw(git_buf_lasterror(&commit),
- "Not enough memory to build the commit data");
- goto cleanup;
- }
+ /* Remove comments by default */
+ if (git_message_prettify(&cleaned_message, message, 1) < 0)
+ goto on_error;
- error = git_repository_odb__weakptr(&odb, repo);
- if (error < GIT_SUCCESS)
- goto cleanup;
-
- error = git_odb_write(oid, odb, commit.ptr, commit.size, GIT_OBJ_COMMIT);
- git_buf_free(&commit);
-
- if (error == GIT_SUCCESS && update_ref != NULL) {
- git_reference *head;
- git_reference *target;
-
- error = git_reference_lookup(&head, repo, update_ref);
- if (error < GIT_SUCCESS)
- return git__rethrow(error, "Failed to create commit");
-
- error = git_reference_resolve(&target, head);
- if (error < GIT_SUCCESS) {
- if (error != GIT_ENOTFOUND) {
- git_reference_free(head);
- return git__rethrow(error, "Failed to create commit");
- }
- /*
- * The target of the reference was not found. This can happen
- * just after a repository has been initialized (the master
- * branch doesn't exist yet, as it doesn't have anything to
- * point to) or after an orphan checkout, so if the target
- * branch doesn't exist yet, create it and return.
- */
- error = git_reference_create_oid(&target, repo, git_reference_target(head), oid, 1);
+ if (git_buf_puts(&commit, git_buf_cstr(&cleaned_message)) < 0)
+ goto on_error;
- git_reference_free(head);
- if (error == GIT_SUCCESS)
- git_reference_free(target);
+ git_buf_free(&cleaned_message);
- return error;
- }
+ if (git_repository_odb__weakptr(&odb, repo) < 0)
+ goto on_error;
- error = git_reference_set_oid(target, oid);
+ if (git_odb_write(oid, odb, commit.ptr, commit.size, GIT_OBJ_COMMIT) < 0)
+ goto on_error;
- git_reference_free(head);
- git_reference_free(target);
- }
+ git_buf_free(&commit);
- if (error < GIT_SUCCESS)
- return git__rethrow(error, "Failed to create commit");
+ if (update_ref != NULL)
+ return update_reference(repo, oid, update_ref);
- return GIT_SUCCESS;
+ return 0;
-cleanup:
+on_error:
git_buf_free(&commit);
- return error;
+ git_buf_free(&cleaned_message);
+ giterr_set(GITERR_OBJECT, "Failed to create commit.");
+ return -1;
}
int git_commit__parse_buffer(git_commit *commit, const void *data, size_t len)
@@ -193,35 +218,40 @@ int git_commit__parse_buffer(git_commit *commit, const void *data, size_t len)
const char *buffer_end = (const char *)data + len;
git_oid parent_oid;
- int error;
git_vector_init(&commit->parent_oids, 4, NULL);
- if ((error = git_oid__parse(&commit->tree_oid, &buffer, buffer_end, "tree ")) < GIT_SUCCESS)
- return git__rethrow(error, "Failed to parse buffer");
+ if (git_oid__parse(&commit->tree_oid, &buffer, buffer_end, "tree ") < 0)
+ goto bad_buffer;
/*
* TODO: commit grafts!
*/
- while (git_oid__parse(&parent_oid, &buffer, buffer_end, "parent ") == GIT_SUCCESS) {
+ while (git_oid__parse(&parent_oid, &buffer, buffer_end, "parent ") == 0) {
git_oid *new_oid;
new_oid = git__malloc(sizeof(git_oid));
+ GITERR_CHECK_ALLOC(new_oid);
+
git_oid_cpy(new_oid, &parent_oid);
- if (git_vector_insert(&commit->parent_oids, new_oid) < GIT_SUCCESS)
- return GIT_ENOMEM;
+ if (git_vector_insert(&commit->parent_oids, new_oid) < 0)
+ return -1;
}
commit->author = git__malloc(sizeof(git_signature));
- if ((error = git_signature__parse(commit->author, &buffer, buffer_end, "author ", '\n')) < GIT_SUCCESS)
- return git__rethrow(error, "Failed to parse commit");
+ GITERR_CHECK_ALLOC(commit->author);
+
+ if (git_signature__parse(commit->author, &buffer, buffer_end, "author ", '\n') < 0)
+ return -1;
/* Always parse the committer; we need the commit time */
commit->committer = git__malloc(sizeof(git_signature));
- if ((error = git_signature__parse(commit->committer, &buffer, buffer_end, "committer ", '\n')) < GIT_SUCCESS)
- return git__rethrow(error, "Failed to parse commit");
+ GITERR_CHECK_ALLOC(commit->committer);
+
+ if (git_signature__parse(commit->committer, &buffer, buffer_end, "committer ", '\n') < 0)
+ return -1;
if (git__prefixcmp(buffer, "encoding ") == 0) {
const char *encoding_end;
@@ -232,8 +262,7 @@ int git_commit__parse_buffer(git_commit *commit, const void *data, size_t len)
encoding_end++;
commit->message_encoding = git__strndup(buffer, encoding_end - buffer);
- if (!commit->message_encoding)
- return GIT_ENOMEM;
+ GITERR_CHECK_ALLOC(commit->message_encoding);
buffer = encoding_end;
}
@@ -244,11 +273,14 @@ int git_commit__parse_buffer(git_commit *commit, const void *data, size_t len)
if (buffer <= buffer_end) {
commit->message = git__strndup(buffer, buffer_end - buffer);
- if (!commit->message)
- return GIT_ENOMEM;
+ GITERR_CHECK_ALLOC(commit->message);
}
- return GIT_SUCCESS;
+ return 0;
+
+bad_buffer:
+ giterr_set(GITERR_OBJECT, "Failed to parse bad commit object");
+ return -1;
}
int git_commit__parse(git_commit *commit, git_odb_object *obj)
@@ -286,8 +318,10 @@ int git_commit_parent(git_commit **parent, git_commit *commit, unsigned int n)
assert(commit);
parent_oid = git_vector_get(&commit->parent_oids, n);
- if (parent_oid == NULL)
- return git__throw(GIT_ENOTFOUND, "Parent %u does not exist", n);
+ if (parent_oid == NULL) {
+ giterr_set(GITERR_INVALID, "Parent %u does not exist", n);
+ return GIT_ENOTFOUND;
+ }
return git_commit_lookup(parent, commit->object.repo, parent_oid);
}