notes-utils.con commit config: don't implicitly use gitdir or commondir (dc8441f)
   1#include "cache.h"
   2#include "config.h"
   3#include "commit.h"
   4#include "refs.h"
   5#include "notes-utils.h"
   6
   7void create_notes_commit(struct notes_tree *t, struct commit_list *parents,
   8                         const char *msg, size_t msg_len,
   9                         unsigned char *result_sha1)
  10{
  11        unsigned char tree_sha1[20];
  12
  13        assert(t->initialized);
  14
  15        if (write_notes_tree(t, tree_sha1))
  16                die("Failed to write notes tree to database");
  17
  18        if (!parents) {
  19                /* Deduce parent commit from t->ref */
  20                unsigned char parent_sha1[20];
  21                if (!read_ref(t->ref, parent_sha1)) {
  22                        struct commit *parent = lookup_commit(parent_sha1);
  23                        if (parse_commit(parent))
  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
  30        if (commit_tree(msg, msg_len, tree_sha1, parents, result_sha1, NULL, NULL))
  31                die("Failed to commit notes tree to database");
  32}
  33
  34void commit_notes(struct notes_tree *t, const char *msg)
  35{
  36        struct strbuf buf = STRBUF_INIT;
  37        unsigned char commit_sha1[20];
  38
  39        if (!t)
  40                t = &default_notes_tree;
  41        if (!t->initialized || !t->update_ref || !*t->update_ref)
  42                die(_("Cannot commit uninitialized/unreferenced notes tree"));
  43        if (!t->dirty)
  44                return; /* don't have to commit an unchanged tree */
  45
  46        /* Prepare commit message and reflog message */
  47        strbuf_addstr(&buf, msg);
  48        strbuf_complete_line(&buf);
  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->update_ref, commit_sha1, NULL, 0,
  53                   UPDATE_REFS_DIE_ON_ERR);
  54
  55        strbuf_release(&buf);
  56}
  57
  58int parse_notes_merge_strategy(const char *v, enum notes_merge_strategy *s)
  59{
  60        if (!strcmp(v, "manual"))
  61                *s = NOTES_MERGE_RESOLVE_MANUAL;
  62        else if (!strcmp(v, "ours"))
  63                *s = NOTES_MERGE_RESOLVE_OURS;
  64        else if (!strcmp(v, "theirs"))
  65                *s = NOTES_MERGE_RESOLVE_THEIRS;
  66        else if (!strcmp(v, "union"))
  67                *s = NOTES_MERGE_RESOLVE_UNION;
  68        else if (!strcmp(v, "cat_sort_uniq"))
  69                *s = NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ;
  70        else
  71                return -1;
  72
  73        return 0;
  74}
  75
  76static combine_notes_fn parse_combine_notes_fn(const char *v)
  77{
  78        if (!strcasecmp(v, "overwrite"))
  79                return combine_notes_overwrite;
  80        else if (!strcasecmp(v, "ignore"))
  81                return combine_notes_ignore;
  82        else if (!strcasecmp(v, "concatenate"))
  83                return combine_notes_concatenate;
  84        else if (!strcasecmp(v, "cat_sort_uniq"))
  85                return combine_notes_cat_sort_uniq;
  86        else
  87                return NULL;
  88}
  89
  90static int notes_rewrite_config(const char *k, const char *v, void *cb)
  91{
  92        struct notes_rewrite_cfg *c = cb;
  93        if (starts_with(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
  94                c->enabled = git_config_bool(k, v);
  95                return 0;
  96        } else if (!c->mode_from_env && !strcmp(k, "notes.rewritemode")) {
  97                if (!v)
  98                        return config_error_nonbool(k);
  99                c->combine = parse_combine_notes_fn(v);
 100                if (!c->combine) {
 101                        error(_("Bad notes.rewriteMode value: '%s'"), v);
 102                        return 1;
 103                }
 104                return 0;
 105        } else if (!c->refs_from_env && !strcmp(k, "notes.rewriteref")) {
 106                /* note that a refs/ prefix is implied in the
 107                 * underlying for_each_glob_ref */
 108                if (starts_with(v, "refs/notes/"))
 109                        string_list_add_refs_by_glob(c->refs, v);
 110                else
 111                        warning(_("Refusing to rewrite notes in %s"
 112                                " (outside of refs/notes/)"), v);
 113                return 0;
 114        }
 115
 116        return 0;
 117}
 118
 119
 120struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd)
 121{
 122        struct notes_rewrite_cfg *c = xmalloc(sizeof(struct notes_rewrite_cfg));
 123        const char *rewrite_mode_env = getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT);
 124        const char *rewrite_refs_env = getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT);
 125        c->cmd = cmd;
 126        c->enabled = 1;
 127        c->combine = combine_notes_concatenate;
 128        c->refs = xcalloc(1, sizeof(struct string_list));
 129        c->refs->strdup_strings = 1;
 130        c->refs_from_env = 0;
 131        c->mode_from_env = 0;
 132        if (rewrite_mode_env) {
 133                c->mode_from_env = 1;
 134                c->combine = parse_combine_notes_fn(rewrite_mode_env);
 135                if (!c->combine)
 136                        /* TRANSLATORS: The first %s is the name of the
 137                           environment variable, the second %s is its value */
 138                        error(_("Bad %s value: '%s'"), GIT_NOTES_REWRITE_MODE_ENVIRONMENT,
 139                                        rewrite_mode_env);
 140        }
 141        if (rewrite_refs_env) {
 142                c->refs_from_env = 1;
 143                string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env);
 144        }
 145        git_config(notes_rewrite_config, c);
 146        if (!c->enabled || !c->refs->nr) {
 147                string_list_clear(c->refs, 0);
 148                free(c->refs);
 149                free(c);
 150                return NULL;
 151        }
 152        c->trees = load_notes_trees(c->refs, NOTES_INIT_WRITABLE);
 153        string_list_clear(c->refs, 0);
 154        free(c->refs);
 155        return c;
 156}
 157
 158int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
 159                          const unsigned char *from_obj, const unsigned char *to_obj)
 160{
 161        int ret = 0;
 162        int i;
 163        for (i = 0; c->trees[i]; i++)
 164                ret = copy_note(c->trees[i], from_obj, to_obj, 1, c->combine) || ret;
 165        return ret;
 166}
 167
 168void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c, const char *msg)
 169{
 170        int i;
 171        for (i = 0; c->trees[i]; i++) {
 172                commit_notes(c->trees[i], msg);
 173                free_notes(c->trees[i]);
 174        }
 175        free(c->trees);
 176        free(c);
 177}