builtin/notes: split create_note() to clarify add vs. remove logic

create_note() has a non-trivial interface, and comprises three loosely related parts: 1. launching the editor with the note contents, if needed 2. appending to an existing note, if append_only was given 3. adding or removing the resulting note, based on whether it's non-empty Split it along those lines to make the logic clearer: The first part goes into a new function - prepare_note_data(), with a simpler interface. The second part is moved into append_edit(), which is the only user of this code. Finally, the add vs. remove decision is moved into the callers (add() and append_edit()), keeping the logic for writing the actual note object in a separate function: write_note_data(). Suggested-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Johan Herland <johan@herland.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johan Herland committed Nov 12, 2014 at 01:40 UTC 52694cdabbf68f19c8289416e7bb3bbef41d8d27
1 file changed +54 -49
builtin/notes.c
+54 -49
@@ -159,9 +159,8 @@ static void write_commented_object(int fd, const unsigned char *object)
159 sha1_to_hex(object));
160 }
161
162 -static void create_note(const unsigned char *object, struct note_data *d,
163 - int append_only, const unsigned char *prev,
164 - unsigned char *result)
162 +static void prepare_note_data(const unsigned char *object, struct note_data *d,
163 + const unsigned char *old_note)
164 {
165 if (d->use_editor || !d->given) {
166 int fd;
@@ -175,8 +174,8 @@ static void create_note(const unsigned char *object, struct note_data *d,
174
175 if (d->given)
176 write_or_die(fd, d->buf.buf, d->buf.len);
178 - else if (prev && !append_only)
179 - copy_obj_to_fd(fd, prev);
177 + else if (old_note)
178 + copy_obj_to_fd(fd, old_note);
179
180 strbuf_addch(&buf, '\n');
181 strbuf_add_commented_lines(&buf, note_template, strlen(note_template));
@@ -194,33 +193,16 @@ static void create_note(const unsigned char *object, struct note_data *d,
193 }
194 stripspace(&d->buf, 1);
195 }
196 +}
197
198 - if (prev && append_only) {
199 - /* Append buf to previous note contents */
200 - unsigned long size;
201 - enum object_type type;
202 - char *prev_buf = read_sha1_file(prev, &type, &size);
203 -
204 - strbuf_grow(&d->buf, size + 1);
205 - if (d->buf.len && prev_buf && size)
206 - strbuf_insert(&d->buf, 0, "\n", 1);
207 - if (prev_buf && size)
208 - strbuf_insert(&d->buf, 0, prev_buf, size);
209 - free(prev_buf);
210 - }
211 -
212 - if (!d->buf.len) {
213 - fprintf(stderr, _("Removing note for object %s\n"),
214 - sha1_to_hex(object));
215 - hashclr(result);
216 - } else {
217 - if (write_sha1_file(d->buf.buf, d->buf.len, blob_type, result)) {
218 - error(_("unable to write note object"));
219 - if (d->edit_path)
220 - error(_("The note contents have been left in %s"),
221 - d->edit_path);
222 - exit(128);
223 - }
198 +static void write_note_data(struct note_data *d, unsigned char *sha1)
199 +{
200 + if (write_sha1_file(d->buf.buf, d->buf.len, blob_type, sha1)) {
201 + error(_("unable to write note object"));
202 + if (d->edit_path)
203 + error(_("The note contents have been left in %s"),
204 + d->edit_path);
205 + exit(128);
206 }
207 }
208
@@ -403,7 +385,6 @@ static int add(int argc, const char **argv, const char *prefix)
385 const char *object_ref;
386 struct notes_tree *t;
387 unsigned char object[20], new_note[20];
406 - char logmsg[100];
388 const unsigned char *note;
389 struct note_data d = { 0, 0, NULL, STRBUF_INIT };
390 struct option options[] = {
@@ -463,17 +444,20 @@ static int add(int argc, const char **argv, const char *prefix)
444 sha1_to_hex(object));
445 }
446
466 - create_note(object, &d, 0, note, new_note);
467 - free_note_data(&d);
468 -
469 - if (is_null_sha1(new_note))
447 + prepare_note_data(object, &d, note);
448 + if (d.buf.len) {
449 + write_note_data(&d, new_note);
450 + if (add_note(t, object, new_note, combine_notes_overwrite))
451 + die("BUG: combine_notes_overwrite failed");
452 + commit_notes(t, "Notes added by 'git notes add'");
453 + } else {
454 + fprintf(stderr, _("Removing note for object %s\n"),
455 + sha1_to_hex(object));
456 remove_note(t, object);
471 - else if (add_note(t, object, new_note, combine_notes_overwrite))
472 - die("BUG: combine_notes_overwrite failed");
457 + commit_notes(t, "Notes removed by 'git notes add'");
458 + }
459
474 - snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
475 - is_null_sha1(new_note) ? "removed" : "added", "add");
476 - commit_notes(t, logmsg);
460 + free_note_data(&d);
461 free_notes(t);
462 return 0;
463 }
@@ -602,17 +586,38 @@ static int append_edit(int argc, const char **argv, const char *prefix)
586 t = init_notes_check(argv[0]);
587 note = get_note(t, object);
588
605 - create_note(object, &d, !edit, note, new_note);
606 - free_note_data(&d);
589 + prepare_note_data(object, &d, edit ? note : NULL);
590
608 - if (is_null_sha1(new_note))
609 - remove_note(t, object);
610 - else if (add_note(t, object, new_note, combine_notes_overwrite))
611 - die("BUG: combine_notes_overwrite failed");
591 + if (note && !edit) {
592 + /* Append buf to previous note contents */
593 + unsigned long size;
594 + enum object_type type;
595 + char *prev_buf = read_sha1_file(note, &type, &size);
596 +
597 + strbuf_grow(&d.buf, size + 1);
598 + if (d.buf.len && prev_buf && size)
599 + strbuf_insert(&d.buf, 0, "\n", 1);
600 + if (prev_buf && size)
601 + strbuf_insert(&d.buf, 0, prev_buf, size);
602 + free(prev_buf);
603 + }
604
613 - snprintf(logmsg, sizeof(logmsg), "Notes %s by 'git notes %s'",
614 - is_null_sha1(new_note) ? "removed" : "added", argv[0]);
605 + if (d.buf.len) {
606 + write_note_data(&d, new_note);
607 + if (add_note(t, object, new_note, combine_notes_overwrite))
608 + die("BUG: combine_notes_overwrite failed");
609 + snprintf(logmsg, sizeof(logmsg), "Notes added by 'git notes %s'",
610 + argv[0]);
611 + } else {
612 + fprintf(stderr, _("Removing note for object %s\n"),
613 + sha1_to_hex(object));
614 + remove_note(t, object);
615 + snprintf(logmsg, sizeof(logmsg), "Notes removed by 'git notes %s'",
616 + argv[0]);
617 + }
618 commit_notes(t, logmsg);
619 +
620 + free_note_data(&d);
621 free_notes(t);
622 return 0;
623 }