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