6 #include "notes-utils.h"
7 #include "repository.h"
9 void create_notes_commit(struct repository
*r
,
11 struct commit_list
*parents
,
12 const char *msg
, size_t msg_len
,
13 struct object_id
*result_oid
)
15 struct object_id tree_oid
;
17 assert(t
->initialized
);
19 if (write_notes_tree(t
, &tree_oid
))
20 die("Failed to write notes tree to database");
23 /* Deduce parent commit from t->ref */
24 struct object_id parent_oid
;
25 if (!read_ref(t
->ref
, &parent_oid
)) {
26 struct commit
*parent
= lookup_commit(r
, &parent_oid
);
27 if (parse_commit(parent
))
28 die("Failed to find/parse commit %s", t
->ref
);
29 commit_list_insert(parent
, &parents
);
31 /* else: t->ref points to nothing, assume root/orphan commit */
34 if (commit_tree(msg
, msg_len
, &tree_oid
, parents
, result_oid
, NULL
,
36 die("Failed to commit notes tree to database");
39 void commit_notes(struct repository
*r
, struct notes_tree
*t
, const char *msg
)
41 struct strbuf buf
= STRBUF_INIT
;
42 struct object_id commit_oid
;
45 t
= &default_notes_tree
;
46 if (!t
->initialized
|| !t
->update_ref
|| !*t
->update_ref
)
47 die(_("Cannot commit uninitialized/unreferenced notes tree"));
49 return; /* don't have to commit an unchanged tree */
51 /* Prepare commit message and reflog message */
52 strbuf_addstr(&buf
, msg
);
53 strbuf_complete_line(&buf
);
55 create_notes_commit(r
, t
, NULL
, buf
.buf
, buf
.len
, &commit_oid
);
56 strbuf_insertstr(&buf
, 0, "notes: ");
57 update_ref(buf
.buf
, t
->update_ref
, &commit_oid
, NULL
, 0,
58 UPDATE_REFS_DIE_ON_ERR
);
63 int parse_notes_merge_strategy(const char *v
, enum notes_merge_strategy
*s
)
65 if (!strcmp(v
, "manual"))
66 *s
= NOTES_MERGE_RESOLVE_MANUAL
;
67 else if (!strcmp(v
, "ours"))
68 *s
= NOTES_MERGE_RESOLVE_OURS
;
69 else if (!strcmp(v
, "theirs"))
70 *s
= NOTES_MERGE_RESOLVE_THEIRS
;
71 else if (!strcmp(v
, "union"))
72 *s
= NOTES_MERGE_RESOLVE_UNION
;
73 else if (!strcmp(v
, "cat_sort_uniq"))
74 *s
= NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ
;
81 static combine_notes_fn
parse_combine_notes_fn(const char *v
)
83 if (!strcasecmp(v
, "overwrite"))
84 return combine_notes_overwrite
;
85 else if (!strcasecmp(v
, "ignore"))
86 return combine_notes_ignore
;
87 else if (!strcasecmp(v
, "concatenate"))
88 return combine_notes_concatenate
;
89 else if (!strcasecmp(v
, "cat_sort_uniq"))
90 return combine_notes_cat_sort_uniq
;
95 static int notes_rewrite_config(const char *k
, const char *v
, void *cb
)
97 struct notes_rewrite_cfg
*c
= cb
;
98 if (starts_with(k
, "notes.rewrite.") && !strcmp(k
+14, c
->cmd
)) {
99 c
->enabled
= git_config_bool(k
, v
);
101 } else if (!c
->mode_from_env
&& !strcmp(k
, "notes.rewritemode")) {
103 return config_error_nonbool(k
);
104 c
->combine
= parse_combine_notes_fn(v
);
106 error(_("Bad notes.rewriteMode value: '%s'"), v
);
110 } else if (!c
->refs_from_env
&& !strcmp(k
, "notes.rewriteref")) {
111 /* note that a refs/ prefix is implied in the
112 * underlying for_each_glob_ref */
113 if (starts_with(v
, "refs/notes/"))
114 string_list_add_refs_by_glob(c
->refs
, v
);
116 warning(_("Refusing to rewrite notes in %s"
117 " (outside of refs/notes/)"), v
);
125 struct notes_rewrite_cfg
*init_copy_notes_for_rewrite(const char *cmd
)
127 struct notes_rewrite_cfg
*c
= xmalloc(sizeof(struct notes_rewrite_cfg
));
128 const char *rewrite_mode_env
= getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT
);
129 const char *rewrite_refs_env
= getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT
);
132 c
->combine
= combine_notes_concatenate
;
133 CALLOC_ARRAY(c
->refs
, 1);
134 c
->refs
->strdup_strings
= 1;
135 c
->refs_from_env
= 0;
136 c
->mode_from_env
= 0;
137 if (rewrite_mode_env
) {
138 c
->mode_from_env
= 1;
139 c
->combine
= parse_combine_notes_fn(rewrite_mode_env
);
142 * TRANSLATORS: The first %s is the name of
143 * the environment variable, the second %s is
146 error(_("Bad %s value: '%s'"), GIT_NOTES_REWRITE_MODE_ENVIRONMENT
,
149 if (rewrite_refs_env
) {
150 c
->refs_from_env
= 1;
151 string_list_add_refs_from_colon_sep(c
->refs
, rewrite_refs_env
);
153 git_config(notes_rewrite_config
, c
);
154 if (!c
->enabled
|| !c
->refs
->nr
) {
155 string_list_clear(c
->refs
, 0);
160 c
->trees
= load_notes_trees(c
->refs
, NOTES_INIT_WRITABLE
);
161 string_list_clear(c
->refs
, 0);
166 int copy_note_for_rewrite(struct notes_rewrite_cfg
*c
,
167 const struct object_id
*from_obj
, const struct object_id
*to_obj
)
171 for (i
= 0; c
->trees
[i
]; i
++)
172 ret
= copy_note(c
->trees
[i
], from_obj
, to_obj
, 1, c
->combine
) || ret
;
176 void finish_copy_notes_for_rewrite(struct repository
*r
,
177 struct notes_rewrite_cfg
*c
,
181 for (i
= 0; c
->trees
[i
]; i
++) {
182 commit_notes(r
, c
->trees
[i
], msg
);
183 free_notes(c
->trees
[i
]);