Skip to contentSkip to conversation

sha1_name: support sha1^{note} to return note sha-1

PATCH In review Nguyễn Thái Ngọc Duy 3 participants 5 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sha1_name: support sha1^{note} to return note sha-1

---
It may be useful for scripting, and looks nice. Though may be not worth adding if there are no actual users.

 Documentation/revisions.txt |    1 +
 sha1_name.c                 |   15 ++++++++++++++-
 2 files changed, 15 insertions(+), 1 deletions(-)
Documentation/revisions.txt +1 -0
index 1725661..5a671fe 100644
@@ -114,6 +114,7 @@ the '$GIT_DIR/refs' directory or from the '$GIT_DIR/packed-refs' file.
   object of that type is found or the object cannot be
   dereferenced anymore (in which case, barf).  '<rev>{caret}0'
   is a short-hand for '<rev>{caret}\{commit\}'.
+  A special type "note" can be used to return the note object.
 
 '<rev>{caret}\{\}', e.g. 'v0.99.8{caret}\{\}'::
   A suffix '{caret}' followed by an empty brace pair
sha1_name.c +14 -1
index c633113..02d28df 100644
@@ -6,6 +6,7 @@
 #include "tree-walk.h"
 #include "refs.h"
 #include "remote.h"
+#include "notes.h"
 
 static int get_sha1_oneline(const char *, unsigned char *, struct commit_list *);
 
@@ -473,7 +474,19 @@ static int peel_onion(const char *name, int len, unsigned char *sha1)
 		expected_type = OBJ_NONE;
 	else if (sp[0] == '/')
 		expected_type = OBJ_COMMIT;
-	else
+	else if (!strncmp("note}", sp, 5)) {
+		const unsigned char *note;
+		struct notes_tree t;
+		if (get_sha1_1(name, sp - name - 2, outer))
+			return -1;
+		memset(&t, 0, sizeof(t));
+		init_notes(&t, NULL, NULL, 0);
+		note = get_note(&t, outer);
+		if (note)
+			hashcpy(sha1, note);
+		free_notes(&t);
+		return note ? 0 : -1;
+	} else
 		return -1;
 
 	if (get_sha1_1(name, sp - name - 2, outer))
Signature · 1 line
1.7.8.36.g69ee2
#1 lore raw
Quoted 2 lines · Nguyen Thai Ngoc Duy

It may be useful for scripting, and looks nice. Though may be not worth adding if there are no actual users.

There can be many notes refs. So I think to do this right, you would want something like:

foo^{note:bar}

which would look in refs/notes/bar (this logic is handled by expand_notes_ref). And "foo^{note}" would be a synonym for the default note ref.

Quoted 8 lines
sha1_name.c +3 -1
@@ -473,7 +474,19 @@ static int peel_onion(const char *name, int len, unsigned char *sha1)
 		expected_type = OBJ_NONE;
 	else if (sp[0] == '/')
 		expected_type = OBJ_COMMIT;
-	else
+	else if (!strncmp("note}", sp, 5)) {
+		const unsigned char *note;
+		struct notes_tree t;

sha1_name.c static int peel_onion(const char *name, int len, unsigned char *sha1) · line 489

Quoted 10 lines
sha1_name.c +10 -0
+		if (get_sha1_1(name, sp - name - 2, outer))
+			return -1;
+		memset(&t, 0, sizeof(t));
+		init_notes(&t, NULL, NULL, 0);
+		note = get_note(&t, outer);
+		if (note)
+			hashcpy(sha1, note);
+		free_notes(&t);
+		return note ? 0 : -1;
+	} else

The notes code is relatively expensive to initialize, with the assumption that the effort will be amortized across multiple lookups (which are made faster). Is it possible to cache this initialized notes structure in case many lookups are done?

It may not be as important as it used to be, either. I think more recent versions of the code will progressively load the notes tree rather than filling it all in at initialization time. But it has been a while since I've done anything with notes.

-Peff

#2 lore raw
Quoted 5 lines · Jeff King
Quoted 2 lines · Nguyen Thai Ngoc Duy

 It may be useful for scripting, and looks nice. Though may be not worth adding if there are no actual users.

Quoted 8 lines

There can be many notes refs. So I think to do this right, you would want something like:

 foo^{note:bar}

which would look in refs/notes/bar (this logic is handled by expand_notes_ref). And "foo^{note}" would be a synonym for the default note ref.

Right. Thanks.

Quoted 15 lines
Quoted 15 lines
sha1_name.c +10 -1
@@ -473,7 +474,19 @@ static int peel_onion(const char *name, int len, unsigned char *sha1)
               expected_type = OBJ_NONE;
       else if (sp[0] == '/')
               expected_type = OBJ_COMMIT;
-     else
+     else if (!strncmp("note}", sp, 5)) {
+             const unsigned char *note;
+             struct notes_tree t;
+             if (get_sha1_1(name, sp - name - 2, outer))
+                     return -1;
+             memset(&t, 0, sizeof(t));
+             init_notes(&t, NULL, NULL, 0);
+             note = get_note(&t, outer);
+             if (note)
+                     hashcpy(sha1, note);
Quoted 8 lines

sha1_name.c static int peel_onion(const char *name, int len, unsigned char *sha1) · line 489

Quoted 3 lines
sha1_name.c +3 -0
+             free_notes(&t);
+             return note ? 0 : -1;
+     } else

The notes code is relatively expensive to initialize, with the assumption that the effort will be amortized across multiple lookups (which are made faster). Is it possible to cache this initialized notes structure in case many lookups are done?

No idea. I have never worked/used notes until yesterday (and these patches were the result). I will look into it.

Quoted 4 lines

It may not be as important as it used to be, either. I think more recent versions of the code will progressively load the notes tree rather than filling it all in at initialization time. But it has been a while since I've done anything with notes.

Signature · 1 line
Duy
#3 lore raw
Quoted 26 lines · Nguyen Thai Ngoc Duy
Quoted 16 lines · Jeff King
Quoted 15 lines · Nguyen Thai Ngoc Duy
sha1_name.c +10 -1
@@ -473,7 +474,19 @@ static int peel_onion(const char *name, int len, unsigned char *sha1)
               expected_type = OBJ_NONE;
       else if (sp[0] == '/')
               expected_type = OBJ_COMMIT;
-     else
+     else if (!strncmp("note}", sp, 5)) {
+             const unsigned char *note;
+             struct notes_tree t;
+             if (get_sha1_1(name, sp - name - 2, outer))
+                     return -1;
+             memset(&t, 0, sizeof(t));
+             init_notes(&t, NULL, NULL, 0);
+             note = get_note(&t, outer);
+             if (note)
+                     hashcpy(sha1, note);
Quoted 8 lines

sha1_name.c static int peel_onion(const char *name, int len, unsigned char *sha1) · line 489

Quoted 3 lines
sha1_name.c +3 -0
+             free_notes(&t);
+             return note ? 0 : -1;
+     } else

The notes code is relatively expensive to initialize, with the assumption that the effort will be amortized across multiple lookups (which are made faster). Is it possible to cache this initialized notes structure in case many lookups are done?

Quoted 2 lines

No idea. I have never worked/used notes until yesterday (and these patches were the result). I will look into it.

IIRC, the initialization loads the top-level notes tree object into memory. Subtrees (if any) are loaded on demand. FTR, if you have less then ~256 notes in the notes tree, there will be no subtrees. As the number of notes grows, the number of subtree levels grow roughly logarithmically with the total number of notes (see determine_fanout() for more details).

...Johan

Signature · 2 lines
Johan Herland, <[email protected]>
www.herland.net
#4 lore raw
Quoted 9 lines · Johan Herland
Quoted 2 lines

No idea. I have never worked/used notes until yesterday (and these patches were the result). I will look into it.

IIRC, the initialization loads the top-level notes tree object into memory. Subtrees (if any) are loaded on demand. FTR, if you have less then ~256 notes in the notes tree, there will be no subtrees. As the number of notes grows, the number of subtree levels grow roughly logarithmically with the total number of notes (see determine_fanout() for more details).

Ah, right. I was thinking back to my original crappy implementation that didn't do fanout. So I don't think an init is that bad. It does look up the ref each time, but it will only load the top-level tree object (which you would need to do a lookup anyway).

So it is probably OK to just do an init/lookup/free each time if it makes the code simpler (and I think it does). We probably won't be looking up sha1^{note} in a tight loop, anyway, since sha1 expressions like that generally come from the command line.

-Peff