1 #include "git-compat-util.h"
4 #include "environment.h"
7 #include "notes-utils.h"
10 void create_notes_commit(struct repository
*r
,
12 struct commit_list
*parents
,
13 const char *msg
, size_t msg_len
,
14 struct object_id
*result_oid
)
16 struct object_id tree_oid
;
18 assert(t
->initialized
);
20 if (write_notes_tree(t
, &tree_oid
))
21 die("Failed to write notes tree to database");
24 /* Deduce parent commit from t->ref */
25 struct object_id parent_oid
;
26 if (!read_ref(t
->ref
, &parent_oid
)) {
27 struct commit
*parent
= lookup_commit(r
, &parent_oid
);
28 if (repo_parse_commit(r
, parent
))
29 die("Failed to find/parse commit %s", t
->ref
);
30 commit_list_insert(parent
, &parents
);
32 /* else: t->ref points to nothing, assume root/orphan commit */
35 if (commit_tree(msg
, msg_len
, &tree_oid
, parents
, result_oid
, NULL
,
37 die("Failed to commit notes tree to database");
40 void commit_notes(struct repository
*r
, struct notes_tree
*t
, const char *msg
)
42 struct strbuf buf
= STRBUF_INIT
;
43 struct object_id commit_oid
;
46 t
= &default_notes_tree
;
47 if (!t
->initialized
|| !t
->update_ref
|| !*t
->update_ref
)
48 die(_("Cannot commit uninitialized/unreferenced notes tree"));
50 return; /* don't have to commit an unchanged tree */
52 /* Prepare commit message and reflog message */
53 strbuf_addstr(&buf
, msg
);
54 strbuf_complete_line(&buf
);
56 create_notes_commit(r
, t
, NULL
, buf
.buf
, buf
.len
, &commit_oid
);
57 strbuf_insertstr(&buf
, 0, "notes: ");
58 update_ref(buf
.buf
, t
->update_ref
, &commit_oid
, NULL
, 0,
59 UPDATE_REFS_DIE_ON_ERR
);
64 int parse_notes_merge_strategy(const char *v
, enum notes_merge_strategy
*s
)
66 if (!strcmp(v
, "manual"))
67 *s
= NOTES_MERGE_RESOLVE_MANUAL
;
68 else if (!strcmp(v
, "ours"))
69 *s
= NOTES_MERGE_RESOLVE_OURS
;
70 else if (!strcmp(v
, "theirs"))
71 *s
= NOTES_MERGE_RESOLVE_THEIRS
;
72 else if (!strcmp(v
, "union"))
73 *s
= NOTES_MERGE_RESOLVE_UNION
;
74 else if (!strcmp(v
, "cat_sort_uniq"))
75 *s
= NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ
;
82 static combine_notes_fn
parse_combine_notes_fn(const char *v
)
84 if (!strcasecmp(v
, "overwrite"))
85 return combine_notes_overwrite
;
86 else if (!strcasecmp(v
, "ignore"))
87 return combine_notes_ignore
;
88 else if (!strcasecmp(v
, "concatenate"))
89 return combine_notes_concatenate
;
90 else if (!strcasecmp(v
, "cat_sort_uniq"))
91 return combine_notes_cat_sort_uniq
;
96 static int notes_rewrite_config(const char *k
, const char *v
,
97 const struct config_context
*ctx UNUSED
,
100 struct notes_rewrite_cfg
*c
= cb
;
101 if (starts_with(k
, "notes.rewrite.") && !strcmp(k
+14, c
->cmd
)) {
102 c
->enabled
= git_config_bool(k
, v
);
104 } else if (!c
->mode_from_env
&& !strcmp(k
, "notes.rewritemode")) {
106 return config_error_nonbool(k
);
107 c
->combine
= parse_combine_notes_fn(v
);
109 error(_("Bad notes.rewriteMode value: '%s'"), v
);
113 } else if (!c
->refs_from_env
&& !strcmp(k
, "notes.rewriteref")) {
115 return config_error_nonbool(k
);
116 /* note that a refs/ prefix is implied in the
117 * underlying for_each_glob_ref */
118 if (starts_with(v
, "refs/notes/"))
119 string_list_add_refs_by_glob(c
->refs
, v
);
121 warning(_("Refusing to rewrite notes in %s"
122 " (outside of refs/notes/)"), v
);
130 struct notes_rewrite_cfg
*init_copy_notes_for_rewrite(const char *cmd
)
132 struct notes_rewrite_cfg
*c
= xmalloc(sizeof(struct notes_rewrite_cfg
));
133 const char *rewrite_mode_env
= getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT
);
134 const char *rewrite_refs_env
= getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT
);
137 c
->combine
= combine_notes_concatenate
;
138 CALLOC_ARRAY(c
->refs
, 1);
139 c
->refs
->strdup_strings
= 1;
140 c
->refs_from_env
= 0;
141 c
->mode_from_env
= 0;
142 if (rewrite_mode_env
) {
143 c
->mode_from_env
= 1;
144 c
->combine
= parse_combine_notes_fn(rewrite_mode_env
);
147 * TRANSLATORS: The first %s is the name of
148 * the environment variable, the second %s is
151 error(_("Bad %s value: '%s'"), GIT_NOTES_REWRITE_MODE_ENVIRONMENT
,
154 if (rewrite_refs_env
) {
155 c
->refs_from_env
= 1;
156 string_list_add_refs_from_colon_sep(c
->refs
, rewrite_refs_env
);
158 git_config(notes_rewrite_config
, c
);
159 if (!c
->enabled
|| !c
->refs
->nr
) {
160 string_list_clear(c
->refs
, 0);
165 c
->trees
= load_notes_trees(c
->refs
, NOTES_INIT_WRITABLE
);
166 string_list_clear(c
->refs
, 0);
171 int copy_note_for_rewrite(struct notes_rewrite_cfg
*c
,
172 const struct object_id
*from_obj
, const struct object_id
*to_obj
)
176 for (i
= 0; c
->trees
[i
]; i
++)
177 ret
= copy_note(c
->trees
[i
], from_obj
, to_obj
, 1, c
->combine
) || ret
;
181 void finish_copy_notes_for_rewrite(struct repository
*r
,
182 struct notes_rewrite_cfg
*c
,
186 for (i
= 0; c
->trees
[i
]; i
++) {
187 commit_notes(r
, c
->trees
[i
], msg
);
188 free_notes(c
->trees
[i
]);