5 #include "notes-utils.h"
6 #include "repository.h"
8 void create_notes_commit(struct notes_tree
*t
, struct commit_list
*parents
,
9 const char *msg
, size_t msg_len
,
10 struct object_id
*result_oid
)
12 struct object_id tree_oid
;
14 assert(t
->initialized
);
16 if (write_notes_tree(t
, &tree_oid
))
17 die("Failed to write notes tree to database");
20 /* Deduce parent commit from t->ref */
21 struct object_id parent_oid
;
22 if (!read_ref(t
->ref
, &parent_oid
)) {
23 struct commit
*parent
= lookup_commit(the_repository
,
25 if (parse_commit(parent
))
26 die("Failed to find/parse commit %s", t
->ref
);
27 commit_list_insert(parent
, &parents
);
29 /* else: t->ref points to nothing, assume root/orphan commit */
32 if (commit_tree(msg
, msg_len
, &tree_oid
, parents
, result_oid
, NULL
,
34 die("Failed to commit notes tree to database");
37 void commit_notes(struct notes_tree
*t
, const char *msg
)
39 struct strbuf buf
= STRBUF_INIT
;
40 struct object_id commit_oid
;
43 t
= &default_notes_tree
;
44 if (!t
->initialized
|| !t
->update_ref
|| !*t
->update_ref
)
45 die(_("Cannot commit uninitialized/unreferenced notes tree"));
47 return; /* don't have to commit an unchanged tree */
49 /* Prepare commit message and reflog message */
50 strbuf_addstr(&buf
, msg
);
51 strbuf_complete_line(&buf
);
53 create_notes_commit(t
, NULL
, buf
.buf
, buf
.len
, &commit_oid
);
54 strbuf_insert(&buf
, 0, "notes: ", 7); /* commit message starts at index 7 */
55 update_ref(buf
.buf
, t
->update_ref
, &commit_oid
, NULL
, 0,
56 UPDATE_REFS_DIE_ON_ERR
);
61 int parse_notes_merge_strategy(const char *v
, enum notes_merge_strategy
*s
)
63 if (!strcmp(v
, "manual"))
64 *s
= NOTES_MERGE_RESOLVE_MANUAL
;
65 else if (!strcmp(v
, "ours"))
66 *s
= NOTES_MERGE_RESOLVE_OURS
;
67 else if (!strcmp(v
, "theirs"))
68 *s
= NOTES_MERGE_RESOLVE_THEIRS
;
69 else if (!strcmp(v
, "union"))
70 *s
= NOTES_MERGE_RESOLVE_UNION
;
71 else if (!strcmp(v
, "cat_sort_uniq"))
72 *s
= NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ
;
79 static combine_notes_fn
parse_combine_notes_fn(const char *v
)
81 if (!strcasecmp(v
, "overwrite"))
82 return combine_notes_overwrite
;
83 else if (!strcasecmp(v
, "ignore"))
84 return combine_notes_ignore
;
85 else if (!strcasecmp(v
, "concatenate"))
86 return combine_notes_concatenate
;
87 else if (!strcasecmp(v
, "cat_sort_uniq"))
88 return combine_notes_cat_sort_uniq
;
93 static int notes_rewrite_config(const char *k
, const char *v
, void *cb
)
95 struct notes_rewrite_cfg
*c
= cb
;
96 if (starts_with(k
, "notes.rewrite.") && !strcmp(k
+14, c
->cmd
)) {
97 c
->enabled
= git_config_bool(k
, v
);
99 } else if (!c
->mode_from_env
&& !strcmp(k
, "notes.rewritemode")) {
101 return config_error_nonbool(k
);
102 c
->combine
= parse_combine_notes_fn(v
);
104 error(_("Bad notes.rewriteMode value: '%s'"), v
);
108 } else if (!c
->refs_from_env
&& !strcmp(k
, "notes.rewriteref")) {
109 /* note that a refs/ prefix is implied in the
110 * underlying for_each_glob_ref */
111 if (starts_with(v
, "refs/notes/"))
112 string_list_add_refs_by_glob(c
->refs
, v
);
114 warning(_("Refusing to rewrite notes in %s"
115 " (outside of refs/notes/)"), v
);
123 struct notes_rewrite_cfg
*init_copy_notes_for_rewrite(const char *cmd
)
125 struct notes_rewrite_cfg
*c
= xmalloc(sizeof(struct notes_rewrite_cfg
));
126 const char *rewrite_mode_env
= getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT
);
127 const char *rewrite_refs_env
= getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT
);
130 c
->combine
= combine_notes_concatenate
;
131 c
->refs
= xcalloc(1, sizeof(struct string_list
));
132 c
->refs
->strdup_strings
= 1;
133 c
->refs_from_env
= 0;
134 c
->mode_from_env
= 0;
135 if (rewrite_mode_env
) {
136 c
->mode_from_env
= 1;
137 c
->combine
= parse_combine_notes_fn(rewrite_mode_env
);
140 * TRANSLATORS: The first %s is the name of
141 * the environment variable, the second %s is
144 error(_("Bad %s value: '%s'"), GIT_NOTES_REWRITE_MODE_ENVIRONMENT
,
147 if (rewrite_refs_env
) {
148 c
->refs_from_env
= 1;
149 string_list_add_refs_from_colon_sep(c
->refs
, rewrite_refs_env
);
151 git_config(notes_rewrite_config
, c
);
152 if (!c
->enabled
|| !c
->refs
->nr
) {
153 string_list_clear(c
->refs
, 0);
158 c
->trees
= load_notes_trees(c
->refs
, NOTES_INIT_WRITABLE
);
159 string_list_clear(c
->refs
, 0);
164 int copy_note_for_rewrite(struct notes_rewrite_cfg
*c
,
165 const struct object_id
*from_obj
, const struct object_id
*to_obj
)
169 for (i
= 0; c
->trees
[i
]; i
++)
170 ret
= copy_note(c
->trees
[i
], from_obj
, to_obj
, 1, c
->combine
) || ret
;
174 void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg
*c
, const char *msg
)
177 for (i
= 0; c
->trees
[i
]; i
++) {
178 commit_notes(c
->trees
[i
], msg
);
179 free_notes(c
->trees
[i
]);