]>
Commit | Line | Data |
---|---|---|
a97a7468 JS |
1 | #ifndef NOTES_H |
2 | #define NOTES_H | |
3 | ||
cd305392 JH |
4 | /* |
5 | * Notes tree object | |
6 | * | |
7 | * Encapsulates the internal notes tree structure associated with a notes ref. | |
8 | * Whenever a struct notes_tree pointer is required below, you may pass NULL in | |
9 | * order to use the default/internal notes tree. E.g. you only need to pass a | |
10 | * non-NULL value if you need to refer to several different notes trees | |
11 | * simultaneously. | |
12 | */ | |
13 | extern struct notes_tree { | |
14 | struct int_node *root; | |
15 | char *ref; | |
16 | int initialized; | |
17 | } default_notes_tree; | |
18 | ||
709f79b0 JH |
19 | /* |
20 | * Flags controlling behaviour of notes tree initialization | |
21 | * | |
22 | * Default behaviour is to initialize the notes tree from the tree object | |
23 | * specified by the given (or default) notes ref. | |
24 | */ | |
25 | #define NOTES_INIT_EMPTY 1 | |
26 | ||
27 | /* | |
cd305392 | 28 | * Initialize the given notes_tree with the notes tree structure at the given |
709f79b0 JH |
29 | * ref. If given ref is NULL, the value of the $GIT_NOTES_REF environment |
30 | * variable is used, and if that is missing, the default notes ref is used | |
31 | * ("refs/notes/commits"). | |
32 | * | |
cd305392 JH |
33 | * If you need to re-intialize a notes_tree structure (e.g. when switching from |
34 | * one notes ref to another), you must first de-initialize the notes_tree | |
35 | * structure by calling free_notes(struct notes_tree *). | |
36 | * | |
37 | * If you pass t == NULL, the default internal notes_tree will be initialized. | |
38 | * | |
39 | * Precondition: The notes_tree structure is zeroed (this can be achieved with | |
40 | * memset(t, 0, sizeof(struct notes_tree))) | |
709f79b0 | 41 | */ |
cd305392 | 42 | void init_notes(struct notes_tree *t, const char *notes_ref, int flags); |
709f79b0 | 43 | |
61a7cca0 | 44 | /* |
cd305392 | 45 | * Add the given note object to the given notes_tree structure |
61a7cca0 | 46 | * |
cd305392 | 47 | * IMPORTANT: The changes made by add_note() to the given notes_tree structure |
61a7cca0 JH |
48 | * are not persistent until a subsequent call to write_notes_tree() returns |
49 | * zero. | |
50 | */ | |
cd305392 | 51 | void add_note(struct notes_tree *t, const unsigned char *object_sha1, |
2626b536 JH |
52 | const unsigned char *note_sha1); |
53 | ||
61a7cca0 | 54 | /* |
cd305392 | 55 | * Remove the given note object from the given notes_tree structure |
61a7cca0 | 56 | * |
cd305392 | 57 | * IMPORTANT: The changes made by remove_note() to the given notes_tree |
61a7cca0 JH |
58 | * structure are not persistent until a subsequent call to write_notes_tree() |
59 | * returns zero. | |
60 | */ | |
cd305392 | 61 | void remove_note(struct notes_tree *t, const unsigned char *object_sha1); |
1ec666b0 | 62 | |
9b391f21 JH |
63 | /* |
64 | * Get the note object SHA1 containing the note data for the given object | |
65 | * | |
66 | * Return NULL if the given object has no notes. | |
67 | */ | |
cd305392 JH |
68 | const unsigned char *get_note(struct notes_tree *t, |
69 | const unsigned char *object_sha1); | |
9b391f21 | 70 | |
73f77b90 JH |
71 | /* |
72 | * Flags controlling behaviour of for_each_note() | |
73 | * | |
74 | * Default behaviour of for_each_note() is to traverse every single note object | |
cd305392 | 75 | * in the given notes tree, unpacking subtree entries along the way. |
73f77b90 JH |
76 | * The following flags can be used to alter the default behaviour: |
77 | * | |
78 | * - DONT_UNPACK_SUBTREES causes for_each_note() NOT to unpack and recurse into | |
79 | * subtree entries while traversing the notes tree. This causes notes within | |
80 | * those subtrees NOT to be passed to the callback. Use this flag if you | |
81 | * don't want to traverse _all_ notes, but only want to traverse the parts | |
82 | * of the notes tree that have already been unpacked (this includes at least | |
83 | * all notes that have been added/changed). | |
84 | * | |
85 | * - YIELD_SUBTREES causes any subtree entries that are encountered to be | |
86 | * passed to the callback, before recursing into them. Subtree entries are | |
87 | * not note objects, but represent intermediate directories in the notes | |
88 | * tree. When passed to the callback, subtree entries will have a trailing | |
89 | * slash in their path, which the callback may use to differentiate between | |
90 | * note entries and subtree entries. Note that already-unpacked subtree | |
91 | * entries are not part of the notes tree, and will therefore not be yielded. | |
92 | * If this flag is used together with DONT_UNPACK_SUBTREES, for_each_note() | |
93 | * will yield the subtree entry, but not recurse into it. | |
94 | */ | |
95 | #define FOR_EACH_NOTE_DONT_UNPACK_SUBTREES 1 | |
96 | #define FOR_EACH_NOTE_YIELD_SUBTREES 2 | |
97 | ||
98 | /* | |
cd305392 | 99 | * Invoke the specified callback function for each note in the given notes_tree |
73f77b90 JH |
100 | * |
101 | * If the callback returns nonzero, the note walk is aborted, and the return | |
102 | * value from the callback is returned from for_each_note(). Hence, a zero | |
103 | * return value from for_each_note() indicates that all notes were walked | |
104 | * successfully. | |
105 | * | |
106 | * IMPORTANT: The callback function is NOT allowed to change the notes tree. | |
107 | * In other words, the following functions can NOT be invoked (on the current | |
108 | * notes tree) from within the callback: | |
109 | * - add_note() | |
110 | * - remove_note() | |
111 | * - free_notes() | |
112 | */ | |
113 | typedef int each_note_fn(const unsigned char *object_sha1, | |
114 | const unsigned char *note_sha1, char *note_path, | |
115 | void *cb_data); | |
cd305392 JH |
116 | int for_each_note(struct notes_tree *t, int flags, each_note_fn fn, |
117 | void *cb_data); | |
73f77b90 | 118 | |
61a7cca0 | 119 | /* |
cd305392 | 120 | * Write the given notes_tree structure to the object database |
61a7cca0 | 121 | * |
cd305392 JH |
122 | * Creates a new tree object encapsulating the current state of the given |
123 | * notes_tree, and stores its SHA1 into the 'result' argument. | |
61a7cca0 JH |
124 | * |
125 | * Returns zero on success, non-zero on failure. | |
126 | * | |
cd305392 JH |
127 | * IMPORTANT: Changes made to the given notes_tree are not persistent until |
128 | * this function has returned zero. Please also remember to create a | |
129 | * corresponding commit object, and update the appropriate notes ref. | |
61a7cca0 | 130 | */ |
cd305392 | 131 | int write_notes_tree(struct notes_tree *t, unsigned char *result); |
61a7cca0 JH |
132 | |
133 | /* | |
cd305392 | 134 | * Free (and de-initialize) the given notes_tree structure |
61a7cca0 | 135 | * |
cd305392 | 136 | * IMPORTANT: Changes made to the given notes_tree since the last, successful |
61a7cca0 JH |
137 | * call to write_notes_tree() will be lost. |
138 | */ | |
cd305392 | 139 | void free_notes(struct notes_tree *t); |
27d57564 | 140 | |
a7e7eff6 | 141 | /* Flags controlling how notes are formatted */ |
c56fcc89 JH |
142 | #define NOTES_SHOW_HEADER 1 |
143 | #define NOTES_INDENT 2 | |
144 | ||
a7e7eff6 JH |
145 | /* |
146 | * Fill the given strbuf with the notes associated with the given object. | |
147 | * | |
cd305392 | 148 | * If the given notes_tree structure is not initialized, it will be auto- |
a7e7eff6 | 149 | * initialized to the default value (see documentation for init_notes() above). |
cd305392 JH |
150 | * If the given notes_tree is NULL, the internal/default notes_tree will be |
151 | * used instead. | |
a7e7eff6 JH |
152 | * |
153 | * 'flags' is a bitwise combination of the above formatting flags. | |
154 | */ | |
cd305392 JH |
155 | void format_note(struct notes_tree *t, const unsigned char *object_sha1, |
156 | struct strbuf *sb, const char *output_encoding, int flags); | |
a97a7468 JS |
157 | |
158 | #endif |