Skip to content

Commit 22d94a7

Browse files
committed
Merge branch 'jh/libify-note-handling'
Make it possible to call into copy-notes API from the sequencer code. * jh/libify-note-handling: Move create_notes_commit() from notes-merge.c into notes-utils.c Move copy_note_for_rewrite + friends from builtin/notes.c to notes-utils.c finish_copy_notes_for_rewrite(): Let caller provide commit message
2 parents 0039d60 + bf9a05b commit 22d94a7

File tree

8 files changed

+203
-189
lines changed

8 files changed

+203
-189
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,7 @@ LIB_H += merge-recursive.h
696696
LIB_H += mergesort.h
697697
LIB_H += notes-cache.h
698698
LIB_H += notes-merge.h
699+
LIB_H += notes-utils.h
699700
LIB_H += notes.h
700701
LIB_H += object.h
701702
LIB_H += pack-revindex.h
@@ -830,6 +831,7 @@ LIB_OBJS += name-hash.o
830831
LIB_OBJS += notes.o
831832
LIB_OBJS += notes-cache.o
832833
LIB_OBJS += notes-merge.o
834+
LIB_OBJS += notes-utils.o
833835
LIB_OBJS += object.o
834836
LIB_OBJS += pack-check.o
835837
LIB_OBJS += pack-revindex.o

builtin.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include "strbuf.h"
66
#include "cache.h"
77
#include "commit.h"
8-
#include "notes.h"
98

109
#define DEFAULT_MERGE_LOG_LEN 20
1110

@@ -26,21 +25,6 @@ struct fmt_merge_msg_opts {
2625
extern int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
2726
struct fmt_merge_msg_opts *);
2827

29-
struct notes_rewrite_cfg {
30-
struct notes_tree **trees;
31-
const char *cmd;
32-
int enabled;
33-
combine_notes_fn combine;
34-
struct string_list *refs;
35-
int refs_from_env;
36-
int mode_from_env;
37-
};
38-
39-
struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd);
40-
int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
41-
const unsigned char *from_obj, const unsigned char *to_obj);
42-
void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c);
43-
4428
extern int textconv_object(const char *path, unsigned mode, const unsigned char *sha1, int sha1_valid, char **buf, unsigned long *buf_size);
4529

4630
extern int cmd_add(int argc, const char **argv, const char *prefix);

builtin/commit.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "gpg-interface.h"
3030
#include "column.h"
3131
#include "sequencer.h"
32+
#include "notes-utils.h"
3233

3334
static const char * const builtin_commit_usage[] = {
3435
N_("git commit [options] [--] <pathspec>..."),
@@ -1593,7 +1594,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
15931594
if (cfg) {
15941595
/* we are amending, so current_head is not NULL */
15951596
copy_note_for_rewrite(cfg, current_head->object.sha1, sha1);
1596-
finish_copy_notes_for_rewrite(cfg);
1597+
finish_copy_notes_for_rewrite(cfg, "Notes added by 'git commit --amend'");
15971598
}
15981599
run_rewrite_hook(current_head->object.sha1, sha1);
15991600
}

builtin/notes.c

Lines changed: 4 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
#include "parse-options.h"
1919
#include "string-list.h"
2020
#include "notes-merge.h"
21-
22-
static void commit_notes(struct notes_tree *t, const char *msg);
23-
static combine_notes_fn parse_combine_notes_fn(const char *v);
21+
#include "notes-utils.h"
2422

2523
static const char * const git_notes_usage[] = {
2624
N_("git notes [--ref <notes_ref>] [list [<object>]]"),
@@ -287,139 +285,13 @@ static int parse_reedit_arg(const struct option *opt, const char *arg, int unset
287285
return parse_reuse_arg(opt, arg, unset);
288286
}
289287

290-
static void commit_notes(struct notes_tree *t, const char *msg)
291-
{
292-
struct strbuf buf = STRBUF_INIT;
293-
unsigned char commit_sha1[20];
294-
295-
if (!t)
296-
t = &default_notes_tree;
297-
if (!t->initialized || !t->ref || !*t->ref)
298-
die(_("Cannot commit uninitialized/unreferenced notes tree"));
299-
if (!t->dirty)
300-
return; /* don't have to commit an unchanged tree */
301-
302-
/* Prepare commit message and reflog message */
303-
strbuf_addstr(&buf, msg);
304-
if (buf.buf[buf.len - 1] != '\n')
305-
strbuf_addch(&buf, '\n'); /* Make sure msg ends with newline */
306-
307-
create_notes_commit(t, NULL, &buf, commit_sha1);
308-
strbuf_insert(&buf, 0, "notes: ", 7); /* commit message starts at index 7 */
309-
update_ref(buf.buf, t->ref, commit_sha1, NULL, 0, DIE_ON_ERR);
310-
311-
strbuf_release(&buf);
312-
}
313-
314-
static combine_notes_fn parse_combine_notes_fn(const char *v)
315-
{
316-
if (!strcasecmp(v, "overwrite"))
317-
return combine_notes_overwrite;
318-
else if (!strcasecmp(v, "ignore"))
319-
return combine_notes_ignore;
320-
else if (!strcasecmp(v, "concatenate"))
321-
return combine_notes_concatenate;
322-
else if (!strcasecmp(v, "cat_sort_uniq"))
323-
return combine_notes_cat_sort_uniq;
324-
else
325-
return NULL;
326-
}
327-
328-
static int notes_rewrite_config(const char *k, const char *v, void *cb)
329-
{
330-
struct notes_rewrite_cfg *c = cb;
331-
if (!prefixcmp(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
332-
c->enabled = git_config_bool(k, v);
333-
return 0;
334-
} else if (!c->mode_from_env && !strcmp(k, "notes.rewritemode")) {
335-
if (!v)
336-
config_error_nonbool(k);
337-
c->combine = parse_combine_notes_fn(v);
338-
if (!c->combine) {
339-
error(_("Bad notes.rewriteMode value: '%s'"), v);
340-
return 1;
341-
}
342-
return 0;
343-
} else if (!c->refs_from_env && !strcmp(k, "notes.rewriteref")) {
344-
/* note that a refs/ prefix is implied in the
345-
* underlying for_each_glob_ref */
346-
if (!prefixcmp(v, "refs/notes/"))
347-
string_list_add_refs_by_glob(c->refs, v);
348-
else
349-
warning(_("Refusing to rewrite notes in %s"
350-
" (outside of refs/notes/)"), v);
351-
return 0;
352-
}
353-
354-
return 0;
355-
}
356-
357-
358-
struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd)
359-
{
360-
struct notes_rewrite_cfg *c = xmalloc(sizeof(struct notes_rewrite_cfg));
361-
const char *rewrite_mode_env = getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT);
362-
const char *rewrite_refs_env = getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT);
363-
c->cmd = cmd;
364-
c->enabled = 1;
365-
c->combine = combine_notes_concatenate;
366-
c->refs = xcalloc(1, sizeof(struct string_list));
367-
c->refs->strdup_strings = 1;
368-
c->refs_from_env = 0;
369-
c->mode_from_env = 0;
370-
if (rewrite_mode_env) {
371-
c->mode_from_env = 1;
372-
c->combine = parse_combine_notes_fn(rewrite_mode_env);
373-
if (!c->combine)
374-
/* TRANSLATORS: The first %s is the name of the
375-
environment variable, the second %s is its value */
376-
error(_("Bad %s value: '%s'"), GIT_NOTES_REWRITE_MODE_ENVIRONMENT,
377-
rewrite_mode_env);
378-
}
379-
if (rewrite_refs_env) {
380-
c->refs_from_env = 1;
381-
string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env);
382-
}
383-
git_config(notes_rewrite_config, c);
384-
if (!c->enabled || !c->refs->nr) {
385-
string_list_clear(c->refs, 0);
386-
free(c->refs);
387-
free(c);
388-
return NULL;
389-
}
390-
c->trees = load_notes_trees(c->refs);
391-
string_list_clear(c->refs, 0);
392-
free(c->refs);
393-
return c;
394-
}
395-
396-
int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
397-
const unsigned char *from_obj, const unsigned char *to_obj)
398-
{
399-
int ret = 0;
400-
int i;
401-
for (i = 0; c->trees[i]; i++)
402-
ret = copy_note(c->trees[i], from_obj, to_obj, 1, c->combine) || ret;
403-
return ret;
404-
}
405-
406-
void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c)
407-
{
408-
int i;
409-
for (i = 0; c->trees[i]; i++) {
410-
commit_notes(c->trees[i], "Notes added by 'git notes copy'");
411-
free_notes(c->trees[i]);
412-
}
413-
free(c->trees);
414-
free(c);
415-
}
416-
417288
static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
418289
{
419290
struct strbuf buf = STRBUF_INIT;
420291
struct notes_rewrite_cfg *c = NULL;
421292
struct notes_tree *t = NULL;
422293
int ret = 0;
294+
const char *msg = "Notes added by 'git notes copy'";
423295

424296
if (rewrite_cmd) {
425297
c = init_copy_notes_for_rewrite(rewrite_cmd);
@@ -461,10 +333,10 @@ static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
461333
}
462334

463335
if (!rewrite_cmd) {
464-
commit_notes(t, "Notes added by 'git notes copy'");
336+
commit_notes(t, msg);
465337
free_notes(t);
466338
} else {
467-
finish_copy_notes_for_rewrite(c);
339+
finish_copy_notes_for_rewrite(c, msg);
468340
}
469341
return ret;
470342
}

notes-merge.c

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "notes.h"
1010
#include "notes-merge.h"
1111
#include "strbuf.h"
12+
#include "notes-utils.h"
1213

1314
struct notes_merge_pair {
1415
unsigned char obj[20], base[20], local[20], remote[20];
@@ -530,32 +531,6 @@ static int merge_from_diffs(struct notes_merge_options *o,
530531
return conflicts ? -1 : 1;
531532
}
532533

533-
void create_notes_commit(struct notes_tree *t, struct commit_list *parents,
534-
const struct strbuf *msg, unsigned char *result_sha1)
535-
{
536-
unsigned char tree_sha1[20];
537-
538-
assert(t->initialized);
539-
540-
if (write_notes_tree(t, tree_sha1))
541-
die("Failed to write notes tree to database");
542-
543-
if (!parents) {
544-
/* Deduce parent commit from t->ref */
545-
unsigned char parent_sha1[20];
546-
if (!read_ref(t->ref, parent_sha1)) {
547-
struct commit *parent = lookup_commit(parent_sha1);
548-
if (!parent || parse_commit(parent))
549-
die("Failed to find/parse commit %s", t->ref);
550-
commit_list_insert(parent, &parents);
551-
}
552-
/* else: t->ref points to nothing, assume root/orphan commit */
553-
}
554-
555-
if (commit_tree(msg, tree_sha1, parents, result_sha1, NULL, NULL))
556-
die("Failed to commit notes tree to database");
557-
}
558-
559534
int notes_merge(struct notes_merge_options *o,
560535
struct notes_tree *local_tree,
561536
unsigned char *result_sha1)

notes-merge.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,6 @@ struct notes_merge_options {
2525

2626
void init_notes_merge_options(struct notes_merge_options *o);
2727

28-
/*
29-
* Create new notes commit from the given notes tree
30-
*
31-
* Properties of the created commit:
32-
* - tree: the result of converting t to a tree object with write_notes_tree().
33-
* - parents: the given parents OR (if NULL) the commit referenced by t->ref.
34-
* - author/committer: the default determined by commmit_tree().
35-
* - commit message: msg
36-
*
37-
* The resulting commit SHA1 is stored in result_sha1.
38-
*/
39-
void create_notes_commit(struct notes_tree *t, struct commit_list *parents,
40-
const struct strbuf *msg, unsigned char *result_sha1);
41-
4228
/*
4329
* Merge notes from o->remote_ref into o->local_ref
4430
*

0 commit comments

Comments
 (0)