summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@microsoft.com>2014-10-03 19:02:29 -0400
committerEdward Thomson <ethomson@microsoft.com>2014-10-26 22:59:48 -0400
commit18b00406c6427eb8c9d96864448474e1d85017de (patch)
treeee02fd12ee4b8f84e5ed53ca59ac771734bba16e /src
parent796b03bd4969f170d8e7a7c9edd567f636b58cb3 (diff)
downloadlibgit2-18b00406c6427eb8c9d96864448474e1d85017de.tar.gz
s/git_merge_head/git_annotated_commit
Rename git_merge_head to git_annotated_commit, as it becomes used in more operations than just merge.
Diffstat (limited to 'src')
-rw-r--r--src/annotated_commit.c121
-rw-r--r--src/annotated_commit.h22
-rw-r--r--src/merge.c186
-rw-r--r--src/merge.h14
-rw-r--r--src/rebase.c40
5 files changed, 205 insertions, 178 deletions
diff --git a/src/annotated_commit.c b/src/annotated_commit.c
new file mode 100644
index 000000000..0a917802a
--- /dev/null
+++ b/src/annotated_commit.c
@@ -0,0 +1,121 @@
+/*
+ * 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.
+ */
+
+#include "common.h"
+#include "annotated_commit.h"
+
+#include "git2/commit.h"
+#include "git2/refs.h"
+#include "git2/repository.h"
+#include "git2/annotated_commit.h"
+
+static int annotated_commit_init(
+ git_annotated_commit **out,
+ git_repository *repo,
+ const git_oid *id,
+ const char *ref_name,
+ const char *remote_url)
+{
+ git_annotated_commit *annotated_commit;
+ int error = 0;
+
+ assert(out && id);
+
+ *out = NULL;
+
+ annotated_commit = git__calloc(1, sizeof(git_annotated_commit));
+ GITERR_CHECK_ALLOC(annotated_commit);
+
+ if (ref_name) {
+ annotated_commit->ref_name = git__strdup(ref_name);
+ GITERR_CHECK_ALLOC(annotated_commit->ref_name);
+ }
+
+ if (remote_url) {
+ annotated_commit->remote_url = git__strdup(remote_url);
+ GITERR_CHECK_ALLOC(annotated_commit->remote_url);
+ }
+
+ git_oid_fmt(annotated_commit->id_str, id);
+ annotated_commit->id_str[GIT_OID_HEXSZ] = '\0';
+
+ if ((error = git_commit_lookup(&annotated_commit->commit, repo, id)) < 0) {
+ git_annotated_commit_free(annotated_commit);
+ return error;
+ }
+
+ *out = annotated_commit;
+ return error;
+}
+
+int git_annotated_commit_from_ref(
+ git_annotated_commit **out,
+ git_repository *repo,
+ const git_reference *ref)
+{
+ git_reference *resolved;
+ int error = 0;
+
+ assert(out && repo && ref);
+
+ *out = NULL;
+
+ if ((error = git_reference_resolve(&resolved, ref)) < 0)
+ return error;
+
+ error = annotated_commit_init(out, repo, git_reference_target(resolved),
+ git_reference_name(ref), NULL);
+
+ git_reference_free(resolved);
+ return error;
+}
+
+int git_annotated_commit_lookup(
+ git_annotated_commit **out,
+ git_repository *repo,
+ const git_oid *id)
+{
+ assert(out && repo && id);
+
+ return annotated_commit_init(out, repo, id, NULL, NULL);
+}
+
+int git_annotated_commit_from_fetchhead(
+ git_annotated_commit **out,
+ git_repository *repo,
+ const char *branch_name,
+ const char *remote_url,
+ const git_oid *id)
+{
+ assert(repo && id && branch_name && remote_url);
+
+ return annotated_commit_init(out, repo, id, branch_name, remote_url);
+}
+
+const git_oid *git_annotated_commit_id(
+ const git_annotated_commit *annotated_commit)
+{
+ assert(annotated_commit);
+ return git_commit_id(annotated_commit->commit);
+}
+
+void git_annotated_commit_free(git_annotated_commit *annotated_commit)
+{
+ if (annotated_commit == NULL)
+ return;
+
+ if (annotated_commit->commit != NULL)
+ git_commit_free(annotated_commit->commit);
+
+ if (annotated_commit->ref_name != NULL)
+ git__free(annotated_commit->ref_name);
+
+ if (annotated_commit->remote_url != NULL)
+ git__free(annotated_commit->remote_url);
+
+ git__free(annotated_commit);
+}
diff --git a/src/annotated_commit.h b/src/annotated_commit.h
new file mode 100644
index 000000000..e873184ae
--- /dev/null
+++ b/src/annotated_commit.h
@@ -0,0 +1,22 @@
+/*
+ * 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.
+ */
+#ifndef INCLUDE_annotated_commit_h__
+#define INCLUDE_annotated_commit_h__
+
+#include "git2/oid.h"
+
+/** Internal structure for merge inputs */
+struct git_annotated_commit {
+ git_commit *commit;
+
+ char *ref_name;
+ char *remote_url;
+
+ char id_str[GIT_OID_HEXSZ+1];
+};
+
+#endif
diff --git a/src/merge.c b/src/merge.c
index 3cafc5d03..1c55e797b 100644
--- a/src/merge.c
+++ b/src/merge.c
@@ -27,6 +27,7 @@
#include "filebuf.h"
#include "config.h"
#include "oidarray.h"
+#include "annotated_commit.h"
#include "git2/types.h"
#include "git2/repository.h"
@@ -40,6 +41,7 @@
#include "git2/config.h"
#include "git2/tree.h"
#include "git2/oidarray.h"
+#include "git2/annotated_commit.h"
#include "git2/sys/index.h"
#include "git2/sys/hashsig.h"
@@ -1837,7 +1839,7 @@ done:
static int write_merge_head(
git_repository *repo,
- const git_merge_head *heads[],
+ const git_annotated_commit *heads[],
size_t heads_len)
{
git_filebuf file = GIT_FILEBUF_INIT;
@@ -1852,7 +1854,7 @@ static int write_merge_head(
goto cleanup;
for (i = 0; i < heads_len; i++) {
- if ((error = git_filebuf_printf(&file, "%s\n", heads[i]->oid_str)) < 0)
+ if ((error = git_filebuf_printf(&file, "%s\n", heads[i]->id_str)) < 0)
goto cleanup;
}
@@ -1894,7 +1896,7 @@ cleanup:
}
struct merge_msg_entry {
- const git_merge_head *merge_head;
+ const git_annotated_commit *merge_head;
bool written;
};
@@ -2082,7 +2084,7 @@ static int merge_msg_write_remotes(
static int write_merge_msg(
git_repository *repo,
- const git_merge_head *heads[],
+ const git_annotated_commit *heads[],
size_t heads_len)
{
git_filebuf file = GIT_FILEBUF_INIT;
@@ -2128,7 +2130,7 @@ static int write_merge_msg(
if ((error = git_filebuf_printf(&file,
"%scommit '%s'", (i > 0) ? "; " : "",
- entries[i].merge_head->oid_str)) < 0)
+ entries[i].merge_head->id_str)) < 0)
goto cleanup;
entries[i].written = 1;
@@ -2176,7 +2178,7 @@ static int write_merge_msg(
continue;
if ((error = git_filebuf_printf(&file, "; commit '%s'",
- entries[i].merge_head->oid_str)) < 0)
+ entries[i].merge_head->id_str)) < 0)
goto cleanup;
}
@@ -2198,15 +2200,15 @@ cleanup:
int git_merge__setup(
git_repository *repo,
- const git_merge_head *our_head,
- const git_merge_head *heads[],
+ const git_annotated_commit *our_head,
+ const git_annotated_commit *heads[],
size_t heads_len)
{
int error = 0;
assert (repo && our_head && heads);
- if ((error = git_repository__set_orig_head(repo, &our_head->oid)) == 0 &&
+ if ((error = git_repository__set_orig_head(repo, git_annotated_commit_id(our_head))) == 0 &&
(error = write_merge_head(repo, heads, heads_len)) == 0 &&
(error = write_merge_mode(repo)) == 0) {
error = write_merge_msg(repo, heads, heads_len);
@@ -2218,10 +2220,10 @@ int git_merge__setup(
/* Merge branches */
static int merge_ancestor_head(
- git_merge_head **ancestor_head,
+ git_annotated_commit **ancestor_head,
git_repository *repo,
- const git_merge_head *our_head,
- const git_merge_head **their_heads,
+ const git_annotated_commit *our_head,
+ const git_annotated_commit **their_heads,
size_t their_heads_len)
{
git_oid *oids, ancestor_oid;
@@ -2236,12 +2238,12 @@ static int merge_ancestor_head(
git_oid_cpy(&oids[0], git_commit_id(our_head->commit));
for (i = 0; i < their_heads_len; i++)
- git_oid_cpy(&oids[i + 1], &their_heads[i]->oid);
+ git_oid_cpy(&oids[i + 1], git_annotated_commit_id(their_heads[i]));
if ((error = git_merge_base_many(&ancestor_oid, repo, their_heads_len + 1, oids)) < 0)
goto on_error;
- error = git_merge_head_from_id(ancestor_head, repo, &ancestor_oid);
+ error = git_annotated_commit_lookup(ancestor_head, repo, &ancestor_oid);
on_error:
git__free(oids);
@@ -2265,10 +2267,10 @@ static int merge_normalize_checkout_opts(
git_repository *repo,
git_checkout_options *checkout_opts,
const git_checkout_options *given_checkout_opts,
- const git_merge_head *ancestor_head,
- const git_merge_head *our_head,
+ const git_annotated_commit *ancestor_head,
+ const git_annotated_commit *our_head,
size_t their_heads_len,
- const git_merge_head **their_heads)
+ const git_annotated_commit **their_heads)
{
int error = 0;
@@ -2302,7 +2304,7 @@ static int merge_normalize_checkout_opts(
if (their_heads_len == 1 && their_heads[0]->ref_name)
checkout_opts->their_label = merge_their_label(their_heads[0]->ref_name);
else if (their_heads_len == 1)
- checkout_opts->their_label = their_heads[0]->oid_str;
+ checkout_opts->their_label = their_heads[0]->id_str;
else
checkout_opts->their_label = "theirs";
}
@@ -2516,13 +2518,13 @@ static int merge_state_cleanup(git_repository *repo)
}
static int merge_heads(
- git_merge_head **ancestor_head_out,
- git_merge_head **our_head_out,
+ git_annotated_commit **ancestor_head_out,
+ git_annotated_commit **our_head_out,
git_repository *repo,
- const git_merge_head **their_heads,
+ const git_annotated_commit **their_heads,
size_t their_heads_len)
{
- git_merge_head *ancestor_head = NULL, *our_head = NULL;
+ git_annotated_commit *ancestor_head = NULL, *our_head = NULL;
git_reference *our_ref = NULL;
int error = 0;
@@ -2533,7 +2535,7 @@ static int merge_heads(
goto done;
if ((error = git_reference_lookup(&our_ref, repo, GIT_HEAD_FILE)) < 0 ||
- (error = git_merge_head_from_ref(&our_head, repo, our_ref)) < 0)
+ (error = git_annotated_commit_from_ref(&our_head, repo, our_ref)) < 0)
goto done;
if ((error = merge_ancestor_head(&ancestor_head, repo, our_head, their_heads, their_heads_len)) < 0) {
@@ -2549,8 +2551,8 @@ static int merge_heads(
done:
if (error < 0) {
- git_merge_head_free(ancestor_head);
- git_merge_head_free(our_head);
+ git_annotated_commit_free(ancestor_head);
+ git_annotated_commit_free(our_head);
}
git_reference_free(our_ref);
@@ -2595,10 +2597,10 @@ int git_merge_analysis(
git_merge_analysis_t *analysis_out,
git_merge_preference_t *preference_out,
git_repository *repo,
- const git_merge_head **their_heads,
+ const git_annotated_commit **their_heads,
size_t their_heads_len)
{
- git_merge_head *ancestor_head = NULL, *our_head = NULL;
+ git_annotated_commit *ancestor_head = NULL, *our_head = NULL;
int error = 0;
assert(analysis_out && preference_out && repo && their_heads);
@@ -2623,11 +2625,13 @@ int git_merge_analysis(
goto done;
/* We're up-to-date if we're trying to merge our own common ancestor. */
- if (ancestor_head && git_oid_equal(&ancestor_head->oid, &their_heads[0]->oid))
+ if (ancestor_head && git_oid_equal(
+ git_annotated_commit_id(ancestor_head), git_annotated_commit_id(their_heads[0])))
*analysis_out |= GIT_MERGE_ANALYSIS_UP_TO_DATE;
/* We're fastforwardable if we're our own common ancestor. */
- else if (ancestor_head && git_oid_equal(&ancestor_head->oid, &our_head->oid))
+ else if (ancestor_head && git_oid_equal(
+ git_annotated_commit_id(ancestor_head), git_annotated_commit_id(our_head)))
*analysis_out |= GIT_MERGE_ANALYSIS_FASTFORWARD | GIT_MERGE_ANALYSIS_NORMAL;
/* Otherwise, just a normal merge is possible. */
@@ -2635,21 +2639,21 @@ int git_merge_analysis(
*analysis_out |= GIT_MERGE_ANALYSIS_NORMAL;
done:
- git_merge_head_free(ancestor_head);
- git_merge_head_free(our_head);
+ git_annotated_commit_free(ancestor_head);
+ git_annotated_commit_free(our_head);
return error;
}
int git_merge(
git_repository *repo,
- const git_merge_head **their_heads,
+ const git_annotated_commit **their_heads,
size_t their_heads_len,
const git_merge_options *merge_opts,
const git_checkout_options *given_checkout_opts)
{
git_reference *our_ref = NULL;
git_checkout_options checkout_opts;
- git_merge_head *ancestor_head = NULL, *our_head = NULL;
+ git_annotated_commit *ancestor_head = NULL, *our_head = NULL;
git_tree *ancestor_tree = NULL, *our_tree = NULL, **their_trees = NULL;
git_index *index_new = NULL;
size_t i;
@@ -2712,126 +2716,14 @@ done:
git__free(their_trees);
- git_merge_head_free(our_head);
- git_merge_head_free(ancestor_head);
+ git_annotated_commit_free(our_head);
+ git_annotated_commit_free(ancestor_head);
git_reference_free(our_ref);
return error;
}
-/* Merge heads are the input to merge */
-
-static int merge_head_init(
- git_merge_head **out,
- git_repository *repo,
- const char *ref_name,
- const char *remote_url,
- const git_oid *oid)
-{
- git_merge_head *head;
- int error = 0;
-
- assert(out && oid);
-
- *out = NULL;
-
- head = git__calloc(1, sizeof(git_merge_head));
- GITERR_CHECK_ALLOC(head);
-
- if (ref_name) {
- head->ref_name = git__strdup(ref_name);
- GITERR_CHECK_ALLOC(head->ref_name);
- }
-
- if (remote_url) {
- head->remote_url = git__strdup(remote_url);
- GITERR_CHECK_ALLOC(head->remote_url);
- }
-
- git_oid_cpy(&head->oid, oid);
-
- git_oid_fmt(head->oid_str, oid);
- head->oid_str[GIT_OID_HEXSZ] = '\0';
-
- if ((error = git_commit_lookup(&head->commit, repo, &head->oid)) < 0) {
- git_merge_head_free(head);
- return error;
- }
-
- *out = head;
- return error;
-}
-
-int git_merge_head_from_ref(
- git_merge_head **out,
- git_repository *repo,
- const git_reference *ref)
-{
- git_reference *resolved;
- int error = 0;
-
- assert(out && repo && ref);
-
- *out = NULL;
-
- if ((error = git_reference_resolve(&resolved, ref)) < 0)
- return error;
-
- error = merge_head_init(out, repo, git_reference_name(ref), NULL,
- git_reference_target(resolved));
-
- git_reference_free(resolved);
- return error;
-}
-
-int git_merge_head_from_id(
- git_merge_head **out,
- git_repository *repo,
- const git_oid *oid)
-{
- assert(out && repo && oid);
-
- return merge_head_init(out, repo, NULL, NULL, oid);
-}
-
-int git_merge_head_from_fetchhead(
- git_merge_head **out,
- git_repository *repo,
- const char *branch_name,
- const char *remote_url,
- const git_oid *oid)
-{
- assert(repo && branch_name && remote_url && oid);
-
- return merge_head_init(out, repo, branch_name, remote_url, oid);
-}
-
-const git_oid *git_merge_head_id(
- const git_merge_head *head)
-{
- assert(head);
-
- return &head->oid;
-}
-
-void git_merge_head_free(git_merge_head *head)
-{
- if (head == NULL)
- return;
-
- if (head->commit != NULL)
- git_object_free((git_object *)head->commit);
-
- if (head->ref_name != NULL)
- git__free(head->ref_name);
-
- if (head->remote_url != NULL)
- git__free(head->remote_url);
-
- git__free(head);
-}
-
int git_merge_init_options(git_merge_options *opts, unsigned int version)
{
GIT_INIT_STRUCTURE_FROM_TEMPLATE(
diff --git a/src/merge.h b/src/merge.h
index cc17389ab..2684c15f8 100644
--- a/src/merge.h
+++ b/src/merge.h
@@ -110,16 +110,6 @@ typedef struct {
int binary:1;
} git_merge_diff;
-/** Internal structure for merge inputs */
-struct git_merge_head {
- char *ref_name;
- char *remote_url;
-
- git_oid oid;
- char oid_str[GIT_OID_HEXSZ+1];
- git_commit *commit;
-};
-
int git_merge__bases_many(
git_commit_list **out,
git_revwalk *walk,
@@ -145,8 +135,8 @@ void git_merge_diff_list__free(git_merge_diff_list *diff_list);
int git_merge__setup(
git_repository *repo,
- const git_merge_head *our_head,
- const git_merge_head *heads[],
+ const git_annotated_commit *our_head,
+ const git_annotated_commit *heads[],
size_t heads_len);
int git_merge__check_result(git_repository *repo, git_index *index_new);
diff --git a/src/rebase.c b/src/rebase.c
index 91e3d9058..6905d17dd 100644
--- a/src/rebase.c
+++ b/src/rebase.c
@@ -13,8 +13,10 @@
#include "merge.h"
#include "array.h"
#include "config.h"
+#include "annotated_commit.h"
#include <git2/types.h>
+#include <git2/annotated_commit.h>
#include <git2/rebase.h>
#include <git2/commit.h>
#include <git2/reset.h>
@@ -352,14 +354,14 @@ static int rebase_setupfile(git_rebase *rebase, const char *filename, int flags,
return error;
}
-static const char *rebase_onto_name(const git_merge_head *onto)
+static const char *rebase_onto_name(const git_annotated_commit *onto)
{
if (onto->ref_name && git__strncmp(onto->ref_name, "refs/heads/", 11) == 0)
return onto->ref_name + 11;
else if (onto->ref_name)
return onto->ref_name;
else
- return onto->oid_str;
+ return onto->id_str;
}
static int rebase_setupfiles_merge(git_rebase *rebase)
@@ -519,9 +521,9 @@ done:
static int rebase_init_operations(
git_rebase *rebase,
git_repository *repo,
- const git_merge_head *branch,
- const git_merge_head *upstream,
- const git_merge_head *onto)
+ const git_annotated_commit *branch,
+ const git_annotated_commit *upstream,
+ const git_annotated_commit *onto)
{
git_revwalk *revwalk = NULL;
git_commit *commit;
@@ -534,8 +536,8 @@ static int rebase_init_operations(
upstream = onto;
if ((error = git_revwalk_new(&revwalk, rebase->repo)) < 0 ||
- (error = git_revwalk_push(revwalk, &branch->oid)) < 0 ||
- (error = git_revwalk_hide(revwalk, &upstream->oid)) < 0)
+ (error = git_revwalk_push(revwalk, git_annotated_commit_id(branch))) < 0 ||
+ (error = git_revwalk_hide(revwalk, git_annotated_commit_id(upstream))) < 0)
goto done;
git_revwalk_sorting(revwalk, GIT_SORT_REVERSE | GIT_SORT_TIME);
@@ -565,9 +567,9 @@ done:
static int rebase_init_merge(
git_rebase *rebase,
git_repository *repo,
- const git_merge_head *branch,
- const git_merge_head *upstream,
- const git_merge_head *onto)
+ const git_annotated_commit *branch,
+ const git_annotated_commit *upstream,
+ const git_annotated_commit *onto)
{
if (rebase_init_operations(rebase, repo, branch, upstream, onto) < 0)
return -1;
@@ -581,9 +583,9 @@ static int rebase_init_merge(
static int rebase_init(
git_rebase *rebase,
git_repository *repo,
- const git_merge_head *branch,
- const git_merge_head *upstream,
- const git_merge_head *onto,
+ const git_annotated_commit *branch,
+ const git_annotated_commit *upstream,
+ const git_annotated_commit *onto,
const git_rebase_options *opts)
{
git_buf state_path = GIT_BUF_INIT;
@@ -597,8 +599,8 @@ static int rebase_init(
rebase->orig_head_name = git__strdup(branch->ref_name ? branch->ref_name : ORIG_DETACHED_HEAD);
rebase->quiet = opts->quiet;
- git_oid_cpy(&rebase->orig_head_id, &branch->oid);
- git_oid_cpy(&rebase->onto_id, &onto->oid);
+ git_oid_cpy(&rebase->orig_head_id, git_annotated_commit_id(branch));
+ git_oid_cpy(&rebase->onto_id, git_annotated_commit_id(onto));
if (!rebase->orig_head_name || !rebase->state_path)
return -1;
@@ -613,9 +615,9 @@ static int rebase_init(
int git_rebase_init(
git_rebase **out,
git_repository *repo,
- const git_merge_head *branch,
- const git_merge_head *upstream,
- const git_merge_head *onto,
+ const git_annotated_commit *branch,
+ const git_annotated_commit *upstream,
+ const git_annotated_commit *onto,
const git_signature *signature,
const git_rebase_options *given_opts)
{
@@ -651,7 +653,7 @@ int git_rebase_init(
(error = git_buf_printf(&reflog,
"rebase: checkout %s", rebase_onto_name(onto))) < 0 ||
(error = git_reference_create(&head_ref, repo, GIT_HEAD_FILE,
- &onto->oid, 1, signature, reflog.ptr)) < 0 ||
+ git_annotated_commit_id(onto), 1, signature, reflog.ptr)) < 0 ||
(error = git_checkout_head(repo, &checkout_opts)) < 0)
goto done;