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 <johan@herland.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johan Herland committed Nov 9, 2014 at 13:30 UTC bebf5c04767b3c67dadfa8b65e6f0ceb258f6b83
1 file changed +54 -55
builtin/notes.c
+54 -55
@@ -92,7 +92,7 @@ static const char * const git_notes_get_ref_usage[] = {
92 static const char note_template[] =
93 "\nWrite/edit the notes for the following object:\n";
94
95 -struct msg_arg {
95 +struct note_data {
96 int given;
97 int use_editor;
98 struct strbuf buf;
@@ -106,7 +106,7 @@ static int list_each_note(const unsigned char *object_sha1,
106 return 0;
107 }
108
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)
110 {
111 unsigned long size;
112 enum object_type type;
@@ -149,13 +149,13 @@ static void write_commented_object(int fd, const unsigned char *object)
149 sha1_to_hex(object));
150 }
151
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,
153 int append_only, const unsigned char *prev,
154 unsigned char *result)
155 {
156 char *path = NULL;
157
158 - if (msg->use_editor || !msg->given) {
158 + if (d->use_editor || !d->given) {
159 int fd;
160 struct strbuf buf = STRBUF_INIT;
161
@@ -165,10 +165,10 @@ static void create_note(const unsigned char *object, struct msg_arg *msg,
165 if (fd < 0)
166 die_errno(_("could not create file '%s'"), path);
167
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);
170 else if (prev && !append_only)
171 - write_note_data(fd, prev);
171 + copy_obj_to_fd(fd, prev);
172
173 strbuf_addch(&buf, '\n');
174 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,
179
180 close(fd);
181 strbuf_release(&buf);
182 - strbuf_reset(&(msg->buf));
182 + strbuf_reset(&d->buf);
183
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"));
186 }
188 - stripspace(&(msg->buf), 1);
187 + stripspace(&d->buf, 1);
188 }
189
190 if (prev && append_only) {
@@ -194,20 +193,20 @@ static void create_note(const unsigned char *object, struct msg_arg *msg,
193 enum object_type type;
194 char *prev_buf = read_sha1_file(prev, &type, &size);
195
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);
199 if (prev_buf && size)
201 - strbuf_insert(&(msg->buf), 0, prev_buf, size);
200 + strbuf_insert(&d->buf, 0, prev_buf, size);
201 free(prev_buf);
202 }
203
205 - if (!msg->buf.len) {
204 + if (!d->buf.len) {
205 fprintf(stderr, _("Removing note for object %s\n"),
206 sha1_to_hex(object));
207 hashclr(result);
208 } 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)) {
210 error(_("unable to write note object"));
211 if (path)
212 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,
223
224 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
225 {
227 - struct msg_arg *msg = opt->value;
226 + struct note_data *d = opt->value;
227
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);
233
235 - msg->given = 1;
234 + d->given = 1;
235 return 0;
236 }
237
238 static int parse_file_arg(const struct option *opt, const char *arg, int unset)
239 {
241 - struct msg_arg *msg = opt->value;
240 + struct note_data *d = opt->value;
241
243 - if (msg->buf.len)
244 - strbuf_addch(&(msg->buf), '\n');
242 + if (d->buf.len)
243 + strbuf_addch(&d->buf, '\n');
244 if (!strcmp(arg, "-")) {
246 - if (strbuf_read(&(msg->buf), 0, 1024) < 0)
245 + if (strbuf_read(&d->buf, 0, 1024) < 0)
246 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)
248 die_errno(_("could not open or read '%s'"), arg);
250 - stripspace(&(msg->buf), 0);
249 + stripspace(&d->buf, 0);
250
252 - msg->given = 1;
251 + d->given = 1;
252 return 0;
253 }
254
255 static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
256 {
258 - struct msg_arg *msg = opt->value;
257 + struct note_data *d = opt->value;
258 char *buf;
259 unsigned char object[20];
260 enum object_type type;
261 unsigned long len;
262
264 - if (msg->buf.len)
265 - strbuf_addch(&(msg->buf), '\n');
263 + if (d->buf.len)
264 + strbuf_addch(&d->buf, '\n');
265
266 if (get_sha1(arg, object))
267 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)
273 free(buf);
274 die(_("Cannot read note data from non-blob object '%s'."), arg);
275 }
277 - strbuf_add(&(msg->buf), buf, len);
276 + strbuf_add(&d->buf, buf, len);
277 free(buf);
278
280 - msg->given = 1;
279 + d->given = 1;
280 return 0;
281 }
282
283 static int parse_reedit_arg(const struct option *opt, const char *arg, int unset)
284 {
286 - struct msg_arg *msg = opt->value;
287 - msg->use_editor = 1;
285 + struct note_data *d = opt->value;
286 + d->use_editor = 1;
287 return parse_reuse_arg(opt, arg, unset);
288 }
289
@@ -403,18 +402,18 @@ static int add(int argc, const char **argv, const char *prefix)
402 unsigned char object[20], new_note[20];
403 char logmsg[100];
404 const unsigned char *note;
406 - struct msg_arg msg = { 0, 0, STRBUF_INIT };
405 + struct note_data d = { 0, 0, STRBUF_INIT };
406 struct option options[] = {
408 - { OPTION_CALLBACK, 'm', "message", &msg, N_("message"),
407 + { OPTION_CALLBACK, 'm', "message", &d, N_("message"),
408 N_("note contents as a string"), PARSE_OPT_NONEG,
409 parse_msg_arg},
411 - { OPTION_CALLBACK, 'F', "file", &msg, N_("file"),
410 + { OPTION_CALLBACK, 'F', "file", &d, N_("file"),
411 N_("note contents in a file"), PARSE_OPT_NONEG,
412 parse_file_arg},
414 - { OPTION_CALLBACK, 'c', "reedit-message", &msg, N_("object"),
413 + { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"),
414 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
415 parse_reedit_arg},
417 - { OPTION_CALLBACK, 'C', "reuse-message", &msg, N_("object"),
416 + { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"),
417 N_("reuse specified note object"), PARSE_OPT_NONEG,
418 parse_reuse_arg},
419 OPT__FORCE(&force, N_("replace existing notes")),
@@ -439,7 +438,7 @@ static int add(int argc, const char **argv, const char *prefix)
438
439 if (note) {
440 if (!force) {
442 - if (!msg.given) {
441 + if (!d.given) {
442 /*
443 * Redirect to "edit" subcommand.
444 *
@@ -460,7 +459,7 @@ static int add(int argc, const char **argv, const char *prefix)
459 sha1_to_hex(object));
460 }
461
463 - create_note(object, &msg, 0, note, new_note);
462 + create_note(object, &d, 0, note, new_note);
463
464 if (is_null_sha1(new_note))
465 remove_note(t, object);
@@ -472,7 +471,7 @@ static int add(int argc, const char **argv, const char *prefix)
471 commit_notes(t, logmsg);
472 out:
473 free_notes(t);
475 - strbuf_release(&(msg.buf));
474 + strbuf_release(&d.buf);
475 return retval;
476 }
477
@@ -560,18 +559,18 @@ static int append_edit(int argc, const char **argv, const char *prefix)
559 const unsigned char *note;
560 char logmsg[100];
561 const char * const *usage;
563 - struct msg_arg msg = { 0, 0, STRBUF_INIT };
562 + struct note_data d = { 0, 0, STRBUF_INIT };
563 struct option options[] = {
565 - { OPTION_CALLBACK, 'm', "message", &msg, N_("message"),
564 + { OPTION_CALLBACK, 'm', "message", &d, N_("message"),
565 N_("note contents as a string"), PARSE_OPT_NONEG,
566 parse_msg_arg},
568 - { OPTION_CALLBACK, 'F', "file", &msg, N_("file"),
567 + { OPTION_CALLBACK, 'F', "file", &d, N_("file"),
568 N_("note contents in a file"), PARSE_OPT_NONEG,
569 parse_file_arg},
571 - { OPTION_CALLBACK, 'c', "reedit-message", &msg, N_("object"),
570 + { OPTION_CALLBACK, 'c', "reedit-message", &d, N_("object"),
571 N_("reuse and edit specified note object"), PARSE_OPT_NONEG,
572 parse_reedit_arg},
574 - { OPTION_CALLBACK, 'C', "reuse-message", &msg, N_("object"),
573 + { OPTION_CALLBACK, 'C', "reuse-message", &d, N_("object"),
574 N_("reuse specified note object"), PARSE_OPT_NONEG,
575 parse_reuse_arg},
576 OPT_END()
@@ -587,7 +586,7 @@ static int append_edit(int argc, const char **argv, const char *prefix)
586 usage_with_options(usage, options);
587 }
588
590 - if (msg.given && edit)
589 + if (d.given && edit)
590 fprintf(stderr, _("The -m/-F/-c/-C options have been deprecated "
591 "for the 'edit' subcommand.\n"
592 "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)
599 t = init_notes_check(argv[0]);
600 note = get_note(t, object);
601
603 - create_note(object, &msg, !edit, note, new_note);
602 + create_note(object, &d, !edit, note, new_note);
603
604 if (is_null_sha1(new_note))
605 remove_note(t, object);
@@ -611,7 +610,7 @@ static int append_edit(int argc, const char **argv, const char *prefix)
610 is_null_sha1(new_note) ? "removed" : "added", argv[0]);
611 commit_notes(t, logmsg);
612 free_notes(t);
614 - strbuf_release(&(msg.buf));
613 + strbuf_release(&d.buf);
614 return 0;
615 }
616