Code

builtin/notes.c: Refactor creation of notes commits.
[git.git] / notes-merge.c
index ab9885039ee00c8f847bb9cc9ab26e103cfd1cca..b9956c3bf3e43bc344de4fcb64a7f2c28e2346bb 100644 (file)
@@ -1,6 +1,7 @@
 #include "cache.h"
 #include "commit.h"
 #include "refs.h"
+#include "notes.h"
 #include "notes-merge.h"
 
 void init_notes_merge_options(struct notes_merge_options *o)
@@ -17,6 +18,32 @@ void init_notes_merge_options(struct notes_merge_options *o)
                } \
        } while (0)
 
+void create_notes_commit(struct notes_tree *t, struct commit_list *parents,
+                        const char *msg, unsigned char *result_sha1)
+{
+       unsigned char tree_sha1[20];
+
+       assert(t->initialized);
+
+       if (write_notes_tree(t, tree_sha1))
+               die("Failed to write notes tree to database");
+
+       if (!parents) {
+               /* Deduce parent commit from t->ref */
+               unsigned char parent_sha1[20];
+               if (!read_ref(t->ref, parent_sha1)) {
+                       struct commit *parent = lookup_commit(parent_sha1);
+                       if (!parent || parse_commit(parent))
+                               die("Failed to find/parse commit %s", t->ref);
+                       commit_list_insert(parent, &parents);
+               }
+               /* else: t->ref points to nothing, assume root/orphan commit */
+       }
+
+       if (commit_tree(msg, tree_sha1, parents, result_sha1, NULL))
+               die("Failed to commit notes tree to database");
+}
+
 int notes_merge(struct notes_merge_options *o,
                unsigned char *result_sha1)
 {