summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEtienne Samson <samson.etienne@gmail.com>2018-08-02 21:38:40 +0200
committerPatrick Steinhardt <ps@pks.im>2020-06-27 14:33:58 +0200
commit0668384efe29a4d92ce1e9d1108187599ac9a067 (patch)
treeed3ef8831efcd1bf636fab8d0c7192e41fe24631
parentd6c62852076005053be9169cb4f3cd9cf9db2aea (diff)
downloadlibgit2-0668384efe29a4d92ce1e9d1108187599ac9a067.tar.gz
repo: basic graft API
This represents (old-style) grafted commits, a.k.a an array of overridden parents for a commit's OID.
-rw-r--r--src/graft.c80
-rw-r--r--src/graft.h29
2 files changed, 109 insertions, 0 deletions
diff --git a/src/graft.c b/src/graft.c
new file mode 100644
index 000000000..21d32fb38
--- /dev/null
+++ b/src/graft.c
@@ -0,0 +1,80 @@
+/*
+ * 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 "graft.h"
+
+int git__graft_register(git_graftmap *grafts, const git_oid *oid, git_array_oid_t parents)
+{
+ git_commit_graft *graft;
+ git_oid *parent_oid;
+ int error;
+ size_t i;
+
+ assert(grafts && oid);
+
+ graft = git__calloc(1, sizeof(*graft));
+ GIT_ERROR_CHECK_ALLOC(graft);
+
+ git_array_init_to_size(graft->parents, git_array_size(parents));
+ git_array_foreach(parents, i, parent_oid) {
+ git_oid *id = git_array_alloc(graft->parents);
+ GIT_ERROR_CHECK_ALLOC(id);
+
+ git_oid_cpy(id, parent_oid);
+ }
+ git_oid_cpy(&graft->oid, oid);
+
+ if ((error = git_oidmap_set(grafts, &graft->oid, graft)) < 0)
+ goto cleanup;
+
+ return 0;
+
+cleanup:
+ git_array_clear(graft->parents);
+ git__free(graft);
+ return error;
+}
+
+int git__graft_unregister(git_graftmap *grafts, const git_oid *oid)
+{
+ git_commit_graft *graft;
+ int error;
+
+ assert(grafts && oid);
+
+ if ((graft = git_oidmap_get(grafts, oid)) == NULL)
+ return GIT_ENOTFOUND;
+
+ if ((error = git_oidmap_delete(grafts, oid)) < 0)
+ return error;
+
+ git__free(graft);
+
+ return 0;
+}
+
+void git__graft_clear(git_graftmap *grafts)
+{
+ git_commit_graft *graft;
+
+ assert(grafts);
+
+ git_oidmap_foreach_value(grafts, graft, {
+ git__free(graft->parents.ptr);
+ git__free(graft);
+ });
+
+ git_oidmap_clear(grafts);
+}
+
+int git__graft_for_oid(git_commit_graft **out, git_graftmap *grafts, const git_oid *oid)
+{
+ assert(out && grafts && oid);
+ if ((*out = git_oidmap_get(grafts, oid)) == NULL)
+ return GIT_ENOTFOUND;
+ return 0;
+}
diff --git a/src/graft.h b/src/graft.h
new file mode 100644
index 000000000..e2afb9711
--- /dev/null
+++ b/src/graft.h
@@ -0,0 +1,29 @@
+/*
+ * 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_graft_h__
+#define INCLUDE_graft_h__
+
+#include "common.h"
+#include "oidarray.h"
+#include "oidmap.h"
+
+/** graft commit */
+typedef struct {
+ git_oid oid;
+ git_array_oid_t parents;
+} git_commit_graft;
+
+/* A special type of git_oidmap with git_commit_grafts as values */
+typedef git_oidmap git_graftmap;
+
+int git__graft_register(git_graftmap *grafts, const git_oid *oid, git_array_oid_t parents);
+int git__graft_unregister(git_graftmap *grafts, const git_oid *oid);
+void git__graft_clear(git_graftmap *grafts);
+
+int git__graft_for_oid(git_commit_graft **out, git_graftmap *grafts, const git_oid *oid);
+
+#endif