notes-utils.con commit refs.c: change ref_transaction_update() to do error checking and return status (8e34800)
   1#include "cache.h"
   2#include "commit.h"
   3#include "refs.h"
   4#include "notes-utils.h"
   5
   6void create_notes_commit(struct notes_tree *t, struct commit_list *parents,
   7                         const struct strbuf *msg, unsigned char *result_sha1)
   8{
   9        unsigned char tree_sha1[20];
  10
  11        assert(t->initialized);
  12
  13        if (write_notes_tree(t, tree_sha1))
  14                die("Failed to write notes tree to database");
  15
  16        if (!parents) {
  17                /* Deduce parent commit from t->ref */
  18                unsigned char parent_sha1[20];
  19                if (!read_ref(t->ref, parent_sha1)) {
  20                        struct commit *parent = lookup_commit(parent_sha1);
  21                        if (parse_commit(parent))
  22                                die("Failed to find/parse commit %s", t->ref);
  23                        commit_list_insert(parent, &parents);
  24                }
  25                /* else: t->ref points to nothing, assume root/orphan commit */
  26        }
  27
  28        if (commit_tree(msg, tree_sha1, parents, result_sha1, NULL, NULL))
  29                die("Failed to commit notes tree to database");
  30}
  31
  32void commit_notes(struct notes_tree *t, const char *msg)
  33{
  34        struct strbuf buf = STRBUF_INIT;
  35        unsigned char commit_sha1[20];
  36
  37        if (!t)
  38                t = &default_notes_tree;
  39        if (!t->initialized || !t->ref || !*t->ref)
  40                die(_("Cannot commit uninitialized/unreferenced notes tree"));
  41        if (!t->dirty)
  42                return; /* don't have to commit an unchanged tree */
  43
  44        /* Prepare commit message and reflog message */
  45        strbuf_addstr(&buf, msg);
  46        if (buf.buf[buf.len - 1] != '\n')
  47                strbuf_addch(&buf, '\n'); /* Make sure msg ends with newline */
  48
  49        create_notes_commit(t, NULL, &buf, commit_sha1);
  50        strbuf_insert(&buf, 0, "notes: ", 7); /* commit message starts at index 7 */
  51        update_ref(buf.buf, t->ref, commit_sha1, NULL, 0,
  52                   UPDATE_REFS_DIE_ON_ERR);
  53
  54        strbuf_release(&buf);
  55}
  56
  57static combine_notes_fn parse_combine_notes_fn(const char *v)
  58{
  59        if (!strcasecmp(v, "overwrite"))
  60                return combine_notes_overwrite;
  61        else if (!strcasecmp(v, "ignore"))
  62                return combine_notes_ignore;
  63        else if (!strcasecmp(v, "concatenate"))
  64                return combine_notes_concatenate;
  65        else if (!strcasecmp(v, "cat_sort_uniq"))
  66                return combine_notes_cat_sort_uniq;
  67        else
  68                return NULL;
  69}
  70
  71static int notes_rewrite_config(const char *k, const char *v, void *cb)
  72{
  73        struct notes_rewrite_cfg *c = cb;
  74        if (starts_with(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
  75                c->enabled = git_config_bool(k, v);
  76                return 0;
  77        } else if (!c->mode_from_env && !strcmp(k, "notes.rewritemode")) {
  78                if (!v)
  79                        return config_error_nonbool(k);
  80                c->combine = parse_combine_notes_fn(v);
  81                if (!c->combine) {
  82                        error(_("Bad notes.rewriteMode value: '%s'"), v);
  83                        return 1;
  84                }
  85                return 0;
  86        } else if (!c->refs_from_env && !strcmp(k, "notes.rewriteref")) {
  87                /* note that a refs/ prefix is implied in the
  88                 * underlying for_each_glob_ref */
  89                if (starts_with(v, "refs/notes/"))
  90                        string_list_add_refs_by_glob(c->refs, v);
  91                else
  92                        warning(_("Refusing to rewrite notes in %s"
  93                                " (outside of refs/notes/)"), v);
  94                return 0;
  95        }
  96
  97        return 0;
  98}
  99
 100
 101struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd)
 102{
 103        struct notes_rewrite_cfg *c = xmalloc(sizeof(struct notes_rewrite_cfg));
 104        const char *rewrite_mode_env = getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT);
 105        const char *rewrite_refs_env = getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT);
 106        c->cmd = cmd;
 107        c->enabled = 1;
 108        c->combine = combine_notes_concatenate;
 109        c->refs = xcalloc(1, sizeof(struct string_list));
 110        c->refs->strdup_strings = 1;
 111        c->refs_from_env = 0;
 112        c->mode_from_env = 0;
 113        if (rewrite_mode_env) {
 114                c->mode_from_env = 1;
 115                c->combine = parse_combine_notes_fn(rewrite_mode_env);
 116                if (!c->combine)
 117                        /* TRANSLATORS: The first %s is the name of the
 118                           environment variable, the second %s is its value */
 119                        error(_("Bad %s value: '%s'"), GIT_NOTES_REWRITE_MODE_ENVIRONMENT,
 120                                        rewrite_mode_env);
 121        }
 122        if (rewrite_refs_env) {
 123                c->refs_from_env = 1;
 124                string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env);
 125        }
 126        git_config(notes_rewrite_config, c);
 127        if (!c->enabled || !c->refs->nr) {
 128                string_list_clear(c->refs, 0);
 129                free(c->refs);
 130                free(c);
 131                return NULL;
 132        }
 133        c->trees = load_notes_trees(c->refs);
 134        string_list_clear(c->refs, 0);
 135        free(c->refs);
 136        return c;
 137}
 138
 139int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
 140                          const unsigned char *from_obj, const unsigned char *to_obj)
 141{
 142        int ret = 0;
 143        int i;
 144        for (i = 0; c->trees[i]; i++)
 145                ret = copy_note(c->trees[i], from_obj, to_obj, 1, c->combine) || ret;
 146        return ret;
 147}
 148
 149void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c, const char *msg)
 150{
 151        int i;
 152        for (i = 0; c->trees[i]; i++) {
 153                commit_notes(c->trees[i], msg);
 154                free_notes(c->trees[i]);
 155        }
 156        free(c->trees);
 157        free(c);
 158}