Notes API: for_each_note(): Traverse the entire notes tree with a callback
[git/mjg.git] / notes.h
blob213191290249ab99dcb2a8f330eae29020e3a197
1 #ifndef NOTES_H
2 #define NOTES_H
4 /*
5 * Flags controlling behaviour of notes tree initialization
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
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").
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().
22 void init_notes(const char *notes_ref, int flags);
24 /* Add the given note object to the internal notes tree structure */
25 void add_note(const unsigned char *object_sha1,
26 const unsigned char *note_sha1);
28 /* Remove the given note object from the internal notes tree structure */
29 void remove_note(const unsigned char *object_sha1);
32 * Get the note object SHA1 containing the note data for the given object
34 * Return NULL if the given object has no notes.
36 const unsigned char *get_note(const unsigned char *object_sha1);
39 * Flags controlling behaviour of for_each_note()
41 * Default behaviour of for_each_note() is to traverse every single note object
42 * in the notes tree, unpacking subtree entries along the way.
43 * The following flags can be used to alter the default behaviour:
45 * - DONT_UNPACK_SUBTREES causes for_each_note() NOT to unpack and recurse into
46 * subtree entries while traversing the notes tree. This causes notes within
47 * those subtrees NOT to be passed to the callback. Use this flag if you
48 * don't want to traverse _all_ notes, but only want to traverse the parts
49 * of the notes tree that have already been unpacked (this includes at least
50 * all notes that have been added/changed).
52 * - YIELD_SUBTREES causes any subtree entries that are encountered to be
53 * passed to the callback, before recursing into them. Subtree entries are
54 * not note objects, but represent intermediate directories in the notes
55 * tree. When passed to the callback, subtree entries will have a trailing
56 * slash in their path, which the callback may use to differentiate between
57 * note entries and subtree entries. Note that already-unpacked subtree
58 * entries are not part of the notes tree, and will therefore not be yielded.
59 * If this flag is used together with DONT_UNPACK_SUBTREES, for_each_note()
60 * will yield the subtree entry, but not recurse into it.
62 #define FOR_EACH_NOTE_DONT_UNPACK_SUBTREES 1
63 #define FOR_EACH_NOTE_YIELD_SUBTREES 2
66 * Invoke the specified callback function for each note
68 * If the callback returns nonzero, the note walk is aborted, and the return
69 * value from the callback is returned from for_each_note(). Hence, a zero
70 * return value from for_each_note() indicates that all notes were walked
71 * successfully.
73 * IMPORTANT: The callback function is NOT allowed to change the notes tree.
74 * In other words, the following functions can NOT be invoked (on the current
75 * notes tree) from within the callback:
76 * - add_note()
77 * - remove_note()
78 * - free_notes()
80 typedef int each_note_fn(const unsigned char *object_sha1,
81 const unsigned char *note_sha1, char *note_path,
82 void *cb_data);
83 int for_each_note(int flags, each_note_fn fn, void *cb_data);
85 /* Free (and de-initialize) the internal notes tree structure */
86 void free_notes(void);
88 /* Flags controlling how notes are formatted */
89 #define NOTES_SHOW_HEADER 1
90 #define NOTES_INDENT 2
93 * Fill the given strbuf with the notes associated with the given object.
95 * If the internal notes structure is not initialized, it will be auto-
96 * initialized to the default value (see documentation for init_notes() above).
98 * 'flags' is a bitwise combination of the above formatting flags.
100 void format_note(const unsigned char *object_sha1, struct strbuf *sb,
101 const char *output_encoding, int flags);
103 #endif