4 #include "notes-utils.h"
5 #include "notes-merge.h" /* for create_notes_commit() */
7 void commit_notes(struct notes_tree
*t
, const char *msg
)
9 struct strbuf buf
= STRBUF_INIT
;
10 unsigned char commit_sha1
[20];
13 t
= &default_notes_tree
;
14 if (!t
->initialized
|| !t
->ref
|| !*t
->ref
)
15 die(_("Cannot commit uninitialized/unreferenced notes tree"));
17 return; /* don't have to commit an unchanged tree */
19 /* Prepare commit message and reflog message */
20 strbuf_addstr(&buf
, msg
);
21 if (buf
.buf
[buf
.len
- 1] != '\n')
22 strbuf_addch(&buf
, '\n'); /* Make sure msg ends with newline */
24 create_notes_commit(t
, NULL
, &buf
, commit_sha1
);
25 strbuf_insert(&buf
, 0, "notes: ", 7); /* commit message starts at index 7 */
26 update_ref(buf
.buf
, t
->ref
, commit_sha1
, NULL
, 0, DIE_ON_ERR
);
31 static combine_notes_fn
parse_combine_notes_fn(const char *v
)
33 if (!strcasecmp(v
, "overwrite"))
34 return combine_notes_overwrite
;
35 else if (!strcasecmp(v
, "ignore"))
36 return combine_notes_ignore
;
37 else if (!strcasecmp(v
, "concatenate"))
38 return combine_notes_concatenate
;
39 else if (!strcasecmp(v
, "cat_sort_uniq"))
40 return combine_notes_cat_sort_uniq
;
45 static int notes_rewrite_config(const char *k
, const char *v
, void *cb
)
47 struct notes_rewrite_cfg
*c
= cb
;
48 if (!prefixcmp(k
, "notes.rewrite.") && !strcmp(k
+14, c
->cmd
)) {
49 c
->enabled
= git_config_bool(k
, v
);
51 } else if (!c
->mode_from_env
&& !strcmp(k
, "notes.rewritemode")) {
53 config_error_nonbool(k
);
54 c
->combine
= parse_combine_notes_fn(v
);
56 error(_("Bad notes.rewriteMode value: '%s'"), v
);
60 } else if (!c
->refs_from_env
&& !strcmp(k
, "notes.rewriteref")) {
61 /* note that a refs/ prefix is implied in the
62 * underlying for_each_glob_ref */
63 if (!prefixcmp(v
, "refs/notes/"))
64 string_list_add_refs_by_glob(c
->refs
, v
);
66 warning(_("Refusing to rewrite notes in %s"
67 " (outside of refs/notes/)"), v
);
75 struct notes_rewrite_cfg
*init_copy_notes_for_rewrite(const char *cmd
)
77 struct notes_rewrite_cfg
*c
= xmalloc(sizeof(struct notes_rewrite_cfg
));
78 const char *rewrite_mode_env
= getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT
);
79 const char *rewrite_refs_env
= getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT
);
82 c
->combine
= combine_notes_concatenate
;
83 c
->refs
= xcalloc(1, sizeof(struct string_list
));
84 c
->refs
->strdup_strings
= 1;
87 if (rewrite_mode_env
) {
89 c
->combine
= parse_combine_notes_fn(rewrite_mode_env
);
91 /* TRANSLATORS: The first %s is the name of the
92 environment variable, the second %s is its value */
93 error(_("Bad %s value: '%s'"), GIT_NOTES_REWRITE_MODE_ENVIRONMENT
,
96 if (rewrite_refs_env
) {
98 string_list_add_refs_from_colon_sep(c
->refs
, rewrite_refs_env
);
100 git_config(notes_rewrite_config
, c
);
101 if (!c
->enabled
|| !c
->refs
->nr
) {
102 string_list_clear(c
->refs
, 0);
107 c
->trees
= load_notes_trees(c
->refs
);
108 string_list_clear(c
->refs
, 0);
113 int copy_note_for_rewrite(struct notes_rewrite_cfg
*c
,
114 const unsigned char *from_obj
, const unsigned char *to_obj
)
118 for (i
= 0; c
->trees
[i
]; i
++)
119 ret
= copy_note(c
->trees
[i
], from_obj
, to_obj
, 1, c
->combine
) || ret
;
123 void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg
*c
, const char *msg
)
126 for (i
= 0; c
->trees
[i
]; i
++) {
127 commit_notes(c
->trees
[i
], msg
);
128 free_notes(c
->trees
[i
]);