Johan Herland | 49c2470 | 2013-06-12 02:13:00 +0200 | [diff] [blame] | 1 | #include "cache.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 2 | #include "config.h" |
Johan Herland | 49c2470 | 2013-06-12 02:13:00 +0200 | [diff] [blame] | 3 | #include "commit.h" |
| 4 | #include "refs.h" |
| 5 | #include "notes-utils.h" |
Stefan Beller | c1f5eb4 | 2018-06-28 18:21:59 -0700 | [diff] [blame] | 6 | #include "repository.h" |
Johan Herland | bf9a05b | 2013-06-12 02:13:01 +0200 | [diff] [blame] | 7 | |
Nguyễn Thái Ngọc Duy | 1d18d75 | 2019-01-12 09:13:23 +0700 | [diff] [blame] | 8 | void create_notes_commit(struct repository *r, |
| 9 | struct notes_tree *t, |
| 10 | struct commit_list *parents, |
Jeff King | 3ffefb5 | 2014-06-10 17:36:52 -0400 | [diff] [blame] | 11 | const char *msg, size_t msg_len, |
Patryk Obara | 5078f34 | 2018-01-28 01:13:16 +0100 | [diff] [blame] | 12 | struct object_id *result_oid) |
Johan Herland | bf9a05b | 2013-06-12 02:13:01 +0200 | [diff] [blame] | 13 | { |
brian m. carlson | 18b74e5 | 2017-05-06 22:10:04 +0000 | [diff] [blame] | 14 | struct object_id tree_oid; |
Johan Herland | bf9a05b | 2013-06-12 02:13:01 +0200 | [diff] [blame] | 15 | |
| 16 | assert(t->initialized); |
| 17 | |
Patryk Obara | bbca96d | 2018-01-28 01:13:18 +0100 | [diff] [blame] | 18 | if (write_notes_tree(t, &tree_oid)) |
Johan Herland | bf9a05b | 2013-06-12 02:13:01 +0200 | [diff] [blame] | 19 | die("Failed to write notes tree to database"); |
| 20 | |
| 21 | if (!parents) { |
| 22 | /* Deduce parent commit from t->ref */ |
brian m. carlson | 18b74e5 | 2017-05-06 22:10:04 +0000 | [diff] [blame] | 23 | struct object_id parent_oid; |
brian m. carlson | 34c290a | 2017-10-15 22:06:56 +0000 | [diff] [blame] | 24 | if (!read_ref(t->ref, &parent_oid)) { |
Nguyễn Thái Ngọc Duy | 1d18d75 | 2019-01-12 09:13:23 +0700 | [diff] [blame] | 25 | struct commit *parent = lookup_commit(r, &parent_oid); |
Jeff King | 5e7d4d3 | 2013-10-24 04:53:19 -0400 | [diff] [blame] | 26 | if (parse_commit(parent)) |
Johan Herland | bf9a05b | 2013-06-12 02:13:01 +0200 | [diff] [blame] | 27 | die("Failed to find/parse commit %s", t->ref); |
| 28 | commit_list_insert(parent, &parents); |
| 29 | } |
| 30 | /* else: t->ref points to nothing, assume root/orphan commit */ |
| 31 | } |
| 32 | |
Patryk Obara | 5078f34 | 2018-01-28 01:13:16 +0100 | [diff] [blame] | 33 | if (commit_tree(msg, msg_len, &tree_oid, parents, result_oid, NULL, |
| 34 | NULL)) |
Johan Herland | bf9a05b | 2013-06-12 02:13:01 +0200 | [diff] [blame] | 35 | die("Failed to commit notes tree to database"); |
| 36 | } |
Johan Herland | 49c2470 | 2013-06-12 02:13:00 +0200 | [diff] [blame] | 37 | |
Nguyễn Thái Ngọc Duy | 1d18d75 | 2019-01-12 09:13:23 +0700 | [diff] [blame] | 38 | void commit_notes(struct repository *r, struct notes_tree *t, const char *msg) |
Johan Herland | 49c2470 | 2013-06-12 02:13:00 +0200 | [diff] [blame] | 39 | { |
| 40 | struct strbuf buf = STRBUF_INIT; |
brian m. carlson | 18b74e5 | 2017-05-06 22:10:04 +0000 | [diff] [blame] | 41 | struct object_id commit_oid; |
Johan Herland | 49c2470 | 2013-06-12 02:13:00 +0200 | [diff] [blame] | 42 | |
| 43 | if (!t) |
| 44 | t = &default_notes_tree; |
Mike Hommey | ee76f92 | 2015-10-08 11:54:43 +0900 | [diff] [blame] | 45 | if (!t->initialized || !t->update_ref || !*t->update_ref) |
Johan Herland | 49c2470 | 2013-06-12 02:13:00 +0200 | [diff] [blame] | 46 | die(_("Cannot commit uninitialized/unreferenced notes tree")); |
| 47 | if (!t->dirty) |
| 48 | return; /* don't have to commit an unchanged tree */ |
| 49 | |
| 50 | /* Prepare commit message and reflog message */ |
| 51 | strbuf_addstr(&buf, msg); |
René Scharfe | a0d4923 | 2014-12-12 20:16:38 +0100 | [diff] [blame] | 52 | strbuf_complete_line(&buf); |
Johan Herland | 49c2470 | 2013-06-12 02:13:00 +0200 | [diff] [blame] | 53 | |
Nguyễn Thái Ngọc Duy | 1d18d75 | 2019-01-12 09:13:23 +0700 | [diff] [blame] | 54 | create_notes_commit(r, t, NULL, buf.buf, buf.len, &commit_oid); |
René Scharfe | a91cc7f | 2020-02-09 14:44:23 +0100 | [diff] [blame] | 55 | strbuf_insertstr(&buf, 0, "notes: "); |
brian m. carlson | ae07777 | 2017-10-15 22:06:51 +0000 | [diff] [blame] | 56 | update_ref(buf.buf, t->update_ref, &commit_oid, NULL, 0, |
Michael Haggerty | f412411 | 2014-04-07 15:47:56 +0200 | [diff] [blame] | 57 | UPDATE_REFS_DIE_ON_ERR); |
Johan Herland | 49c2470 | 2013-06-12 02:13:00 +0200 | [diff] [blame] | 58 | |
| 59 | strbuf_release(&buf); |
| 60 | } |
| 61 | |
Jacob Keller | 93efcad | 2015-08-17 14:33:31 -0700 | [diff] [blame] | 62 | int parse_notes_merge_strategy(const char *v, enum notes_merge_strategy *s) |
| 63 | { |
| 64 | if (!strcmp(v, "manual")) |
| 65 | *s = NOTES_MERGE_RESOLVE_MANUAL; |
| 66 | else if (!strcmp(v, "ours")) |
| 67 | *s = NOTES_MERGE_RESOLVE_OURS; |
| 68 | else if (!strcmp(v, "theirs")) |
| 69 | *s = NOTES_MERGE_RESOLVE_THEIRS; |
| 70 | else if (!strcmp(v, "union")) |
| 71 | *s = NOTES_MERGE_RESOLVE_UNION; |
| 72 | else if (!strcmp(v, "cat_sort_uniq")) |
| 73 | *s = NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ; |
| 74 | else |
| 75 | return -1; |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
Johan Herland | 49c2470 | 2013-06-12 02:13:00 +0200 | [diff] [blame] | 80 | static combine_notes_fn parse_combine_notes_fn(const char *v) |
| 81 | { |
| 82 | if (!strcasecmp(v, "overwrite")) |
| 83 | return combine_notes_overwrite; |
| 84 | else if (!strcasecmp(v, "ignore")) |
| 85 | return combine_notes_ignore; |
| 86 | else if (!strcasecmp(v, "concatenate")) |
| 87 | return combine_notes_concatenate; |
| 88 | else if (!strcasecmp(v, "cat_sort_uniq")) |
| 89 | return combine_notes_cat_sort_uniq; |
| 90 | else |
| 91 | return NULL; |
| 92 | } |
| 93 | |
| 94 | static int notes_rewrite_config(const char *k, const char *v, void *cb) |
| 95 | { |
| 96 | struct notes_rewrite_cfg *c = cb; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 97 | if (starts_with(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) { |
Johan Herland | 49c2470 | 2013-06-12 02:13:00 +0200 | [diff] [blame] | 98 | c->enabled = git_config_bool(k, v); |
| 99 | return 0; |
| 100 | } else if (!c->mode_from_env && !strcmp(k, "notes.rewritemode")) { |
| 101 | if (!v) |
John Keeping | aa012e9 | 2014-02-16 16:06:02 +0000 | [diff] [blame] | 102 | return config_error_nonbool(k); |
Johan Herland | 49c2470 | 2013-06-12 02:13:00 +0200 | [diff] [blame] | 103 | c->combine = parse_combine_notes_fn(v); |
| 104 | if (!c->combine) { |
| 105 | error(_("Bad notes.rewriteMode value: '%s'"), v); |
| 106 | return 1; |
| 107 | } |
| 108 | return 0; |
| 109 | } else if (!c->refs_from_env && !strcmp(k, "notes.rewriteref")) { |
| 110 | /* note that a refs/ prefix is implied in the |
| 111 | * underlying for_each_glob_ref */ |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 112 | if (starts_with(v, "refs/notes/")) |
Johan Herland | 49c2470 | 2013-06-12 02:13:00 +0200 | [diff] [blame] | 113 | string_list_add_refs_by_glob(c->refs, v); |
| 114 | else |
| 115 | warning(_("Refusing to rewrite notes in %s" |
| 116 | " (outside of refs/notes/)"), v); |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd) |
| 125 | { |
| 126 | struct notes_rewrite_cfg *c = xmalloc(sizeof(struct notes_rewrite_cfg)); |
| 127 | const char *rewrite_mode_env = getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT); |
| 128 | const char *rewrite_refs_env = getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT); |
| 129 | c->cmd = cmd; |
| 130 | c->enabled = 1; |
| 131 | c->combine = combine_notes_concatenate; |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 132 | CALLOC_ARRAY(c->refs, 1); |
Johan Herland | 49c2470 | 2013-06-12 02:13:00 +0200 | [diff] [blame] | 133 | c->refs->strdup_strings = 1; |
| 134 | c->refs_from_env = 0; |
| 135 | c->mode_from_env = 0; |
| 136 | if (rewrite_mode_env) { |
| 137 | c->mode_from_env = 1; |
| 138 | c->combine = parse_combine_notes_fn(rewrite_mode_env); |
| 139 | if (!c->combine) |
Ævar Arnfjörð Bjarmason | 66f5f6d | 2017-05-11 21:20:12 +0000 | [diff] [blame] | 140 | /* |
| 141 | * TRANSLATORS: The first %s is the name of |
| 142 | * the environment variable, the second %s is |
| 143 | * its value. |
| 144 | */ |
Johan Herland | 49c2470 | 2013-06-12 02:13:00 +0200 | [diff] [blame] | 145 | error(_("Bad %s value: '%s'"), GIT_NOTES_REWRITE_MODE_ENVIRONMENT, |
| 146 | rewrite_mode_env); |
| 147 | } |
| 148 | if (rewrite_refs_env) { |
| 149 | c->refs_from_env = 1; |
| 150 | string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env); |
| 151 | } |
| 152 | git_config(notes_rewrite_config, c); |
| 153 | if (!c->enabled || !c->refs->nr) { |
| 154 | string_list_clear(c->refs, 0); |
| 155 | free(c->refs); |
| 156 | free(c); |
| 157 | return NULL; |
| 158 | } |
Mike Hommey | ee76f92 | 2015-10-08 11:54:43 +0900 | [diff] [blame] | 159 | c->trees = load_notes_trees(c->refs, NOTES_INIT_WRITABLE); |
Johan Herland | 49c2470 | 2013-06-12 02:13:00 +0200 | [diff] [blame] | 160 | string_list_clear(c->refs, 0); |
| 161 | free(c->refs); |
| 162 | return c; |
| 163 | } |
| 164 | |
| 165 | int copy_note_for_rewrite(struct notes_rewrite_cfg *c, |
brian m. carlson | bb7e473 | 2017-05-30 10:30:42 -0700 | [diff] [blame] | 166 | const struct object_id *from_obj, const struct object_id *to_obj) |
Johan Herland | 49c2470 | 2013-06-12 02:13:00 +0200 | [diff] [blame] | 167 | { |
| 168 | int ret = 0; |
| 169 | int i; |
| 170 | for (i = 0; c->trees[i]; i++) |
| 171 | ret = copy_note(c->trees[i], from_obj, to_obj, 1, c->combine) || ret; |
| 172 | return ret; |
| 173 | } |
| 174 | |
Nguyễn Thái Ngọc Duy | 1d18d75 | 2019-01-12 09:13:23 +0700 | [diff] [blame] | 175 | void finish_copy_notes_for_rewrite(struct repository *r, |
| 176 | struct notes_rewrite_cfg *c, |
| 177 | const char *msg) |
Johan Herland | 49c2470 | 2013-06-12 02:13:00 +0200 | [diff] [blame] | 178 | { |
| 179 | int i; |
| 180 | for (i = 0; c->trees[i]; i++) { |
Nguyễn Thái Ngọc Duy | 1d18d75 | 2019-01-12 09:13:23 +0700 | [diff] [blame] | 181 | commit_notes(r, c->trees[i], msg); |
Johan Herland | 49c2470 | 2013-06-12 02:13:00 +0200 | [diff] [blame] | 182 | free_notes(c->trees[i]); |
| 183 | } |
| 184 | free(c->trees); |
| 185 | free(c); |
| 186 | } |