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" |
Johan Herland | bf9a05b | 2013-06-12 02:13:01 +0200 | [diff] [blame] | 6 | |
| 7 | void create_notes_commit(struct notes_tree *t, struct commit_list *parents, |
Jeff King | 3ffefb5 | 2014-06-10 17:36:52 -0400 | [diff] [blame] | 8 | const char *msg, size_t msg_len, |
Patryk Obara | 5078f34 | 2018-01-28 01:13:16 +0100 | [diff] [blame] | 9 | struct object_id *result_oid) |
Johan Herland | bf9a05b | 2013-06-12 02:13:01 +0200 | [diff] [blame] | 10 | { |
brian m. carlson | 18b74e5 | 2017-05-06 22:10:04 +0000 | [diff] [blame] | 11 | struct object_id tree_oid; |
Johan Herland | bf9a05b | 2013-06-12 02:13:01 +0200 | [diff] [blame] | 12 | |
| 13 | assert(t->initialized); |
| 14 | |
Patryk Obara | bbca96d | 2018-01-28 01:13:18 +0100 | [diff] [blame] | 15 | if (write_notes_tree(t, &tree_oid)) |
Johan Herland | bf9a05b | 2013-06-12 02:13:01 +0200 | [diff] [blame] | 16 | die("Failed to write notes tree to database"); |
| 17 | |
| 18 | if (!parents) { |
| 19 | /* Deduce parent commit from t->ref */ |
brian m. carlson | 18b74e5 | 2017-05-06 22:10:04 +0000 | [diff] [blame] | 20 | struct object_id parent_oid; |
brian m. carlson | 34c290a | 2017-10-15 22:06:56 +0000 | [diff] [blame] | 21 | if (!read_ref(t->ref, &parent_oid)) { |
brian m. carlson | bc83266 | 2017-05-06 22:10:10 +0000 | [diff] [blame] | 22 | struct commit *parent = lookup_commit(&parent_oid); |
Jeff King | 5e7d4d3 | 2013-10-24 04:53:19 -0400 | [diff] [blame] | 23 | if (parse_commit(parent)) |
Johan Herland | bf9a05b | 2013-06-12 02:13:01 +0200 | [diff] [blame] | 24 | die("Failed to find/parse commit %s", t->ref); |
| 25 | commit_list_insert(parent, &parents); |
| 26 | } |
| 27 | /* else: t->ref points to nothing, assume root/orphan commit */ |
| 28 | } |
| 29 | |
Patryk Obara | 5078f34 | 2018-01-28 01:13:16 +0100 | [diff] [blame] | 30 | if (commit_tree(msg, msg_len, &tree_oid, parents, result_oid, NULL, |
| 31 | NULL)) |
Johan Herland | bf9a05b | 2013-06-12 02:13:01 +0200 | [diff] [blame] | 32 | die("Failed to commit notes tree to database"); |
| 33 | } |
Johan Herland | 49c2470 | 2013-06-12 02:13:00 +0200 | [diff] [blame] | 34 | |
| 35 | void commit_notes(struct notes_tree *t, const char *msg) |
| 36 | { |
| 37 | struct strbuf buf = STRBUF_INIT; |
brian m. carlson | 18b74e5 | 2017-05-06 22:10:04 +0000 | [diff] [blame] | 38 | struct object_id commit_oid; |
Johan Herland | 49c2470 | 2013-06-12 02:13:00 +0200 | [diff] [blame] | 39 | |
| 40 | if (!t) |
| 41 | t = &default_notes_tree; |
Mike Hommey | ee76f92 | 2015-10-08 11:54:43 +0900 | [diff] [blame] | 42 | if (!t->initialized || !t->update_ref || !*t->update_ref) |
Johan Herland | 49c2470 | 2013-06-12 02:13:00 +0200 | [diff] [blame] | 43 | die(_("Cannot commit uninitialized/unreferenced notes tree")); |
| 44 | if (!t->dirty) |
| 45 | return; /* don't have to commit an unchanged tree */ |
| 46 | |
| 47 | /* Prepare commit message and reflog message */ |
| 48 | strbuf_addstr(&buf, msg); |
René Scharfe | a0d4923 | 2014-12-12 20:16:38 +0100 | [diff] [blame] | 49 | strbuf_complete_line(&buf); |
Johan Herland | 49c2470 | 2013-06-12 02:13:00 +0200 | [diff] [blame] | 50 | |
Patryk Obara | 5078f34 | 2018-01-28 01:13:16 +0100 | [diff] [blame] | 51 | create_notes_commit(t, NULL, buf.buf, buf.len, &commit_oid); |
Johan Herland | 49c2470 | 2013-06-12 02:13:00 +0200 | [diff] [blame] | 52 | strbuf_insert(&buf, 0, "notes: ", 7); /* commit message starts at index 7 */ |
brian m. carlson | ae07777 | 2017-10-15 22:06:51 +0000 | [diff] [blame] | 53 | update_ref(buf.buf, t->update_ref, &commit_oid, NULL, 0, |
Michael Haggerty | f412411 | 2014-04-07 15:47:56 +0200 | [diff] [blame] | 54 | UPDATE_REFS_DIE_ON_ERR); |
Johan Herland | 49c2470 | 2013-06-12 02:13:00 +0200 | [diff] [blame] | 55 | |
| 56 | strbuf_release(&buf); |
| 57 | } |
| 58 | |
Jacob Keller | 93efcad | 2015-08-17 14:33:31 -0700 | [diff] [blame] | 59 | int parse_notes_merge_strategy(const char *v, enum notes_merge_strategy *s) |
| 60 | { |
| 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; |
| 71 | else |
| 72 | return -1; |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
Johan Herland | 49c2470 | 2013-06-12 02:13:00 +0200 | [diff] [blame] | 77 | static combine_notes_fn parse_combine_notes_fn(const char *v) |
| 78 | { |
| 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; |
| 87 | else |
| 88 | return NULL; |
| 89 | } |
| 90 | |
| 91 | static int notes_rewrite_config(const char *k, const char *v, void *cb) |
| 92 | { |
| 93 | struct notes_rewrite_cfg *c = cb; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 94 | if (starts_with(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) { |
Johan Herland | 49c2470 | 2013-06-12 02:13:00 +0200 | [diff] [blame] | 95 | c->enabled = git_config_bool(k, v); |
| 96 | return 0; |
| 97 | } else if (!c->mode_from_env && !strcmp(k, "notes.rewritemode")) { |
| 98 | if (!v) |
John Keeping | aa012e9 | 2014-02-16 16:06:02 +0000 | [diff] [blame] | 99 | return config_error_nonbool(k); |
Johan Herland | 49c2470 | 2013-06-12 02:13:00 +0200 | [diff] [blame] | 100 | c->combine = parse_combine_notes_fn(v); |
| 101 | if (!c->combine) { |
| 102 | error(_("Bad notes.rewriteMode value: '%s'"), v); |
| 103 | return 1; |
| 104 | } |
| 105 | return 0; |
| 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 */ |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 109 | if (starts_with(v, "refs/notes/")) |
Johan Herland | 49c2470 | 2013-06-12 02:13:00 +0200 | [diff] [blame] | 110 | string_list_add_refs_by_glob(c->refs, v); |
| 111 | else |
| 112 | warning(_("Refusing to rewrite notes in %s" |
| 113 | " (outside of refs/notes/)"), v); |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | |
| 121 | struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd) |
| 122 | { |
| 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); |
| 126 | c->cmd = cmd; |
| 127 | c->enabled = 1; |
| 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); |
| 136 | if (!c->combine) |
Ævar Arnfjörð Bjarmason | 66f5f6d | 2017-05-11 21:20:12 +0000 | [diff] [blame] | 137 | /* |
| 138 | * TRANSLATORS: The first %s is the name of |
| 139 | * the environment variable, the second %s is |
| 140 | * its value. |
| 141 | */ |
Johan Herland | 49c2470 | 2013-06-12 02:13:00 +0200 | [diff] [blame] | 142 | error(_("Bad %s value: '%s'"), GIT_NOTES_REWRITE_MODE_ENVIRONMENT, |
| 143 | rewrite_mode_env); |
| 144 | } |
| 145 | if (rewrite_refs_env) { |
| 146 | c->refs_from_env = 1; |
| 147 | string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env); |
| 148 | } |
| 149 | git_config(notes_rewrite_config, c); |
| 150 | if (!c->enabled || !c->refs->nr) { |
| 151 | string_list_clear(c->refs, 0); |
| 152 | free(c->refs); |
| 153 | free(c); |
| 154 | return NULL; |
| 155 | } |
Mike Hommey | ee76f92 | 2015-10-08 11:54:43 +0900 | [diff] [blame] | 156 | c->trees = load_notes_trees(c->refs, NOTES_INIT_WRITABLE); |
Johan Herland | 49c2470 | 2013-06-12 02:13:00 +0200 | [diff] [blame] | 157 | string_list_clear(c->refs, 0); |
| 158 | free(c->refs); |
| 159 | return c; |
| 160 | } |
| 161 | |
| 162 | int copy_note_for_rewrite(struct notes_rewrite_cfg *c, |
brian m. carlson | bb7e473 | 2017-05-30 10:30:42 -0700 | [diff] [blame] | 163 | const struct object_id *from_obj, const struct object_id *to_obj) |
Johan Herland | 49c2470 | 2013-06-12 02:13:00 +0200 | [diff] [blame] | 164 | { |
| 165 | int ret = 0; |
| 166 | int i; |
| 167 | for (i = 0; c->trees[i]; i++) |
| 168 | ret = copy_note(c->trees[i], from_obj, to_obj, 1, c->combine) || ret; |
| 169 | return ret; |
| 170 | } |
| 171 | |
| 172 | void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c, const char *msg) |
| 173 | { |
| 174 | int i; |
| 175 | for (i = 0; c->trees[i]; i++) { |
| 176 | commit_notes(c->trees[i], msg); |
| 177 | free_notes(c->trees[i]); |
| 178 | } |
| 179 | free(c->trees); |
| 180 | free(c); |
| 181 | } |