Skip to content

Commit 709f79b

Browse files
jherlandgitster
authored andcommitted
Notes API: init_notes(): Initialize the notes tree from the given notes ref
Created by a simple refactoring of initialize_notes(). Also add a new 'flags' parameter, which is a bitwise combination of notes initialization flags. For now, there is only one flag - NOTES_INIT_EMPTY - which indicates that the notes tree should not auto-load the contents of the given (or default) notes ref, but rather should leave the notes tree initialized to an empty state. This will become useful in the future when manipulating the notes tree through the notes API. Signed-off-by: Johan Herland <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3b78cdb commit 709f79b

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

notes.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -340,15 +340,28 @@ static void load_subtree(struct leaf_node *subtree, struct int_node *node,
340340
free(buf);
341341
}
342342

343-
static void initialize_notes(const char *notes_ref_name)
343+
void init_notes(const char *notes_ref, int flags)
344344
{
345345
unsigned char sha1[20], object_sha1[20];
346346
unsigned mode;
347347
struct leaf_node root_tree;
348348

349-
if (!notes_ref_name || read_ref(notes_ref_name, object_sha1) ||
350-
get_tree_entry(object_sha1, "", sha1, &mode))
349+
assert(!initialized);
350+
initialized = 1;
351+
352+
if (!notes_ref)
353+
notes_ref = getenv(GIT_NOTES_REF_ENVIRONMENT);
354+
if (!notes_ref)
355+
notes_ref = notes_ref_name; /* value of core.notesRef config */
356+
if (!notes_ref)
357+
notes_ref = GIT_NOTES_DEFAULT_REF;
358+
359+
if (flags & NOTES_INIT_EMPTY || !notes_ref ||
360+
read_ref(notes_ref, object_sha1))
351361
return;
362+
if (get_tree_entry(object_sha1, "", sha1, &mode))
363+
die("Failed to read notes tree referenced by %s (%s)",
364+
notes_ref, object_sha1);
352365

353366
hashclr(root_tree.key_sha1);
354367
hashcpy(root_tree.val_sha1, sha1);
@@ -379,15 +392,8 @@ void format_note(const unsigned char *object_sha1, struct strbuf *sb,
379392
unsigned long linelen, msglen;
380393
enum object_type type;
381394

382-
if (!initialized) {
383-
const char *env = getenv(GIT_NOTES_REF_ENVIRONMENT);
384-
if (env)
385-
notes_ref_name = getenv(GIT_NOTES_REF_ENVIRONMENT);
386-
else if (!notes_ref_name)
387-
notes_ref_name = GIT_NOTES_DEFAULT_REF;
388-
initialize_notes(notes_ref_name);
389-
initialized = 1;
390-
}
395+
if (!initialized)
396+
init_notes(NULL, 0);
391397

392398
sha1 = lookup_notes(object_sha1);
393399
if (!sha1)

notes.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
#ifndef NOTES_H
22
#define NOTES_H
33

4+
/*
5+
* Flags controlling behaviour of notes tree initialization
6+
*
7+
* Default behaviour is to initialize the notes tree from the tree object
8+
* specified by the given (or default) notes ref.
9+
*/
10+
#define NOTES_INIT_EMPTY 1
11+
12+
/*
13+
* Initialize internal notes tree structure with the notes tree at the given
14+
* ref. If given ref is NULL, the value of the $GIT_NOTES_REF environment
15+
* variable is used, and if that is missing, the default notes ref is used
16+
* ("refs/notes/commits").
17+
*
18+
* If you need to re-intialize the internal notes tree structure (e.g. loading
19+
* from a different notes ref), please first de-initialize the current notes
20+
* tree by calling free_notes().
21+
*/
22+
void init_notes(const char *notes_ref, int flags);
23+
424
/* Free (and de-initialize) the internal notes tree structure */
525
void free_notes(void);
626

0 commit comments

Comments
 (0)