Skip to content

Commit 49c2470

Browse files
jherlandgitster
authored andcommitted
Move copy_note_for_rewrite + friends from builtin/notes.c to notes-utils.c
This is a pure code movement of the machinery for copying notes to rewritten objects. This code was located in builtin/notes.c for historical reasons. In order to make it available to builtin/commit.c it was declared in builtin.h. This was more of an accident of history than a concious design, and we now want to make this machinery more widely available. Hence, this patch moves the code into the new notes-utils.[hc] files which are included into libgit.a. Except for adjusting #includes accordingly, this patch merely moves the relevant functions verbatim into the new files. Cc: Thomas Rast <[email protected]> Signed-off-by: Johan Herland <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 80a1466 commit 49c2470

File tree

6 files changed

+159
-146
lines changed

6 files changed

+159
-146
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,7 @@ LIB_H += merge-recursive.h
682682
LIB_H += mergesort.h
683683
LIB_H += notes-cache.h
684684
LIB_H += notes-merge.h
685+
LIB_H += notes-utils.h
685686
LIB_H += notes.h
686687
LIB_H += object.h
687688
LIB_H += pack-refs.h
@@ -815,6 +816,7 @@ LIB_OBJS += name-hash.o
815816
LIB_OBJS += notes.o
816817
LIB_OBJS += notes-cache.o
817818
LIB_OBJS += notes-merge.o
819+
LIB_OBJS += notes-utils.o
818820
LIB_OBJS += object.o
819821
LIB_OBJS += pack-check.o
820822
LIB_OBJS += pack-refs.o

builtin.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include "strbuf.h"
66
#include "cache.h"
77
#include "commit.h"
8-
#include "notes.h"
98

109
#define DEFAULT_MERGE_LOG_LEN 20
1110

@@ -23,21 +22,6 @@ struct fmt_merge_msg_opts {
2322
extern int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
2423
struct fmt_merge_msg_opts *);
2524

26-
struct notes_rewrite_cfg {
27-
struct notes_tree **trees;
28-
const char *cmd;
29-
int enabled;
30-
combine_notes_fn combine;
31-
struct string_list *refs;
32-
int refs_from_env;
33-
int mode_from_env;
34-
};
35-
36-
struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd);
37-
int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
38-
const unsigned char *from_obj, const unsigned char *to_obj);
39-
void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c, const char *msg);
40-
4125
extern int textconv_object(const char *path, unsigned mode, const unsigned char *sha1, int sha1_valid, char **buf, unsigned long *buf_size);
4226

4327
extern int cmd_add(int argc, const char **argv, const char *prefix);

builtin/commit.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "gpg-interface.h"
3030
#include "column.h"
3131
#include "sequencer.h"
32+
#include "notes-utils.h"
3233

3334
static const char * const builtin_commit_usage[] = {
3435
N_("git commit [options] [--] <pathspec>..."),

builtin/notes.c

Lines changed: 1 addition & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
#include "parse-options.h"
1919
#include "string-list.h"
2020
#include "notes-merge.h"
21-
22-
static void commit_notes(struct notes_tree *t, const char *msg);
23-
static combine_notes_fn parse_combine_notes_fn(const char *v);
21+
#include "notes-utils.h"
2422

2523
static const char * const git_notes_usage[] = {
2624
N_("git notes [--ref <notes_ref>] [list [<object>]]"),
@@ -287,133 +285,6 @@ static int parse_reedit_arg(const struct option *opt, const char *arg, int unset
287285
return parse_reuse_arg(opt, arg, unset);
288286
}
289287

290-
static void commit_notes(struct notes_tree *t, const char *msg)
291-
{
292-
struct strbuf buf = STRBUF_INIT;
293-
unsigned char commit_sha1[20];
294-
295-
if (!t)
296-
t = &default_notes_tree;
297-
if (!t->initialized || !t->ref || !*t->ref)
298-
die(_("Cannot commit uninitialized/unreferenced notes tree"));
299-
if (!t->dirty)
300-
return; /* don't have to commit an unchanged tree */
301-
302-
/* Prepare commit message and reflog message */
303-
strbuf_addstr(&buf, msg);
304-
if (buf.buf[buf.len - 1] != '\n')
305-
strbuf_addch(&buf, '\n'); /* Make sure msg ends with newline */
306-
307-
create_notes_commit(t, NULL, &buf, commit_sha1);
308-
strbuf_insert(&buf, 0, "notes: ", 7); /* commit message starts at index 7 */
309-
update_ref(buf.buf, t->ref, commit_sha1, NULL, 0, DIE_ON_ERR);
310-
311-
strbuf_release(&buf);
312-
}
313-
314-
static combine_notes_fn parse_combine_notes_fn(const char *v)
315-
{
316-
if (!strcasecmp(v, "overwrite"))
317-
return combine_notes_overwrite;
318-
else if (!strcasecmp(v, "ignore"))
319-
return combine_notes_ignore;
320-
else if (!strcasecmp(v, "concatenate"))
321-
return combine_notes_concatenate;
322-
else if (!strcasecmp(v, "cat_sort_uniq"))
323-
return combine_notes_cat_sort_uniq;
324-
else
325-
return NULL;
326-
}
327-
328-
static int notes_rewrite_config(const char *k, const char *v, void *cb)
329-
{
330-
struct notes_rewrite_cfg *c = cb;
331-
if (!prefixcmp(k, "notes.rewrite.") && !strcmp(k+14, c->cmd)) {
332-
c->enabled = git_config_bool(k, v);
333-
return 0;
334-
} else if (!c->mode_from_env && !strcmp(k, "notes.rewritemode")) {
335-
if (!v)
336-
config_error_nonbool(k);
337-
c->combine = parse_combine_notes_fn(v);
338-
if (!c->combine) {
339-
error(_("Bad notes.rewriteMode value: '%s'"), v);
340-
return 1;
341-
}
342-
return 0;
343-
} else if (!c->refs_from_env && !strcmp(k, "notes.rewriteref")) {
344-
/* note that a refs/ prefix is implied in the
345-
* underlying for_each_glob_ref */
346-
if (!prefixcmp(v, "refs/notes/"))
347-
string_list_add_refs_by_glob(c->refs, v);
348-
else
349-
warning(_("Refusing to rewrite notes in %s"
350-
" (outside of refs/notes/)"), v);
351-
return 0;
352-
}
353-
354-
return 0;
355-
}
356-
357-
358-
struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd)
359-
{
360-
struct notes_rewrite_cfg *c = xmalloc(sizeof(struct notes_rewrite_cfg));
361-
const char *rewrite_mode_env = getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT);
362-
const char *rewrite_refs_env = getenv(GIT_NOTES_REWRITE_REF_ENVIRONMENT);
363-
c->cmd = cmd;
364-
c->enabled = 1;
365-
c->combine = combine_notes_concatenate;
366-
c->refs = xcalloc(1, sizeof(struct string_list));
367-
c->refs->strdup_strings = 1;
368-
c->refs_from_env = 0;
369-
c->mode_from_env = 0;
370-
if (rewrite_mode_env) {
371-
c->mode_from_env = 1;
372-
c->combine = parse_combine_notes_fn(rewrite_mode_env);
373-
if (!c->combine)
374-
/* TRANSLATORS: The first %s is the name of the
375-
environment variable, the second %s is its value */
376-
error(_("Bad %s value: '%s'"), GIT_NOTES_REWRITE_MODE_ENVIRONMENT,
377-
rewrite_mode_env);
378-
}
379-
if (rewrite_refs_env) {
380-
c->refs_from_env = 1;
381-
string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env);
382-
}
383-
git_config(notes_rewrite_config, c);
384-
if (!c->enabled || !c->refs->nr) {
385-
string_list_clear(c->refs, 0);
386-
free(c->refs);
387-
free(c);
388-
return NULL;
389-
}
390-
c->trees = load_notes_trees(c->refs);
391-
string_list_clear(c->refs, 0);
392-
free(c->refs);
393-
return c;
394-
}
395-
396-
int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
397-
const unsigned char *from_obj, const unsigned char *to_obj)
398-
{
399-
int ret = 0;
400-
int i;
401-
for (i = 0; c->trees[i]; i++)
402-
ret = copy_note(c->trees[i], from_obj, to_obj, 1, c->combine) || ret;
403-
return ret;
404-
}
405-
406-
void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c, const char *msg)
407-
{
408-
int i;
409-
for (i = 0; c->trees[i]; i++) {
410-
commit_notes(c->trees[i], msg);
411-
free_notes(c->trees[i]);
412-
}
413-
free(c->trees);
414-
free(c);
415-
}
416-
417288
static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
418289
{
419290
struct strbuf buf = STRBUF_INIT;

notes-utils.c

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
7+
void 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+
31+
static 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+
45+
static 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+
75+
struct 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+
113+
int 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+
123+
void 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+
}

notes-utils.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef NOTES_UTILS_H
2+
#define NOTES_UTILS_H
3+
4+
#include "notes.h"
5+
6+
void commit_notes(struct notes_tree *t, const char *msg);
7+
8+
struct notes_rewrite_cfg {
9+
struct notes_tree **trees;
10+
const char *cmd;
11+
int enabled;
12+
combine_notes_fn combine;
13+
struct string_list *refs;
14+
int refs_from_env;
15+
int mode_from_env;
16+
};
17+
18+
struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd);
19+
int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
20+
const unsigned char *from_obj, const unsigned char *to_obj);
21+
void finish_copy_notes_for_rewrite(struct notes_rewrite_cfg *c, const char *msg);
22+
23+
#endif

0 commit comments

Comments
 (0)