@matheusbsbs / matheus-bagliano / commits / 56881843d4
Fork 0

builtin/notes.c: Refactor creation of notes commits.

Create new function create_notes_commit() which is slightly more general than commit_notes() (accepts multiple commit parents and does not auto-update the notes ref). This function will be used by the notes-merge functionality in future patches. Also rewrite builtin/notes.c:commit_notes() to reuse this new function. Signed-off-by: Johan Herland <johan@herland.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johan Herland committed Nov 9, 2010 at 22:49 UTC 56881843d4d916a166ac4c6ba1803e5ceba9c44d
4 files changed +47 -24
builtin.h
+1 -1
@@ -17,7 +17,7 @@ extern void prune_packed_objects(int);
17 extern int fmt_merge_msg(int merge_summary, struct strbuf *in,
18 struct strbuf *out);
19 extern int fmt_merge_msg_shortlog(struct strbuf *in, struct strbuf *out);
20 -extern int commit_notes(struct notes_tree *t, const char *msg);
20 +extern void commit_notes(struct notes_tree *t, const char *msg);
21
22 struct notes_rewrite_cfg {
23 struct notes_tree **trees;
builtin/notes.c
+5 -23
@@ -288,18 +288,17 @@ static int parse_reedit_arg(const struct option *opt, const char *arg, int unset
288 return parse_reuse_arg(opt, arg, unset);
289 }
290
291 -int commit_notes(struct notes_tree *t, const char *msg)
291 +void commit_notes(struct notes_tree *t, const char *msg)
292 {
293 - struct commit_list *parent;
294 - unsigned char tree_sha1[20], prev_commit[20], new_commit[20];
293 struct strbuf buf = STRBUF_INIT;
294 + unsigned char commit_sha1[20];
295
296 if (!t)
297 t = &default_notes_tree;
298 if (!t->initialized || !t->ref || !*t->ref)
299 die("Cannot commit uninitialized/unreferenced notes tree");
300 if (!t->dirty)
302 - return 0; /* don't have to commit an unchanged tree */
301 + return; /* don't have to commit an unchanged tree */
302
303 /* Prepare commit message and reflog message */
304 strbuf_addstr(&buf, "notes: "); /* commit message starts at index 7 */
@@ -307,27 +306,10 @@ int commit_notes(struct notes_tree *t, const char *msg)
306 if (buf.buf[buf.len - 1] != '\n')
307 strbuf_addch(&buf, '\n'); /* Make sure msg ends with newline */
308
310 - /* Convert notes tree to tree object */
311 - if (write_notes_tree(t, tree_sha1))
312 - die("Failed to write current notes tree to database");
313 -
314 - /* Create new commit for the tree object */
315 - if (!read_ref(t->ref, prev_commit)) { /* retrieve parent commit */
316 - parent = xmalloc(sizeof(*parent));
317 - parent->item = lookup_commit(prev_commit);
318 - parent->next = NULL;
319 - } else {
320 - hashclr(prev_commit);
321 - parent = NULL;
322 - }
323 - if (commit_tree(buf.buf + 7, tree_sha1, parent, new_commit, NULL))
324 - die("Failed to commit notes tree to database");
325 -
326 - /* Update notes ref with new commit */
327 - update_ref(buf.buf, t->ref, new_commit, prev_commit, 0, DIE_ON_ERR);
309 + create_notes_commit(t, NULL, buf.buf + 7, commit_sha1);
310 + update_ref(buf.buf, t->ref, commit_sha1, NULL, 0, DIE_ON_ERR);
311
312 strbuf_release(&buf);
330 - return 0;
313 }
314
315 combine_notes_fn parse_combine_notes_fn(const char *v)
notes-merge.c
+27
@@ -1,6 +1,7 @@
1 #include "cache.h"
2 #include "commit.h"
3 #include "refs.h"
4 +#include "notes.h"
5 #include "notes-merge.h"
6
7 void init_notes_merge_options(struct notes_merge_options *o)
@@ -17,6 +18,32 @@ void init_notes_merge_options(struct notes_merge_options *o)
18 } \
19 } while (0)
20
21 +void create_notes_commit(struct notes_tree *t, struct commit_list *parents,
22 + const char *msg, unsigned char *result_sha1)
23 +{
24 + unsigned char tree_sha1[20];
25 +
26 + assert(t->initialized);
27 +
28 + if (write_notes_tree(t, tree_sha1))
29 + die("Failed to write notes tree to database");
30 +
31 + if (!parents) {
32 + /* Deduce parent commit from t->ref */
33 + unsigned char parent_sha1[20];
34 + if (!read_ref(t->ref, parent_sha1)) {
35 + struct commit *parent = lookup_commit(parent_sha1);
36 + if (!parent || parse_commit(parent))
37 + die("Failed to find/parse commit %s", t->ref);
38 + commit_list_insert(parent, &parents);
39 + }
40 + /* else: t->ref points to nothing, assume root/orphan commit */
41 + }
42 +
43 + if (commit_tree(msg, tree_sha1, parents, result_sha1, NULL))
44 + die("Failed to commit notes tree to database");
45 +}
46 +
47 int notes_merge(struct notes_merge_options *o,
48 unsigned char *result_sha1)
49 {
notes-merge.h
+14
@@ -14,6 +14,20 @@ struct notes_merge_options {
14
15 void init_notes_merge_options(struct notes_merge_options *o);
16
17 +/*
18 + * Create new notes commit from the given notes tree
19 + *
20 + * Properties of the created commit:
21 + * - tree: the result of converting t to a tree object with write_notes_tree().
22 + * - parents: the given parents OR (if NULL) the commit referenced by t->ref.
23 + * - author/committer: the default determined by commmit_tree().
24 + * - commit message: msg
25 + *
26 + * The resulting commit SHA1 is stored in result_sha1.
27 + */
28 +void create_notes_commit(struct notes_tree *t, struct commit_list *parents,
29 + const char *msg, unsigned char *result_sha1);
30 +
31 /*
32 * Merge notes from o->remote_ref into o->local_ref
33 *