From: "Đoàn Trần Công Danh" <congdanhqx@gmail.com>
To: Bence Ferdinandy <bence@ferdinandy.com>
Cc: git@vger.kernel.org,
"Kristoffer Haugsbakk" <code@khaugsbakk.name>,
"Johannes Schindelin" <Johannes.Schindelin@gmx.de>,
"Junio C Hamano" <gitster@pobox.com>,
"SZEDER Gábor" <szeder.dev@gmail.com>,
"Teng Long" <dyroneteng@gmail.com>
Subject: Re: [RFC PATCH] notes: add prepend command
Date: Thu, 24 Oct 2024 18:19:13 +0700 [thread overview]
Message-ID: <ZxotMcKv5rEIMZ8q@danh.dev> (raw)
In-Reply-To: <20241023201430.986389-1-bence@ferdinandy.com>
On 2024-10-23 22:14:24+0200, Bence Ferdinandy <bence@ferdinandy.com> wrote:
> -static int append_edit(int argc, const char **argv, const char *prefix)
> +
> +static int append_prepend_edit(int argc, const char **argv, const char *prefix, int prepend)
> {
> int allow_empty = 0;
> const char *object_ref;
> @@ -716,11 +718,18 @@ static int append_edit(int argc, const char **argv, const char *prefix)
>
> if (!prev_buf)
> die(_("unable to read %s"), oid_to_hex(note));
> - if (size)
> - strbuf_add(&buf, prev_buf, size);
> - if (d.buf.len && size)
> - append_separator(&buf);
> - strbuf_insert(&d.buf, 0, buf.buf, buf.len);
> + if (prepend) {
> + if (d.buf.len && size)
> + append_separator(&buf);
> + if (size)
> + strbuf_add(&buf, prev_buf, size);
> + } else {
> + if (size)
> + strbuf_add(&buf, prev_buf, size);
> + if (d.buf.len && size)
> + append_separator(&buf);
> + }
> + strbuf_insert(&d.buf, prepend ? d.buf.len : 0, buf.buf, buf.len);
>
> free(prev_buf);
> strbuf_release(&buf);
Without prejudice about whether we should take this command.
(I think we shouldn't, just like we shouldn't top-posting).
I think this diff should be written like this for easier reasoning:
----- 8< -----------------
@@ -711,19 +713,27 @@ static int append_edit(int argc, const char **argv, const char *prefix)
/* Append buf to previous note contents */
unsigned long size;
enum object_type type;
- struct strbuf buf = STRBUF_INIT;
char *prev_buf = repo_read_object_file(the_repository, note, &type, &size);
if (!prev_buf)
die(_("unable to read %s"), oid_to_hex(note));
- if (size)
+ if (!size) {
+ // no existing notes, use whatever we have here
+ } else if (prepend) {
+ if (d.buf.len)
+ append_separator(&d.buf);
+ strbuf_add(&d.buf, prev_buf, size);
+ } else {
+ struct strbuf buf = STRBUF_INIT;
strbuf_add(&buf, prev_buf, size);
- if (d.buf.len && size)
- append_separator(&buf);
- strbuf_insert(&d.buf, 0, buf.buf, buf.len);
+ if (d.buf.len)
+ append_separator(&buf);
+ strbuf_addbuf(&buf, &d.buf);
+ strbuf_swap(&buf, &d.buf);
+ strbuf_release(&buf);
+ }
free(prev_buf);
- strbuf_release(&buf);
}
if (d.buf.len || allow_empty) {
-------------- 8< --------------------
Even if we don't take this subcommand, I think we should re-write the
append part, so:
- we can see the append logic better,
- we can avoid the `strbuf_insert` which will require memmove/memcpy.
Well, the second point is micro-optimisation, so take it with a grain
of salt.
Also tests.
-------------- 8< -----------------------
diff --git a/t/t3301-notes.sh b/t/t3301-notes.sh
index 99137fb235731..5a7ad40fde6a8 100755
--- a/t/t3301-notes.sh
+++ b/t/t3301-notes.sh
@@ -558,6 +558,20 @@ test_expect_success 'listing non-existing notes fails' '
test_must_be_empty actual
'
+test_expect_success 'append: specify a separator with an empty arg' '
+ test_when_finished git notes remove HEAD &&
+ cat >expect <<-\EOF &&
+ notes-2
+
+ notes-1
+ EOF
+
+ git notes add -m "notes-1" &&
+ git notes prepend --separator="" -m "notes-2" &&
+ git notes show >actual &&
+ test_cmp expect actual
+'
+
test_expect_success 'append: specify a separator with an empty arg' '
test_when_finished git notes remove HEAD &&
cat >expect <<-\EOF &&
----------- >8 --------------
--
Danh
next prev parent reply other threads:[~2024-10-24 11:19 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-23 20:14 [RFC PATCH] notes: add prepend command Bence Ferdinandy
2024-10-23 20:20 ` Taylor Blau
2024-10-23 20:32 ` Bence Ferdinandy
2024-10-24 11:19 ` Đoàn Trần Công Danh [this message]
2024-10-26 22:34 ` Bence Ferdinandy
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ZxotMcKv5rEIMZ8q@danh.dev \
--to=congdanhqx@gmail.com \
--cc=Johannes.Schindelin@gmx.de \
--cc=bence@ferdinandy.com \
--cc=code@khaugsbakk.name \
--cc=dyroneteng@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=szeder.dev@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).