Coverage Report

Created: 2024-09-08 06:23

/src/git/notes-utils.c
Line
Count
Source (jump to first uncovered line)
1
#define USE_THE_REPOSITORY_VARIABLE
2
3
#include "git-compat-util.h"
4
#include "config.h"
5
#include "commit.h"
6
#include "environment.h"
7
#include "gettext.h"
8
#include "refs.h"
9
#include "notes-utils.h"
10
#include "strbuf.h"
11
12
void create_notes_commit(struct repository *r,
13
       struct notes_tree *t,
14
       const struct commit_list *parents,
15
       const char *msg, size_t msg_len,
16
       struct object_id *result_oid)
17
0
{
18
0
  struct commit_list *parents_to_free = NULL;
19
0
  struct object_id tree_oid;
20
21
0
  assert(t->initialized);
22
23
0
  if (write_notes_tree(t, &tree_oid))
24
0
    die("Failed to write notes tree to database");
25
26
0
  if (!parents) {
27
    /* Deduce parent commit from t->ref */
28
0
    struct object_id parent_oid;
29
0
    if (!refs_read_ref(get_main_ref_store(the_repository), t->ref, &parent_oid)) {
30
0
      struct commit *parent = lookup_commit(r, &parent_oid);
31
0
      if (repo_parse_commit(r, parent))
32
0
        die("Failed to find/parse commit %s", t->ref);
33
0
      commit_list_insert(parent, &parents_to_free);
34
0
      parents = parents_to_free;
35
0
    }
36
    /* else: t->ref points to nothing, assume root/orphan commit */
37
0
  }
38
39
0
  if (commit_tree(msg, msg_len, &tree_oid, parents, result_oid, NULL,
40
0
      NULL))
41
0
    die("Failed to commit notes tree to database");
42
43
0
  free_commit_list(parents_to_free);
44
0
}
45
46
void commit_notes(struct repository *r, struct notes_tree *t, const char *msg)
47
0
{
48
0
  struct strbuf buf = STRBUF_INIT;
49
0
  struct object_id commit_oid;
50
51
0
  if (!t)
52
0
    t = &default_notes_tree;
53
0
  if (!t->initialized || !t->update_ref || !*t->update_ref)
54
0
    die(_("Cannot commit uninitialized/unreferenced notes tree"));
55
0
  if (!t->dirty)
56
0
    return; /* don't have to commit an unchanged tree */
57
58
  /* Prepare commit message and reflog message */
59
0
  strbuf_addstr(&buf, msg);
60
0
  strbuf_complete_line(&buf);
61
62
0
  create_notes_commit(r, t, NULL, buf.buf, buf.len, &commit_oid);
63
0
  strbuf_insertstr(&buf, 0, "notes: ");
64
0
  refs_update_ref(get_main_ref_store(the_repository), buf.buf,
65
0
      t->update_ref, &commit_oid, NULL, 0,
66
0
      UPDATE_REFS_DIE_ON_ERR);
67
68
0
  strbuf_release(&buf);
69
0
}
70
71
int parse_notes_merge_strategy(const char *v, enum notes_merge_strategy *s)
72
0
{
73
0
  if (!strcmp(v, "manual"))
74
0
    *s = NOTES_MERGE_RESOLVE_MANUAL;
75
0
  else if (!strcmp(v, "ours"))
76
0
    *s = NOTES_MERGE_RESOLVE_OURS;
77
0
  else if (!strcmp(v, "theirs"))
78
0
    *s = NOTES_MERGE_RESOLVE_THEIRS;
79
0
  else if (!strcmp(v, "union"))
80
0
    *s = NOTES_MERGE_RESOLVE_UNION;
81
0
  else if (!strcmp(v, "cat_sort_uniq"))
82
0
    *s = NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ;
83
0
  else
84
0
    return -1;
85
86
0
  return 0;
87
0
}
88
89
static combine_notes_fn parse_combine_notes_fn(const char *v)
90
0
{
91
0
  if (!strcasecmp(v, "overwrite"))
92
0
    return combine_notes_overwrite;
93
0
  else if (!strcasecmp(v, "ignore"))
94
0
    return combine_notes_ignore;
95
0
  else if (!strcasecmp(v, "concatenate"))
96
0
    return combine_notes_concatenate;
97
0
  else if (!strcasecmp(v, "cat_sort_uniq"))
98
0
    return combine_notes_cat_sort_uniq;
99
0
  else
100
0
    return NULL;
101
0
}
102
103
static int notes_rewrite_config(const char *k, const char *v,
104
        const struct config_context *ctx UNUSED,
105
        void *cb)
106
0
{
107
0
  struct notes_rewrite_cfg *c = cb;
108
0
  if (starts_with(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
109
0
    c->enabled = git_config_bool(k, v);
110
0
    return 0;
111
0
  } else if (!c->mode_from_env && !strcmp(k, "notes.rewritemode")) {
112
0
    if (!v)
113
0
      return config_error_nonbool(k);
114
0
    c->combine = parse_combine_notes_fn(v);
115
0
    if (!c->combine) {
116
0
      error(_("Bad notes.rewriteMode value: '%s'"), v);
117
0
      return 1;
118
0
    }
119
0
    return 0;
120
0
  } else if (!c->refs_from_env && !strcmp(k, "notes.rewriteref")) {
121
0
    if (!v)
122
0
      return config_error_nonbool(k);
123
    /* note that a refs/ prefix is implied in the
124
     * underlying for_each_glob_ref */
125
0
    if (starts_with(v, "refs/notes/"))
126
0
      string_list_add_refs_by_glob(c->refs, v);
127
0
    else
128
0
      warning(_("Refusing to rewrite notes in %s"
129
0
        " (outside of refs/notes/)"), v);
130
0
    return 0;
131
0
  }
132
133
0
  return 0;
134
0
}
135
136
137
struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd)
138
0
{
139
0
  struct notes_rewrite_cfg *c = xmalloc(sizeof(struct notes_rewrite_cfg));
140
0
  const char *rewrite_mode_env = getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT);
141
0
  const char *rewrite_refs_env = getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT);
142
0
  c->cmd = cmd;
143
0
  c->enabled = 1;
144
0
  c->combine = combine_notes_concatenate;
145
0
  CALLOC_ARRAY(c->refs, 1);
146
0
  c->refs->strdup_strings = 1;
147
0
  c->refs_from_env = 0;
148
0
  c->mode_from_env = 0;
149
0
  if (rewrite_mode_env) {
150
0
    c->mode_from_env = 1;
151
0
    c->combine = parse_combine_notes_fn(rewrite_mode_env);
152
0
    if (!c->combine)
153
      /*
154
       * TRANSLATORS: The first %s is the name of
155
       * the environment variable, the second %s is
156
       * its value.
157
       */
158
0
      error(_("Bad %s value: '%s'"), GIT_NOTES_REWRITE_MODE_ENVIRONMENT,
159
0
          rewrite_mode_env);
160
0
  }
161
0
  if (rewrite_refs_env) {
162
0
    c->refs_from_env = 1;
163
0
    string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env);
164
0
  }
165
0
  git_config(notes_rewrite_config, c);
166
0
  if (!c->enabled || !c->refs->nr) {
167
0
    string_list_clear(c->refs, 0);
168
0
    free(c->refs);
169
0
    free(c);
170
0
    return NULL;
171
0
  }
172
0
  c->trees = load_notes_trees(c->refs, NOTES_INIT_WRITABLE);
173
0
  string_list_clear(c->refs, 0);
174
0
  free(c->refs);
175
0
  return c;
176
0
}
177
178
int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
179
        const struct object_id *from_obj, const struct object_id *to_obj)
180
0
{
181
0
  int ret = 0;
182
0
  int i;
183
0
  for (i = 0; c->trees[i]; i++)
184
0
    ret = copy_note(c->trees[i], from_obj, to_obj, 1, c->combine) || ret;
185
0
  return ret;
186
0
}
187
188
void finish_copy_notes_for_rewrite(struct repository *r,
189
           struct notes_rewrite_cfg *c,
190
           const char *msg)
191
0
{
192
0
  int i;
193
0
  for (i = 0; c->trees[i]; i++) {
194
0
    commit_notes(r, c->trees[i], msg);
195
0
    free_notes(c->trees[i]);
196
0
    free(c->trees[i]);
197
0
  }
198
0
  free(c->trees);
199
0
  free(c);
200
0
}