builtin/notes: refactor note file path into struct note_data

Move the 'path' variable from create_note() and into the note_data struct. Unify cleanup of note_data objects with a free_note_data() function. This might not make too much sense on its own, but it makes the future refactoring of create_note() considerably cleaner. 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 4282af0fc91e7a2667f8cba7282a5b70347f6e6f
1 file changed +21 -17
builtin/notes.c
+21 -17
@@ -95,9 +95,19 @@ static const char note_template[] =
95 struct note_data {
96 int given;
97 int use_editor;
98 + char *edit_path;
99 struct strbuf buf;
100 };
101
102 +static void free_note_data(struct note_data *d)
103 +{
104 + if (d->edit_path) {
105 + unlink_or_warn(d->edit_path);
106 + free(d->edit_path);
107 + }
108 + strbuf_release(&d->buf);
109 +}
110 +
111 static int list_each_note(const unsigned char *object_sha1,
112 const unsigned char *note_sha1, char *note_path,
113 void *cb_data)
@@ -153,17 +163,15 @@ static void create_note(const unsigned char *object, struct note_data *d,
163 int append_only, const unsigned char *prev,
164 unsigned char *result)
165 {
156 - char *path = NULL;
157 -
166 if (d->use_editor || !d->given) {
167 int fd;
168 struct strbuf buf = STRBUF_INIT;
169
170 /* write the template message before editing: */
163 - path = git_pathdup("NOTES_EDITMSG");
164 - fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
171 + d->edit_path = git_pathdup("NOTES_EDITMSG");
172 + fd = open(d->edit_path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
173 if (fd < 0)
166 - die_errno(_("could not create file '%s'"), path);
174 + die_errno(_("could not create file '%s'"), d->edit_path);
175
176 if (d->given)
177 write_or_die(fd, d->buf.buf, d->buf.len);
@@ -181,7 +189,7 @@ static void create_note(const unsigned char *object, struct note_data *d,
189 strbuf_release(&buf);
190 strbuf_reset(&d->buf);
191
184 - if (launch_editor(path, &d->buf, NULL)) {
192 + if (launch_editor(d->edit_path, &d->buf, NULL)) {
193 die(_("Please supply the note contents using either -m or -F option"));
194 }
195 stripspace(&d->buf, 1);
@@ -208,17 +216,12 @@ static void create_note(const unsigned char *object, struct note_data *d,
216 } else {
217 if (write_sha1_file(d->buf.buf, d->buf.len, blob_type, result)) {
218 error(_("unable to write note object"));
211 - if (path)
219 + if (d->edit_path)
220 error(_("The note contents have been left in %s"),
213 - path);
221 + d->edit_path);
222 exit(128);
223 }
224 }
217 -
218 - if (path) {
219 - unlink_or_warn(path);
220 - free(path);
221 - }
225 }
226
227 static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
@@ -402,7 +405,7 @@ static int add(int argc, const char **argv, const char *prefix)
405 unsigned char object[20], new_note[20];
406 char logmsg[100];
407 const unsigned char *note;
405 - struct note_data d = { 0, 0, STRBUF_INIT };
408 + struct note_data d = { 0, 0, NULL, STRBUF_INIT };
409 struct option options[] = {
410 { OPTION_CALLBACK, 'm', "message", &d, N_("message"),
411 N_("note contents as a string"), PARSE_OPT_NONEG,
@@ -447,6 +450,7 @@ static int add(int argc, const char **argv, const char *prefix)
450 * therefore still in argv[0-1].
451 */
452 argv[0] = "edit";
453 + free_note_data(&d);
454 free_notes(t);
455 return append_edit(argc, argv, prefix);
456 }
@@ -460,6 +464,7 @@ static int add(int argc, const char **argv, const char *prefix)
464 }
465
466 create_note(object, &d, 0, note, new_note);
467 + free_note_data(&d);
468
469 if (is_null_sha1(new_note))
470 remove_note(t, object);
@@ -471,7 +476,6 @@ static int add(int argc, const char **argv, const char *prefix)
476 commit_notes(t, logmsg);
477 out:
478 free_notes(t);
474 - strbuf_release(&d.buf);
479 return retval;
480 }
481
@@ -559,7 +563,7 @@ static int append_edit(int argc, const char **argv, const char *prefix)
563 const unsigned char *note;
564 char logmsg[100];
565 const char * const *usage;
562 - struct note_data d = { 0, 0, STRBUF_INIT };
566 + struct note_data d = { 0, 0, NULL, STRBUF_INIT };
567 struct option options[] = {
568 { OPTION_CALLBACK, 'm', "message", &d, N_("message"),
569 N_("note contents as a string"), PARSE_OPT_NONEG,
@@ -600,6 +604,7 @@ static int append_edit(int argc, const char **argv, const char *prefix)
604 note = get_note(t, object);
605
606 create_note(object, &d, !edit, note, new_note);
607 + free_note_data(&d);
608
609 if (is_null_sha1(new_note))
610 remove_note(t, object);
@@ -610,7 +615,6 @@ static int append_edit(int argc, const char **argv, const char *prefix)
615 is_null_sha1(new_note) ? "removed" : "added", argv[0]);
616 commit_notes(t, logmsg);
617 free_notes(t);
613 - strbuf_release(&d.buf);
618 return 0;
619 }
620