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:
Diffstat (limited to 'src/commit.c')
-rw-r--r--src/commit.c204
1 files changed, 80 insertions, 124 deletions
diff --git a/src/commit.c b/src/commit.c
index 2bf12f3a5..c7b83ed43 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2009-2012 the libgit2 contributors
+ * Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
@@ -18,31 +18,22 @@
#include <stdarg.h>
-#define COMMIT_BASIC_PARSE 0x0
-#define COMMIT_FULL_PARSE 0x1
-
-#define COMMIT_PRINT(commit) {\
- char oid[41]; oid[40] = 0;\
- git_oid_fmt(oid, &commit->object.id);\
- printf("Oid: %s | In degree: %d | Time: %u\n", oid, commit->in_degree, commit->commit_time);\
-}
-
static void clear_parents(git_commit *commit)
{
- unsigned int i;
+ size_t i;
- for (i = 0; i < commit->parent_oids.length; ++i) {
- git_oid *parent = git_vector_get(&commit->parent_oids, i);
+ for (i = 0; i < commit->parent_ids.length; ++i) {
+ git_oid *parent = git_vector_get(&commit->parent_ids, i);
git__free(parent);
}
- git_vector_clear(&commit->parent_oids);
+ git_vector_clear(&commit->parent_ids);
}
void git_commit__free(git_commit *commit)
{
clear_parents(commit);
- git_vector_free(&commit->parent_oids);
+ git_vector_free(&commit->parent_ids);
git_signature_free(commit->author);
git_signature_free(commit->committer);
@@ -52,11 +43,6 @@ void git_commit__free(git_commit *commit)
git__free(commit);
}
-const git_oid *git_commit_id(git_commit *c)
-{
- return git_object_id((git_object *)c);
-}
-
int git_commit_create_v(
git_oid *oid,
git_repository *repo,
@@ -90,66 +76,6 @@ int git_commit_create_v(
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;
- }
-
- /* 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(
git_oid *oid,
git_repository *repo,
@@ -162,7 +88,7 @@ int git_commit_create(
int parent_count,
const git_commit *parents[])
{
- git_buf commit = GIT_BUF_INIT, cleaned_message = GIT_BUF_INIT;
+ git_buf commit = GIT_BUF_INIT;
int i;
git_odb *odb;
@@ -183,15 +109,9 @@ int git_commit_create(
git_buf_putc(&commit, '\n');
- /* Remove comments by default */
- if (git_message_prettify(&cleaned_message, message, 1) < 0)
+ if (git_buf_puts(&commit, message) < 0)
goto on_error;
- if (git_buf_puts(&commit, git_buf_cstr(&cleaned_message)) < 0)
- goto on_error;
-
- git_buf_free(&cleaned_message);
-
if (git_repository_odb__weakptr(&odb, repo) < 0)
goto on_error;
@@ -201,13 +121,12 @@ int git_commit_create(
git_buf_free(&commit);
if (update_ref != NULL)
- return update_reference(repo, oid, update_ref);
+ return git_reference__update_terminal(repo, update_ref, oid);
return 0;
on_error:
git_buf_free(&commit);
- git_buf_free(&cleaned_message);
giterr_set(GITERR_OBJECT, "Failed to create commit.");
return -1;
}
@@ -216,27 +135,25 @@ int git_commit__parse_buffer(git_commit *commit, const void *data, size_t len)
{
const char *buffer = data;
const char *buffer_end = (const char *)data + len;
+ git_oid parent_id;
- git_oid parent_oid;
-
- git_vector_init(&commit->parent_oids, 4, NULL);
+ if (git_vector_init(&commit->parent_ids, 4, NULL) < 0)
+ return -1;
- if (git_oid__parse(&commit->tree_oid, &buffer, buffer_end, "tree ") < 0)
+ if (git_oid__parse(&commit->tree_id, &buffer, buffer_end, "tree ") < 0)
goto bad_buffer;
/*
* TODO: commit grafts!
*/
- 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);
+ while (git_oid__parse(&parent_id, &buffer, buffer_end, "parent ") == 0) {
+ git_oid *new_id = git__malloc(sizeof(git_oid));
+ GITERR_CHECK_ALLOC(new_id);
- git_oid_cpy(new_oid, &parent_oid);
+ git_oid_cpy(new_id, &parent_id);
- if (git_vector_insert(&commit->parent_oids, new_oid) < 0)
+ if (git_vector_insert(&commit->parent_ids, new_id) < 0)
return -1;
}
@@ -253,24 +170,30 @@ int git_commit__parse_buffer(git_commit *commit, const void *data, size_t len)
if (git_signature__parse(commit->committer, &buffer, buffer_end, "committer ", '\n') < 0)
return -1;
- if (git__prefixcmp(buffer, "encoding ") == 0) {
- const char *encoding_end;
- buffer += strlen("encoding ");
+ /* Parse add'l header entries until blank line found */
+ while (buffer < buffer_end && *buffer != '\n') {
+ const char *eoln = buffer;
+ while (eoln < buffer_end && *eoln != '\n')
+ ++eoln;
+
+ if (git__prefixcmp(buffer, "encoding ") == 0) {
+ buffer += strlen("encoding ");
- encoding_end = buffer;
- while (encoding_end < buffer_end && *encoding_end != '\n')
- encoding_end++;
+ commit->message_encoding = git__strndup(buffer, eoln - buffer);
+ GITERR_CHECK_ALLOC(commit->message_encoding);
+ }
- commit->message_encoding = git__strndup(buffer, encoding_end - buffer);
- GITERR_CHECK_ALLOC(commit->message_encoding);
+ if (eoln < buffer_end && *eoln == '\n')
+ ++eoln;
- buffer = encoding_end;
+ buffer = eoln;
}
- /* parse commit message */
- while (buffer < buffer_end - 1 && *buffer == '\n')
+ /* buffer is now at the end of the header, double-check and move forward into the message */
+ if (buffer < buffer_end && *buffer == '\n')
buffer++;
+ /* parse commit message */
if (buffer <= buffer_end) {
commit->message = git__strndup(buffer, buffer_end - buffer);
GITERR_CHECK_ALLOC(commit->message);
@@ -290,7 +213,7 @@ int git_commit__parse(git_commit *commit, git_odb_object *obj)
}
#define GIT_COMMIT_GETTER(_rvalue, _name, _return) \
- _rvalue git_commit_##_name(git_commit *commit) \
+ _rvalue git_commit_##_name(const git_commit *commit) \
{\
assert(commit); \
return _return; \
@@ -302,33 +225,66 @@ GIT_COMMIT_GETTER(const char *, message, commit->message)
GIT_COMMIT_GETTER(const char *, message_encoding, commit->message_encoding)
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, commit->parent_oids.length)
-GIT_COMMIT_GETTER(const git_oid *, tree_oid, &commit->tree_oid);
+GIT_COMMIT_GETTER(unsigned int, parentcount, (unsigned int)commit->parent_ids.length)
+GIT_COMMIT_GETTER(const git_oid *, tree_id, &commit->tree_id);
+int git_commit_tree(git_tree **tree_out, const git_commit *commit)
+{
+ assert(commit);
+ return git_tree_lookup(tree_out, commit->object.repo, &commit->tree_id);
+}
-int git_commit_tree(git_tree **tree_out, git_commit *commit)
+const git_oid *git_commit_parent_id(git_commit *commit, unsigned int n)
{
assert(commit);
- return git_tree_lookup(tree_out, commit->object.repo, &commit->tree_oid);
+
+ return git_vector_get(&commit->parent_ids, n);
}
int git_commit_parent(git_commit **parent, git_commit *commit, unsigned int n)
{
- git_oid *parent_oid;
+ const git_oid *parent_id;
assert(commit);
- parent_oid = git_vector_get(&commit->parent_oids, n);
- if (parent_oid == NULL) {
+ parent_id = git_commit_parent_id(commit, n);
+ if (parent_id == NULL) {
giterr_set(GITERR_INVALID, "Parent %u does not exist", n);
return GIT_ENOTFOUND;
}
- return git_commit_lookup(parent, commit->object.repo, parent_oid);
+ return git_commit_lookup(parent, commit->object.repo, parent_id);
}
-const git_oid *git_commit_parent_oid(git_commit *commit, unsigned int n)
+int git_commit_nth_gen_ancestor(
+ git_commit **ancestor,
+ const git_commit *commit,
+ unsigned int n)
{
- assert(commit);
+ git_commit *current, *parent;
+ int error;
+
+ assert(ancestor && commit);
+
+ current = (git_commit *)commit;
+
+ if (n == 0)
+ return git_commit_lookup(
+ ancestor,
+ commit->object.repo,
+ git_object_id((const git_object *)commit));
+
+ while (n--) {
+ error = git_commit_parent(&parent, (git_commit *)current, 0);
- return git_vector_get(&commit->parent_oids, n);
+ if (current != commit)
+ git_commit_free(current);
+
+ if (error < 0)
+ return error;
+
+ current = parent;
+ }
+
+ *ancestor = parent;
+ return 0;
}