5 #include "notes-utils.h"
7 void create_notes_commit(struct notes_tree
*t
, struct commit_list
*parents
,
8 const char *msg
, size_t msg_len
,
9 struct object_id
*result_oid
)
11 struct object_id tree_oid
;
13 assert(t
->initialized
);
15 if (write_notes_tree(t
, &tree_oid
))
16 die("Failed to write notes tree to database");
19 /* Deduce parent commit from t->ref */
20 struct object_id parent_oid
;
21 if (!read_ref(t
->ref
, &parent_oid
)) {
22 struct commit
*parent
= lookup_commit(&parent_oid
);
23 if (parse_commit(parent
))
24 die("Failed to find/parse commit %s", t
->ref
);
25 commit_list_insert(parent
, &parents
);
27 /* else: t->ref points to nothing, assume root/orphan commit */
30 if (commit_tree(msg
, msg_len
, &tree_oid
, parents
, result_oid
, NULL
,
32 die("Failed to commit notes tree to database");
35 void commit_notes(struct notes_tree
*t
, const char *msg
)
37 struct strbuf buf
= STRBUF_INIT
;
38 struct object_id commit_oid
;
41 t
= &default_notes_tree
;
42 if (!t
->initialized
|| !t
->update_ref
|| !*t
->update_ref
)
43 die(_("Cannot commit uninitialized/unreferenced notes tree"));
45 return; /* don't have to commit an unchanged tree */
47 /* Prepare commit message and reflog message */
48 strbuf_addstr(&buf
, msg
);
49 strbuf_complete_line(&buf
);
51 create_notes_commit(t
, NULL
, buf
.buf
, buf
.len
, &commit_oid
);
52 strbuf_insert(&buf
, 0, "notes: ", 7); /* commit message starts at index 7 */
53 update_ref(buf
.buf
, t
->update_ref
, &commit_oid
, NULL
, 0,
54 UPDATE_REFS_DIE_ON_ERR
);
59 int parse_notes_merge_strategy(const char *v
, enum notes_merge_strategy
*s
)
61 if (!strcmp(v
, "manual"))
62 *s
= NOTES_MERGE_RESOLVE_MANUAL
;
63 else if (!strcmp(v
, "ours"))
64 *s
= NOTES_MERGE_RESOLVE_OURS
;
65 else if (!strcmp(v
, "theirs"))
66 *s
= NOTES_MERGE_RESOLVE_THEIRS
;
67 else if (!strcmp(v
, "union"))
68 *s
= NOTES_MERGE_RESOLVE_UNION
;
69 else if (!strcmp(v
, "cat_sort_uniq"))
70 *s
= NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ
;
77 static combine_notes_fn
parse_combine_notes_fn(const char *v
)
79 if (!strcasecmp(v
, "overwrite"))
80 return combine_notes_overwrite
;
81 else if (!strcasecmp(v
, "ignore"))
82 return combine_notes_ignore
;
83 else if (!strcasecmp(v
, "concatenate"))
84 return combine_notes_concatenate
;
85 else if (!strcasecmp(v
, "cat_sort_uniq"))
86 return combine_notes_cat_sort_uniq
;
91 static int notes_rewrite_config(const char *k
, const char *v
, void *cb
)
93 struct notes_rewrite_cfg
*c
= cb
;
94 if (starts_with(k
, "notes.rewrite.") && !strcmp(k
+14, c
->cmd
)) {
95 c
->enabled
= git_config_bool(k
, v
);
97 } else if (!c
->mode_from_env
&& !strcmp(k
, "notes.rewritemode")) {
99 return config_error_nonbool(k
);
100 c
->combine
= parse_combine_notes_fn(v
);
102 error(_("Bad notes.rewriteMode value: '%s'"), v
);
106 } else if (!c
->refs_from_env
&& !strcmp(k
, "notes.rewriteref")) {
107 /* note that a refs/ prefix is implied in the
108 * underlying for_each_glob_ref */
109 if (starts_with(v
, "refs/notes/"))
110 string_list_add_refs_by_glob(c
->refs
, v
);
112 warning(_("Refusing to rewrite notes in %s"
113 " (outside of refs/notes/)"), v
);
121 struct notes_rewrite_cfg
*init_copy_notes_for_rewrite(const char *cmd
)
123 struct notes_rewrite_cfg
*c
= xmalloc(sizeof(struct notes_rewrite_cfg
));
124 const char *rewrite_mode_env
= getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT
);
125 const char *rewrite_refs_env
= getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT
);
128 c
->combine
= combine_notes_concatenate
;
129 c
->refs
= xcalloc(1, sizeof(struct string_list
));
130 c
->refs
->strdup_strings
= 1;
131 c
->refs_from_env
= 0;
132 c
->mode_from_env
= 0;
133 if (rewrite_mode_env
) {
134 c
->mode_from_env
= 1;
135 c
->combine
= parse_combine_notes_fn(rewrite_mode_env
);
138 * TRANSLATORS: The first %s is the name of
139 * the environment variable, the second %s is
142 error(_("Bad %s value: '%s'"), GIT_NOTES_REWRITE_MODE_ENVIRONMENT
,
145 if (rewrite_refs_env
) {
146 c
->refs_from_env
= 1;
147 string_list_add_refs_from_colon_sep(c
->refs
, rewrite_refs_env
);
149 git_config(notes_rewrite_config
, c
);
150 if (!c
->enabled
|| !c
->refs
->nr
) {
151 string_list_clear(c
->refs
, 0);
156 c
->trees
= load_notes_trees(c
->refs
, NOTES_INIT_WRITABLE
);
157 string_list_clear(c
->refs
, 0);
162 int copy_note_for_rewrite(struct notes_rewrite_cfg
*c
,
163 const struct object_id
*from_obj
, const struct object_id
*to_obj
)
167 for (i
= 0; c
->trees
[i
]; i
++)
168 ret
= copy_note(c
->trees
[i
], from_obj
, to_obj
, 1, c
->combine
) || ret
;
172 void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg
*c
, const char *msg
)
175 for (i
= 0; c
->trees
[i
]; i
++) {
176 commit_notes(c
->trees
[i
], msg
);
177 free_notes(c
->trees
[i
]);