log --format existence of notes?
Hi,
based on the man pages it doesn't seem possible, but maybe I'm missing something.
I would like to put together a "log --format=" which is similar to --oneline, but where if there's a note for the commit it's marked with e.g. a notebook symbol. There's %N, but that prints the entire note, so it doesn't work well with one commit per line.
E.g. something like this, where Taylor's commit does not have a note, but mine do:
eb12efd995 2024-10-08 Bence Ferdin.. remote set-head: refactor for readability 📓 a189f4e077 2024-09-28 Bence Ferdin.. refs: atomically record overwritten ref in update_symref 📓 ae0b28db8c 2024-10-19 Bence Ferdin.. t/t5505-remote: set default branch to main 📓15030f9556 2024-10-15 Taylor Blau The second batch
Is there a way to do this now? If not, maybe a "%N?" might be a nice addition?
Thanks,
Bence
Signature · 1 line
bence.ferdinandy.com
Quoted 6 lines · Bence Ferdinandy
based on the man pages it doesn't seem possible, but maybe I'm missing something.
I would like to put together a "log --format=" which is similar to --oneline, but where if there's a note for the commit it's marked with e.g. a notebook symbol. There's %N, but that prints the entire note, so it doesn't work well with one commit per line.
I do not think it is doable. Unlike the format language in the for-each-ref/branch --list family of commands, the pretty-format language in the log family of commands lack more involved conditional formatting features.
Unifying these two formatting languages to port features from one to the other would be needed, I guess. If we had a note support in the latter, something like
$ git branch -l --format='%(subject)%(if)%(note:amlog)%(then)📓%(end)'
may have worked.
Quoted 9 lines · Junio C Hamano
Quoted 6 lines · Bence Ferdinandy
based on the man pages it doesn't seem possible, but maybe I'm missing something.
I would like to put together a "log --format=" which is similar to --oneline, but where if there's a note for the commit it's marked with e.g. a notebook symbol. There's %N, but that prints the entire note, so it doesn't work well with one commit per line.
Quoted 7 lines
I do not think it is doable. Unlike the format language in the for-each-ref/branch --list family of commands, the pretty-format language in the log family of commands lack more involved conditional formatting features.
Unifying these two formatting languages to port features from one to the other would be needed, I guess.
Is this already on the roadmap with a plan on how to do it? It sounds like switching log format to the same formatting as the other commands would make life easier in the long run.
Quoted 6 lines
If we had a note support in the
latter, something like$ git branch -l --format='%(subject)%(if)%(note:amlog)%(then)📓%(end)'may have worked.
Conditional formatting definitely would sound useful for logs as well :)
On the other hand: would a quick win in analogy to "%G?" be considered? A "%N?" that either outputs a "N" for note and a "-" for no note? Or something like that. A bit ugly, but gets the job done.
Thanks,
Bence
PATCH 1/1 ref-filter: add "notes" atom
Quoted 16 lines · Bence Ferdinandy
Quoted 14 lines · Junio C Hamano
Quoted 6 lines · Bence Ferdinandy
based on the man pages it doesn't seem possible, but maybe I'm missing something.
I would like to put together a "log --format=" which is similar to --oneline, but where if there's a note for the commit it's marked with e.g. a notebook symbol. There's %N, but that prints the entire note, so it doesn't work well with one commit per line.
I do not think it is doable. Unlike the format language in the for-each-ref/branch --list family of commands, the pretty-format language in the log family of commands lack more involved conditional formatting features.
Quoted 6 lines
Quoted 2 lines
Unifying these two formatting languages to port features from one to the other would be needed, I guess.
Is this already on the roadmap with a plan on how to do it? It sounds like switching log format to the same formatting as the other commands would make life easier in the long run.
Yes, but a fixed plan doesn't exist really. There has been effort in the past (and even now) to re-implement formats from pretty in ref-filter and when that is done, change everything to use ref-filter, but the parsing mechanism in ref-filter is a bit broken in some really really rare cases [1].
Quoted 8 lines
Quoted 6 lines
If we had a note support in the
latter, something like$ git branch -l --format='%(subject)%(if)%(note:amlog)%(then)📓%(end)'may have worked.
Conditional formatting definitely would sound useful for logs as well :)
I agree that having the "%(if)..." would be good in pretty.
[1] See thread
https://lore.kernel.org/git/[email protected]/
In the meantime, here is an implementation of "%(notes)" atom in ref-filter. I know this doesn't really address the "log" family since we are not adding this format in pretty, but we can now do
$ git branch -l --format='%(subject)%(if)%(notes:amlog)%(then)📓%(end)'
------------------------ >8 ------------------------
Subject: [PATCH] ref-filter: add "notes" atom
Introduce new "%(notes)" format which is capable of showing the notes associated with any particular ref. This new atom may also be given an argument specifying where to look for the notes, for example refs/notes/amlog, associated with the ref.
The behavior is the same as doing
$ git notes show <refname>
for the bare "%(notes)" format and
$ git notes --ref <notes-ref> show <refname>
for the "%(notes:notes-ref)" format.
--- Documentation/git-for-each-ref.txt | 7 ++++ ref-filter.c | 54 ++++++++++++++++++++++++++++++ t/t6300-for-each-ref.sh | 28 ++++++++++++++++ 3 files changed, 89 insertions(+)
Documentation/git-for-each-ref.txt +7 -0
@@ -329,6 +329,13 @@ exclude=<pattern>;; in linkgit:git-describe[1] for details. -- +notes:<notes-ref>:: + Show notes associated with a ref. Defaults to getting notes + from `refs/notes/commits`. If `"notes-ref"` is given then notes + retrieved from that ref are shown; for the given ref. For + example, `%(notes:amlog)` will retrieve the notes from + `refs/notes/amlog` for the given ref. See linkgit:git-notes[1]. + In addition to the above, for commit and tag objects, the header field names (`tree`, `parent`, `object`, `type`, and `tag`) can be used to specify the value in the header field.
ref-filter.c +54 -0
104 lines not on this page · raw message
t/t6300-for-each-ref.sh +28 -0
@@ -847,6 +847,34 @@ test_expect_success 'err on bad describe atom arg' ' ) ' +test_expect_success 'bare notes atom' ' + test_when_finished "git checkout main && git branch -D notes-atom " && + + git checkout -b notes-atom && + test_commit --no-tag "commit on notes-atom" && + + git notes add -m "some msg" refs/heads/notes-atom && + git notes show refs/heads/notes-atom >expect && + git for-each-ref --format="%(notes)" refs/heads/notes-atom >actual && + test_cmp expect actual +' + +test_expect_success 'notes atom with notes ref' ' + test_when_finished \ + "git checkout main && git branch -D notes-atom-refname" && + + git checkout -b notes-atom-refname && + test_commit --no-tag "commit on notes-atom-refname" && + + git notes --ref notes-ref add -m "some msg" \ + refs/heads/notes-atom-refname && + git notes --ref notes-ref show \ + refs/heads/notes-atom-refname >expect && + git for-each-ref --format="%(notes:notes-ref)" \ + refs/heads/notes-atom-refname >actual && + test_cmp expect actual +' + cat >expected <<\EOF heads/main tags/main
Signature · 1 line
2.47.0.289.g3d75e57c9c
Re: [PATCH] ref-filter: add "notes" atom
Quoted 27 lines · Kousik Sanagavarapu
ref-filter.c +27
-0+static void grab_notes_values (struct atom_value *val,int deref, +struct object *obj) +{ +for (int i =0 ; i < used_atom_cnt; i++) { +struct used_atom *atom = &used_atom[i]; +const char *name = atom->name; +struct atom_value *v = &val[i]; + +struct child_process cmd =CHILD_PROCESS_INIT ; +struct strbuf out =STRBUF_INIT ; +struct strbuf err =STRBUF_INIT ; + +if (atom->atom_type !=ATOM_NOTES ) +continue ; + +if (!!deref != (*name =='*' )) +continue ; + + cmd.git_cmd =1 ; +strvec_push (&cmd.args,"notes" ); +if (atom->u.notes_refname) { +strvec_push (&cmd.args,"--ref" ); +strvec_push (&cmd.args, atom->u.notes_refname); + } +strvec_push (&cmd.args,"show" ); +strvec_push (&cmd.args,oid_to_hex (&obj->oid)); +if (pipe_command (&cmd,NULL ,0 , &out,0 , &err,0 ) <0 ) {
ref-filter.c static void grab_describe_values(struct atom_value *val, int deref, · line 2008
Quoted 10 lines
ref-filter.c +10
-0+error (_ ("failed to run 'notes'" )); + v->s =xstrdup ("" ); +continue ; + } +strbuf_rtrim (&out); + v->s =strbuf_detach (&out,NULL ); + +strbuf_release (&err); + } +}
I suspect that this was written to mimick what is done for describe.
The describe codepath has a (semi-)valid reason to fork out to a subprocess, as computation of describe smudges the object flags of in-core object database and it is not trivial to call into the helper functions twice.
But showing notes for a single commit is merely an internal call to get_note() away, so unless the note object is not a blob (which should be absolutely rare), spawning a subprocess for each and every ref tip feels a bit heavier than acceptable. We'd probably need to maintain a table of notes_trees, one per <note-ref> used as %(notes:<note-ref>) in the format string, and init_notes() on them while parsing the atoms, and in this codepath it would be a look-up of notes_tree from the table based on the u.notes_refname by calling get_note() to learn the object name, plus reading the object contents into the v->s member when the note object is a blob (and fallback the above code when it is not a blob, which is a rare-case, if we really want to handle them).
Re: [PATCH] ref-filter: add "notes" atom
Quoted 47 lines · Junio C Hamano
Quoted 27 lines · Kousik Sanagavarapu
ref-filter.c +27
-0+static void grab_notes_values (struct atom_value *val,int deref, +struct object *obj) +{ +for (int i =0 ; i < used_atom_cnt; i++) { +struct used_atom *atom = &used_atom[i]; +const char *name = atom->name; +struct atom_value *v = &val[i]; + +struct child_process cmd =CHILD_PROCESS_INIT ; +struct strbuf out =STRBUF_INIT ; +struct strbuf err =STRBUF_INIT ; + +if (atom->atom_type !=ATOM_NOTES ) +continue ; + +if (!!deref != (*name =='*' )) +continue ; + + cmd.git_cmd =1 ; +strvec_push (&cmd.args,"notes" ); +if (atom->u.notes_refname) { +strvec_push (&cmd.args,"--ref" ); +strvec_push (&cmd.args, atom->u.notes_refname); + } +strvec_push (&cmd.args,"show" ); +strvec_push (&cmd.args,oid_to_hex (&obj->oid)); +if (pipe_command (&cmd,NULL ,0 , &out,0 , &err,0 ) <0 ) {ref-filter.c static void grab_describe_values(struct atom_value *val, int deref, · line 2008
Quoted 10 lines
ref-filter.c +10
-0+error (_ ("failed to run 'notes'" )); + v->s =xstrdup ("" ); +continue ; + } +strbuf_rtrim (&out); + v->s =strbuf_detach (&out,NULL ); + +strbuf_release (&err); + } +}I suspect that this was written to mimick what is done for describe.
The describe codepath has a (semi-)valid reason to fork out to a subprocess, as computation of describe smudges the object flags of in-core object database and it is not trivial to call into the helper functions twice.
Quoted 12 lines
But showing notes for a single commit is merely an internal call to
get_note()away, so unless the note object is not a blob (which should be absolutely rare), spawning a subprocess for each and every ref tip feels a bit heavier than acceptable. We'd probably need to maintain a table ofnotes_trees, one per <note-ref> used as %(notes:<note-ref>) in the format string, andinit_notes()on them while parsing the atoms, and in this codepath it would be a look-up ofnotes_treefrom the table based on the u.notes_refname by callingget_note()to learn the object name, plus reading the object contents into thev->smember when the note object is a blob (and fallback the above code when it is not a blob, which is a rare-case, if we really want to handle them).
Hi,
I was supposed to send a v2 a lot earlier but due to time constraints (my semester is ending and there's work related to that) I've not been able to work on this. So if anyone is interested, they may work on this and submit v2. I personally think this format would be a nice addition to the ref-filter framework and Junio's msg above gives a really nice explanation on how to do things.
I will work on it in the earliest when I find time but just in case someone else is interested, they may pick this up and work on it.
Thanks
Hi,
Quoted 3 lines · Bence Ferdinandy
On the other hand: would a quick win in analogy to "%G?" be considered? A "%N?" that either outputs a "N" for note and a "-" for no note? Or something like that. A bit ugly, but gets the job done.
FWIW my main use case for notes is to record build/test results.
Having formats for "first line of notes" or "last line of notes" (with newline removed) would be really useful for building a poor man's CI that can display checkmarks next to commit messages.
Simon