notes.hon commit Notes API: Allow multiple concurrent notes trees with new struct notes_tree (cd30539)
   1#ifndef NOTES_H
   2#define NOTES_H
   3
   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 */
  13extern struct notes_tree {
  14        struct int_node *root;
  15        char *ref;
  16        int initialized;
  17} default_notes_tree;
  18
  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/*
  28 * Initialize the given notes_tree with the notes tree structure at the given
  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 *
  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)))
  41 */
  42void init_notes(struct notes_tree *t, const char *notes_ref, int flags);
  43
  44/*
  45 * Add the given note object to the given notes_tree structure
  46 *
  47 * IMPORTANT: The changes made by add_note() to the given notes_tree structure
  48 * are not persistent until a subsequent call to write_notes_tree() returns
  49 * zero.
  50 */
  51void add_note(struct notes_tree *t, const unsigned char *object_sha1,
  52                const unsigned char *note_sha1);
  53
  54/*
  55 * Remove the given note object from the given notes_tree structure
  56 *
  57 * IMPORTANT: The changes made by remove_note() to the given notes_tree
  58 * structure are not persistent until a subsequent call to write_notes_tree()
  59 * returns zero.
  60 */
  61void remove_note(struct notes_tree *t, const unsigned char *object_sha1);
  62
  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 */
  68const unsigned char *get_note(struct notes_tree *t,
  69                const unsigned char *object_sha1);
  70
  71/*
  72 * Flags controlling behaviour of for_each_note()
  73 *
  74 * Default behaviour of for_each_note() is to traverse every single note object
  75 * in the given notes tree, unpacking subtree entries along the way.
  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/*
  99 * Invoke the specified callback function for each note in the given notes_tree
 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 */
 113typedef int each_note_fn(const unsigned char *object_sha1,
 114                const unsigned char *note_sha1, char *note_path,
 115                void *cb_data);
 116int for_each_note(struct notes_tree *t, int flags, each_note_fn fn,
 117                void *cb_data);
 118
 119/*
 120 * Write the given notes_tree structure to the object database
 121 *
 122 * Creates a new tree object encapsulating the current state of the given
 123 * notes_tree, and stores its SHA1 into the 'result' argument.
 124 *
 125 * Returns zero on success, non-zero on failure.
 126 *
 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.
 130 */
 131int write_notes_tree(struct notes_tree *t, unsigned char *result);
 132
 133/*
 134 * Free (and de-initialize) the given notes_tree structure
 135 *
 136 * IMPORTANT: Changes made to the given notes_tree since the last, successful
 137 * call to write_notes_tree() will be lost.
 138 */
 139void free_notes(struct notes_tree *t);
 140
 141/* Flags controlling how notes are formatted */
 142#define NOTES_SHOW_HEADER 1
 143#define NOTES_INDENT 2
 144
 145/*
 146 * Fill the given strbuf with the notes associated with the given object.
 147 *
 148 * If the given notes_tree structure is not initialized, it will be auto-
 149 * initialized to the default value (see documentation for init_notes() above).
 150 * If the given notes_tree is NULL, the internal/default notes_tree will be
 151 * used instead.
 152 *
 153 * 'flags' is a bitwise combination of the above formatting flags.
 154 */
 155void format_note(struct notes_tree *t, const unsigned char *object_sha1,
 156                struct strbuf *sb, const char *output_encoding, int flags);
 157
 158#endif