@matheusbsbs / matheus-bagliano / commits / 709f79b089
Fork 0

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 <johan@herland.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johan Herland committed Feb 13, 2010 at 22:28 UTC 709f79b0894859a6624e99b3a0c4714dd4ece494
2 files changed +38 -12
notes.c
+18 -12
@@ -340,15 +340,28 @@ static void load_subtree(struct leaf_node *subtree, struct int_node *node,
340 free(buf);
341 }
342
343 -static void initialize_notes(const char *notes_ref_name)
343 +void init_notes(const char *notes_ref, int flags)
344 {
345 unsigned char sha1[20], object_sha1[20];
346 unsigned mode;
347 struct leaf_node root_tree;
348
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))
361 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);
365
366 hashclr(root_tree.key_sha1);
367 hashcpy(root_tree.val_sha1, sha1);
@@ -379,15 +392,8 @@ void format_note(const unsigned char *object_sha1, struct strbuf *sb,
392 unsigned long linelen, msglen;
393 enum object_type type;
394
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);
397
398 sha1 = lookup_notes(object_sha1);
399 if (!sha1)
notes.h
+20
@@ -1,6 +1,26 @@
1 #ifndef NOTES_H
2 #define NOTES_H
3
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 +
24 /* Free (and de-initialize) the internal notes tree structure */
25 void free_notes(void);
26