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