Skip to content

Commit bebf5c0

Browse files
jherlandgitster
authored andcommitted
builtin/notes: improve naming
In preparation for some needed refactoring, rename struct msg_arg to struct note_data, and rename its instances from "msg" to "d" (also removing some unnecessary parentheses). The 'msg_arg' name was inherited from tag.c, but is not really a good name for the contents of a note. Also rename write_note_data() to copy_obj_to_fd(), which more aptly describes what it actually does: Copying the contents of a git object (given by its SHA1) into a given file descriptor. Signed-off-by: Johan Herland <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d0923b6 commit bebf5c0

File tree

1 file changed

+54
-55
lines changed

1 file changed

+54
-55
lines changed

builtin/notes.c

Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ static const char * const git_notes_get_ref_usage[] = {
9292
static const char note_template[] =
9393
"\nWrite/edit the notes for the following object:\n";
9494

95-
struct msg_arg {
95+
struct note_data {
9696
int given;
9797
int use_editor;
9898
struct strbuf buf;
@@ -106,7 +106,7 @@ static int list_each_note(const unsigned char *object_sha1,
106106
return 0;
107107
}
108108

109-
static void write_note_data(int fd, const unsigned char *sha1)
109+
static void copy_obj_to_fd(int fd, const unsigned char *sha1)
110110
{
111111
unsigned long size;
112112
enum object_type type;
@@ -149,13 +149,13 @@ static void write_commented_object(int fd, const unsigned char *object)
149149
sha1_to_hex(object));
150150
}
151151

152-
static void create_note(const unsigned char *object, struct msg_arg *msg,
152+
static void create_note(const unsigned char *object, struct note_data *d,
153153
int append_only, const unsigned char *prev,
154154
unsigned char *result)
155155
{
156156
char *path = NULL;
157157

158-
if (msg->use_editor || !msg->given) {
158+
if (d->use_editor || !d->given) {
159159
int fd;
160160
struct strbuf buf = STRBUF_INIT;
161161

@@ -165,10 +165,10 @@ static void create_note(const unsigned char *object, struct msg_arg *msg,
165165
if (fd < 0)
166166
die_errno(_("could not create file '%s'"), path);
167167

168-
if (msg->given)
169-
write_or_die(fd, msg->buf.buf, msg->buf.len);
168+
if (d->given)
169+
write_or_die(fd, d->buf.buf, d->buf.len);
170170
else if (prev && !append_only)
171-
write_note_data(fd, prev);
171+
copy_obj_to_fd(fd, prev);
172172

173173
strbuf_addch(&buf, '\n');
174174
strbuf_add_commented_lines(&buf, note_template, strlen(note_template));
@@ -179,13 +179,12 @@ static void create_note(const unsigned char *object, struct msg_arg *msg,
179179

180180
close(fd);
181181
strbuf_release(&buf);
182-
strbuf_reset(&(msg->buf));
182+
strbuf_reset(&d->buf);
183183

184-
if (launch_editor(path, &(msg->buf), NULL)) {
185-
die(_("Please supply the note contents using either -m" \
186-
" or -F option"));
184+
if (launch_editor(path, &d->buf, NULL)) {
185+
die(_("Please supply the note contents using either -m or -F option"));
187186
}
188-
stripspace(&(msg->buf), 1);
187+
stripspace(&d->buf, 1);
189188
}
190189

191190
if (prev && append_only) {
@@ -194,20 +193,20 @@ static void create_note(const unsigned char *object, struct msg_arg *msg,
194193
enum object_type type;
195194
char *prev_buf = read_sha1_file(prev, &type, &size);
196195

197-
strbuf_grow(&(msg->buf), size + 1);
198-
if (msg->buf.len && prev_buf && size)
199-
strbuf_insert(&(msg->buf), 0, "\n", 1);
196+
strbuf_grow(&d->buf, size + 1);
197+
if (d->buf.len && prev_buf && size)
198+
strbuf_insert(&d->buf, 0, "\n", 1);
200199
if (prev_buf && size)
201-
strbuf_insert(&(msg->buf), 0, prev_buf, size);
200+
strbuf_insert(&d->buf, 0, prev_buf, size);
202201
free(prev_buf);
203202
}
204203

205-
if (!msg->buf.len) {
204+
if (!d->buf.len) {
206205
fprintf(stderr, _("Removing note for object %s\n"),
207206
sha1_to_hex(object));
208207
hashclr(result);
209208
} else {
210-
if (write_sha1_file(msg->buf.buf, msg->buf.len, blob_type, result)) {
209+
if (write_sha1_file(d->buf.buf, d->buf.len, blob_type, result)) {
211210
error(_("unable to write note object"));
212211
if (path)
213212
error(_("The note contents have been left in %s"),
@@ -224,45 +223,45 @@ static void create_note(const unsigned char *object, struct msg_arg *msg,
224223

225224
static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
226225
{
227-
struct msg_arg *msg = opt->value;
226+
struct note_data *d = opt->value;
228227

229-
strbuf_grow(&(msg->buf), strlen(arg) + 2);
230-
if (msg->buf.len)
231-
strbuf_addch(&(msg->buf), '\n');
232-
strbuf_addstr(&(msg->buf), arg);
233-
stripspace(&(msg->buf), 0);
228+
strbuf_grow(&d->buf, strlen(arg) + 2);
229+
if (d->buf.len)
230+
strbuf_addch(&d->buf, '\n');
231+
strbuf_addstr(&d->buf, arg);
232+
stripspace(&d->buf, 0);
234233

235-
msg->given = 1;
234+
d->given = 1;
236235
return 0;
237236
}
238237

239238
static int parse_file_arg(const struct option *opt, const char *arg, int unset)
240239
{
241-
struct msg_arg *msg = opt->value;
240+
struct note_data *d = opt->value;
242241

243-
if (msg->buf.len)
244-
strbuf_addch(&(msg->buf), '\n');
242+
if (d->buf.len)
243+
strbuf_addch(&d->buf, '\n');
245244
if (!strcmp(arg, "-")) {
246-
if (strbuf_read(&(msg->buf), 0, 1024) < 0)
245+
if (strbuf_read(&d->buf, 0, 1024) < 0)
247246
die_errno(_("cannot read '%s'"), arg);
248-
} else if (strbuf_read_file(&(msg->buf), arg, 1024) < 0)
247+
} else if (strbuf_read_file(&d->buf, arg, 1024) < 0)
249248
die_errno(_("could not open or read '%s'"), arg);
250-
stripspace(&(msg->buf), 0);
249+
stripspace(&d->buf, 0);
251250

252-
msg->given = 1;
251+
d->given = 1;
253252
return 0;
254253
}
255254

256255
static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
257256
{
258-
struct msg_arg *msg = opt->value;
257+
struct note_data *d = opt->value;
259258
char *buf;
260259
unsigned char object[20];
261260
enum object_type type;
262261
unsigned long len;
263262

264-
if (msg->buf.len)
265-
strbuf_addch(&(msg->buf), '\n');
263+
if (d->buf.len)
264+
strbuf_addch(&d->buf, '\n');
266265

267266
if (get_sha1(arg, object))
268267
die(_("Failed to resolve '%s' as a valid ref."), arg);
@@ -274,17 +273,17 @@ static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
274273
free(buf);
275274
die(_("Cannot read note data from non-blob object '%s'."), arg);
276275
}
277-
strbuf_add(&(msg->buf), buf, len);
276+
strbuf_add(&d->buf, buf, len);
278277
free(buf);
279278

280-
msg->given = 1;
279+
d->given = 1;
281280
return 0;
282281
}
283282

284283
static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
285284
{
286-
struct msg_arg *msg = opt->value;
287-
msg->use_editor = 1;
285+
struct note_data *d = opt->value;
286+
d->use_editor = 1;
288287
return parse_reuse_arg(opt, arg, unset);
289288
}
290289

@@ -403,18 +402,18 @@ static int add(int argc, const char **argv, const char *prefix)
403402
unsigned char object[20], new_note[20];
404403
char logmsg[100];
405404
const unsigned char *note;
406-
struct msg_arg msg = { 0, 0, STRBUF_INIT };
405+
struct note_data d = { 0, 0, STRBUF_INIT };
407406
struct option options[] = {
408-
{ OPTION_CALLBACK, 'm', "message", &msg, N_("message"),
407+
{ OPTION_CALLBACK, 'm', "message", &d, N_("message"),
409408
N_("note contents as a string"), PARSE_OPT_NONEG,
410409
parse_msg_arg},
411-
{ OPTION_CALLBACK, 'F', "file", &msg, N_("file"),
410+
{ OPTION_CALLBACK, 'F', "file", &d, N_("file"),
412411
N_("note contents in a file"), PARSE_OPT_NONEG,
413412
parse_file_arg},
414-
{ OPTION_CALLBACK, 'c', "reedit-message", &msg, N_("object"),
413+
{ OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"),
415414
N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
416415
parse_reedit_arg},
417-
{ OPTION_CALLBACK, 'C', "reuse-message", &msg, N_("object"),
416+
{ OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"),
418417
N_("reuse specified note object"), PARSE_OPT_NONEG,
419418
parse_reuse_arg},
420419
OPT__FORCE(&force, N_("replace existing notes")),
@@ -439,7 +438,7 @@ static int add(int argc, const char **argv, const char *prefix)
439438

440439
if (note) {
441440
if (!force) {
442-
if (!msg.given) {
441+
if (!d.given) {
443442
/*
444443
* Redirect to "edit" subcommand.
445444
*
@@ -460,7 +459,7 @@ static int add(int argc, const char **argv, const char *prefix)
460459
sha1_to_hex(object));
461460
}
462461

463-
create_note(object, &msg, 0, note, new_note);
462+
create_note(object, &d, 0, note, new_note);
464463

465464
if (is_null_sha1(new_note))
466465
remove_note(t, object);
@@ -472,7 +471,7 @@ static int add(int argc, const char **argv, const char *prefix)
472471
commit_notes(t, logmsg);
473472
out:
474473
free_notes(t);
475-
strbuf_release(&(msg.buf));
474+
strbuf_release(&d.buf);
476475
return retval;
477476
}
478477

@@ -560,18 +559,18 @@ static int append_edit(int argc, const char **argv, const char *prefix)
560559
const unsigned char *note;
561560
char logmsg[100];
562561
const char * const *usage;
563-
struct msg_arg msg = { 0, 0, STRBUF_INIT };
562+
struct note_data d = { 0, 0, STRBUF_INIT };
564563
struct option options[] = {
565-
{ OPTION_CALLBACK, 'm', "message", &msg, N_("message"),
564+
{ OPTION_CALLBACK, 'm', "message", &d, N_("message"),
566565
N_("note contents as a string"), PARSE_OPT_NONEG,
567566
parse_msg_arg},
568-
{ OPTION_CALLBACK, 'F', "file", &msg, N_("file"),
567+
{ OPTION_CALLBACK, 'F', "file", &d, N_("file"),
569568
N_("note contents in a file"), PARSE_OPT_NONEG,
570569
parse_file_arg},
571-
{ OPTION_CALLBACK, 'c', "reedit-message", &msg, N_("object"),
570+
{ OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"),
572571
N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
573572
parse_reedit_arg},
574-
{ OPTION_CALLBACK, 'C', "reuse-message", &msg, N_("object"),
573+
{ OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"),
575574
N_("reuse specified note object"), PARSE_OPT_NONEG,
576575
parse_reuse_arg},
577576
OPT_END()
@@ -587,7 +586,7 @@ static int append_edit(int argc, const char **argv, const char *prefix)
587586
usage_with_options(usage, options);
588587
}
589588

590-
if (msg.given && edit)
589+
if (d.given && edit)
591590
fprintf(stderr, _("The -m/-F/-c/-C options have been deprecated "
592591
"for the 'edit' subcommand.\n"
593592
"Please use 'git notes add -f -m/-F/-c/-C' instead.\n"));
@@ -600,7 +599,7 @@ static int append_edit(int argc, const char **argv, const char *prefix)
600599
t = init_notes_check(argv[0]);
601600
note = get_note(t, object);
602601

603-
create_note(object, &msg, !edit, note, new_note);
602+
create_note(object, &d, !edit, note, new_note);
604603

605604
if (is_null_sha1(new_note))
606605
remove_note(t, object);
@@ -611,7 +610,7 @@ static int append_edit(int argc, const char **argv, const char *prefix)
611610
is_null_sha1(new_note) ? "removed" : "added", argv[0]);
612611
commit_notes(t, logmsg);
613612
free_notes(t);
614-
strbuf_release(&(msg.buf));
613+
strbuf_release(&d.buf);
615614
return 0;
616615
}
617616

0 commit comments

Comments
 (0)