Skip to content

Commit 52694cd

Browse files
jherlandgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Johan Herland <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b0de56c commit 52694cd

File tree

1 file changed

+54
-49
lines changed

1 file changed

+54
-49
lines changed

builtin/notes.c

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,8 @@ static void write_commented_object(int fd, const unsigned char *object)
159159
sha1_to_hex(object));
160160
}
161161

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)
165164
{
166165
if (d->use_editor || !d->given) {
167166
int fd;
@@ -175,8 +174,8 @@ static void create_note(const unsigned char *object, struct note_data *d,
175174

176175
if (d->given)
177176
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);
180179

181180
strbuf_addch(&buf, '\n');
182181
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,
194193
}
195194
stripspace(&d->buf, 1);
196195
}
196+
}
197197

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);
224206
}
225207
}
226208

@@ -403,7 +385,6 @@ static int add(int argc, const char **argv, const char *prefix)
403385
const char *object_ref;
404386
struct notes_tree *t;
405387
unsigned char object[20], new_note[20];
406-
char logmsg[100];
407388
const unsigned char *note;
408389
struct note_data d = { 0, 0, NULL, STRBUF_INIT };
409390
struct option options[] = {
@@ -463,17 +444,20 @@ static int add(int argc, const char **argv, const char *prefix)
463444
sha1_to_hex(object));
464445
}
465446

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));
470456
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+
}
473459

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);
477461
free_notes(t);
478462
return 0;
479463
}
@@ -602,17 +586,38 @@ static int append_edit(int argc, const char **argv, const char *prefix)
602586
t = init_notes_check(argv[0]);
603587
note = get_note(t, object);
604588

605-
create_note(object, &d, !edit, note, new_note);
606-
free_note_data(&d);
589+
prepare_note_data(object, &d, edit ? note : NULL);
607590

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+
}
612604

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+
}
615618
commit_notes(t, logmsg);
619+
620+
free_note_data(&d);
616621
free_notes(t);
617622
return 0;
618623
}

0 commit comments

Comments
 (0)